@langchain/openai 0.4.0-rc.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/legacy.cjs DELETED
@@ -1,458 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.OpenAIChat = void 0;
4
- const openai_1 = require("openai");
5
- const outputs_1 = require("@langchain/core/outputs");
6
- const env_1 = require("@langchain/core/utils/env");
7
- const llms_1 = require("@langchain/core/language_models/llms");
8
- const azure_js_1 = require("./utils/azure.cjs");
9
- const openai_js_1 = require("./utils/openai.cjs");
10
- /**
11
- * @deprecated For legacy compatibility. Use ChatOpenAI instead.
12
- *
13
- * Wrapper around OpenAI large language models that use the Chat endpoint.
14
- *
15
- * To use you should have the `openai` package installed, with the
16
- * `OPENAI_API_KEY` environment variable set.
17
- *
18
- * To use with Azure you should have the `openai` package installed, with the
19
- * `AZURE_OPENAI_API_KEY`,
20
- * `AZURE_OPENAI_API_INSTANCE_NAME`,
21
- * `AZURE_OPENAI_API_DEPLOYMENT_NAME`
22
- * and `AZURE_OPENAI_API_VERSION` environment variable set.
23
- *
24
- * @remarks
25
- * Any parameters that are valid to be passed to {@link
26
- * https://platform.openai.com/docs/api-reference/chat/create |
27
- * `openai.createCompletion`} can be passed through {@link modelKwargs}, even
28
- * if not explicitly available on this class.
29
- *
30
- * @augments BaseLLM
31
- * @augments OpenAIInput
32
- * @augments AzureOpenAIChatInput
33
- * @example
34
- * ```typescript
35
- * const model = new OpenAIChat({
36
- * prefixMessages: [
37
- * {
38
- * role: "system",
39
- * content: "You are a helpful assistant that answers in pirate language",
40
- * },
41
- * ],
42
- * maxTokens: 50,
43
- * });
44
- *
45
- * const res = await model.invoke(
46
- * "What would be a good company name for a company that makes colorful socks?"
47
- * );
48
- * console.log({ res });
49
- * ```
50
- */
51
- class OpenAIChat extends llms_1.LLM {
52
- static lc_name() {
53
- return "OpenAIChat";
54
- }
55
- get callKeys() {
56
- return [...super.callKeys, "options", "promptIndex"];
57
- }
58
- get lc_secrets() {
59
- return {
60
- openAIApiKey: "OPENAI_API_KEY",
61
- azureOpenAIApiKey: "AZURE_OPENAI_API_KEY",
62
- organization: "OPENAI_ORGANIZATION",
63
- };
64
- }
65
- get lc_aliases() {
66
- return {
67
- modelName: "model",
68
- openAIApiKey: "openai_api_key",
69
- azureOpenAIApiVersion: "azure_openai_api_version",
70
- azureOpenAIApiKey: "azure_openai_api_key",
71
- azureOpenAIApiInstanceName: "azure_openai_api_instance_name",
72
- azureOpenAIApiDeploymentName: "azure_openai_api_deployment_name",
73
- };
74
- }
75
- constructor(fields,
76
- /** @deprecated */
77
- configuration) {
78
- super(fields ?? {});
79
- Object.defineProperty(this, "lc_serializable", {
80
- enumerable: true,
81
- configurable: true,
82
- writable: true,
83
- value: true
84
- });
85
- Object.defineProperty(this, "temperature", {
86
- enumerable: true,
87
- configurable: true,
88
- writable: true,
89
- value: 1
90
- });
91
- Object.defineProperty(this, "topP", {
92
- enumerable: true,
93
- configurable: true,
94
- writable: true,
95
- value: 1
96
- });
97
- Object.defineProperty(this, "frequencyPenalty", {
98
- enumerable: true,
99
- configurable: true,
100
- writable: true,
101
- value: 0
102
- });
103
- Object.defineProperty(this, "presencePenalty", {
104
- enumerable: true,
105
- configurable: true,
106
- writable: true,
107
- value: 0
108
- });
109
- Object.defineProperty(this, "n", {
110
- enumerable: true,
111
- configurable: true,
112
- writable: true,
113
- value: 1
114
- });
115
- Object.defineProperty(this, "logitBias", {
116
- enumerable: true,
117
- configurable: true,
118
- writable: true,
119
- value: void 0
120
- });
121
- Object.defineProperty(this, "maxTokens", {
122
- enumerable: true,
123
- configurable: true,
124
- writable: true,
125
- value: void 0
126
- });
127
- Object.defineProperty(this, "modelName", {
128
- enumerable: true,
129
- configurable: true,
130
- writable: true,
131
- value: "gpt-3.5-turbo"
132
- });
133
- Object.defineProperty(this, "model", {
134
- enumerable: true,
135
- configurable: true,
136
- writable: true,
137
- value: "gpt-3.5-turbo"
138
- });
139
- Object.defineProperty(this, "prefixMessages", {
140
- enumerable: true,
141
- configurable: true,
142
- writable: true,
143
- value: void 0
144
- });
145
- Object.defineProperty(this, "modelKwargs", {
146
- enumerable: true,
147
- configurable: true,
148
- writable: true,
149
- value: void 0
150
- });
151
- Object.defineProperty(this, "timeout", {
152
- enumerable: true,
153
- configurable: true,
154
- writable: true,
155
- value: void 0
156
- });
157
- Object.defineProperty(this, "stop", {
158
- enumerable: true,
159
- configurable: true,
160
- writable: true,
161
- value: void 0
162
- });
163
- Object.defineProperty(this, "user", {
164
- enumerable: true,
165
- configurable: true,
166
- writable: true,
167
- value: void 0
168
- });
169
- Object.defineProperty(this, "streaming", {
170
- enumerable: true,
171
- configurable: true,
172
- writable: true,
173
- value: false
174
- });
175
- Object.defineProperty(this, "openAIApiKey", {
176
- enumerable: true,
177
- configurable: true,
178
- writable: true,
179
- value: void 0
180
- });
181
- Object.defineProperty(this, "azureOpenAIApiVersion", {
182
- enumerable: true,
183
- configurable: true,
184
- writable: true,
185
- value: void 0
186
- });
187
- Object.defineProperty(this, "azureOpenAIApiKey", {
188
- enumerable: true,
189
- configurable: true,
190
- writable: true,
191
- value: void 0
192
- });
193
- Object.defineProperty(this, "azureOpenAIApiInstanceName", {
194
- enumerable: true,
195
- configurable: true,
196
- writable: true,
197
- value: void 0
198
- });
199
- Object.defineProperty(this, "azureOpenAIApiDeploymentName", {
200
- enumerable: true,
201
- configurable: true,
202
- writable: true,
203
- value: void 0
204
- });
205
- Object.defineProperty(this, "azureOpenAIBasePath", {
206
- enumerable: true,
207
- configurable: true,
208
- writable: true,
209
- value: void 0
210
- });
211
- Object.defineProperty(this, "organization", {
212
- enumerable: true,
213
- configurable: true,
214
- writable: true,
215
- value: void 0
216
- });
217
- Object.defineProperty(this, "client", {
218
- enumerable: true,
219
- configurable: true,
220
- writable: true,
221
- value: void 0
222
- });
223
- Object.defineProperty(this, "clientConfig", {
224
- enumerable: true,
225
- configurable: true,
226
- writable: true,
227
- value: void 0
228
- });
229
- this.openAIApiKey =
230
- fields?.apiKey ??
231
- fields?.openAIApiKey ??
232
- (0, env_1.getEnvironmentVariable)("OPENAI_API_KEY");
233
- this.azureOpenAIApiKey =
234
- fields?.azureOpenAIApiKey ??
235
- (0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_KEY");
236
- if (!this.azureOpenAIApiKey && !this.openAIApiKey) {
237
- throw new Error("OpenAI or Azure OpenAI API key not found");
238
- }
239
- this.azureOpenAIApiInstanceName =
240
- fields?.azureOpenAIApiInstanceName ??
241
- (0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_INSTANCE_NAME");
242
- this.azureOpenAIApiDeploymentName =
243
- (fields?.azureOpenAIApiCompletionsDeploymentName ||
244
- fields?.azureOpenAIApiDeploymentName) ??
245
- ((0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_COMPLETIONS_DEPLOYMENT_NAME") ||
246
- (0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_DEPLOYMENT_NAME"));
247
- this.azureOpenAIApiVersion =
248
- fields?.azureOpenAIApiVersion ??
249
- (0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_VERSION");
250
- this.azureOpenAIBasePath =
251
- fields?.azureOpenAIBasePath ??
252
- (0, env_1.getEnvironmentVariable)("AZURE_OPENAI_BASE_PATH");
253
- this.organization =
254
- fields?.configuration?.organization ??
255
- (0, env_1.getEnvironmentVariable)("OPENAI_ORGANIZATION");
256
- this.modelName = fields?.model ?? fields?.modelName ?? this.modelName;
257
- this.prefixMessages = fields?.prefixMessages ?? this.prefixMessages;
258
- this.modelKwargs = fields?.modelKwargs ?? {};
259
- this.timeout = fields?.timeout;
260
- this.temperature = fields?.temperature ?? this.temperature;
261
- this.topP = fields?.topP ?? this.topP;
262
- this.frequencyPenalty = fields?.frequencyPenalty ?? this.frequencyPenalty;
263
- this.presencePenalty = fields?.presencePenalty ?? this.presencePenalty;
264
- this.n = fields?.n ?? this.n;
265
- this.logitBias = fields?.logitBias;
266
- this.maxTokens = fields?.maxTokens;
267
- this.stop = fields?.stop;
268
- this.user = fields?.user;
269
- this.streaming = fields?.streaming ?? false;
270
- if (this.n > 1) {
271
- throw new Error("Cannot use n > 1 in OpenAIChat LLM. Use ChatOpenAI Chat Model instead.");
272
- }
273
- if (this.azureOpenAIApiKey) {
274
- if (!this.azureOpenAIApiInstanceName && !this.azureOpenAIBasePath) {
275
- throw new Error("Azure OpenAI API instance name not found");
276
- }
277
- if (!this.azureOpenAIApiDeploymentName) {
278
- throw new Error("Azure OpenAI API deployment name not found");
279
- }
280
- if (!this.azureOpenAIApiVersion) {
281
- throw new Error("Azure OpenAI API version not found");
282
- }
283
- this.openAIApiKey = this.openAIApiKey ?? "";
284
- }
285
- this.clientConfig = {
286
- apiKey: this.openAIApiKey,
287
- organization: this.organization,
288
- baseURL: configuration?.basePath ?? fields?.configuration?.basePath,
289
- dangerouslyAllowBrowser: true,
290
- defaultHeaders: configuration?.baseOptions?.headers ??
291
- fields?.configuration?.baseOptions?.headers,
292
- defaultQuery: configuration?.baseOptions?.params ??
293
- fields?.configuration?.baseOptions?.params,
294
- ...configuration,
295
- ...fields?.configuration,
296
- };
297
- }
298
- /**
299
- * Get the parameters used to invoke the model
300
- */
301
- invocationParams(options) {
302
- return {
303
- model: this.modelName,
304
- temperature: this.temperature,
305
- top_p: this.topP,
306
- frequency_penalty: this.frequencyPenalty,
307
- presence_penalty: this.presencePenalty,
308
- n: this.n,
309
- logit_bias: this.logitBias,
310
- max_tokens: this.maxTokens === -1 ? undefined : this.maxTokens,
311
- stop: options?.stop ?? this.stop,
312
- user: this.user,
313
- stream: this.streaming,
314
- ...this.modelKwargs,
315
- };
316
- }
317
- /** @ignore */
318
- _identifyingParams() {
319
- return {
320
- model_name: this.modelName,
321
- ...this.invocationParams(),
322
- ...this.clientConfig,
323
- };
324
- }
325
- /**
326
- * Get the identifying parameters for the model
327
- */
328
- identifyingParams() {
329
- return {
330
- model_name: this.modelName,
331
- ...this.invocationParams(),
332
- ...this.clientConfig,
333
- };
334
- }
335
- /**
336
- * Formats the messages for the OpenAI API.
337
- * @param prompt The prompt to be formatted.
338
- * @returns Array of formatted messages.
339
- */
340
- formatMessages(prompt) {
341
- const message = {
342
- role: "user",
343
- content: prompt,
344
- };
345
- return this.prefixMessages ? [...this.prefixMessages, message] : [message];
346
- }
347
- async *_streamResponseChunks(prompt, options, runManager) {
348
- const params = {
349
- ...this.invocationParams(options),
350
- messages: this.formatMessages(prompt),
351
- stream: true,
352
- };
353
- const stream = await this.completionWithRetry(params, options);
354
- for await (const data of stream) {
355
- const choice = data?.choices[0];
356
- if (!choice) {
357
- continue;
358
- }
359
- const { delta } = choice;
360
- const generationChunk = new outputs_1.GenerationChunk({
361
- text: delta.content ?? "",
362
- });
363
- yield generationChunk;
364
- const newTokenIndices = {
365
- prompt: options.promptIndex ?? 0,
366
- completion: choice.index ?? 0,
367
- };
368
- // eslint-disable-next-line no-void
369
- void runManager?.handleLLMNewToken(generationChunk.text ?? "", newTokenIndices);
370
- }
371
- if (options.signal?.aborted) {
372
- throw new Error("AbortError");
373
- }
374
- }
375
- /** @ignore */
376
- async _call(prompt, options, runManager) {
377
- const params = this.invocationParams(options);
378
- if (params.stream) {
379
- const stream = await this._streamResponseChunks(prompt, options, runManager);
380
- let finalChunk;
381
- for await (const chunk of stream) {
382
- if (finalChunk === undefined) {
383
- finalChunk = chunk;
384
- }
385
- else {
386
- finalChunk = finalChunk.concat(chunk);
387
- }
388
- }
389
- return finalChunk?.text ?? "";
390
- }
391
- else {
392
- const response = await this.completionWithRetry({
393
- ...params,
394
- stream: false,
395
- messages: this.formatMessages(prompt),
396
- }, {
397
- signal: options.signal,
398
- ...options.options,
399
- });
400
- return response?.choices[0]?.message?.content ?? "";
401
- }
402
- }
403
- async completionWithRetry(request, options) {
404
- const requestOptions = this._getClientOptions(options);
405
- return this.caller.call(async () => {
406
- try {
407
- const res = await this.client.chat.completions.create(request, requestOptions);
408
- return res;
409
- }
410
- catch (e) {
411
- const error = (0, openai_js_1.wrapOpenAIClientError)(e);
412
- throw error;
413
- }
414
- });
415
- }
416
- /** @ignore */
417
- _getClientOptions(options) {
418
- if (!this.client) {
419
- const openAIEndpointConfig = {
420
- azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,
421
- azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,
422
- azureOpenAIApiKey: this.azureOpenAIApiKey,
423
- azureOpenAIBasePath: this.azureOpenAIBasePath,
424
- baseURL: this.clientConfig.baseURL,
425
- };
426
- const endpoint = (0, azure_js_1.getEndpoint)(openAIEndpointConfig);
427
- const params = {
428
- ...this.clientConfig,
429
- baseURL: endpoint,
430
- timeout: this.timeout,
431
- maxRetries: 0,
432
- };
433
- if (!params.baseURL) {
434
- delete params.baseURL;
435
- }
436
- this.client = new openai_1.OpenAI(params);
437
- }
438
- const requestOptions = {
439
- ...this.clientConfig,
440
- ...options,
441
- };
442
- if (this.azureOpenAIApiKey) {
443
- requestOptions.headers = {
444
- "api-key": this.azureOpenAIApiKey,
445
- ...requestOptions.headers,
446
- };
447
- requestOptions.query = {
448
- "api-version": this.azureOpenAIApiVersion,
449
- ...requestOptions.query,
450
- };
451
- }
452
- return requestOptions;
453
- }
454
- _llmType() {
455
- return "openai";
456
- }
457
- }
458
- exports.OpenAIChat = OpenAIChat;
package/dist/legacy.d.ts DELETED
@@ -1,127 +0,0 @@
1
- import { type ClientOptions, OpenAI as OpenAIClient } from "openai";
2
- import { CallbackManagerForLLMRun } from "@langchain/core/callbacks/manager";
3
- import { GenerationChunk } from "@langchain/core/outputs";
4
- import { type BaseLLMParams, LLM } from "@langchain/core/language_models/llms";
5
- import { AzureOpenAIInput, OpenAICallOptions, OpenAIChatInput, OpenAICoreRequestOptions, LegacyOpenAIInput } from "./types.js";
6
- export { type AzureOpenAIInput, type OpenAIChatInput };
7
- /**
8
- * Interface that extends the OpenAICallOptions interface and includes an
9
- * optional promptIndex property. It represents the options that can be
10
- * passed when making a call to the OpenAI Chat API.
11
- */
12
- export interface OpenAIChatCallOptions extends OpenAICallOptions {
13
- promptIndex?: number;
14
- }
15
- /**
16
- * @deprecated For legacy compatibility. Use ChatOpenAI instead.
17
- *
18
- * Wrapper around OpenAI large language models that use the Chat endpoint.
19
- *
20
- * To use you should have the `openai` package installed, with the
21
- * `OPENAI_API_KEY` environment variable set.
22
- *
23
- * To use with Azure you should have the `openai` package installed, with the
24
- * `AZURE_OPENAI_API_KEY`,
25
- * `AZURE_OPENAI_API_INSTANCE_NAME`,
26
- * `AZURE_OPENAI_API_DEPLOYMENT_NAME`
27
- * and `AZURE_OPENAI_API_VERSION` environment variable set.
28
- *
29
- * @remarks
30
- * Any parameters that are valid to be passed to {@link
31
- * https://platform.openai.com/docs/api-reference/chat/create |
32
- * `openai.createCompletion`} can be passed through {@link modelKwargs}, even
33
- * if not explicitly available on this class.
34
- *
35
- * @augments BaseLLM
36
- * @augments OpenAIInput
37
- * @augments AzureOpenAIChatInput
38
- * @example
39
- * ```typescript
40
- * const model = new OpenAIChat({
41
- * prefixMessages: [
42
- * {
43
- * role: "system",
44
- * content: "You are a helpful assistant that answers in pirate language",
45
- * },
46
- * ],
47
- * maxTokens: 50,
48
- * });
49
- *
50
- * const res = await model.invoke(
51
- * "What would be a good company name for a company that makes colorful socks?"
52
- * );
53
- * console.log({ res });
54
- * ```
55
- */
56
- export declare class OpenAIChat extends LLM<OpenAIChatCallOptions> implements OpenAIChatInput, AzureOpenAIInput {
57
- static lc_name(): string;
58
- get callKeys(): string[];
59
- lc_serializable: boolean;
60
- get lc_secrets(): {
61
- [key: string]: string;
62
- } | undefined;
63
- get lc_aliases(): Record<string, string>;
64
- temperature: number;
65
- topP: number;
66
- frequencyPenalty: number;
67
- presencePenalty: number;
68
- n: number;
69
- logitBias?: Record<string, number>;
70
- maxTokens?: number;
71
- modelName: string;
72
- model: string;
73
- prefixMessages?: OpenAIClient.Chat.ChatCompletionMessageParam[];
74
- modelKwargs?: OpenAIChatInput["modelKwargs"];
75
- timeout?: number;
76
- stop?: string[];
77
- user?: string;
78
- streaming: boolean;
79
- openAIApiKey?: string;
80
- azureOpenAIApiVersion?: string;
81
- azureOpenAIApiKey?: string;
82
- azureOpenAIApiInstanceName?: string;
83
- azureOpenAIApiDeploymentName?: string;
84
- azureOpenAIBasePath?: string;
85
- organization?: string;
86
- private client;
87
- private clientConfig;
88
- constructor(fields?: Partial<OpenAIChatInput> & Partial<AzureOpenAIInput> & BaseLLMParams & {
89
- configuration?: ClientOptions & LegacyOpenAIInput;
90
- },
91
- /** @deprecated */
92
- configuration?: ClientOptions & LegacyOpenAIInput);
93
- /**
94
- * Get the parameters used to invoke the model
95
- */
96
- invocationParams(options?: this["ParsedCallOptions"]): Omit<OpenAIClient.Chat.ChatCompletionCreateParams, "messages">;
97
- /** @ignore */
98
- _identifyingParams(): Omit<OpenAIClient.Chat.ChatCompletionCreateParams, "messages"> & {
99
- model_name: string;
100
- } & ClientOptions;
101
- /**
102
- * Get the identifying parameters for the model
103
- */
104
- identifyingParams(): Omit<OpenAIClient.Chat.ChatCompletionCreateParams, "messages"> & {
105
- model_name: string;
106
- } & ClientOptions;
107
- /**
108
- * Formats the messages for the OpenAI API.
109
- * @param prompt The prompt to be formatted.
110
- * @returns Array of formatted messages.
111
- */
112
- private formatMessages;
113
- _streamResponseChunks(prompt: string, options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): AsyncGenerator<GenerationChunk>;
114
- /** @ignore */
115
- _call(prompt: string, options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<string>;
116
- /**
117
- * Calls the OpenAI API with retry logic in case of failures.
118
- * @param request The request to send to the OpenAI API.
119
- * @param options Optional configuration for the API call.
120
- * @returns The response from the OpenAI API.
121
- */
122
- completionWithRetry(request: OpenAIClient.Chat.ChatCompletionCreateParamsStreaming, options?: OpenAICoreRequestOptions): Promise<AsyncIterable<OpenAIClient.Chat.Completions.ChatCompletionChunk>>;
123
- completionWithRetry(request: OpenAIClient.Chat.ChatCompletionCreateParamsNonStreaming, options?: OpenAICoreRequestOptions): Promise<OpenAIClient.Chat.Completions.ChatCompletion>;
124
- /** @ignore */
125
- private _getClientOptions;
126
- _llmType(): string;
127
- }