-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathopenai.ts
282 lines (262 loc) · 8.09 KB
/
openai.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import OpenAI from 'openai'
import { AsyncIterableX, from, last } from 'ix/asynciterable'
import { catchError, flatMap, map } from 'ix/asynciterable/operators'
import { multicast } from './ixMulticast'
import { throwingPromiseToResult } from './catchAsync'
import { ChatCompletionChunk } from 'openai/resources/chat'
import { Result, resultError, resultSuccess } from './result'
import { Stream } from 'openai/streaming'
import { APIError } from 'openai/error'
export type OpenAiMessage = OpenAI.Chat.Completions.ChatCompletionMessageParam
export interface OpenAIStreamChunk {
type: 'chunk'
cumulativeResponse: string
delta: string
}
export interface OpenAIStreamEndedNormally {
type: 'streamEndedNormally'
}
export interface OpenAIStreamEndedAbonormally {
type: 'streamEndedAbonormally'
endReason: 'tokenLimitReached' | 'emptyDelta' | 'errorDuringStream'
errorMessageForUser?: string
}
export type OpenAIStreamItem =
| OpenAIStreamChunk
| OpenAIStreamEndedNormally
| OpenAIStreamEndedAbonormally
export interface OpenAIStreamCreationError {
kind:
| 'invalid_api_key'
| 'insufficient_quota'
| 'invalid_request_error'
| 'context_length_exceeded'
| 'unknown'
messageForUser: string
}
type Credentials =
| { type: 'openai'; key: string }
| { type: 'helicone'; key: string }
export function makeOpenAiInstance(
credentials: Credentials,
userIdentifierForLoggingAndAbuseDetection: string,
): OpenAI {
let openai: OpenAI
switch (credentials.type) {
case 'openai':
openai = new OpenAI({
apiKey: credentials.key,
baseURL: 'https://api.openai.com/v1',
})
break
case 'helicone':
openai = new OpenAI({
apiKey: credentials.key,
baseURL: 'https://oai.hconeai.com/v1',
defaultHeaders: {
'Helicone-User-Id': userIdentifierForLoggingAndAbuseDetection,
// Do not store user's data
'Helicone-Omit-Request': 'true',
'Helicone-Omit-Response': 'true',
},
})
break
default:
throw new Error('Invalid credential type')
}
return openai
}
/**
* Returns AsyncIterableX to support nifty features like mapping.
*
* IMPORTANT: The iterable returned is multiplexed, meaning every time you get
* an iterator usually using await for of loop, it will start iterating from
* the very beginning.
*
* This is a desired behavior, since oftentimes we want to run different
* stateful operations on the stream. This is similar to an observer / listener
* model of RxJS, accept we can use await for loops :D
*
* The reason why the LLM stream is multiplexed is because is the stream that
* kicks off most of the processes.
*/
export async function streamLlm(
messages: OpenAiMessage[],
/*
* I don't need the logger if I will be passing the entire session,
* Probably the session itself should contain the logger as a property /
* method
*/
logger: (text: string) => Promise<void>,
userIdentifierForLoggingAndAbuseDetection: string,
openai: OpenAI,
): Promise<
Result<
[AsyncIterableX<OpenAIStreamItem>, AbortController],
OpenAIStreamCreationError
>
> {
/*
* Compare AsyncGenerators / AsyncIterators: https://javascript.info/async-iterators-generators
* Basically openai decided to not return AsyncGenerator,
* which is more powerful (compare type definitions) but instead return an
* AsyncIteratable for stream
*/
const streamResult = await throwingPromiseToResult<
Stream<ChatCompletionChunk>,
APIError
>(
openai.chat.completions.create({
model: 'gpt-4',
temperature: 0.4,
messages,
stream: true,
user: userIdentifierForLoggingAndAbuseDetection,
}),
)
/**
* Check for known errors invalid_api_key, spending limit..., token count...,
* other Move termination of the session outside of this function
*
* Other scenarios to test: Disable network midway through the stream
*
* Once the request fails promt the user to enter their own key - have a
* command for this Maybe add a button after certain error message types?
* We should classify the error type here
*
* MAKE SURE NOT TO FORGET: session.sessionEndedEventEmitter.fire()
*/
if (streamResult.type === 'error') {
const error = streamResult.error
console.log(JSON.stringify(error, null, 2))
let llmError: OpenAIStreamCreationError
switch (error.code) {
case 'invalid_api_key':
llmError = {
kind: 'invalid_api_key',
messageForUser: error.message,
}
break
case 'insufficient_quota':
llmError = {
kind: 'insufficient_quota',
messageForUser: error.message,
}
break
case 'invalid_request_error':
llmError = {
kind: 'invalid_request_error',
messageForUser: error.message,
}
break
case 'context_length_exceeded':
llmError = {
kind: 'context_length_exceeded',
messageForUser: error.message,
}
break
default:
llmError = {
kind: 'unknown',
messageForUser: error.message,
}
break
}
return resultError(llmError)
}
const stream = streamResult.value
let currentContent = ''
const simplifiedStream = from(stream).pipe(
flatMap((part: ChatCompletionChunk): OpenAIStreamItem[] => {
if (part.choices[0]?.finish_reason) {
/*
* We are done
* Refactor: Update the return type to represent different kinds of
* stream terminations.
*/
console.log(part.choices[0]?.finish_reason)
if (part.choices[0]?.finish_reason === 'length') {
const message = `Token limit reached for this request, try again with less context or a task that requires less tokens in the response`
console.error(message)
return [
{
type: 'streamEndedAbonormally',
endReason: 'tokenLimitReached',
errorMessageForUser: message,
},
]
} else {
return [
{
type: 'streamEndedNormally',
},
]
}
}
// Refactor: These details should have stayed in openai.ts
const delta = part.choices[0]?.delta?.content
if (!delta) {
// It seems the first delta is always empty, ignore it
return []
}
currentContent += delta
return [
{
type: 'chunk',
cumulativeResponse: currentContent,
delta,
},
]
}),
map((item: OpenAIStreamItem): OpenAIStreamItem => {
/*
* Gotcha:
* Due to multicast and iterating over the stream multiple times all
* the mappings are performed however many times there are for await
* loops.
*
* Thus logging happens before multicasting.
*
* This is
* 1. not performant
* 2. breaks stateful things like logging
*/
if (item.type === 'chunk') {
void logger(item.delta)
}
return item
}),
catchError((error: any): AsyncIterableX<OpenAIStreamEndedAbonormally> => {
console.error(error)
let message = 'Unknown error occurred'
if (error instanceof Error) {
message = error.message
}
return from([
{
type: 'streamEndedAbonormally',
endReason: 'errorDuringStream',
errorMessageForUser: message,
},
])
}),
)
// Multiplex the stream, so that we can iterate over it multiple times
const multicastStream = multicast(simplifiedStream)
void logger(`\n# Messages submitted:\n`)
for (const { content, role } of messages) {
void logger(`\n## [${role}]:\n\`\`\`md\n${content}\n\`\`\`\n`)
}
void logger(`\n# [assistant, latest response]:\n\`\`\`md\n`)
// Detect end of stream and free up the llm resource
void last(multicastStream).catch((error: Error) => {
console.error(error)
void logger(
`\n# [error occurred in stream]:\n\`\`\`md\n${
error as unknown as any
}\`\`\`\n`,
)
return undefined
})
return resultSuccess([multicastStream, stream.controller])
}