-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
153 lines (130 loc) · 3.22 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import {
OpenAI,
Metadata,
Settings,
Document,
OpenAIAgent,
FunctionTool,
QueryEngineTool,
VectorStoreIndex,
LlamaParseReader,
QdrantVectorStore,
HuggingFaceEmbedding,
} from "llamaindex";
import fs from "node:fs/promises";
import 'dotenv/config'
import readline from 'readline';
async function main() {
Settings.llm = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
model: "gpt-3.5-turbo",
});
const PARSING_CACHE = "./cache.json"
const vectorStore = new QdrantVectorStore({
url: "http://localhost:6333",
});
Settings.callbackManager.on("llm-tool-call", (event) => {
// console.log(event.detail)
});
Settings.callbackManager.on("llm-tool-result", (event) => {
// console.log(event.detail)
});
Settings.embedModel = new HuggingFaceEmbedding({
modelType: "BAAI/bge-small-en-v1.5",
quantized: false,
});
// load cache.json and parse it
let cache = {};
let cacheExists = false;
try {
await fs.access(PARSING_CACHE, fs.constants.F_OK);
cacheExists = true;
} catch (e) {
console.log("No cache found");
}
if (cacheExists) {
cache = JSON.parse(await fs.readFile(PARSING_CACHE, "utf-8"));
}
const filesToParse = [
"./data/ICS_EUR_Ukraine_29AUG2023_PUBLIC.pdf"
];
// load our data, reading only files we haven't seen before
let documents: Document<Metadata>[] = [];
const reader = new LlamaParseReader({
resultType: "markdown",
language: "en"
});
for (let file of filesToParse) {
if (!cache[file]) {
documents = documents.concat(await reader.loadData(file));
cache[file] = true;
}
}
// write the cache back to disk
await fs.writeFile(PARSING_CACHE, JSON.stringify(cache));
const index = await VectorStoreIndex.fromDocuments(documents, {
vectorStores: {
"TEXT": vectorStore
}
});
const retriever = await index.asRetriever({
similarityTopK: 10
});
const queryEngine = await index.asQueryEngine({
retriever,
});
const sumNumbers = ({ a, b }) => {
return `${a + b}`;
};
const tools = [
new QueryEngineTool({
queryEngine: queryEngine,
metadata: {
name: "ukraines_strengthens_tool",
description: `This tool can answer detailed questions about the Ukraine's strengthens.`,
},
}),
FunctionTool.from(sumNumbers, {
name: "sumNumbers",
description: "Use this function to sum two numbers",
parameters: {
type: "object",
properties: {
a: {
type: "number",
description: "First number to sum",
},
b: {
type: "number",
description: "Second number to sum",
},
},
required: ["a", "b"],
},
}),
];
const agent = new OpenAIAgent({ tools });
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const askQuestion = (query) => {
return new Promise((resolve) => {
rl.question(query, (answer) => {
resolve(answer);
});
});
};
while (true) {
const question = await askQuestion("Enter your question (or 'exit' to quit): ");
if (question.toLowerCase() === 'exit') {
console.log("Exiting the program.");
rl.close();
break;
}
const response = await agent.chat({ message: question });
console.log("Response:", response.message.content);
console.log();
}
}
main().catch(console.error);