θΏ™ζ˜―indexlocζδΎ›ηš„ζœεŠ‘οΌŒδΈθ¦θΎ“ε…₯任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"chalk": "^4",
"check-disk-space": "^3.4.0",
"cheerio": "^1.0.0",
"chromadb": "^1.5.2",
"chromadb": "^2.0.1",
"cohere-ai": "^7.9.5",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
Expand Down
53 changes: 32 additions & 21 deletions server/utils/vectorDbProviders/chroma/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,10 @@ const Chroma = {
queryEmbeddings: queryVector,
nResults: topN,
});

response.ids[0].forEach((_, i) => {
if (
this.distanceToSimilarity(response.distances[0][i]) <
similarityThreshold
)
return;
const similarity = this.distanceToSimilarity(response.distances[0][i]);
if (similarity < similarityThreshold) return;

if (
filterIdentifiers.includes(sourceIdentifier(response.metadatas[0][i]))
Expand All @@ -144,9 +142,10 @@ const Chroma = {
);
return;
}

result.contextTexts.push(response.documents[0][i]);
result.sourceDocuments.push(response.metadatas[0][i]);
result.scores.push(this.distanceToSimilarity(response.distances[0][i]));
result.scores.push(similarity);
});

return result;
Expand Down Expand Up @@ -200,6 +199,7 @@ const Chroma = {
const { client } = await this.connect();
const collection = await client.getOrCreateCollection({
name: this.normalize(namespace),
// returns [-1, 1] unit vector
metadata: { "hnsw:space": "cosine" },
});
const { chunks } = cacheResult;
Expand Down Expand Up @@ -299,13 +299,18 @@ const Chroma = {

if (vectors.length > 0) {
const chunks = [];

console.log("Inserting vectorized chunks into Chroma collection.");
for (const chunk of toChunks(vectors, 500)) chunks.push(chunk);

const additionResult = await collection.add(submission);
if (!additionResult)
throw new Error("Error embedding into ChromaDB", additionResult);
try {
await collection.add(submission);
console.log(
`Successfully added ${submission.ids.length} vectors to collection ${this.normalize(namespace)}`
);
} catch (error) {
console.error("Error adding to ChromaDB:", error);
throw new Error(`Error embedding into ChromaDB: ${error.message}`);
}

await storeVectorResult(chunks, fullFilePath);
}
Expand Down Expand Up @@ -356,18 +361,24 @@ const Chroma = {
}

const queryVector = await LLMConnector.embedTextInput(input);
const { contextTexts, sourceDocuments } = await this.similarityResponse({
client,
namespace,
queryVector,
similarityThreshold,
topN,
filterIdentifiers,
});
const { contextTexts, sourceDocuments, scores } =
await this.similarityResponse({
client,
namespace,
queryVector,
similarityThreshold,
topN,
filterIdentifiers,
});

const sources = sourceDocuments.map((metadata, i) => ({
metadata: {
...metadata,
text: contextTexts[i],
score: scores?.[i] || null,
},
}));

const sources = sourceDocuments.map((metadata, i) => {
return { metadata: { ...metadata, text: contextTexts[i] } };
});
return {
contextTexts,
sources: this.curateSources(sources),
Expand Down
Loading