-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
84 lines (72 loc) · 2.22 KB
/
Copy pathindex.ts
File metadata and controls
84 lines (72 loc) · 2.22 KB
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
import "dotenv/config";
import OpenAI from "openai";
import readline from "readline";
import { createAssistant } from "./openai/createAssistant.js";
import { createThread } from "./openai/createThread.js";
import { createRun } from "./openai/createRun.js";
import { performRun } from "./openai/performRuns.js";
import type { Thread } from "openai/resources/beta/threads/threads";
import type { Assistant } from "openai/resources/beta/assistants";
const client = new OpenAI();
// Create interface for reading from command line
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
// Type-safe promise-based question function
const question = (query: string): Promise<string> => {
return new Promise((resolve) => rl.question(query, resolve));
};
async function chat(thread: Thread, assistant: Assistant): Promise<void> {
while (true) {
// Get user input
const userInput = await question("\nYou: ");
// Allow user to exit
if (userInput.toLowerCase() === "exit") {
rl.close();
break;
}
try {
// Add the user's message to the thread
await client.beta.threads.messages.create(thread.id, {
role: "user",
content: userInput,
});
// Create and perform the run
const run = await createRun(client, thread, assistant.id);
const result = await performRun(run, client, thread);
if (result?.type === "text") {
console.log("\nAlt:", result.text.value);
}
} catch (error) {
console.error(
"Error during chat:",
error instanceof Error ? error.message : "Unknown error"
);
rl.close();
break;
}
}
}
async function main(): Promise<void> {
try {
const assistant = await createAssistant(client);
const thread = await createThread(client);
console.log('Chat started! Type "exit" to end the conversation.');
await chat(thread, assistant);
} catch (error) {
console.error(
"Error in main:",
error instanceof Error ? error.message : "Unknown error"
);
rl.close();
process.exit(1);
}
}
main().catch((error) => {
console.error(
"Unhandled error:",
error instanceof Error ? error.message : "Unknown error"
);
process.exit(1);
});