forked from Y-AIFE/ChatGPT-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo-with-prompt2.ts
60 lines (57 loc) · 1.54 KB
/
demo-with-prompt2.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
import apiKey from './.key'
import { ChatGPT } from '../src'
import { getReadLine } from '../src/utils'
const api = new ChatGPT({
apiKey: apiKey, // get api key
debug: true,
requestConfig: {
proxy: {
protocol: 'http',
host: '127.0.0.1',
port: 7890,
},
},
})
void (async function () {
console.log('---------------------your question------------------')
console.log({ text: '说说你的看法', systemPrompt: '你是一个前端技术专家' })
let prevRes: any
await api.sendMessage({
text: '说说你对未来几年前端发展的方向会有哪些',
systemPrompt: '你是一个前端技术专家',
onProgress(e) {
console.log('[onProgress1]', e)
}, onEnd(e) {
console.log('[onEnd1]', e)
prevRes = e.data
}
})
console.log(prevRes && prevRes.data)
let line = ''
const readline = getReadLine()
console.log('---------------------your question------------------')
while ((line = await readline())) {
prevRes = await basicRunner(
line,
prevRes.id,
)
console.log('---------------------ChatGPT says------------------------')
console.log(prevRes)
console.log('---------------------your question------------------')
}
})()
function basicRunner(text: string, parentMessageId?: string) {
return new Promise((resolve, reject) => {
api.sendMessage({
text,
parentMessageId,
onProgress(e) {
console.log('[onProgress2]', e)
},
onEnd(e) {
console.log('[onEnd]2', e)
resolve(e.data)
},
})
})
}