-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathmulti_turn.js
More file actions
34 lines (25 loc) · 883 Bytes
/
multi_turn.js
File metadata and controls
34 lines (25 loc) · 883 Bytes
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
import OpenAI from "openai";
import readline from "readline";
const openai = new OpenAI();
// Create a readline interface to read user input from the terminal
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const messages = [{ role: "system", content: "You are a helpful assistant." }];
// Send an array of messages to the Chat Completions API
async function chat() {
rl.question("You: ", async (input) => {
messages.push({ role: "user", content: input });
const completion = await openai.chat.completions.create({
model: "gpt-4o-mini",
messages: messages,
});
const response = completion.choices[0].message.content;
console.log(`Assistant: ${response}`);
messages.push({ role: "assistant", content: response });
chat();
});
}
// Start a multi-turn conversation in the terminal
chat();