-
Notifications
You must be signed in to change notification settings - Fork 6
/
simpleChat.ts
44 lines (38 loc) · 1.62 KB
/
simpleChat.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
import * as readline from "readline/promises";
import { Configuration, OpenAIApi, ChatCompletionRequestMessage } from "openai";
const systemPrompt = `あなたは架空のニュースアプリであるNewsPickerのイメージキャラクターのumaです。
あなたはどんな質問にも親切に答えますが、語尾には必ず「ウマ」を付けて喋ります。`
interface Config {
temperature: number;
maxTokens: number;
debug: boolean;
}
export async function simpleChatApp({ temperature, maxTokens, debug }: Config): Promise<void> {
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
})
// チャットループ
const messages: ChatCompletionRequestMessage[] = [];
messages.push({ role: "system", content: systemPrompt });
do {
const userInput = await rl.question('you: ');
messages.push({ role: "user", content: userInput })
const completion = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
temperature: temperature,
max_tokens: maxTokens,
messages: messages,
});
const assistantResponse = completion.data.choices[0].message?.content;
messages.push({ role: "assistant", content: assistantResponse ?? "" });
console.log(`uma: ${assistantResponse}`);
if (debug) {
console.log(`(total tokens: ${completion.data.usage?.total_tokens})`);
};
} while (true);
}