-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
openai-chat.ts
536 lines (462 loc) Β· 14.8 KB
/
openai-chat.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
import { type ClientOptions, OpenAI as OpenAIClient } from "openai";
import { CallbackManagerForLLMRun } from "../callbacks/manager.js";
import { Generation, GenerationChunk, LLMResult } from "../schema/index.js";
import {
AzureOpenAIInput,
OpenAICallOptions,
OpenAIChatInput,
OpenAICoreRequestOptions,
LegacyOpenAIInput,
} from "../types/openai-types.js";
import { OpenAIEndpointConfig, getEndpoint } from "../util/azure.js";
import { getEnvironmentVariable } from "../util/env.js";
import { promptLayerTrackRequest } from "../util/prompt-layer.js";
import { BaseLLMParams, LLM } from "./base.js";
import { wrapOpenAIClientError } from "../util/openai.js";
export { type AzureOpenAIInput, type OpenAIChatInput };
/**
* Interface that extends the OpenAICallOptions interface and includes an
* optional promptIndex property. It represents the options that can be
* passed when making a call to the OpenAI Chat API.
*/
export interface OpenAIChatCallOptions extends OpenAICallOptions {
promptIndex?: number;
}
/**
* Wrapper around OpenAI large language models that use the Chat endpoint.
*
* To use you should have the `openai` package installed, with the
* `OPENAI_API_KEY` environment variable set.
*
* To use with Azure you should have the `openai` package installed, with the
* `AZURE_OPENAI_API_KEY`,
* `AZURE_OPENAI_API_INSTANCE_NAME`,
* `AZURE_OPENAI_API_DEPLOYMENT_NAME`
* and `AZURE_OPENAI_API_VERSION` environment variable set.
*
* @remarks
* Any parameters that are valid to be passed to {@link
* https://platform.openai.com/docs/api-reference/chat/create |
* `openai.createCompletion`} can be passed through {@link modelKwargs}, even
* if not explicitly available on this class.
*
* @augments BaseLLM
* @augments OpenAIInput
* @augments AzureOpenAIChatInput
*/
export class OpenAIChat
extends LLM<OpenAIChatCallOptions>
implements OpenAIChatInput, AzureOpenAIInput
{
static lc_name() {
return "OpenAIChat";
}
get callKeys() {
return [...super.callKeys, "options", "promptIndex"];
}
lc_serializable = true;
get lc_secrets(): { [key: string]: string } | undefined {
return {
openAIApiKey: "OPENAI_API_KEY",
azureOpenAIApiKey: "AZURE_OPENAI_API_KEY",
organization: "OPENAI_ORGANIZATION",
};
}
get lc_aliases(): Record<string, string> {
return {
modelName: "model",
openAIApiKey: "openai_api_key",
azureOpenAIApiVersion: "azure_openai_api_version",
azureOpenAIApiKey: "azure_openai_api_key",
azureOpenAIApiInstanceName: "azure_openai_api_instance_name",
azureOpenAIApiDeploymentName: "azure_openai_api_deployment_name",
};
}
temperature = 1;
topP = 1;
frequencyPenalty = 0;
presencePenalty = 0;
n = 1;
logitBias?: Record<string, number>;
maxTokens?: number;
modelName = "gpt-3.5-turbo";
prefixMessages?: OpenAIClient.Chat.ChatCompletionMessageParam[];
modelKwargs?: OpenAIChatInput["modelKwargs"];
timeout?: number;
stop?: string[];
user?: string;
streaming = false;
openAIApiKey?: string;
azureOpenAIApiVersion?: string;
azureOpenAIApiKey?: string;
azureOpenAIApiInstanceName?: string;
azureOpenAIApiDeploymentName?: string;
azureOpenAIBasePath?: string;
organization?: string;
private client: OpenAIClient;
private clientConfig: ClientOptions;
constructor(
fields?: Partial<OpenAIChatInput> &
Partial<AzureOpenAIInput> &
BaseLLMParams & {
configuration?: ClientOptions & LegacyOpenAIInput;
},
/** @deprecated */
configuration?: ClientOptions & LegacyOpenAIInput
) {
super(fields ?? {});
this.openAIApiKey =
fields?.openAIApiKey ?? getEnvironmentVariable("OPENAI_API_KEY");
this.azureOpenAIApiKey =
fields?.azureOpenAIApiKey ??
getEnvironmentVariable("AZURE_OPENAI_API_KEY");
if (!this.azureOpenAIApiKey && !this.openAIApiKey) {
throw new Error("OpenAI or Azure OpenAI API key not found");
}
this.azureOpenAIApiInstanceName =
fields?.azureOpenAIApiInstanceName ??
getEnvironmentVariable("AZURE_OPENAI_API_INSTANCE_NAME");
this.azureOpenAIApiDeploymentName =
(fields?.azureOpenAIApiCompletionsDeploymentName ||
fields?.azureOpenAIApiDeploymentName) ??
(getEnvironmentVariable("AZURE_OPENAI_API_COMPLETIONS_DEPLOYMENT_NAME") ||
getEnvironmentVariable("AZURE_OPENAI_API_DEPLOYMENT_NAME"));
this.azureOpenAIApiVersion =
fields?.azureOpenAIApiVersion ??
getEnvironmentVariable("AZURE_OPENAI_API_VERSION");
this.azureOpenAIBasePath =
fields?.azureOpenAIBasePath ??
getEnvironmentVariable("AZURE_OPENAI_BASE_PATH");
this.organization =
fields?.configuration?.organization ??
getEnvironmentVariable("OPENAI_ORGANIZATION");
this.modelName = fields?.modelName ?? this.modelName;
this.prefixMessages = fields?.prefixMessages ?? this.prefixMessages;
this.modelKwargs = fields?.modelKwargs ?? {};
this.timeout = fields?.timeout;
this.temperature = fields?.temperature ?? this.temperature;
this.topP = fields?.topP ?? this.topP;
this.frequencyPenalty = fields?.frequencyPenalty ?? this.frequencyPenalty;
this.presencePenalty = fields?.presencePenalty ?? this.presencePenalty;
this.n = fields?.n ?? this.n;
this.logitBias = fields?.logitBias;
this.maxTokens = fields?.maxTokens;
this.stop = fields?.stop;
this.user = fields?.user;
this.streaming = fields?.streaming ?? false;
if (this.n > 1) {
throw new Error(
"Cannot use n > 1 in OpenAIChat LLM. Use ChatOpenAI Chat Model instead."
);
}
if (this.azureOpenAIApiKey) {
if (!this.azureOpenAIApiInstanceName && !this.azureOpenAIBasePath) {
throw new Error("Azure OpenAI API instance name not found");
}
if (!this.azureOpenAIApiDeploymentName) {
throw new Error("Azure OpenAI API deployment name not found");
}
if (!this.azureOpenAIApiVersion) {
throw new Error("Azure OpenAI API version not found");
}
this.openAIApiKey = this.openAIApiKey ?? "";
}
this.clientConfig = {
apiKey: this.openAIApiKey,
organization: this.organization,
baseURL: configuration?.basePath ?? fields?.configuration?.basePath,
dangerouslyAllowBrowser: true,
defaultHeaders:
configuration?.baseOptions?.headers ??
fields?.configuration?.baseOptions?.headers,
defaultQuery:
configuration?.baseOptions?.params ??
fields?.configuration?.baseOptions?.params,
...configuration,
...fields?.configuration,
};
}
/**
* Get the parameters used to invoke the model
*/
invocationParams(
options?: this["ParsedCallOptions"]
): Omit<OpenAIClient.Chat.ChatCompletionCreateParams, "messages"> {
return {
model: this.modelName,
temperature: this.temperature,
top_p: this.topP,
frequency_penalty: this.frequencyPenalty,
presence_penalty: this.presencePenalty,
n: this.n,
logit_bias: this.logitBias,
max_tokens: this.maxTokens === -1 ? undefined : this.maxTokens,
stop: options?.stop ?? this.stop,
user: this.user,
stream: this.streaming,
...this.modelKwargs,
};
}
/** @ignore */
_identifyingParams(): Omit<
OpenAIClient.Chat.ChatCompletionCreateParams,
"messages"
> & {
model_name: string;
} & ClientOptions {
return {
model_name: this.modelName,
...this.invocationParams(),
...this.clientConfig,
};
}
/**
* Get the identifying parameters for the model
*/
identifyingParams(): Omit<
OpenAIClient.Chat.ChatCompletionCreateParams,
"messages"
> & {
model_name: string;
} & ClientOptions {
return {
model_name: this.modelName,
...this.invocationParams(),
...this.clientConfig,
};
}
/**
* Formats the messages for the OpenAI API.
* @param prompt The prompt to be formatted.
* @returns Array of formatted messages.
*/
private formatMessages(
prompt: string
): OpenAIClient.Chat.ChatCompletionMessageParam[] {
const message: OpenAIClient.Chat.ChatCompletionMessageParam = {
role: "user",
content: prompt,
};
return this.prefixMessages ? [...this.prefixMessages, message] : [message];
}
async *_streamResponseChunks(
prompt: string,
options: this["ParsedCallOptions"],
runManager?: CallbackManagerForLLMRun
): AsyncGenerator<GenerationChunk> {
const params = {
...this.invocationParams(options),
messages: this.formatMessages(prompt),
stream: true as const,
};
const stream = await this.completionWithRetry(params, options);
for await (const data of stream) {
const choice = data?.choices[0];
if (!choice) {
continue;
}
const { delta } = choice;
const generationChunk = new GenerationChunk({
text: delta.content ?? "",
});
yield generationChunk;
const newTokenIndices = {
prompt: options.promptIndex ?? 0,
completion: choice.index ?? 0,
};
// eslint-disable-next-line no-void
void runManager?.handleLLMNewToken(
generationChunk.text ?? "",
newTokenIndices
);
}
if (options.signal?.aborted) {
throw new Error("AbortError");
}
}
/** @ignore */
async _call(
prompt: string,
options: this["ParsedCallOptions"],
runManager?: CallbackManagerForLLMRun
): Promise<string> {
const params = this.invocationParams(options);
if (params.stream) {
const stream = await this._streamResponseChunks(
prompt,
options,
runManager
);
let finalChunk: GenerationChunk | undefined;
for await (const chunk of stream) {
if (finalChunk === undefined) {
finalChunk = chunk;
} else {
finalChunk = finalChunk.concat(chunk);
}
}
return finalChunk?.text ?? "";
} else {
const response = await this.completionWithRetry(
{
...params,
stream: false,
messages: this.formatMessages(prompt),
},
{
signal: options.signal,
...options.options,
}
);
return response?.choices[0]?.message?.content ?? "";
}
}
/**
* Calls the OpenAI API with retry logic in case of failures.
* @param request The request to send to the OpenAI API.
* @param options Optional configuration for the API call.
* @returns The response from the OpenAI API.
*/
async completionWithRetry(
request: OpenAIClient.Chat.ChatCompletionCreateParamsStreaming,
options?: OpenAICoreRequestOptions
): Promise<AsyncIterable<OpenAIClient.Chat.Completions.ChatCompletionChunk>>;
async completionWithRetry(
request: OpenAIClient.Chat.ChatCompletionCreateParamsNonStreaming,
options?: OpenAICoreRequestOptions
): Promise<OpenAIClient.Chat.Completions.ChatCompletion>;
async completionWithRetry(
request:
| OpenAIClient.Chat.ChatCompletionCreateParamsStreaming
| OpenAIClient.Chat.ChatCompletionCreateParamsNonStreaming,
options?: OpenAICoreRequestOptions
): Promise<
| AsyncIterable<OpenAIClient.Chat.Completions.ChatCompletionChunk>
| OpenAIClient.Chat.Completions.ChatCompletion
> {
const requestOptions = this._getClientOptions(options);
return this.caller.call(async () => {
try {
const res = await this.client.chat.completions.create(
request,
requestOptions
);
return res;
} catch (e) {
const error = wrapOpenAIClientError(e);
throw error;
}
});
}
/** @ignore */
private _getClientOptions(options: OpenAICoreRequestOptions | undefined) {
if (!this.client) {
const openAIEndpointConfig: OpenAIEndpointConfig = {
azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,
azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,
azureOpenAIApiKey: this.azureOpenAIApiKey,
azureOpenAIBasePath: this.azureOpenAIBasePath,
baseURL: this.clientConfig.baseURL,
};
const endpoint = getEndpoint(openAIEndpointConfig);
const params = {
...this.clientConfig,
baseURL: endpoint,
timeout: this.timeout,
maxRetries: 0,
};
if (!params.baseURL) {
delete params.baseURL;
}
this.client = new OpenAIClient(params);
}
const requestOptions = {
...this.clientConfig,
...options,
} as OpenAICoreRequestOptions;
if (this.azureOpenAIApiKey) {
requestOptions.headers = {
"api-key": this.azureOpenAIApiKey,
...requestOptions.headers,
};
requestOptions.query = {
"api-version": this.azureOpenAIApiVersion,
...requestOptions.query,
};
}
return requestOptions;
}
_llmType() {
return "openai";
}
}
/**
* PromptLayer wrapper to OpenAIChat
*/
export class PromptLayerOpenAIChat extends OpenAIChat {
get lc_secrets(): { [key: string]: string } | undefined {
return {
promptLayerApiKey: "PROMPTLAYER_API_KEY",
};
}
lc_serializable = false;
promptLayerApiKey?: string;
plTags?: string[];
returnPromptLayerId?: boolean;
constructor(
fields?: ConstructorParameters<typeof OpenAIChat>[0] & {
promptLayerApiKey?: string;
plTags?: string[];
returnPromptLayerId?: boolean;
}
) {
super(fields);
this.plTags = fields?.plTags ?? [];
this.returnPromptLayerId = fields?.returnPromptLayerId ?? false;
this.promptLayerApiKey =
fields?.promptLayerApiKey ??
getEnvironmentVariable("PROMPTLAYER_API_KEY");
if (!this.promptLayerApiKey) {
throw new Error("Missing PromptLayer API key");
}
}
async _generate(
prompts: string[],
options: this["ParsedCallOptions"],
runManager?: CallbackManagerForLLMRun
): Promise<LLMResult> {
let choice: Generation[];
const generations: Generation[][] = await Promise.all(
prompts.map(async (prompt) => {
const requestStartTime = Date.now();
const text = await this._call(prompt, options, runManager);
const requestEndTime = Date.now();
choice = [{ text }];
const parsedResp = {
text,
};
const promptLayerRespBody = await promptLayerTrackRequest(
this.caller,
"langchain.PromptLayerOpenAIChat",
// eslint-disable-next-line @typescript-eslint/no-explicit-any
{ ...this._identifyingParams(), prompt } as any,
this.plTags,
parsedResp,
requestStartTime,
requestEndTime,
this.promptLayerApiKey
);
if (
this.returnPromptLayerId === true &&
promptLayerRespBody.success === true
) {
choice[0].generationInfo = {
promptLayerRequestId: promptLayerRespBody.request_id,
};
}
return choice;
})
);
return { generations };
}
}