@langchain/openai 0.0.27 → 0.0.29

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.
@@ -1,12 +1,12 @@
1
1
  import { OpenAI as OpenAIClient } from "openai";
2
- import { AIMessage, AIMessageChunk, ChatMessage, ChatMessageChunk, FunctionMessageChunk, HumanMessageChunk, SystemMessageChunk, ToolMessageChunk, } from "@langchain/core/messages";
2
+ import { AIMessage, AIMessageChunk, ChatMessage, ChatMessageChunk, FunctionMessageChunk, HumanMessageChunk, SystemMessageChunk, ToolMessageChunk, isAIMessage, } from "@langchain/core/messages";
3
3
  import { ChatGenerationChunk, } from "@langchain/core/outputs";
4
4
  import { getEnvironmentVariable } from "@langchain/core/utils/env";
5
5
  import { BaseChatModel, } from "@langchain/core/language_models/chat_models";
6
6
  import { convertToOpenAITool } from "@langchain/core/utils/function_calling";
7
7
  import { RunnablePassthrough, RunnableSequence, } from "@langchain/core/runnables";
8
8
  import { JsonOutputParser, StructuredOutputParser, } from "@langchain/core/output_parsers";
9
- import { JsonOutputKeyToolsParser } from "@langchain/core/output_parsers/openai_tools";
9
+ import { JsonOutputKeyToolsParser, convertLangChainToolCallToOpenAI, makeInvalidToolCall, parseToolCall, } from "@langchain/core/output_parsers/openai_tools";
10
10
  import { zodToJsonSchema } from "zod-to-json-schema";
11
11
  import { getEndpoint } from "./utils/azure.js";
12
12
  import { wrapOpenAIClientError } from "./utils/openai.js";
@@ -44,12 +44,30 @@ export function messageToOpenAIRole(message) {
44
44
  }
45
45
  }
