@langchain/openai 0.2.1 → 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
  }
@@ -514,7 +515,7 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
514
515
  /**
515
516
  * Get the parameters used to invoke the model
516
517
  */
517
- invocationParams(options) {
518
+ invocationParams(options, extra) {
518
519
  function isStructuredToolArray(tools) {
519
520
  return (tools !== undefined &&
520
521
  tools.every((tool) => Array.isArray(tool.lc_namespace)));
@@ -523,7 +524,7 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
523
524
  if (options?.stream_options !== undefined) {
524
525
  streamOptionsConfig = { stream_options: options.stream_options };
525
526
  }
526
- else if (this.streamUsage && this.streaming) {
527
+ else if (this.streamUsage && (this.streaming || extra?.streaming)) {
527
528
  streamOptionsConfig = { stream_options: { include_usage: true } };
528
529
  }
529
530
  const params = {
@@ -566,7 +567,9 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
566
567
  async *_streamResponseChunks(messages, options, runManager) {
567
568
  const messagesMapped = convertMessagesToOpenAIParams(messages);
568
569
  const params = {
569
- ...this.invocationParams(options),
570
+ ...this.invocationParams(options, {
571
+ streaming: true,
572
+ }),
570
573
  messages: messagesMapped,
571
574
  stream: true,
572
575
  };
@@ -609,8 +612,7 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
609
612
  generationInfo,
610
613
  });
611
614
  yield generationChunk;
612
- // eslint-disable-next-line no-void
613
- void runManager?.handleLLMNewToken(generationChunk.text ?? "", newTokenIndices, undefined, undefined, undefined, { chunk: generationChunk });
615
+ await runManager?.handleLLMNewToken(generationChunk.text ?? "", newTokenIndices, undefined, undefined, undefined, { chunk: generationChunk });
614
616
  }
615
617
  if (usage) {
616
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
  }
@@ -510,7 +511,7 @@ export class ChatOpenAI extends BaseChatModel {
510
511
  /**
511
512
  * Get the parameters used to invoke the model
512
513
  */
513
- invocationParams(options) {
514
+ invocationParams(options, extra) {
514
515
  function isStructuredToolArray(tools) {
515
516
  return (tools !== undefined &&
516
517
  tools.every((tool) => Array.isArray(tool.lc_namespace)));
@@ -519,7 +520,7 @@ export class ChatOpenAI extends BaseChatModel {
519
520
  if (options?.stream_options !== undefined) {
520
521
  streamOptionsConfig = { stream_options: options.stream_options };
521
522
  }
522
- else if (this.streamUsage && this.streaming) {
523
+ else if (this.streamUsage && (this.streaming || extra?.streaming)) {
523
524
  streamOptionsConfig = { stream_options: { include_usage: true } };
524
525
  }
525
526
  const params = {
@@ -562,7 +563,9 @@ export class ChatOpenAI extends BaseChatModel {
562
563
  async *_streamResponseChunks(messages, options, runManager) {
563
564
  const messagesMapped = convertMessagesToOpenAIParams(messages);
564
565
  const params = {
565
- ...this.invocationParams(options),
566
+ ...this.invocationParams(options, {
567
+ streaming: true,
568
+ }),
566
569
  messages: messagesMapped,
567
570
  stream: true,
568
571
  };
@@ -605,8 +608,7 @@ export class ChatOpenAI extends BaseChatModel {
605
608
  generationInfo,
606
609
  });
607
610
  yield generationChunk;
608
- // eslint-disable-next-line no-void
609
- void runManager?.handleLLMNewToken(generationChunk.text ?? "", newTokenIndices, undefined, undefined, undefined, { chunk: generationChunk });
611
+ await runManager?.handleLLMNewToken(generationChunk.text ?? "", newTokenIndices, undefined, undefined, undefined, { chunk: generationChunk });
610
612
  }
611
613
  if (usage) {
612
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.1",
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",