Skip to content

Commit 727a54f

Browse files
author
hehua2008
committed
Add embedding support for message.content which is string/object array type
1 parent 741ba8f commit 727a54f

File tree

9 files changed

+181
-10
lines changed

9 files changed

+181
-10
lines changed

server/models/workspaceChats.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,29 @@ const WorkspaceChats = {
1111
apiSessionId = null,
1212
}) {
1313
try {
14+
let promptString;
15+
if (typeof prompt === "string" || prompt instanceof String) {
16+
promptString = prompt;
17+
} else if (Array.isArray(prompt)) {
18+
promptString = prompt
19+
.map((x) => {
20+
if (typeof x.text === "string" || x.text instanceof String) {
21+
return x.text;
22+
} else if (typeof x === "string" || x instanceof String) {
23+
return x;
24+
} else {
25+
return JSON.stringify(x);
26+
}
27+
})
28+
.join("\n\n");
29+
} else {
30+
promptString = JSON.stringify(prompt);
31+
}
32+
1433
const chat = await prisma.workspace_chats.create({
1534
data: {
1635
workspaceId,
17-
prompt,
36+
prompt: promptString,
1837
response: JSON.stringify(response),
1938
user_id: user?.id || null,
2039
thread_id: threadId,

server/utils/vectorDbProviders/astra/index.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,26 @@ const AstraDB = {
268268
};
269269
}
270270

271-
const queryVector = await LLMConnector.embedTextInput(input);
271+
let textInput = "";
272+
if (typeof input === "string" || input instanceof String) {
273+
textInput = input;
274+
} else if (Array.isArray(input)) {
275+
textInput = [];
276+
for (const x of input) {
277+
if (typeof x.text === "string" || x.text instanceof String) {
278+
textInput.push(x.text);
279+
} else if (typeof x === "string" || x instanceof String) {
280+
textInput.push(x);
281+
}
282+
}
283+
} else {
284+
return {
285+
contextTexts: [],
286+
sources: [],
287+
message: "Invalid query - the type of input is not string or array !",
288+
};
289+
}
290+
const queryVector = await LLMConnector.embedTextInput(textInput);
272291
const { contextTexts, sourceDocuments } = await this.similarityResponse({
273292
client,
274293
namespace,

server/utils/vectorDbProviders/chroma/index.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,26 @@ const Chroma = {
355355
};
356356
}
357357

358-
const queryVector = await LLMConnector.embedTextInput(input);
358+
let textInput = "";
359+
if (typeof input === "string" || input instanceof String) {
360+
textInput = input;
361+
} else if (Array.isArray(input)) {
362+
textInput = [];
363+
for (const x of input) {
364+
if (typeof x.text === "string" || x.text instanceof String) {
365+
textInput.push(x.text);
366+
} else if (typeof x === "string" || x instanceof String) {
367+
textInput.push(x);
368+
}
369+
}
370+
} else {
371+
return {
372+
contextTexts: [],
373+
sources: [],
374+
message: "Invalid query - the type of input is not string or array !",
375+
};
376+
}
377+
const queryVector = await LLMConnector.embedTextInput(textInput);
359378
const { contextTexts, sourceDocuments } = await this.similarityResponse({
360379
client,
361380
namespace,

server/utils/vectorDbProviders/lance/index.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,12 +400,31 @@ const LanceDb = {
400400
};
401401
}
402402

403-
const queryVector = await LLMConnector.embedTextInput(input);
403+
let textInput = "";
404+
if (typeof input === "string" || input instanceof String) {
405+
textInput = input;
406+
} else if (Array.isArray(input)) {
407+
textInput = [];
408+
for (const x of input) {
409+
if (typeof x.text === "string" || x.text instanceof String) {
410+
textInput.push(x.text);
411+
} else if (typeof x === "string" || x instanceof String) {
412+
textInput.push(x);
413+
}
414+
}
415+
} else {
416+
return {
417+
contextTexts: [],
418+
sources: [],
419+
message: "Invalid query - the type of input is not string or array !",
420+
};
421+
}
422+
const queryVector = await LLMConnector.embedTextInput(textInput);
404423
const result = rerank
405424
? await this.rerankedSimilarityResponse({
406425
client,
407426
namespace,
408-
query: input,
427+
query: textInput,
409428
queryVector,
410429
similarityThreshold,
411430
topN,

server/utils/vectorDbProviders/milvus/index.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,26 @@ const Milvus = {
311311
};
312312
}
313313

314-
const queryVector = await LLMConnector.embedTextInput(input);
314+
let textInput = "";
315+
if (typeof input === "string" || input instanceof String) {
316+
textInput = input;
317+
} else if (Array.isArray(input)) {
318+
textInput = [];
319+
for (const x of input) {
320+
if (typeof x.text === "string" || x.text instanceof String) {
321+
textInput.push(x.text);
322+
} else if (typeof x === "string" || x instanceof String) {
323+
textInput.push(x);
324+
}
325+
}
326+
} else {
327+
return {
328+
contextTexts: [],
329+
sources: [],
330+
message: "Invalid query - the type of input is not string or array !",
331+
};
332+
}
333+
const queryVector = await LLMConnector.embedTextInput(textInput);
315334
const { contextTexts, sourceDocuments } = await this.similarityResponse({
316335
client,
317336
namespace,

server/utils/vectorDbProviders/pinecone/index.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,26 @@ const PineconeDB = {
253253
"Invalid namespace - has it been collected and populated yet?"
254254
);
255255

256-
const queryVector = await LLMConnector.embedTextInput(input);
256+
let textInput = "";
257+
if (typeof input === "string" || input instanceof String) {
258+
textInput = input;
259+
} else if (Array.isArray(input)) {
260+
textInput = [];
261+
for (const x of input) {
262+
if (typeof x.text === "string" || x.text instanceof String) {
263+
textInput.push(x.text);
264+
} else if (typeof x === "string" || x instanceof String) {
265+
textInput.push(x);
266+
}
267+
}
268+
} else {
269+
return {
270+
contextTexts: [],
271+
sources: [],
272+
message: "Invalid query - the type of input is not string or array !",
273+
};
274+
}
275+
const queryVector = await LLMConnector.embedTextInput(textInput);
257276
const { contextTexts, sourceDocuments } = await this.similarityResponse({
258277
client: pineconeIndex,
259278
namespace,

server/utils/vectorDbProviders/qdrant/index.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,26 @@ const QDrant = {
336336
};
337337
}
338338

339-
const queryVector = await LLMConnector.embedTextInput(input);
339+
let textInput = "";
340+
if (typeof input === "string" || input instanceof String) {
341+
textInput = input;
342+
} else if (Array.isArray(input)) {
343+
textInput = [];
344+
for (const x of input) {
345+
if (typeof x.text === "string" || x.text instanceof String) {
346+
textInput.push(x.text);
347+
} else if (typeof x === "string" || x instanceof String) {
348+
textInput.push(x);
349+
}
350+
}
351+
} else {
352+
return {
353+
contextTexts: [],
354+
sources: [],
355+
message: "Invalid query - the type of input is not string or array !",
356+
};
357+
}
358+
const queryVector = await LLMConnector.embedTextInput(textInput);
340359
const { contextTexts, sourceDocuments } = await this.similarityResponse({
341360
client,
342361
namespace,

server/utils/vectorDbProviders/weaviate/index.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,26 @@ const Weaviate = {
380380
};
381381
}
382382

383-
const queryVector = await LLMConnector.embedTextInput(input);
383+
let textInput = "";
384+
if (typeof input === "string" || input instanceof String) {
385+
textInput = input;
386+
} else if (Array.isArray(input)) {
387+
textInput = [];
388+
for (const x of input) {
389+
if (typeof x.text === "string" || x.text instanceof String) {
390+
textInput.push(x.text);
391+
} else if (typeof x === "string" || x instanceof String) {
392+
textInput.push(x);
393+
}
394+
}
395+
} else {
396+
return {
397+
contextTexts: [],
398+
sources: [],
399+
message: "Invalid query - the type of input is not string or array !",
400+
};
401+
}
402+
const queryVector = await LLMConnector.embedTextInput(textInput);
384403
const { contextTexts, sourceDocuments } = await this.similarityResponse({
385404
client,
386405
namespace,

server/utils/vectorDbProviders/zilliz/index.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,26 @@ const Zilliz = {
304304
};
305305
}
306306

307-
const queryVector = await LLMConnector.embedTextInput(input);
307+
let textInput = "";
308+
if (typeof input === "string" || input instanceof String) {
309+
textInput = input;
310+
} else if (Array.isArray(input)) {
311+
textInput = [];
312+
for (const x of input) {
313+
if (typeof x.text === "string" || x.text instanceof String) {
314+
textInput.push(x.text);
315+
} else if (typeof x === "string" || x instanceof String) {
316+
textInput.push(x);
317+
}
318+
}
319+
} else {
320+
return {
321+
contextTexts: [],
322+
sources: [],
323+
message: "Invalid query - the type of input is not string or array !",
324+
};
325+
}
326+
const queryVector = await LLMConnector.embedTextInput(textInput);
308327
const { contextTexts, sourceDocuments } = await this.similarityResponse({
309328
client,
310329
namespace,

0 commit comments

Comments
 (0)