-
Notifications
You must be signed in to change notification settings - Fork 1k
/
index.ts
144 lines (125 loc) · 4.01 KB
/
index.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { DEFAULT_CHATGPT_SYSTEM_MESSAGE } from '~app/consts'
import { UserConfig } from '~services/user-config'
import { ChatError, ErrorCode } from '~utils/errors'
import { parseSSEResponse } from '~utils/sse'
import { AbstractBot, SendMessageParams } from '../abstract-bot'
import { ChatMessage } from './consts'
import { updateTokenUsage } from './usage'
interface ConversationContext {
messages: ChatMessage[]
}
const CONTEXT_SIZE = 9
export abstract class AbstractChatGPTApiBot extends AbstractBot {
private conversationContext?: ConversationContext
buildMessages(prompt: string): ChatMessage[] {
const currentDate = new Date().toISOString().split('T')[0]
const systemMessage = this.getSystemMessage().replace('{current_date}', currentDate)
return [
{ role: 'system', content: systemMessage },
...this.conversationContext!.messages.slice(-(CONTEXT_SIZE + 1)),
{ role: 'user', content: prompt },
]
}
getSystemMessage() {
return DEFAULT_CHATGPT_SYSTEM_MESSAGE
}
async doSendMessage(params: SendMessageParams) {
if (!this.conversationContext) {
this.conversationContext = { messages: [] }
}
const resp = await this.fetchCompletionApi(this.buildMessages(params.prompt), params.signal)
this.conversationContext.messages.push({
role: 'user',
content: params.rawUserInput || params.prompt,
})
let done = false
const result: ChatMessage = { role: 'assistant', content: '' }
const finish = () => {
done = true
params.onEvent({ type: 'DONE' })
const messages = this.conversationContext!.messages
messages.push(result)
updateTokenUsage(messages).catch(console.error)
}
await parseSSEResponse(resp, (message) => {
console.debug('chatgpt sse message', message)
if (message === '[DONE]') {
finish()
return
}
let data
try {
data = JSON.parse(message)
} catch (err) {
console.error(err)
return
}
if (data?.choices?.length) {
const delta = data.choices[0].delta
if (delta?.content) {
result.content += delta.content
params.onEvent({
type: 'UPDATE_ANSWER',
data: { text: result.content },
})
}
}
})
if (!done) {
finish()
}
}
resetConversation() {
this.conversationContext = undefined
}
abstract fetchCompletionApi(messages: ChatMessage[], signal?: AbortSignal): Promise<Response>
}
export class ChatGPTApiBot extends AbstractChatGPTApiBot {
constructor(
private config: Pick<
UserConfig,
'openaiApiKey' | 'openaiApiHost' | 'chatgptApiModel' | 'chatgptApiTemperature' | 'chatgptApiSystemMessage'
>,
) {
super()
}
getSystemMessage() {
return this.config.chatgptApiSystemMessage || DEFAULT_CHATGPT_SYSTEM_MESSAGE
}
async fetchCompletionApi(messages: ChatMessage[], signal?: AbortSignal) {
const { openaiApiKey, openaiApiHost, chatgptApiModel } = this.config
const resp = await fetch(`${openaiApiHost}/v1/chat/completions`, {
method: 'POST',
signal,
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${openaiApiKey}`,
},
body: JSON.stringify({
model: this.getModelName(),
messages,
stream: true,
}),
})
if (!resp.ok && resp.status === 404 && chatgptApiModel.includes('gpt-4')) {
throw new ChatError(`You don't have API access to ${chatgptApiModel} model`, ErrorCode.GPT4_MODEL_WAITLIST)
}
if (!resp.ok) {
const error = await resp.text()
if (error.includes('insufficient_quota')) {
throw new ChatError('Insufficient ChatGPT API usage quota', ErrorCode.CHATGPT_INSUFFICIENT_QUOTA)
}
}
return resp
}
private getModelName() {
const { chatgptApiModel } = this.config
if (chatgptApiModel === 'gpt-4-turbo') {
return 'gpt-4-1106-preview'
}
return chatgptApiModel
}
get name() {
return `ChatGPT (API/${this.config.chatgptApiModel})`
}
}