mirror of
https://github.com/Mintplex-Labs/anything-llm.git
synced 2024-11-05 06:20:10 +01:00
282 return relevancy score with similarityresponse (#304)
* include score value in similarityResponse for weaviate * include score value in si milarityResponse for qdrant * include score value in si milarityResponse for pinecone * include score value in similarityResponse for chroma * include score value in similarityResponse for lancedb * distance to similarity --------- Co-authored-by: timothycarambat <rambat1010@gmail.com>
This commit is contained in:
parent
26dba59249
commit
669d7a396d
@ -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);
|
||||
@ -59,6 +65,7 @@ const Chroma = {
|
||||
const result = {
|
||||
contextTexts: [],
|
||||
sourceDocuments: [],
|
||||
scores: [],
|
||||
};
|
||||
|
||||
const response = await collection.query({
|
||||
@ -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;
|
||||
|
@ -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()) };
|
||||
@ -54,6 +60,7 @@ const LanceDb = {
|
||||
const result = {
|
||||
contextTexts: [],
|
||||
sourceDocuments: [],
|
||||
scores: [],
|
||||
};
|
||||
|
||||
const response = await collection
|
||||
@ -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;
|
||||
|
@ -41,6 +41,7 @@ const Pinecone = {
|
||||
const result = {
|
||||
contextTexts: [],
|
||||
sourceDocuments: [],
|
||||
scores: [],
|
||||
};
|
||||
const response = await index.query({
|
||||
queryRequest: {
|
||||
@ -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;
|
||||
|
@ -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) => {
|
||||
@ -64,6 +66,7 @@ const QDrant = {
|
||||
...(response?.payload || {}),
|
||||
id: response.id,
|
||||
});
|
||||
result.scores.push(response.score);
|
||||
});
|
||||
|
||||
return result;
|
||||
|
@ -77,6 +77,7 @@ const Weaviate = {
|
||||
const result = {
|
||||
contextTexts: [],
|
||||
sourceDocuments: [],
|
||||
scores: [],
|
||||
};
|
||||
|
||||
const weaviateClass = await this.namespace(client, namespace);
|
||||
@ -84,7 +85,7 @@ const Weaviate = {
|
||||
const queryResponse = await client.graphql
|
||||
.get()
|
||||
.withClassName(camelCase(namespace))
|
||||
.withFields(`${fields} _additional { id }`)
|
||||
.withFields(`${fields} _additional { id certainty }`)
|
||||
.withNearVector({ vector: queryVector })
|
||||
.withLimit(4)
|
||||
.do();
|
||||
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user