Skip to content

Commit 68f3764

Browse files
committed
fix(ai-bot): correct configuration to call ai.lies.exposed from remote
1 parent 6ed7cc8 commit 68f3764

21 files changed

+375
-616
lines changed

helm/templates/services/localai/localai-env.configmap.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
apiVersion: v1
22
data:
3+
DEBUG: "true"
34
LOCALAI_LOG_LEVEL: debug
45
LOCALAI_THREADS: "4"
56
LOCALAI_API_KEY: {{ .Values.localai.openAIApiKey | quote }}
7+
LOCALAI_SINGLE_ACTIVE_BACKEND: "true"
68
kind: ConfigMap
79
metadata:
810
name: localai-env

helm/templates/services/localai/localai.deployment.yaml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ metadata:
44
name: localai
55
namespace: {{ .Release.Namespace }}
66
{{ tuple . "localai" | include "localai.labels" | indent 2 }}
7-
7+
intel.feature.node.kubernetes.io/gpu: "true"
88
spec:
99
replicas: 1
1010
selector:
@@ -13,6 +13,8 @@ spec:
1313
template:
1414
metadata:
1515
{{ tuple . "localai" | include "localai.labels" | indent 6 }}
16+
intel.feature.node.kubernetes.io/gpu: "true"
17+
1618
spec:
1719
containers:
1820
- name: localai
@@ -40,20 +42,22 @@ spec:
4042
volumeMounts:
4143
- name: localai-models-pv
4244
mountPath: /models
43-
- name: localai-backends-pv
44-
mountPath: /backends
45+
# - name: localai-backends-pv
46+
# mountPath: /backends
4547
resources:
4648
limits:
4749
memory: 8Gi
4850
cpu: 4000m
51+
gpu.intel.com/i915: 1
4952
requests:
5053
memory: 4Gi
5154
cpu: 1000m
55+
gpu.intel.com/i915: 1
5256
restartPolicy: Always
5357
volumes:
5458
- name: localai-models-pv
5559
persistentVolumeClaim:
5660
claimName: localai-models-pv-claim
57-
- name: localai-backends-pv
58-
persistentVolumeClaim:
59-
claimName: localai-backends-pv-claim
61+
# - name: localai-backends-pv
62+
# persistentVolumeClaim:
63+
# claimName: localai-backends-pv-claim

