-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
chat.mjs
112 lines (102 loc) · 3.56 KB
/
chat.mjs
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env node
import { createInterface } from 'node:readline'
import { promisify } from 'node:util'
import { EOL } from 'node:os'
import { Configuration, OpenAIApi } from 'openai'
import { parse } from 'm.args'
import say from 'say'
/* eslint-disable camelcase */
const configuration = new Configuration({ apiKey: process.env.npm_config_openai_secret })
// if system message is provided, then throttle should be set from 2 onwards since the system message is within the messages count
// if throttle is set to 1 no history will be sent to to gpt
// if throttle is set to 2 only the last message will be sent to gpt
const { system = '', throttle = Number.POSITIVE_INFINITY, talk = false } = parse(process.argv)
const stats = {
prompt: 0,
completion: 0,
total: 0,
push ({ prompt_tokens, completion_tokens, total_tokens }) {
this.prompt += prompt_tokens
this.completion += completion_tokens
this.total += total_tokens
}
}
const readLine = createInterface({ input: process.stdin, output: process.stdout })
const openAI = new OpenAIApi(configuration)
const speak = promisify(say.speak).bind(say)
const prompt = promisify(readLine.question).bind(readLine)
const createChatCompletion = (messages, model = 'gpt-3.5-turbo') => openAI.createChatCompletion({ model, messages })
const messages = [{ role: 'system', content: system }]
console.time('duration')
process.on('exit', () => {
process.stdout.clearLine()
process.stdout.clearLine()
console.log('\n---')
console.log('tokens: %d (prompt: %d, completion: %d)', stats.total, stats.prompt, stats.completion)
console.log('cost: $%s', (stats.total * 0.000002).toFixed(4))
console.log('date: %s', new Date().toISOString())
console.timeEnd('duration')
})
while (true) {
try {
messages.push({ role: 'user', content: await prompt('> ') })
const { data: { usage, choices: [{ message: { content } }] } } = await createChatCompletion([
{ role: 'system', content: system },
...messages
])
stats.push(usage)
readLine.write(content)
if (talk) await speak(content, 'Daniel', 0.175)
readLine.write(EOL + EOL)
} catch (err) {
console.error('Error:', err)
readLine.close()
} finally {
console.log('messages length:', messages.length)
if (messages.length > throttle) {
messages.splice(1, messages.length - throttle)
console.log('messages length:', messages.length)
}
}
}
// setImmediate(
// async function asyncRecursiveQuestionLoop (ai, rl, [system, ...messages]) {
// rl.question('> ', async content => {
// try {
// const {
// data: {
// usage: { prompt_tokens, completion_tokens, total_tokens },
// choices: [{ message }]
// }
// } = await ai.createChatCompletion({
// model: 'gpt-3.5-turbo',
// messages: [{
// role: 'system',
// content
// },
// ...messages,
// {
// role: 'user',
// content
// }]
// })
// rl.write(message.content)
// say.speak(message.content)
// usage.prompt_tokens += prompt_tokens
// usage.completion_tokens += completion_tokens
// usage.total_tokens += total_tokens
// } catch (error) {
// console.error('openai error:', error)
// } finally {
// asyncRecursiveQuestionLoop(ai, rl, [...messages, { role: 'user', content }])
// }
// })
// },
// ,
// ,
// [{
// role: 'system',
// content: process.argv.slice(2).join(String.fromCharCode(160))
// }]
// )
/* eslint-enable camelcase */