这是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
8 changes: 8 additions & 0 deletions server/utils/vectorDbProviders/chroma/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ const Chroma = {
}
return totalVectors;
},
distanceToSimilarity: function (distance = null) {
if (distance === null || typeof distance !== "number") return 0.0;
if (distance >= 1.0) return 1;
if (distance <= 0) return 0;
return 1 - distance;
},
namespaceCount: async function (_namespace = null) {
const { client } = await this.connect();
const namespace = await this.namespace(client, _namespace);
Expand All @@ -59,6 +65,7 @@ const Chroma = {
const result = {
contextTexts: [],
sourceDocuments: [],
scores: [],
};

const response = await collection.query({
Expand All @@ -68,6 +75,7 @@ const Chroma = {
response.ids[0].forEach((_, i) => {
result.contextTexts.push(response.documents[0][i]);
result.sourceDocuments.push(response.metadatas[0][i]);
result.scores.push(this.distanceToSimilarity(response.distances[0][i]));
});

return result;
Expand Down
8 changes: 8 additions & 0 deletions server/utils/vectorDbProviders/lance/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ const LanceDb = {
const client = await lancedb.connect(this.uri);
return { client };
},
distanceToSimilarity: function (distance = null) {
if (distance === null || typeof distance !== "number") return 0.0;
if (distance >= 1.0) return 1;
if (distance <= 0) return 0;
return 1 - distance;
},
heartbeat: async function () {
await this.connect();
return { heartbeat: Number(new Date()) };
Expand Down Expand Up @@ -54,6 +60,7 @@ const LanceDb = {
const result = {
contextTexts: [],
sourceDocuments: [],
scores: [],
};

const response = await collection
Expand All @@ -66,6 +73,7 @@ const LanceDb = {
const { vector: _, ...rest } = item;
result.contextTexts.push(rest.text);
result.sourceDocuments.push(rest);
result.scores.push(this.distanceToSimilarity(item.score));
});

return result;
Expand Down
2 changes: 2 additions & 0 deletions server/utils/vectorDbProviders/pinecone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const Pinecone = {
const result = {
contextTexts: [],
sourceDocuments: [],
scores: [],
};
const response = await index.query({
queryRequest: {
Expand All @@ -54,6 +55,7 @@ const Pinecone = {
response.matches.forEach((match) => {
result.contextTexts.push(match.metadata.text);
result.sourceDocuments.push(match);
result.scores.push(match.score);
});

return result;
Expand Down
3 changes: 3 additions & 0 deletions server/utils/vectorDbProviders/qdrant/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ const QDrant = {
const result = {
contextTexts: [],
sourceDocuments: [],
scores: [],
};

const responses = await client.search(namespace, {
vector: queryVector,
limit: 4,
with_payload: true,
});

responses.forEach((response) => {
Expand All @@ -64,6 +66,7 @@ const QDrant = {
...(response?.payload || {}),
id: response.id,
});
result.scores.push(response.score);
});

return result;
Expand Down
6 changes: 4 additions & 2 deletions server/utils/vectorDbProviders/weaviate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,15 @@ const Weaviate = {
const result = {
contextTexts: [],
sourceDocuments: [],
scores: [],
};

const weaviateClass = await this.namespace(client, namespace);
const fields = weaviateClass.properties.map((prop) => prop.name).join(" ");
const queryResponse = await client.graphql
.get()
.withClassName(camelCase(namespace))
.withFields(`${fields} _additional { id }`)
.withFields(`${fields} _additional { id certainty }`)
.withNearVector({ vector: queryVector })
.withLimit(4)
.do();
Expand All @@ -94,11 +95,12 @@ const Weaviate = {
// In Weaviate we have to pluck id from _additional and spread it into the rest
// of the properties.
const {
_additional: { id },
_additional: { id, certainty },
...rest
} = response;
result.contextTexts.push(rest.text);
result.sourceDocuments.push({ ...rest, id });
result.scores.push(certainty);
});

return result;
Expand Down