@langchain/openai 0.4.2 → 0.4.4

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.
@@ -440,6 +440,20 @@ class AzureChatOpenAI extends chat_models_js_1.ChatOpenAI {
440
440
  azureOpenAIApiKey: "AZURE_OPENAI_API_KEY",
441
441
  };
442
442
  }
443
+ get lc_serializable_keys() {
444
+ return [
445
+ ...super.lc_serializable_keys,
446
+ "azureOpenAIApiKey",
447
+ "azureOpenAIApiVersion",
448
+ "azureOpenAIBasePath",
449
+ "azureOpenAIEndpoint",
450
+ "azureOpenAIApiInstanceName",
451
+ "azureOpenAIApiDeploymentName",
452
+ "deploymentName",
453
+ "openAIApiKey",
454
+ "openAIApiVersion",
455
+ ];
456
+ }
443
457
  constructor(fields) {
444
458
  super(fields);
445
459
  Object.defineProperty(this, "azureOpenAIApiVersion", {
@@ -540,11 +554,14 @@ class AzureChatOpenAI extends chat_models_js_1.ChatOpenAI {
540
554
  if (!params.baseURL) {
541
555
  delete params.baseURL;
542
556
  }
557
+ let env = (0, env_1.getEnv)();
558
+ if (env === "node" || env === "deno") {
559
+ env = `(${env}/${process.version}; ${process.platform}; ${process.arch})`;
560
+ }
561
+ const specifiedUserAgent = params.defaultHeaders?.["User-Agent"];
543
562
  params.defaultHeaders = {
544
563
  ...params.defaultHeaders,
545
- "User-Agent": params.defaultHeaders?.["User-Agent"]
546
- ? `${params.defaultHeaders["User-Agent"]}: langchainjs-azure-openai-v2`
547
- : `langchainjs-azure-openai-v2`,
564
+ "User-Agent": `langchainjs-azure-openai/2.0.0 (${env})${specifiedUserAgent ? ` ${specifiedUserAgent}` : ""}`,
548
565
  };
549
566
  this.client = new openai_1.AzureOpenAI({
550
567
  apiVersion: this.azureOpenAIApiVersion,
@@ -432,6 +432,7 @@ export declare class AzureChatOpenAI extends ChatOpenAI {
432
432
  get lc_secrets(): {
433
433
  [key: string]: string;
434
434
  } | undefined;
435
+ get lc_serializable_keys(): string[];
435
436
  constructor(fields?: Partial<OpenAIChatInput> & Partial<AzureOpenAIInput> & {
436
437
  openAIApiKey?: string;
437
438
  openAIApiVersion?: string;
@@ -1,5 +1,5 @@
1
1
  import { AzureOpenAI as AzureOpenAIClient } from "openai";
2
- import { getEnvironmentVariable } from "@langchain/core/utils/env";
2
+ import { getEnv, getEnvironmentVariable } from "@langchain/core/utils/env";
3
3
  import { ChatOpenAI, } from "../chat_models.js";
4
4
  import { getEndpoint } from "../utils/azure.js";
5
5
  /**
@@ -437,6 +437,20 @@ export class AzureChatOpenAI extends ChatOpenAI {
437
437
  azureOpenAIApiKey: "AZURE_OPENAI_API_KEY",
438
438
  };
439
439
  }
440
+ get lc_serializable_keys() {
441
+ return [
442
+ ...super.lc_serializable_keys,
443
+ "azureOpenAIApiKey",
444
+ "azureOpenAIApiVersion",
445
+ "azureOpenAIBasePath",
446
+ "azureOpenAIEndpoint",
447
+ "azureOpenAIApiInstanceName",
448
+ "azureOpenAIApiDeploymentName",
449
+ "deploymentName",
450
+ "openAIApiKey",
451
+ "openAIApiVersion",
452
+ ];
453
+ }
440
454
  constructor(fields) {
441
455
  super(fields);
442
456
  Object.defineProperty(this, "azureOpenAIApiVersion", {
@@ -537,11 +551,14 @@ export class AzureChatOpenAI extends ChatOpenAI {
537
551
  if (!params.baseURL) {
538
552
  delete params.baseURL;
539
553
  }
554
+ let env = getEnv();
555
+ if (env === "node" || env === "deno") {
556
+ env = `(${env}/${process.version}; ${process.platform}; ${process.arch})`;
557
+ }
558
+ const specifiedUserAgent = params.defaultHeaders?.["User-Agent"];
540
559
  params.defaultHeaders = {
541
560
  ...params.defaultHeaders,
542
- "User-Agent": params.defaultHeaders?.["User-Agent"]
543
- ? `${params.defaultHeaders["User-Agent"]}: langchainjs-azure-openai-v2`
544
- : `langchainjs-azure-openai-v2`,
561
+ "User-Agent": `langchainjs-azure-openai/2.0.0 (${env})${specifiedUserAgent ? ` ${specifiedUserAgent}` : ""}`,
545
562
  };
546
563
  this.client = new AzureOpenAIClient({
547
564
  apiVersion: this.azureOpenAIApiVersion,
@@ -55,7 +55,7 @@ function _convertMessagesToOpenAIParams(messages, model) {
55
55
  // TODO: Function messages do not support array content, fix cast
56
56
  return messages.flatMap((message) => {
57
57
  let role = messageToOpenAIRole(message);
58
- if (role === "system" && model?.startsWith("o1")) {
58
+ if (role === "system" && isReasoningModel(model)) {
59
59
  role = "developer";
60
60
  }
61
61
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -112,6 +112,9 @@ function _convertChatOpenAIToolTypeToOpenAITool(tool, fields) {
112
112
  }
113
113
  return (0, tools_js_1._convertToOpenAITool)(tool, fields);
114
114
  }
115
+ function isReasoningModel(model) {
116
+ return model?.startsWith("o1") || model?.startsWith("o3");
117
+ }
115
118
  /**
116
119
  * OpenAI chat model integration.
117
120
  *
@@ -682,6 +685,44 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
682
685
  apiKey: "openai_api_key",
683
686
  };
684
687
  }
688
+ get lc_serializable_keys() {
689
+ return [
690
+ "configuration",
691
+ "logprobs",
692
+ "topLogprobs",
693
+ "prefixMessages",
694
+ "supportsStrictToolCalling",
695
+ "modalities",
696
+ "audio",
697
+ "reasoningEffort",
698
+ "temperature",
699
+ "maxTokens",
700
+ "topP",
701
+ "frequencyPenalty",
702
+ "presencePenalty",
703
+ "n",
704
+ "logitBias",
705
+ "user",
706
+ "streaming",
707
+ "streamUsage",
708
+ "modelName",
709
+ "model",
710
+ "modelKwargs",
711
+ "stop",
712
+ "stopSequences",
713
+ "timeout",
714
+ "openAIApiKey",
715
+ "apiKey",
716
+ "cache",
717
+ "maxConcurrency",
718
+ "maxRetries",
719
+ "verbose",
720
+ "callbacks",
721
+ "tags",
722
+ "metadata",
723
+ "disableStreaming",
724
+ ];
725
+ }
685
726
  constructor(fields) {
686
727
  super(fields ?? {});
687
728
  Object.defineProperty(this, "lc_serializable", {
@@ -880,7 +921,6 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
880
921
  this.topP = fields?.topP ?? this.topP;
881
922
  this.frequencyPenalty = fields?.frequencyPenalty ?? this.frequencyPenalty;
882
923
  this.presencePenalty = fields?.presencePenalty ?? this.presencePenalty;
883
- this.maxTokens = fields?.maxTokens;
884
924
  this.logprobs = fields?.logprobs;
885
925
  this.topLogprobs = fields?.topLogprobs;
886
926
  this.n = fields?.n ?? this.n;
@@ -892,6 +932,7 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
892
932
  this.audio = fields?.audio;
893
933
  this.modalities = fields?.modalities;
894
934
  this.reasoningEffort = fields?.reasoningEffort;
935
+ this.maxTokens = fields?.maxCompletionTokens ?? fields?.maxTokens;
895
936
  if (this.model === "o1") {
896
937
  this.disableStreaming = true;
897
938
  }
@@ -968,7 +1009,6 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
968
1009
  top_p: this.topP,
969
1010
  frequency_penalty: this.frequencyPenalty,
970
1011
  presence_penalty: this.presencePenalty,
971
- max_tokens: this.maxTokens === -1 ? undefined : this.maxTokens,
972
1012
  logprobs: this.logprobs,
973
1013
  top_logprobs: this.topLogprobs,
974
1014
  n: this.n,
@@ -1002,6 +1042,13 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
1002
1042
  if (reasoningEffort !== undefined) {
1003
1043
  params.reasoning_effort = reasoningEffort;
1004
1044
  }
1045
+ if (isReasoningModel(params.model)) {
1046
+ params.max_completion_tokens =
1047
+ this.maxTokens === -1 ? undefined : this.maxTokens;
1048
+ }
1049
+ else {
1050
+ params.max_tokens = this.maxTokens === -1 ? undefined : this.maxTokens;
1051
+ }
1005
1052
  return params;
1006
1053
  }
1007
1054
  _convertOpenAIChatCompletionMessageToBaseMessage(message, rawResponse) {
@@ -655,6 +655,7 @@ export declare class ChatOpenAI<CallOptions extends ChatOpenAICallOptions = Chat
655
655
  [key: string]: string;
656
656
  } | undefined;
657
657
  get lc_aliases(): Record<string, string>;
658
+ get lc_serializable_keys(): string[];
658
659
  temperature?: number;
659
660
  topP?: number;
660
661
  frequencyPenalty?: number;
@@ -51,7 +51,7 @@ export function _convertMessagesToOpenAIParams(messages, model) {
51
51
  // TODO: Function messages do not support array content, fix cast
52
52
  return messages.flatMap((message) => {
53
53
  let role = messageToOpenAIRole(message);
54
- if (role === "system" && model?.startsWith("o1")) {
54
+ if (role === "system" && isReasoningModel(model)) {
55
55
  role = "developer";
56
56
  }
57
57
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -107,6 +107,9 @@ function _convertChatOpenAIToolTypeToOpenAITool(tool, fields) {
107
107
  }
108
108
  return _convertToOpenAITool(tool, fields);
109
109
  }
110
+ function isReasoningModel(model) {
111
+ return model?.startsWith("o1") || model?.startsWith("o3");
112
+ }
110
113
  /**
111
114
  * OpenAI chat model integration.
112
115
  *
@@ -677,6 +680,44 @@ export class ChatOpenAI extends BaseChatModel {
677
680
  apiKey: "openai_api_key",
678
681
  };
679
682
  }
683
+ get lc_serializable_keys() {
684
+ return [
685
+ "configuration",
686
+ "logprobs",
687
+ "topLogprobs",
688
+ "prefixMessages",
689
+ "supportsStrictToolCalling",
690
+ "modalities",
691
+ "audio",
692
+ "reasoningEffort",
693
+ "temperature",
694
+ "maxTokens",
695
+ "topP",
696
+ "frequencyPenalty",
697
+ "presencePenalty",
698
+ "n",
699
+ "logitBias",
700
+ "user",
701
+ "streaming",
702
+ "streamUsage",
703
+ "modelName",
704
+ "model",
705
+ "modelKwargs",
706
+ "stop",
707
+ "stopSequences",
708
+ "timeout",
709
+ "openAIApiKey",
710
+ "apiKey",
711
+ "cache",
712
+ "maxConcurrency",
713
+ "maxRetries",
714
+ "verbose",
715
+ "callbacks",
716
+ "tags",
717
+ "metadata",
718
+ "disableStreaming",
719
+ ];
720
+ }
680
721
  constructor(fields) {
681
722
  super(fields ?? {});
682
723
  Object.defineProperty(this, "lc_serializable", {
@@ -875,7 +916,6 @@ export class ChatOpenAI extends BaseChatModel {
875
916
  this.topP = fields?.topP ?? this.topP;
876
917
  this.frequencyPenalty = fields?.frequencyPenalty ?? this.frequencyPenalty;
877
918
  this.presencePenalty = fields?.presencePenalty ?? this.presencePenalty;
878
- this.maxTokens = fields?.maxTokens;
879
919
  this.logprobs = fields?.logprobs;
880
920
  this.topLogprobs = fields?.topLogprobs;
881
921
  this.n = fields?.n ?? this.n;
@@ -887,6 +927,7 @@ export class ChatOpenAI extends BaseChatModel {
887
927
  this.audio = fields?.audio;
888
928
  this.modalities = fields?.modalities;
889
929
  this.reasoningEffort = fields?.reasoningEffort;
930
+ this.maxTokens = fields?.maxCompletionTokens ?? fields?.maxTokens;
890
931
  if (this.model === "o1") {
891
932
  this.disableStreaming = true;
892
933
  }
@@ -963,7 +1004,6 @@ export class ChatOpenAI extends BaseChatModel {
963
1004
  top_p: this.topP,
964
1005
  frequency_penalty: this.frequencyPenalty,
965
1006
  presence_penalty: this.presencePenalty,
966
- max_tokens: this.maxTokens === -1 ? undefined : this.maxTokens,
967
1007
  logprobs: this.logprobs,
968
1008
  top_logprobs: this.topLogprobs,
969
1009
  n: this.n,
@@ -997,6 +1037,13 @@ export class ChatOpenAI extends BaseChatModel {
997
1037
  if (reasoningEffort !== undefined) {
998
1038
  params.reasoning_effort = reasoningEffort;
999
1039
  }
1040
+ if (isReasoningModel(params.model)) {
1041
+ params.max_completion_tokens =
1042
+ this.maxTokens === -1 ? undefined : this.maxTokens;
1043
+ }
1044
+ else {
1045
+ params.max_tokens = this.maxTokens === -1 ? undefined : this.maxTokens;
1046
+ }
1000
1047
  return params;
1001
1048
  }
1002
1049
  _convertOpenAIChatCompletionMessageToBaseMessage(message, rawResponse) {
package/dist/types.d.ts CHANGED
@@ -12,6 +12,12 @@ export declare interface OpenAIBaseInput {
12
12
  * tokens as possible given the prompt and the model's maximum context size.
13
13
  */
14
14
  maxTokens?: number;
15
+ /**
16
+ * Maximum number of tokens to generate in the completion. -1 returns as many
17
+ * tokens as possible given the prompt and the model's maximum context size.
18
+ * Alias for `maxTokens` for reasoning models.
19
+ */
20
+ maxCompletionTokens?: number;
15
21
  /** Total probability mass of tokens to consider at each step */
16
22
  topP: number;
17
23
  /** Penalizes repeated tokens according to frequency */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/openai",
3
- "version": "0.4.2",
3
+ "version": "0.4.4",
4
4
  "description": "OpenAI integrations for LangChain.js",
5
5
  "type": "module",
6
6
  "engines": {
@@ -41,7 +41,7 @@
41
41
  "zod-to-json-schema": "^3.22.3"
42
42
  },
43
43
  "peerDependencies": {
44
- "@langchain/core": ">=0.3.29 <0.4.0"
44
+ "@langchain/core": ">=0.3.39 <0.4.0"
45
45
  },
46
46
  "devDependencies": {
47
47
  "@azure/identity": "^4.2.1",