@langchain/openai 0.0.34 → 0.1.0

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.
@@ -529,6 +529,9 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
529
529
  tool_choice: options?.tool_choice,
530
530
  response_format: options?.response_format,
531
531
  seed: options?.seed,
532
+ ...(options?.stream_options !== undefined
533
+ ? { stream_options: options.stream_options }
534
+ : {}),
532
535
  ...this.modelKwargs,
533
536
  };
534
537
  return params;
@@ -550,8 +553,12 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
550
553
  };
551
554
  let defaultRole;
552
555
  const streamIterable = await this.completionWithRetry(params, options);
556
+ let usage;
553
557
  for await (const data of streamIterable) {
554
558
  const choice = data?.choices[0];
559
+ if (data.usage) {
560
+ usage = data.usage;
561
+ }
555
562
  if (!choice) {
556
563
  continue;
557
564
  }
@@ -586,6 +593,20 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
586
593
  // eslint-disable-next-line no-void
587
594
  void runManager?.handleLLMNewToken(generationChunk.text ?? "", newTokenIndices, undefined, undefined, undefined, { chunk: generationChunk });
588
595
  }
596
+ if (usage) {
597
+ const generationChunk = new outputs_1.ChatGenerationChunk({
598
+ message: new messages_1.AIMessageChunk({
599
+ content: "",
600
+ usage_metadata: {
601
+ input_tokens: usage.prompt_tokens,
602
+ output_tokens: usage.completion_tokens,
603
+ total_tokens: usage.total_tokens,
604
+ },
605
+ }),
606
+ text: "",
607
+ });
608
+ yield generationChunk;
609
+ }
589
610
  if (options.signal?.aborted) {
590
611
  throw new Error("AbortError");
591
612
  }
@@ -27,6 +27,9 @@ export interface ChatOpenAICallOptions extends OpenAICallOptions, BaseFunctionCa
27
27
  type: "json_object";
28
28
  };
29
29
  seed?: number;
30
+ stream_options?: {
31
+ include_usage: boolean;
32
+ };
30
33
  }
31
34
  /**
32
35
  * Wrapper around OpenAI large language models that use the Chat endpoint.
@@ -525,6 +525,9 @@ export class ChatOpenAI extends BaseChatModel {
525
525
  tool_choice: options?.tool_choice,
526
526
  response_format: options?.response_format,
527
527
  seed: options?.seed,
528
+ ...(options?.stream_options !== undefined
529
+ ? { stream_options: options.stream_options }
530
+ : {}),
528
531
  ...this.modelKwargs,
529
532
  };
530
533
  return params;
@@ -546,8 +549,12 @@ export class ChatOpenAI extends BaseChatModel {
546
549
  };
547
550
  let defaultRole;
548
551
  const streamIterable = await this.completionWithRetry(params, options);
552
+ let usage;
549
553
  for await (const data of streamIterable) {
550
554
  const choice = data?.choices[0];
555
+ if (data.usage) {
556
+ usage = data.usage;
557
+ }
551
558
  if (!choice) {
552
559
  continue;
553
560
  }
@@ -582,6 +589,20 @@ export class ChatOpenAI extends BaseChatModel {
582
589
  // eslint-disable-next-line no-void
583
590
  void runManager?.handleLLMNewToken(generationChunk.text ?? "", newTokenIndices, undefined, undefined, undefined, { chunk: generationChunk });
584
591
  }
592
+ if (usage) {
593
+ const generationChunk = new ChatGenerationChunk({
594
+ message: new AIMessageChunk({
595
+ content: "",
596
+ usage_metadata: {
597
+ input_tokens: usage.prompt_tokens,
598
+ output_tokens: usage.completion_tokens,
599
+ total_tokens: usage.total_tokens,
600
+ },
601
+ }),
602
+ text: "",
603
+ });
604
+ yield generationChunk;
605
+ }
585
606
  if (options.signal?.aborted) {
586
607
  throw new Error("AbortError");
587
608
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/openai",
3
- "version": "0.0.34",
3
+ "version": "0.1.0",
4
4
  "description": "OpenAI integrations for LangChain.js",
5
5
  "type": "module",
6
6
  "engines": {
@@ -40,7 +40,7 @@
40
40
  "author": "LangChain",
41
41
  "license": "MIT",
42
42
  "dependencies": {
43
- "@langchain/core": ">0.1.56 <0.3.0",
43
+ "@langchain/core": ">=0.2.5 <0.3.0",
44
44
  "js-tiktoken": "^1.0.12",
45
45
  "openai": "^4.41.1",
46
46
  "zod": "^3.22.4",