-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathquery-rag.js
More file actions
78 lines (62 loc) · 1.99 KB
/
Copy pathquery-rag.js
File metadata and controls
78 lines (62 loc) · 1.99 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { createInterface } from "node:readline/promises";
import { Pinecone } from "@pinecone-database/pinecone";
import OpenAI from "openai";
const OPENAI_API_KEY = process.env.OPENAI_API_KEY;
if (!OPENAI_API_KEY) {
throw new Error("Missing OpenAI API key");
}
const PINECONE_API_KEY = process.env.PINECONE_API_KEY;
if (!PINECONE_API_KEY) {
throw new Error("Missing Pinecone API key");
}
const openai = new OpenAI({
baseURL: process.env.OPENAI_API_ENDPOINT,
apiKey: OPENAI_API_KEY,
});
const pc = new Pinecone({
apiKey: PINECONE_API_KEY,
});
const rl = createInterface({
input: process.stdin,
output: process.stdout,
});
const query = await rl.question("Your question: ");
rl.close();
const index = pc.index("demo");
const db = index.namespace("employees1");
const queryEmbedding = await openai.embeddings.create({
model: "text-embedding-3-small",
input: query,
dimensions: 1536,
});
const queryVector = queryEmbedding.data[0].embedding;
const response = await db.namespace("employees1").query({
vector: queryVector,
topK: 5,
includeMetadata: true,
});
const employeeData = response.matches.map((match) => match.metadata);
const aiResponse = await openai.chat.completions.create({
model: process.env.OPENAI_MODEL_NAME || "gpt-4.1-mini",
messages: [
{
role: "system",
content:
"You are a helpful assistant that answers questions about employees based on provided data.",
},
{
role: "user",
content: `The user's query / question was:\n${query}\n\nThe following data was retrieved related to this query:\n${JSON.stringify(
employeeData
)}\n\nPlease provide a detailed response to the user's query based on the retrieved data.`,
},
],
});
console.log(aiResponse.choices[0].message.content);
// Your question: who's working in marketing?
// The employees working in the Marketing department are:
// 1. Quinn (ID: 17)
// 2. Grace (ID: 7)
// 3. Liam (ID: 12)
// 4. Bob (ID: 2)
// Maria (ID: 13) is in the Sales department, not Marketing.