packages/@liexp/backend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
"vitest-mock-extended": "^3.1.0"
7474
},
7575
"peerDependencies": {
76-
"@langchain/core": "^0.3.19",
76+
"@langchain/core": "^0.3.77",
7777
"@napi-rs/canvas": "^0.1.73",
7878
"debug": "^4.4.0",
7979
"effect": "^3.17.13",

packages/@liexp/backend/src/flows/ai/createEventFromDocuments.flow.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export const createEventFromDocuments = <
3131
return pipe(
3232
fp.RTE.Do,
3333
fp.RTE.bind("prompt", () => getCreateEventPromptPartial<C>(prompt, type)),
34-
fp.RTE.bind("retriever", () => getStoreRetriever(documents)),
34+
fp.RTE.bind("retriever", () => getStoreRetriever(documents, question)),
3535
fp.RTE.bind(
3636
"model",
3737
() => (ctx) =>
@@ -48,7 +48,7 @@ export const createEventFromDocuments = <
4848

4949
return runRagChain<EventCommonProps, C>(
5050
{
51-
context: retriever.pipe(formatDocumentsAsString),
51+
context: () => formatDocumentsAsString(retriever),
5252
},
5353
prompt.pipe(model).pipe(parser),
5454
question,

packages/@liexp/backend/src/flows/ai/runRagChain.ts

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,30 +39,49 @@ export const runRagChain =
3939
}, toAPIError);
4040
};
4141

42-
export const runRagChainStream =
42+
export const runRagChainStream = <
43+
C extends LangchainContext & LoggerContext = LangchainContext & LoggerContext,
44+
>(
45+
inputs: RunnableLike,
46+
chain: RunnableLike,
47+
question: string,
48+
mode: "stream" | "invoke" = "invoke",
49+
): ReaderTaskEither<C, APIError, string> => {
50+
return runRunnableSequence(
51+
RunnableSequence.from([
52+
{ ...inputs, question: new RunnablePassthrough() },
53+
chain,
54+
]),
55+
question,
56+
mode,
57+
);
58+
};
59+
60+
export const runRunnableSequence =
4361
<
4462
C extends LangchainContext & LoggerContext = LangchainContext &
4563
LoggerContext,
4664
>(
47-
inputs: RunnableLike,
48-
chain: RunnableLike,
65+
inputs: RunnableSequence,
4966
question: string,
67+
mode: "stream" | "invoke" = "stream",
5068
): ReaderTaskEither<C, APIError, string> =>
5169
(ctx) => {
5270
return fp.TE.tryCatch(async () => {
53-
const ragChain = RunnableSequence.from([
54-
{ ...inputs, question: new RunnablePassthrough() },
55-
chain,
56-
]);
57-
58-
const stream = await ragChain.stream(question);
71+
ctx.logger.debug.log("Running sequence in mode %s", mode);
5972

6073
let output = "";
61-
for await (const chunk of stream) {
62-
output += chunk;
74+
if (mode === "stream") {
75+
const stream = await inputs.stream(question);
76+
77+
for await (const chunk of stream) {
78+
output += chunk;
79+
}
80+
} else {
81+
output = await inputs.invoke(question);
6382
}
6483

65-
ctx.logger.debug.log("RAG chain stream output %O", output);
84+
ctx.logger.debug.log("Output %s", output);
6685

6786
return output;
6887
}, toAPIError);

packages/@liexp/backend/src/flows/ai/storeRetriever.flow.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { type Document } from "@langchain/core/documents";
2-
import { type VectorStoreRetriever } from "@langchain/core/vectorstores";
32
import { fp, pipe } from "@liexp/core/lib/fp/index.js";
43
import {
54
toAPIError,
@@ -14,16 +13,22 @@ import { type LoggerContext } from "../../context/logger.context.js";
1413
export const getStoreRetriever =
1514
<C extends LangchainContext & LoggerContext>(
1615
documents: Document[],
17-
): ReaderTaskEither<C, APIError, VectorStoreRetriever<MemoryVectorStore>> =>
16+
question: string,
17+
): ReaderTaskEither<C, APIError, Document[]> =>
1818
(ctx) => {
1919
return pipe(
2020
fp.TE.tryCatch(async () => {
2121
const textSplitter = new RecursiveCharacterTextSplitter({
22-
chunkSize: 2000,
23-
chunkOverlap: 1000,
22+
chunkSize: 1000,
23+
chunkOverlap: 200,
2424
});
2525
const splits = await textSplitter.splitDocuments(documents);
2626

27+
ctx.logger.debug.log(
28+
`Split documents into %d sub-documents.`,
29+
splits.length,
30+
);
31+
2732
const vectorStore = await MemoryVectorStore.fromDocuments(
2833
splits,
2934
ctx.langchain.embeddings,
@@ -34,7 +39,11 @@ export const getStoreRetriever =
3439
documents.length,
3540
);
3641

37-
return vectorStore.asRetriever({ verbose: true });
42+
const retriever = vectorStore.asRetriever({ verbose: true });
43+
44+
const retrieved = await retriever.invoke(question);
45+
46+
return retrieved;
3847
}, toAPIError),
3948
);
4049
};

packages/@liexp/backend/src/flows/ai/updateEventFromDocuments.flow.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ export const updateEventFromDocuments = <
3030
): ReaderTaskEither<C, APIError, EventCommonProps> => {
3131
return pipe(
3232
fp.RTE.Do,
33-
fp.RTE.bind("prompt", () => getCreateEventPromptPartial<C>(prompt, type)),
34-
fp.RTE.bind("retriever", () => getStoreRetriever(documents)),
33+
fp.RTE.bind("prompt", () => getCreateEventPromptPartial(prompt, type)),
34+
fp.RTE.bind("retriever", () => getStoreRetriever(documents, question)),
3535
fp.RTE.bind(
3636
"model",
3737
() => (ctx) =>
3838
fp.TE.right(
39-
ctx.langchain.chat.bind({
39+
ctx.langchain.chat.withConfig({
4040
response_format: {
4141
type: "json_object",
4242
},
@@ -48,7 +48,7 @@ export const updateEventFromDocuments = <
4848

4949
return runRagChain<EventCommonProps, C>(
5050
{
51-
context: retriever.pipe(formatDocumentsAsString),
51+
context: () => formatDocumentsAsString(retriever),
5252
},
5353
prompt.pipe(model).pipe(parser),
5454
question,

packages/@liexp/backend/src/providers/ai/langchain.provider.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export interface LangchainProvider {
7878
chat: ChatOpenAI;
7979
embeddings: OpenAIEmbeddings;
8080
queryDocument: <Args extends { text: string; question?: string }>(
81-
url: LangchainDocument[],
81+
docs: LangchainDocument[],
8282
question: string,
8383
options?: { model?: AvailableModels; prompt?: PromptFn<Args> },
8484
) => Promise<string>;
@@ -102,14 +102,15 @@ export const GetLangchainProvider = (
102102
const options = {
103103
...opts,
104104
};
105+
105106
const chat = new ChatOpenAI({
106107
model: chatModel,
107108
temperature: 0,
108109
apiKey: opts.apiKey,
109110
timeout: 60 * 30 * 1000, // 30 minutes
110111
maxConcurrency: 1,
111112
maxRetries: 2,
112-
streamUsage: false,
113+
streaming: true,
113114
...opts.options?.chat,
114115
configuration: {
115116
baseURL: opts.baseURL,
@@ -121,6 +122,7 @@ export const GetLangchainProvider = (
121122

122123
const embeddings = new OpenAIEmbeddings({
123124
model: embeddingsModel,
125+
modelName: embeddingsModel,
124126
apiKey: opts.apiKey,
125127
timeout: 60 * 30 * 1000, // 30 minutes,
126128
...opts.options?.embeddings,
@@ -141,20 +143,19 @@ export const GetLangchainProvider = (
141143
chat,
142144
embeddings,
143145
queryDocument: async (content, question, options) => {
144-
const model =
145-
options?.model ?? opts.models?.embeddings ?? "text-embedding-ada-002";
146+
const model = options?.model ?? embeddingsModel;
146147

147-
const chatModel = options?.model ?? opts.models?.chat ?? "gpt-4o";
148+
const chatModel2 = options?.model ?? chatModel;
148149

149150
langchainLogger.info.log(
150151
"queryDocument use embedding model %s to query document with size %d using chat model %s",
151152
model,
152153
content.length,
153-
chatModel,
154+
chatModel2,
154155
);
155156

156157
const chat = new ChatOpenAI({
157-
model: chatModel,
158+
model: chatModel2,
158159
temperature: 0,
159160
apiKey: opts.apiKey,
160161
configuration: {
@@ -168,6 +169,7 @@ export const GetLangchainProvider = (
168169
model,
169170
apiKey: opts.apiKey,
170171
timeout: 60 * 30 * 1000, // 30 minutes
172+
encodingFormat: "base64",
171173
configuration: {
172174
baseURL: opts.baseURL,
173175
...opts.options?.embeddings.configuration,
@@ -229,7 +231,7 @@ export const GetLangchainProvider = (
229231
configuration: {
230232
baseURL: opts.baseURL,
231233
},
232-
streaming: true,
234+
// streaming: true,
233235
});
234236

235237
const textSplitter = new RecursiveCharacterTextSplitter({

packages/@liexp/backend/src/providers/fs/fs.provider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export const GetFSClient = (ctx: GetFSClientContext): FSClient => {
7171
};
7272

7373
const getObject: FSClient["getObject"] = (filePath) => {
74-
fsLogger.debug.log("Getting object from path %s", filePath);
74+
// fsLogger.debug.log("Getting object from path %s", filePath);
7575
return pipe(
7676
fp.IOE.tryCatch(
7777
() => ctx.client.readFileSync(filePath, "utf-8"),

packages/@liexp/ui/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@
135135
"peerDependencies": {
136136
"axios": "^1",
137137
"effect": "^3",
138-
"fp-ts": "^2"
138+
"fp-ts": "^2",
139+
"openai": "^5.20.2"
139140
},
140141
"packageManager": "[email protected]+sha512.34e538c329b5553014ca8e8f4535997f96180a1d0f614339357449935350d924e22f8614682191264ec33d1462ac21561aff97f6bb18065351c162c7e8f6de67"
141142
}

0 commit comments

Comments
 (0)