-
Notifications
You must be signed in to change notification settings - Fork 101
/
index.ts
59 lines (49 loc) · 2.01 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
import chalk from "chalk";
import { ChromaClient, Collection, OpenAIEmbeddingFunction } from "chromadb";
import * as console from "console";
import * as dotenv from "dotenv";
import * as readline from "readline";
import { addMemory, answerFromMemory, answerFromSearch, checkForBrain } from "./utils/index.js";
dotenv.config();
(async () => {
const client = new ChromaClient();
const embedder = new OpenAIEmbeddingFunction(process.env.OPENAI_API_KEY);
// await client.deleteCollection(process.env.COLLECTION_NAME); // ONLY UNCOMMENT IF YOU WANT TO RESET THE BRAIN
const brain: Collection = await checkForBrain(client, embedder);
console.log(chalk.yellow(`\nMemory count: ${await brain.count()}\n`));
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const askQuestion = () => {
rl.question(chalk.blue("\nWhat would you like to know?\n"), async (input) => {
console.log(chalk.yellow(`\nRecalling...\n`));
const memoryAnswer = await answerFromMemory(brain, input);
if (memoryAnswer.includes("INSUFFICIENT_DATA")) {
console.log(chalk.yellow(`\nInsuffient memories. Now learning...\n`));
const searchAnswer = await answerFromSearch(brain, input);
console.log(chalk.green(`${searchAnswer}\n`));
if (process.env.REVIEW_MEMORIES === "true") {
rl.question(chalk.blue("\nIs this answer accurate? (y/n)\n"), async (review) => {
if (review === "y") {
await addMemory(brain, searchAnswer);
console.log(chalk.yellow(`\nAdded memory!\n`));
} else {
console.log(chalk.yellow(`\nMemory discarded.\n`));
}
askQuestion();
});
} else {
await addMemory(brain, searchAnswer);
console.log(chalk.yellow(`\nAdded memory!\n`));
askQuestion();
}
askQuestion();
} else {
console.log(chalk.green(`\n${memoryAnswer}\n`));
askQuestion();
}
});
};
askQuestion();
})();