forked from Y-AIFE/ChatGPT-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo-clear-conversation.ts
46 lines (43 loc) · 1.09 KB
/
demo-clear-conversation.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
import { getReadLine } from '../src/utils'
import { ChatGPT } from '../src'
import apiKey from './.key'
void (async function () {
let prevRes: any
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.text)
console.log('---------------------your question------------------')
}
})()
const api = new ChatGPT({
apiKey,
debug: true,
requestConfig: {
proxy: {
protocol: 'http',
host: '127.0.0.1',
port: 33210,
},
},
})
async function basicRunner(text: string, parentMessageId?: string) {
try {
if (/@clear@/.test(text)) {
await api.clear1Conversation(parentMessageId)
console.log('messages cleared')
} else {
const res = await api.sendMessage({
text,
parentMessageId,
})
return res
}
} catch (e: any) {
console.log('e', e.message)
return {}
}
}