-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.ts
82 lines (68 loc) · 2.24 KB
/
index.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
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
80
81
82
import "dotenv/config";
import readline from "node:readline/promises";
import OpenAI from "openai";
import { LiteralClient } from "@literalai/client";
import { createPrompt, getEmbeddingsTable } from "./rag";
// Use OpenAI Completion API to generate and answer based on the context that LanceDB provides
const openai = new OpenAI();
// Initialize the Literal Client
const literalClient = new LiteralClient();
// Instrument the OpenAI API calls
literalClient.instrumentation.openai();
const run = async () => {
// You need to provide an OpenAI API key, here we read it from the OPENAI_API_KEY environment variable
const apiKey = process.env.OPENAI_API_KEY!;
// Get the embeddings table
const tbl = await getEmbeddingsTable(apiKey);
await literalClient.thread({ name: "LanceDB RAG" }).wrap(async () => {
// Create a readline interface to interact with the user
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
try {
while (true) {
const query = await rl.question("Prompt: ");
const modelResponse = await literalClient
.run({ name: "Run" })
.wrap(async () => {
const results = await literalClient
.step({
name: "Retrieve",
type: "retrieval",
input: { query },
})
.wrap(() =>
tbl
.search(query)
.select(["title", "text", "context"])
.limit(3)
.execute()
);
const response = await openai.chat.completions.create({
model: "gpt-4o-mini",
messages: [
{
role: "system",
content: createPrompt(query, results as any),
},
],
max_tokens: 400,
temperature: 0,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
});
return response.choices[0].message;
});
console.log(modelResponse.content);
console.log();
}
} catch (err) {
console.log("Error: ", err);
} finally {
rl.close();
}
});
};
run();