@langchain/openai 0.2.0 → 0.2.2

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.
@@ -108,6 +108,7 @@ delta, messageId, defaultRole) {
108
108
  args: rawToolCall.function?.arguments,
109
109
  id: rawToolCall.id,
110
110
  index: rawToolCall.index,
111
+ type: "tool_call_chunk",
111
112
  });
112
113
  }
113
114
  }
@@ -465,8 +466,6 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
465
466
  this.stop = fields?.stopSequences ?? fields?.stop;
466
467
  this.stopSequences = this?.stop;
467
468
  this.user = fields?.user;
468
- this.streaming = fields?.streaming ?? false;
469
- this.streamUsage = fields?.streamUsage ?? this.streamUsage;
470
469
  if (this.azureOpenAIApiKey || this.azureADTokenProvider) {
471
470
  if (!this.azureOpenAIApiInstanceName && !this.azureOpenAIBasePath) {
472
471
  throw new Error("Azure OpenAI API instance name not found");
@@ -478,7 +477,11 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
478
477
  throw new Error("Azure OpenAI API version not found");
479
478
  }
480
479
  this.apiKey = this.apiKey ?? "";
480
+ // Streaming usage is not supported by Azure deployments, so default to false
481
+ this.streamUsage = false;
481
482
  }
483
+ this.streaming = fields?.streaming ?? false;
484
+ this.streamUsage = fields?.streamUsage ?? this.streamUsage;
482
485
  this.clientConfig = {
483
486
  apiKey: this.apiKey,
484
487
  organization: this.organization,
@@ -512,7 +515,7 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
512
515
  /**
513
516
  * Get the parameters used to invoke the model
514
517
  */
515
- invocationParams(options) {
518
+ invocationParams(options, extra) {
516
519
  function isStructuredToolArray(tools) {
517
520
  return (tools !== undefined &&
518
521
  tools.every((tool) => Array.isArray(tool.lc_namespace)));
@@ -521,7 +524,7 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
521
524
  if (options?.stream_options !== undefined) {
522
525
  streamOptionsConfig = { stream_options: options.stream_options };
523
526
  }
524
- else if (this.streamUsage && this.streaming) {
527
+ else if (this.streamUsage && (this.streaming || extra?.streaming)) {
525
528
  streamOptionsConfig = { stream_options: { include_usage: true } };
526
529
  }
527
530
  const params = {
@@ -564,7 +567,9 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
564
567
  async *_streamResponseChunks(messages, options, runManager) {
565
568
  const messagesMapped = convertMessagesToOpenAIParams(messages);
566
569
  const params = {
567
- ...this.invocationParams(options),
570
+ ...this.invocationParams(options, {
571
+ streaming: true,
572
+ }),
568
573
  messages: messagesMapped,
569
574
  stream: true,
570
575
  };
@@ -607,8 +612,7 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
607
612
  generationInfo,
608
613
  });
609
614
  yield generationChunk;
610
- // eslint-disable-next-line no-void
611
- void runManager?.handleLLMNewToken(generationChunk.text ?? "", newTokenIndices, undefined, undefined, undefined, { chunk: generationChunk });
615
+ await runManager?.handleLLMNewToken(generationChunk.text ?? "", newTokenIndices, undefined, undefined, undefined, { chunk: generationChunk });
612
616
  }
613
617
  if (usage) {
614
618
  const generationChunk = new outputs_1.ChatGenerationChunk({
@@ -6,7 +6,7 @@ import { type StructuredToolInterface } from "@langchain/core/tools";
6
6
  import { BaseChatModel, LangSmithParams, type BaseChatModelParams } from "@langchain/core/language_models/chat_models";
7
7
  import type { BaseFunctionCallOptions, BaseLanguageModelInput, StructuredOutputMethodOptions, StructuredOutputMethodParams } from "@langchain/core/language_models/base";
8
8
  import { z } from "zod";
9
- import { Runnable } from "@langchain/core/runnables";
9
+ import { Runnable, RunnableToolLike } from "@langchain/core/runnables";
10
10
  import type { AzureOpenAIInput, OpenAICallOptions, OpenAIChatInput, OpenAICoreRequestOptions, LegacyOpenAIInput } from "./types.js";
11
11
  export type { AzureOpenAIInput, OpenAICallOptions, OpenAIChatInput };
12
12
  interface TokenUsage {
@@ -121,11 +121,13 @@ export declare class ChatOpenAI<CallOptions extends ChatOpenAICallOptions = Chat
121
121
  /** @deprecated */
122
122
  configuration?: ClientOptions & LegacyOpenAIInput);
123
123
  getLsParams(options: this["ParsedCallOptions"]): LangSmithParams;
124
- bindTools(tools: (Record<string, unknown> | StructuredToolInterface)[], kwargs?: Partial<CallOptions>): Runnable<BaseLanguageModelInput, AIMessageChunk, CallOptions>;
124
+ bindTools(tools: (Record<string, unknown> | StructuredToolInterface | RunnableToolLike)[], kwargs?: Partial<CallOptions>): Runnable<BaseLanguageModelInput, AIMessageChunk, CallOptions>;
125
125
  /**
126
126
  * Get the parameters used to invoke the model
127
127
  */
128
- invocationParams(options?: this["ParsedCallOptions"]): Omit<OpenAIClient.Chat.ChatCompletionCreateParams, "messages">;
128
+ invocationParams(options?: this["ParsedCallOptions"], extra?: {
129
+ streaming?: boolean;
130
+ }): Omit<OpenAIClient.Chat.ChatCompletionCreateParams, "messages">;
129
131
  /** @ignore */
130
132
  _identifyingParams(): Omit<OpenAIClient.Chat.ChatCompletionCreateParams, "messages"> & {
131
133
  model_name: string;
@@ -104,6 +104,7 @@ delta, messageId, defaultRole) {
104
104
  args: rawToolCall.function?.arguments,
105
105
  id: rawToolCall.id,
106
106
  index: rawToolCall.index,
107
+ type: "tool_call_chunk",
107
108
  });
108
109
  }
109
110
  }
@@ -461,8 +462,6 @@ export class ChatOpenAI extends BaseChatModel {
461
462
  this.stop = fields?.stopSequences ?? fields?.stop;
462
463
  this.stopSequences = this?.stop;
463
464
  this.user = fields?.user;
464
- this.streaming = fields?.streaming ?? false;
465
- this.streamUsage = fields?.streamUsage ?? this.streamUsage;
466
465
  if (this.azureOpenAIApiKey || this.azureADTokenProvider) {
467
466
  if (!this.azureOpenAIApiInstanceName && !this.azureOpenAIBasePath) {
468
467
  throw new Error("Azure OpenAI API instance name not found");
@@ -474,7 +473,11 @@ export class ChatOpenAI extends BaseChatModel {
474
473
  throw new Error("Azure OpenAI API version not found");
475
474
  }
476
475
  this.apiKey = this.apiKey ?? "";
476
+ // Streaming usage is not supported by Azure deployments, so default to false
477
+ this.streamUsage = false;
477
478
  }
479
+ this.streaming = fields?.streaming ?? false;
480
+ this.streamUsage = fields?.streamUsage ?? this.streamUsage;
478
481
  this.clientConfig = {
479
482
  apiKey: this.apiKey,
480
483
  organization: this.organization,
@@ -508,7 +511,7 @@ export class ChatOpenAI extends BaseChatModel {
508
511
  /**
509
512
  * Get the parameters used to invoke the model
510
513
  */
511
- invocationParams(options) {
514
+ invocationParams(options, extra) {
512
515
  function isStructuredToolArray(tools) {
513
516
  return (tools !== undefined &&
514
517
  tools.every((tool) => Array.isArray(tool.lc_namespace)));
@@ -517,7 +520,7 @@ export class ChatOpenAI extends BaseChatModel {
517
520
  if (options?.stream_options !== undefined) {
518
521
  streamOptionsConfig = { stream_options: options.stream_options };
519
522
  }
520
- else if (this.streamUsage && this.streaming) {
523
+ else if (this.streamUsage && (this.streaming || extra?.streaming)) {
521
524
  streamOptionsConfig = { stream_options: { include_usage: true } };
522
525
  }
523
526
  const params = {
@@ -560,7 +563,9 @@ export class ChatOpenAI extends BaseChatModel {
560
563
  async *_streamResponseChunks(messages, options, runManager) {
561
564
  const messagesMapped = convertMessagesToOpenAIParams(messages);
562
565
  const params = {
563
- ...this.invocationParams(options),
566
+ ...this.invocationParams(options, {
567
+ streaming: true,
568
+ }),
564
569
  messages: messagesMapped,
565
570
  stream: true,
566
571
  };
@@ -603,8 +608,7 @@ export class ChatOpenAI extends BaseChatModel {
603
608
  generationInfo,
604
609
  });
605
610
  yield generationChunk;
606
- // eslint-disable-next-line no-void
607
- void runManager?.handleLLMNewToken(generationChunk.text ?? "", newTokenIndices, undefined, undefined, undefined, { chunk: generationChunk });
611
+ await runManager?.handleLLMNewToken(generationChunk.text ?? "", newTokenIndices, undefined, undefined, undefined, { chunk: generationChunk });
608
612
  }
609
613
  if (usage) {
610
614
  const generationChunk = new ChatGenerationChunk({
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DallEAPIWrapper = void 0;
4
+ /* eslint-disable no-param-reassign */
4
5
  const env_1 = require("@langchain/core/utils/env");
5
6
  const openai_1 = require("openai");
6
7
  const tools_1 = require("@langchain/core/tools");
@@ -12,6 +13,13 @@ class DallEAPIWrapper extends tools_1.Tool {
12
13
  return "DallEAPIWrapper";
13
14
  }
14
15
  constructor(fields) {
16
+ // Shim for new base tool param name
17
+ if (fields?.responseFormat !== undefined &&
18
+ ["url", "b64_json"].includes(fields.responseFormat)) {
19
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
20
+ fields.dallEResponseFormat = fields.responseFormat;
21
+ fields.responseFormat = "content";
22
+ }
15
23
  super(fields);
16
24
  Object.defineProperty(this, "name", {
17
25
  enumerable: true,
@@ -61,7 +69,7 @@ class DallEAPIWrapper extends tools_1.Tool {
61
69
  writable: true,
62
70
  value: "1024x1024"
63
71
  });
64
- Object.defineProperty(this, "responseFormat", {
72
+ Object.defineProperty(this, "dallEResponseFormat", {
65
73
  enumerable: true,
66
74
  configurable: true,
67
75
  writable: true,
@@ -88,7 +96,8 @@ class DallEAPIWrapper extends tools_1.Tool {
88
96
  this.quality = fields?.quality ?? this.quality;
89
97
  this.n = fields?.n ?? this.n;
90
98
  this.size = fields?.size ?? this.size;
91
- this.responseFormat = fields?.responseFormat ?? this.responseFormat;
99
+ this.dallEResponseFormat =
100
+ fields?.dallEResponseFormat ?? this.dallEResponseFormat;
92
101
  this.user = fields?.user;
93
102
  }
94
103
  /** @ignore */
@@ -98,13 +107,13 @@ class DallEAPIWrapper extends tools_1.Tool {
98
107
  prompt: input,
99
108
  n: this.n,
100
109
  size: this.size,
101
- response_format: this.responseFormat,
110
+ response_format: this.dallEResponseFormat,
102
111
  style: this.style,
103
112
  quality: this.quality,
104
113
  user: this.user,
105
114
  });
106
115
  let data = "";
107
- if (this.responseFormat === "url") {
116
+ if (this.dallEResponseFormat === "url") {
108
117
  [data] = response.data
109
118
  .map((item) => item.url)
110
119
  .filter((url) => url !== "undefined");
@@ -58,7 +58,11 @@ export interface DallEAPIWrapperParams extends ToolParams {
58
58
  * Must be one of "url" or "b64_json".
59
59
  * @default "url"
60
60
  */
61
- responseFormat?: "url" | "b64_json";
61
+ dallEResponseFormat?: "url" | "b64_json";
62
+ /**
63
+ * @deprecated Use dallEResponseFormat instead for the Dall-E response type.
64
+ */
65
+ responseFormat?: any;
62
66
  /**
63
67
  * A unique identifier representing your end-user, which will help
64
68
  * OpenAI to monitor and detect abuse.
@@ -83,7 +87,7 @@ export declare class DallEAPIWrapper extends Tool {
83
87
  private quality;
84
88
  private n;
85
89
  private size;
86
- private responseFormat;
90
+ private dallEResponseFormat;
87
91
  private user?;
88
92
  constructor(fields?: DallEAPIWrapperParams);
89
93
  /** @ignore */
@@ -1,3 +1,4 @@
1
+ /* eslint-disable no-param-reassign */
1
2
  import { getEnvironmentVariable } from "@langchain/core/utils/env";
2
3
  import { OpenAI as OpenAIClient } from "openai";
3
4
  import { Tool } from "@langchain/core/tools";
@@ -9,6 +10,13 @@ export class DallEAPIWrapper extends Tool {
9
10
  return "DallEAPIWrapper";
10
11
  }
11
12
  constructor(fields) {
13
+ // Shim for new base tool param name
14
+ if (fields?.responseFormat !== undefined &&
15
+ ["url", "b64_json"].includes(fields.responseFormat)) {
16
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
17
+ fields.dallEResponseFormat = fields.responseFormat;
18
+ fields.responseFormat = "content";
19
+ }
12
20
  super(fields);
13
21
  Object.defineProperty(this, "name", {
14
22
  enumerable: true,
@@ -58,7 +66,7 @@ export class DallEAPIWrapper extends Tool {
58
66
  writable: true,
59
67
  value: "1024x1024"
60
68
  });
61
- Object.defineProperty(this, "responseFormat", {
69
+ Object.defineProperty(this, "dallEResponseFormat", {
62
70
  enumerable: true,
63
71
  configurable: true,
64
72
  writable: true,
@@ -85,7 +93,8 @@ export class DallEAPIWrapper extends Tool {
85
93
  this.quality = fields?.quality ?? this.quality;
86
94
  this.n = fields?.n ?? this.n;
87
95
  this.size = fields?.size ?? this.size;
88
- this.responseFormat = fields?.responseFormat ?? this.responseFormat;
96
+ this.dallEResponseFormat =
97
+ fields?.dallEResponseFormat ?? this.dallEResponseFormat;
89
98
  this.user = fields?.user;
90
99
  }
91
100
  /** @ignore */
@@ -95,13 +104,13 @@ export class DallEAPIWrapper extends Tool {
95
104
  prompt: input,
96
105
  n: this.n,
97
106
  size: this.size,
98
- response_format: this.responseFormat,
107
+ response_format: this.dallEResponseFormat,
99
108
  style: this.style,
100
109
  quality: this.quality,
101
110
  user: this.user,
102
111
  });
103
112
  let data = "";
104
- if (this.responseFormat === "url") {
113
+ if (this.dallEResponseFormat === "url") {
105
114
  [data] = response.data
106
115
  .map((item) => item.url)
107
116
  .filter((url) => url !== "undefined");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/openai",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "OpenAI integrations for LangChain.js",
5
5
  "type": "module",
6
6
  "engines": {
@@ -35,7 +35,7 @@
35
35
  "author": "LangChain",
36
36
  "license": "MIT",
37
37
  "dependencies": {
38
- "@langchain/core": ">=0.2.8 <0.3.0",
38
+ "@langchain/core": ">=0.2.16 <0.3.0",
39
39
  "js-tiktoken": "^1.0.12",
40
40
  "openai": "^4.49.1",
41
41
  "zod": "^3.22.4",