-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchat-repl.ts
58 lines (50 loc) · 1.56 KB
/
chat-repl.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
import * as process from "node:process";
import * as readline from "node:readline/promises";
// import { Client } from "@commonbase/sdk";
import { Client } from "../index";
import { ChatMessage } from "../client/types";
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const client = new Client({
apiKey: process.env.CB_API_KEY!,
projectId: process.env.CB_PROJECT_ID,
});
async function main() {
console.log("Welcome to the Commonbase chat assistant!");
console.log("Type your question and press enter to send it.");
console.log("Type 'exit' or 'quit' to quit.");
const messages: ChatMessage[] = [];
// eslint-disable-next-line no-constant-condition
while (true) {
const question = (await rl.question("> ")).trim();
if (["exit", "quit"].includes(question)) {
break;
}
if (question.length === 0) {
console.log("Type your question and press enter to send it.");
continue;
}
messages.push({
role: "user",
content: question,
});
const completionStream = await client.streamChatCompletion({
messages,
userId: "anonymous-example-user",
provider: "cb-openai-eu",
});
for await (const completionResult of completionStream) {
if (completionResult.completed) {
process.stdout.write("\n\n");
messages.push(completionResult.bestChoice.toAssistantChatMessage());
continue;
}
process.stdout.write(completionResult.bestChoice.text);
}
}
console.log("bye 👋");
process.exit(0);
}
main().catch(console.error);