46
46
  function openAIResponseToChatMessage(message) {
47
+ const rawToolCalls = message.tool_calls;
47
48
  switch (message.role) {
48
- case "assistant":
49
- return new AIMessage(message.content || "", {
50
- function_call: message.function_call,
51
- tool_calls: message.tool_calls,
49
+ case "assistant": {
50
+ const toolCalls = [];
51
+ const invalidToolCalls = [];
52
+ for (const rawToolCall of rawToolCalls ?? []) {
53
+ try {
54
+ toolCalls.push(parseToolCall(rawToolCall, { returnId: true }));
55
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
56
+ }
57
+ catch (e) {
58
+ invalidToolCalls.push(makeInvalidToolCall(rawToolCall, e.message));
59
+ }
60
+ }
61
+ return new AIMessage({
62
+ content: message.content || "",
63
+ tool_calls: toolCalls,
64
+ invalid_tool_calls: invalidToolCalls,
65
+ additional_kwargs: {
66
+ function_call: message.function_call,
67
+ tool_calls: rawToolCalls,
68
+ },
52
69
  });
70
+ }
53
71
  default:
54
72
  return new ChatMessage(message.content || "", message.role ?? "unknown");
55
73
  }
@@ -77,7 +95,22 @@ delta, defaultRole) {
77
95
  return new HumanMessageChunk({ content });
78
96
  }
79
97
  else if (role === "assistant") {
80
- return new AIMessageChunk({ content, additional_kwargs });
98
+ const toolCallChunks = [];
99
+ if (Array.isArray(delta.tool_calls)) {
100
+ for (const rawToolCall of delta.tool_calls) {
101
+ toolCallChunks.push({
102
+ name: rawToolCall.function?.name,
103
+ args: rawToolCall.function?.arguments,
104
+ id: rawToolCall.id,
105
+ index: rawToolCall.index,
106
+ });
107
+ }
108
+ }
109
+ return new AIMessageChunk({
110
+ content,
111
+ tool_call_chunks: toolCallChunks,
112
+ additional_kwargs,
113
+ });
81
114
  }
82
115
  else if (role === "system") {
83
116
  return new SystemMessageChunk({ content });
@@ -114,11 +147,16 @@ function convertMessagesToOpenAIParams(messages) {
114
147
  if (message.additional_kwargs.function_call != null) {
115
148
  completionParam.function_call = message.additional_kwargs.function_call;
116
149
  }
117
- if (message.additional_kwargs.tool_calls != null) {
118
- completionParam.tool_calls = message.additional_kwargs.tool_calls;
150
+ if (isAIMessage(message) && !!message.tool_calls?.length) {
151
+ completionParam.tool_calls = message.tool_calls.map(convertLangChainToolCallToOpenAI);
119
152
  }
120
- if (message.tool_call_id != null) {
121
- completionParam.tool_call_id = message.tool_call_id;
153
+ else {
154
+ if (message.additional_kwargs.tool_calls != null) {
155
+ completionParam.tool_calls = message.additional_kwargs.tool_calls;
156
+ }
157
+ if (message.tool_call_id != null) {
158
+ completionParam.tool_call_id = message.tool_call_id;
159
+ }
122
160
  }
123
161
  return completionParam;
124
162
  });
@@ -329,6 +367,12 @@ export class ChatOpenAI extends BaseChatModel {
329
367
  writable: true,
330
368
  value: void 0
331
369
  });
370
+ Object.defineProperty(this, "azureADTokenProvider", {
371
+ enumerable: true,
372
+ configurable: true,
373
+ writable: true,
374
+ value: void 0
375
+ });
332
376
  Object.defineProperty(this, "azureOpenAIApiInstanceName", {
333
377
  enumerable: true,
334
378
  configurable: true,
@@ -373,8 +417,9 @@ export class ChatOpenAI extends BaseChatModel {
373
417
  this.azureOpenAIApiKey =
374
418
  fields?.azureOpenAIApiKey ??
375
419
  getEnvironmentVariable("AZURE_OPENAI_API_KEY");
376
- if (!this.azureOpenAIApiKey && !this.apiKey) {
377
- throw new Error("OpenAI or Azure OpenAI API key not found");
420
+ this.azureADTokenProvider = fields?.azureADTokenProvider ?? undefined;
421
+ if (!this.azureOpenAIApiKey && !this.apiKey && !this.azureADTokenProvider) {
422
+ throw new Error("OpenAI or Azure OpenAI API key or Token Provider not found");
378
423
  }
379
424
  this.azureOpenAIApiInstanceName =
380
425
  fields?.azureOpenAIApiInstanceName ??
@@ -408,7 +453,7 @@ export class ChatOpenAI extends BaseChatModel {
408
453
  this.stopSequences = this?.stop;
409
454
  this.user = fields?.user;
410
455
  this.streaming = fields?.streaming ?? false;
411
- if (this.azureOpenAIApiKey) {
456
+ if (this.azureOpenAIApiKey || this.azureADTokenProvider) {
412
457
  if (!this.azureOpenAIApiInstanceName && !this.azureOpenAIBasePath) {
413
458
  throw new Error("Azure OpenAI API instance name not found");
414
459
  }
@@ -433,6 +478,12 @@ export class ChatOpenAI extends BaseChatModel {
433
478
  ...fields?.configuration,
434
479
  };
435
480
  }
481
+ bindTools(tools, kwargs) {
482
+ return this.bind({
483
+ tools: tools.map(convertToOpenAITool),
484
+ ...kwargs,
485
+ });
486
+ }
436
487
  /**
437
488
  * Get the parameters used to invoke the model
438
489
  */
@@ -79,6 +79,12 @@ class OpenAIEmbeddings extends embeddings_1.Embeddings {
79
79
  writable: true,
80
80
  value: void 0
81
81
  });
82
+ Object.defineProperty(this, "azureADTokenProvider", {
83
+ enumerable: true,
84
+ configurable: true,
85
+ writable: true,
86
+ value: void 0
87
+ });
82
88
  Object.defineProperty(this, "azureOpenAIApiInstanceName", {
83
89
  enumerable: true,
84
90
  configurable: true,
@@ -120,8 +126,9 @@ class OpenAIEmbeddings extends embeddings_1.Embeddings {
120
126
  (0, env_1.getEnvironmentVariable)("OPENAI_API_KEY");
121
127
  const azureApiKey = fieldsWithDefaults?.azureOpenAIApiKey ??
122
128
  (0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_KEY");
123
- if (!azureApiKey && !apiKey) {
124
- throw new Error("OpenAI or Azure OpenAI API key not found");
129
+ this.azureADTokenProvider = fields?.azureADTokenProvider ?? undefined;
130
+ if (!azureApiKey && !apiKey && !this.azureADTokenProvider) {
131
+ throw new Error("OpenAI or Azure OpenAI API key or Token Provider not found");
125
132
  }
126
133
  const azureApiInstanceName = fieldsWithDefaults?.azureOpenAIApiInstanceName ??
127
134
  (0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_INSTANCE_NAME");
@@ -150,7 +157,7 @@ class OpenAIEmbeddings extends embeddings_1.Embeddings {
150
157
  this.azureOpenAIApiKey = azureApiKey;
151
158
  this.azureOpenAIApiInstanceName = azureApiInstanceName;
152
159
  this.azureOpenAIApiDeploymentName = azureApiDeploymentName;
153
- if (this.azureOpenAIApiKey) {
160
+ if (this.azureOpenAIApiKey || this.azureADTokenProvider) {
154
161
  if (!this.azureOpenAIApiInstanceName && !this.azureOpenAIBasePath) {
155
162
  throw new Error("Azure OpenAI API instance name not found");
156
163
  }
@@ -1,4 +1,4 @@
1
- import { type ClientOptions } from "openai";
1
+ import { type ClientOptions, OpenAI as OpenAIClient } from "openai";
2
2
  import { Embeddings, type EmbeddingsParams } from "@langchain/core/embeddings";
3
3
  import { AzureOpenAIInput, LegacyOpenAIInput } from "./types.js";
4
4
  /**
@@ -62,12 +62,13 @@ export declare class OpenAIEmbeddings extends Embeddings implements OpenAIEmbedd
62
62
  timeout?: number;
63
63
  azureOpenAIApiVersion?: string;
64
64
  azureOpenAIApiKey?: string;
65
+ azureADTokenProvider?: () => Promise<string>;
65
66
  azureOpenAIApiInstanceName?: string;
66
67
  azureOpenAIApiDeploymentName?: string;
67
68
  azureOpenAIBasePath?: string;
68
69
  organization?: string;
69
- private client;
70
- private clientConfig;
70
+ protected client: OpenAIClient;
71
+ protected clientConfig: ClientOptions;
71
72
  constructor(fields?: Partial<OpenAIEmbeddingsParams> & Partial<AzureOpenAIInput> & {
72
73
  verbose?: boolean;
73
74
  /**
@@ -101,5 +102,5 @@ export declare class OpenAIEmbeddings extends Embeddings implements OpenAIEmbedd
101
102
  * @param request Request to send to the OpenAI API.
102
103
  * @returns Promise that resolves to the response from the API.
103
104
  */
104
- private embeddingWithRetry;
105
+ protected embeddingWithRetry(request: OpenAIClient.EmbeddingCreateParams): Promise<OpenAIClient.Embeddings.CreateEmbeddingResponse>;
105
106
  }
@@ -76,6 +76,12 @@ export class OpenAIEmbeddings extends Embeddings {
76
76
  writable: true,
77
77
  value: void 0
78
78
  });
79
+ Object.defineProperty(this, "azureADTokenProvider", {
80
+ enumerable: true,
81
+ configurable: true,
82
+ writable: true,
83
+ value: void 0
84
+ });
79
85
  Object.defineProperty(this, "azureOpenAIApiInstanceName", {
80
86
  enumerable: true,
81
87
  configurable: true,
@@ -117,8 +123,9 @@ export class OpenAIEmbeddings extends Embeddings {
117
123
  getEnvironmentVariable("OPENAI_API_KEY");
118
124
  const azureApiKey = fieldsWithDefaults?.azureOpenAIApiKey ??
119
125
  getEnvironmentVariable("AZURE_OPENAI_API_KEY");
120
- if (!azureApiKey && !apiKey) {
121
- throw new Error("OpenAI or Azure OpenAI API key not found");
126
+ this.azureADTokenProvider = fields?.azureADTokenProvider ?? undefined;
127
+ if (!azureApiKey && !apiKey && !this.azureADTokenProvider) {
128
+ throw new Error("OpenAI or Azure OpenAI API key or Token Provider not found");
122
129
  }
123
130
  const azureApiInstanceName = fieldsWithDefaults?.azureOpenAIApiInstanceName ??
124
131
  getEnvironmentVariable("AZURE_OPENAI_API_INSTANCE_NAME");
@@ -147,7 +154,7 @@ export class OpenAIEmbeddings extends Embeddings {
147
154
  this.azureOpenAIApiKey = azureApiKey;
148
155
  this.azureOpenAIApiInstanceName = azureApiInstanceName;
149
156
  this.azureOpenAIApiDeploymentName = azureApiDeploymentName;
150
- if (this.azureOpenAIApiKey) {
157
+ if (this.azureOpenAIApiKey || this.azureADTokenProvider) {
151
158
  if (!this.azureOpenAIApiInstanceName && !this.azureOpenAIBasePath) {
152
159
  throw new Error("Azure OpenAI API instance name not found");
153
160
  }
package/dist/index.cjs CHANGED
@@ -22,6 +22,7 @@ __exportStar(require("./chat_models.cjs"), exports);
22
22
  __exportStar(require("./azure/chat_models.cjs"), exports);
23
23
  __exportStar(require("./llms.cjs"), exports);
24
24
  __exportStar(require("./azure/llms.cjs"), exports);
25
+ __exportStar(require("./azure/embeddings.cjs"), exports);
25
26
  __exportStar(require("./embeddings.cjs"), exports);
26
27
  __exportStar(require("./types.cjs"), exports);
27
28
  __exportStar(require("./utils/openai.cjs"), exports);
package/dist/index.d.ts CHANGED
@@ -3,6 +3,7 @@ export * from "./chat_models.js";
3
3
  export * from "./azure/chat_models.js";
4
4
  export * from "./llms.js";
5
5
  export * from "./azure/llms.js";
6
+ export * from "./azure/embeddings.js";
6
7
  export * from "./embeddings.js";
7
8
  export * from "./types.js";
8
9
  export * from "./utils/openai.js";
package/dist/index.js CHANGED
@@ -3,6 +3,7 @@ export * from "./chat_models.js";
3
3
  export * from "./azure/chat_models.js";
4
4
  export * from "./llms.js";
5
5
  export * from "./azure/llms.js";
6
+ export * from "./azure/embeddings.js";
6
7
  export * from "./embeddings.js";
7
8
  export * from "./types.js";
8
9
  export * from "./utils/openai.js";
package/dist/llms.cjs CHANGED
@@ -211,6 +211,12 @@ class OpenAI extends llms_1.BaseLLM {
211
211
  writable: true,
212
212
  value: void 0
213
213
  });
214
+ Object.defineProperty(this, "azureADTokenProvider", {
215
+ enumerable: true,
216
+ configurable: true,
217
+ writable: true,
218
+ value: void 0
219
+ });
214
220
  Object.defineProperty(this, "azureOpenAIApiInstanceName", {
215
221
  enumerable: true,
216
222
  configurable: true,
@@ -256,8 +262,9 @@ class OpenAI extends llms_1.BaseLLM {
256
262
  this.azureOpenAIApiKey =
257
263
  fields?.azureOpenAIApiKey ??
258
264
  (0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_KEY");
259
- if (!this.azureOpenAIApiKey && !this.apiKey) {
260
- throw new Error("OpenAI or Azure OpenAI API key not found");
265
+ this.azureADTokenProvider = fields?.azureADTokenProvider ?? undefined;
266
+ if (!this.azureOpenAIApiKey && !this.apiKey && !this.azureADTokenProvider) {
267
+ throw new Error("OpenAI or Azure OpenAI API key or Token Provider not found");
261
268
  }
262
269
  this.azureOpenAIApiInstanceName =
263
270
  fields?.azureOpenAIApiInstanceName ??
@@ -296,7 +303,7 @@ class OpenAI extends llms_1.BaseLLM {
296
303
  if (this.streaming && this.bestOf && this.bestOf > 1) {
297
304
  throw new Error("Cannot stream results when bestOf > 1");
298
305
  }
299
- if (this.azureOpenAIApiKey) {
306
+ if (this.azureOpenAIApiKey || this.azureADTokenProvider) {
300
307
  if (!this.azureOpenAIApiInstanceName && !this.azureOpenAIBasePath) {
301
308
  throw new Error("Azure OpenAI API instance name not found");
302
309
  }
package/dist/llms.d.ts CHANGED
@@ -67,12 +67,13 @@ export declare class OpenAI<CallOptions extends OpenAICallOptions = OpenAICallOp
67
67
  apiKey?: string;
68
68
  azureOpenAIApiVersion?: string;
69
69
  azureOpenAIApiKey?: string;
70
+ azureADTokenProvider?: () => Promise<string>;
70
71
  azureOpenAIApiInstanceName?: string;
71
72
  azureOpenAIApiDeploymentName?: string;
72
73
  azureOpenAIBasePath?: string;
73
74
  organization?: string;
74
- private client;
75
- private clientConfig;
75
+ protected client: OpenAIClient;
76
+ protected clientConfig: ClientOptions;
76
77
  constructor(fields?: Partial<OpenAIInput> & Partial<AzureOpenAIInput> & BaseLLMParams & {
77
78
  configuration?: ClientOptions & LegacyOpenAIInput;
78
79
  },
@@ -124,6 +125,6 @@ export declare class OpenAI<CallOptions extends OpenAICallOptions = OpenAICallOp
124
125
  * @param options Optional configuration for the API call.
125
126
  * @returns The response from the OpenAI API.
126
127
  */
127
- private _getClientOptions;
128
+ protected _getClientOptions(options: OpenAICoreRequestOptions | undefined): OpenAICoreRequestOptions;
128
129
  _llmType(): string;
129
130
  }
package/dist/llms.js CHANGED
@@ -208,6 +208,12 @@ export class OpenAI extends BaseLLM {
208
208
  writable: true,
209
209
  value: void 0
210
210
  });
211
+ Object.defineProperty(this, "azureADTokenProvider", {
212
+ enumerable: true,
213
+ configurable: true,
214
+ writable: true,
215
+ value: void 0
216
+ });
211
217
  Object.defineProperty(this, "azureOpenAIApiInstanceName", {
212
218
  enumerable: true,
213
219
  configurable: true,
@@ -253,8 +259,9 @@ export class OpenAI extends BaseLLM {
253
259
  this.azureOpenAIApiKey =
254
260
  fields?.azureOpenAIApiKey ??
255
261
  getEnvironmentVariable("AZURE_OPENAI_API_KEY");
256
- if (!this.azureOpenAIApiKey && !this.apiKey) {
257
- throw new Error("OpenAI or Azure OpenAI API key not found");
262
+ this.azureADTokenProvider = fields?.azureADTokenProvider ?? undefined;
263
+ if (!this.azureOpenAIApiKey && !this.apiKey && !this.azureADTokenProvider) {
264
+ throw new Error("OpenAI or Azure OpenAI API key or Token Provider not found");
258
265
  }
259
266
  this.azureOpenAIApiInstanceName =
260
267
  fields?.azureOpenAIApiInstanceName ??
@@ -293,7 +300,7 @@ export class OpenAI extends BaseLLM {
293
300
  if (this.streaming && this.bestOf && this.bestOf > 1) {
294
301
  throw new Error("Cannot stream results when bestOf > 1");
295
302
  }
296
- if (this.azureOpenAIApiKey) {
303
+ if (this.azureOpenAIApiKey || this.azureADTokenProvider) {
297
304
  if (!this.azureOpenAIApiInstanceName && !this.azureOpenAIBasePath) {
298
305
  throw new Error("Azure OpenAI API instance name not found");
299
306
  }
@@ -0,0 +1 @@
1
+ export {};