-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.ts
42 lines (37 loc) · 1.24 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { Pinecone } from "@pinecone-database/pinecone";
import { FeatureExtractionPipeline, pipeline } from "@xenova/transformers";
import { modelname, namespace, topK } from "./app/config";
import { HfInference } from '@huggingface/inference'
const hf = new HfInference(process.env.HF_TOKEN)
export async function queryPineconeVectorStore(
client: Pinecone,
indexName: string,
namespace: string,
query: string
): Promise<string> {
const apiOutput = await hf.featureExtraction({
model: "mixedbread-ai/mxbai-embed-large-v1",
inputs: query,
});
console.log(apiOutput);
const queryEmbedding = Array.from(apiOutput);
// console.log("Querying database vector store...");
const index = client.Index(indexName);
const queryResponse = await index.namespace(namespace).query({
topK: 5,
vector: queryEmbedding as any,
includeMetadata: true,
// includeValues: true,
includeValues: false
});
console.log(queryResponse);
if (queryResponse.matches.length > 0) {
const concatenatedRetrievals = queryResponse.matches
.map((match,index) =>`\nClinical Finding ${index+1}: \n ${match.metadata?.chunk}`)
.join(". \n\n");
return concatenatedRetrievals;
} else {
return "<nomatches>";
}
return "";
}