-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
63 lines (50 loc) · 1.89 KB
/
index.ts
File metadata and controls
63 lines (50 loc) · 1.89 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
import OpenAI from "openai";
import {QdrantClient} from '@qdrant/js-client-rest';
import * as readline from "node:readline";
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
const qdrant = new QdrantClient({
url: process.env.QDRANT_URL,
apiKey: process.env.QDRANT_API_KEY,
});
async function embed(text: string): Promise<number[]> {
const res = await openai.embeddings.create({
model: 'text-embedding-3-small',
input: text,
encoding_format: 'float'
});
return res.data[0].embedding;
}
async function chatLoop() {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
console.log("Bienvenue dans le chatbot support des paroles de Vald ! Tape 'exit' pour quitter.\n");
rl.on("line", async (userQuery: string) => {
if (userQuery.trim().toLowerCase() === "exit") {
rl.close();
return;
}
const queryVec = await embed(userQuery);
const hits = await qdrant.search("lyrics_support_channel", {
vector: queryVec,
limit: 1,
with_payload: true,
});
const context = hits[0]?.payload?.text ?? "Aucun contexte trouvé.";
const prompt = `
Tu es un chatbot chargé de faire du support pour une société d'explication de paroles de chanson.
Voici la question de l'utilisateur : ${userQuery}
Voici un extrait de chanson à utiliser comme contexte : ${context}
Réponds de façon claire, concise (plus concis que notre cher Victor).`;
const completion = await openai.chat.completions.create({
model: "gpt-4-turbo",
messages: [{ role: "user", content: prompt }],
});
console.log("\n→ Réponse du bot :", completion.choices[0].message.content);
console.log("\nPose une autre question ou tape 'exit' pour quitter.\n");
});
}
chatLoop();