-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.js
More file actions
56 lines (42 loc) · 1.53 KB
/
chat.js
File metadata and controls
56 lines (42 loc) · 1.53 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
import readline from "node:readline/promises";
import Groq from "groq-sdk";
import { vectorStore } from "./prepare.js";
const groq = new Groq({ apiKey: process.env.GROQ_API_KEY });
export async function chat() {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
while (true) {
const question = await rl.question("You: ");
if (question === "bye") {
break;
}
// 🔍 Retrieval
const relevantChunks = await vectorStore.similaritySearch(question, 3);
if (!relevantChunks || relevantChunks.length === 0) {
console.log("I don't know based on the provided information.");
continue;
}
const context = relevantChunks
.map((chunk) => chunk.pageContent)
.join("\n\n");
// ✅ SAFE SYSTEM PROMPT (NO JSON / NO COMMA ISSUE)
const SYSTEM_PROMPT =
"You are a company internal assistant. Answer only from the given context. Do not use external knowledge. Do not guess or assume anything. If the answer is not clearly present in the context, reply exactly: I don't know based on the provided information. Keep the answer short and factual.";
const userQuery = `Context:
${context}
Question:
${question}`;
const completion = await groq.chat.completions.create({
model: "openai/gpt-oss-20b",
messages: [
{ role: "system", content: SYSTEM_PROMPT },
{ role: "user", content: userQuery },
],
});
console.log("\nAssistant:", completion.choices[0].message.content, "\n");
}
rl.close();
}
chat();