-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathrag-cars.js
79 lines (61 loc) · 2.54 KB
/
rag-cars.js
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
79
// This example demonstrates how to use the Retrieval Augmented Generation (RAG)
// to answer questions based on a hybrid car data set.
// The code below reads the CSV file, searches for matches to the user question,
// and then generates a response based on the information found.
import { fileURLToPath } from 'node:url';
import { dirname } from 'node:path';
import process from "node:process";
import fs from "node:fs";
import { OpenAI } from "openai";
// Change the current working directory to the directory of the script
const __dirname = dirname(fileURLToPath(import.meta.url));
process.chdir(__dirname);
// 1. Ask a question about hybrid cars
// -----------------------------------
const question = `what's the fastest prius`;
// 2. Retriever component: search the data for relevant information
// ----------------------------------------------------------------
// Load CSV data as an array of objects
const rows = fs.readFileSync("./hybrid.csv", "utf8").split("\n");
const columns = rows[0].split(",");
// Search the data using a very naive search
const words = question
.toLowerCase()
.replaceAll(/[.?!()'":,]/g, "")
.split(" ")
.filter((word) => word.length > 2);
const matches = rows.slice(1).filter((row) => words.some((word) => row.toLowerCase().includes(word)));
// Format as a markdown table, since language models understand markdown
const table =
`| ${columns.join(" | ")} |\n` +
`|${columns.map(() => "---").join(" | ")}|\n` +
matches.map((row) => `| ${row.replaceAll(",", " | ")} |\n`).join("");
console.log(`Found ${matches.length} matches:`);
console.log(table);
// 3. Context augmentation: create a combined prompt with the search results
// --------------------------------------------------------------------------
const augmentedPrompt = `
## Instructions
Answer questions about hybrid cars using only the sources below.
If there's not enough data in provided sources, say that you don't know.
Be brief and straight to the point.
## Sources
${table}
## Question
${question}
`;
// 4. Generator component: use the search results to generate a response
// ---------------------------------------------------------------------
const openai = new OpenAI({
baseURL: "https://models.inference.ai.azure.com",
apiKey: process.env.GITHUB_TOKEN,
});
const chunks = await openai.chat.completions.create({
model: "gpt-4o-mini",
messages: [{ role: "user", content: augmentedPrompt }],
stream: true,
});
console.log(`Answer for "${question}":`);
for await (const chunk of chunks) {
process.stdout.write(chunk.choices[0].delta.content ?? "");
}