forked from Y-AIFE/ChatGPT-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo-in-cmd-nostream-nostore.ts
62 lines (58 loc) · 1.72 KB
/
demo-in-cmd-nostream-nostore.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
59
60
61
62
import { getReadLine, genId } from '../src/utils'
import {
ChatGPT,
IChatCompletionStreamOnEndData,
IChatGPTResponse,
TCommonMessage,
IChatGPTUserMessage,
ERole,
} from '../src'
import apiKey from './.key'
const api = new ChatGPT({
apiKey,
debug: true,
requestConfig: {
proxy: {
protocol: 'http',
host: '127.0.0.1',
port: 7890,
},
},
})
const messages: TCommonMessage[] = []
function genUserMsg(text: string, parentMessageId?: string) :IChatGPTUserMessage{
return {
id: genId(),
text,
role: ERole.user,
parentMessageId,
}
}
function pushMsg(msgs: TCommonMessage[], msg: TCommonMessage) {
if(msgs.length) {
msg.parentMessageId = msgs[msgs.length - 1].id
}
msgs.push(msg)
}
pushMsg(messages, genUserMsg('你好'))
void (async function () {
let prevRes = (await basicRunner(messages)) as IChatCompletionStreamOnEndData
console.log('---------------------ChatGPT says------------------------')
console.log((prevRes.data as IChatGPTResponse).text)
pushMsg(messages, prevRes.data as IChatGPTResponse)
let line = ''
const readline = getReadLine()
console.log('---------------------your question------------------')
while ((line = await readline())) {
pushMsg(messages, genUserMsg(line))
prevRes = await basicRunner(messages) as IChatCompletionStreamOnEndData
console.log('---------------------ChatGPT says------------------------')
console.log((prevRes.data as IChatGPTResponse).text)
pushMsg(messages, prevRes.data as IChatGPTResponse)
console.log('---------------------your question------------------')
}
})()
async function basicRunner(msgs: TCommonMessage[]) {
return await api.sendMessage(msgs)
}
// @todo stream chat initialMessages 测试