@langchain/openai 0.2.7 → 0.2.9

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.
@@ -259,8 +259,8 @@ const azure_js_1 = require("../utils/azure.cjs");
259
259
  * rating: z.number().optional().describe("How funny the joke is, from 1 to 10")
260
260
  * }).describe('Joke to tell user.');
261
261
  *
262
- * const structuredLlm = llm.withStructuredOutput(Joke);
263
- * const jokeResult = await structuredLlm.invoke("Tell me a joke about cats", { name: "Joke" });
262
+ * const structuredLlm = llm.withStructuredOutput(Joke, { name: "Joke" });
263
+ * const jokeResult = await structuredLlm.invoke("Tell me a joke about cats");
264
264
  * console.log(jokeResult);
265
265
  * ```
266
266
  *
@@ -257,8 +257,8 @@ import { AzureOpenAIInput, LegacyOpenAIInput, OpenAIChatInput, OpenAICoreRequest
257
257
  * rating: z.number().optional().describe("How funny the joke is, from 1 to 10")
258
258
  * }).describe('Joke to tell user.');
259
259
  *
260
- * const structuredLlm = llm.withStructuredOutput(Joke);
261
- * const jokeResult = await structuredLlm.invoke("Tell me a joke about cats", { name: "Joke" });
260
+ * const structuredLlm = llm.withStructuredOutput(Joke, { name: "Joke" });
261
+ * const jokeResult = await structuredLlm.invoke("Tell me a joke about cats");
262
262
  * console.log(jokeResult);
263
263
  * ```
264
264
  *
@@ -256,8 +256,8 @@ import { getEndpoint } from "../utils/azure.js";
256
256
  * rating: z.number().optional().describe("How funny the joke is, from 1 to 10")
257
257
  * }).describe('Joke to tell user.');
258
258
  *
259
- * const structuredLlm = llm.withStructuredOutput(Joke);
260
- * const jokeResult = await structuredLlm.invoke("Tell me a joke about cats", { name: "Joke" });
259
+ * const structuredLlm = llm.withStructuredOutput(Joke, { name: "Joke" });
260
+ * const jokeResult = await structuredLlm.invoke("Tell me a joke about cats");
261
261
  * console.log(jokeResult);
262
262
  * ```
263
263
  *
@@ -456,8 +456,8 @@ function _convertChatOpenAIToolTypeToOpenAITool(tool, fields) {
456
456
  * rating: z.number().optional().describe("How funny the joke is, from 1 to 10")
457
457
  * }).describe('Joke to tell user.');
458
458
  *
459
- * const structuredLlm = llm.withStructuredOutput(Joke);
460
- * const jokeResult = await structuredLlm.invoke("Tell me a joke about cats", { name: "Joke" });
459
+ * const structuredLlm = llm.withStructuredOutput(Joke, { name: "Joke" });
460
+ * const jokeResult = await structuredLlm.invoke("Tell me a joke about cats");
461
461
  * console.log(jokeResult);
462
462
  * ```
463
463
  *
@@ -614,6 +614,51 @@ function _convertChatOpenAIToolTypeToOpenAITool(tool, fields) {
614
614
  * </details>
615
615
  *
616
616
  * <br />
617
+ *
618
+ * <details>
619
+ * <summary><strong>JSON Schema Structured Output</strong></summary>
620
+ *
621
+ * ```typescript
622
+ * const llmForJsonSchema = new ChatOpenAI({
623
+ * model: "gpt-4o-2024-08-06",
624
+ * }).withStructuredOutput(
625
+ * z.object({
626
+ * command: z.string().describe("The command to execute"),
627
+ * expectedOutput: z.string().describe("The expected output of the command"),
628
+ * options: z
629
+ * .array(z.string())
630
+ * .describe("The options you can pass to the command"),
631
+ * }),
632
+ * {
633
+ * method: "jsonSchema",
634
+ * strict: true, // Optional when using the `jsonSchema` method
635
+ * }
636
+ * );
637
+ *
638
+ * const jsonSchemaRes = await llmForJsonSchema.invoke(
639
+ * "What is the command to list files in a directory?"
640
+ * );
641
+ * console.log(jsonSchemaRes);
642
+ * ```
643
+ *
644
+ * ```txt
645
+ * {
646
+ * command: 'ls',
647
+ * expectedOutput: 'A list of files and subdirectories within the specified directory.',
648
+ * options: [
649
+ * '-a: include directory entries whose names begin with a dot (.).',
650
+ * '-l: use a long listing format.',
651
+ * '-h: with -l, print sizes in human readable format (e.g., 1K, 234M, 2G).',
652
+ * '-t: sort by time, newest first.',
653
+ * '-r: reverse order while sorting.',
654
+ * '-S: sort by file size, largest first.',
655
+ * '-R: list subdirectories recursively.'
656
+ * ]
657
+ * }
658
+ * ```
659
+ * </details>
660
+ *
661
+ * <br />
617
662
  */
618
663
  class ChatOpenAI extends chat_models_1.BaseChatModel {
619
664
  static lc_name() {
@@ -854,6 +899,7 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
854
899
  this.openAIApiKey =
855
900
  fields?.apiKey ??
856
901
  fields?.openAIApiKey ??
902
+ fields?.configuration?.apiKey ??
857
903
  (0, env_1.getEnvironmentVariable)("OPENAI_API_KEY");
858
904
  this.apiKey = this.openAIApiKey;
859
905
  this.azureOpenAIApiKey =
@@ -1069,7 +1115,7 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
1069
1115
  }
1070
1116
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
1071
1117
  const generationInfo = { ...newTokenIndices };
1072
- if (choice.finish_reason !== undefined) {
1118
+ if (choice.finish_reason != null) {
1073
1119
  generationInfo.finish_reason = choice.finish_reason;
1074
1120
  // Only include system fingerprint in the last chunk for now
1075
1121
  // to avoid concatenation issues
@@ -334,8 +334,8 @@ export interface ChatOpenAIFields extends Partial<OpenAIChatInput>, Partial<Azur
334
334
  * rating: z.number().optional().describe("How funny the joke is, from 1 to 10")
335
335
  * }).describe('Joke to tell user.');
336
336
  *
337
- * const structuredLlm = llm.withStructuredOutput(Joke);
338
- * const jokeResult = await structuredLlm.invoke("Tell me a joke about cats", { name: "Joke" });
337
+ * const structuredLlm = llm.withStructuredOutput(Joke, { name: "Joke" });
338
+ * const jokeResult = await structuredLlm.invoke("Tell me a joke about cats");
339
339
  * console.log(jokeResult);
340
340
  * ```
341
341
  *
@@ -492,6 +492,51 @@ export interface ChatOpenAIFields extends Partial<OpenAIChatInput>, Partial<Azur
492
492
  * </details>
493
493
  *
494
494
  * <br />
495
+ *
496
+ * <details>
497
+ * <summary><strong>JSON Schema Structured Output</strong></summary>
498
+ *
499
+ * ```typescript
500
+ * const llmForJsonSchema = new ChatOpenAI({
501
+ * model: "gpt-4o-2024-08-06",
502
+ * }).withStructuredOutput(
503
+ * z.object({
504
+ * command: z.string().describe("The command to execute"),
505
+ * expectedOutput: z.string().describe("The expected output of the command"),
506
+ * options: z
507
+ * .array(z.string())
508
+ * .describe("The options you can pass to the command"),
509
+ * }),
510
+ * {
511
+ * method: "jsonSchema",
512
+ * strict: true, // Optional when using the `jsonSchema` method
513
+ * }
514
+ * );
515
+ *
516
+ * const jsonSchemaRes = await llmForJsonSchema.invoke(
517
+ * "What is the command to list files in a directory?"
518
+ * );
519
+ * console.log(jsonSchemaRes);
520
+ * ```
521
+ *
522
+ * ```txt
523
+ * {
524
+ * command: 'ls',
525
+ * expectedOutput: 'A list of files and subdirectories within the specified directory.',
526
+ * options: [
527
+ * '-a: include directory entries whose names begin with a dot (.).',
528
+ * '-l: use a long listing format.',
529
+ * '-h: with -l, print sizes in human readable format (e.g., 1K, 234M, 2G).',
530
+ * '-t: sort by time, newest first.',
531
+ * '-r: reverse order while sorting.',
532
+ * '-S: sort by file size, largest first.',
533
+ * '-R: list subdirectories recursively.'
534
+ * ]
535
+ * }
536
+ * ```
537
+ * </details>
538
+ *
539
+ * <br />
495
540
  */
496
541
  export declare class ChatOpenAI<CallOptions extends ChatOpenAICallOptions = ChatOpenAICallOptions> extends BaseChatModel<CallOptions, AIMessageChunk> implements OpenAIChatInput, AzureOpenAIInput {
497
542
  static lc_name(): string;
@@ -452,8 +452,8 @@ function _convertChatOpenAIToolTypeToOpenAITool(tool, fields) {
452
452
  * rating: z.number().optional().describe("How funny the joke is, from 1 to 10")
453
453
  * }).describe('Joke to tell user.');
454
454
  *
455
- * const structuredLlm = llm.withStructuredOutput(Joke);
456
- * const jokeResult = await structuredLlm.invoke("Tell me a joke about cats", { name: "Joke" });
455
+ * const structuredLlm = llm.withStructuredOutput(Joke, { name: "Joke" });
456
+ * const jokeResult = await structuredLlm.invoke("Tell me a joke about cats");
457
457
  * console.log(jokeResult);
458
458
  * ```
459
459
  *
@@ -610,6 +610,51 @@ function _convertChatOpenAIToolTypeToOpenAITool(tool, fields) {
610
610
  * </details>
611
611
  *
612
612
  * <br />
613
+ *
614
+ * <details>
615
+ * <summary><strong>JSON Schema Structured Output</strong></summary>
616
+ *
617
+ * ```typescript
618
+ * const llmForJsonSchema = new ChatOpenAI({
619
+ * model: "gpt-4o-2024-08-06",
620
+ * }).withStructuredOutput(
621
+ * z.object({
622
+ * command: z.string().describe("The command to execute"),
623
+ * expectedOutput: z.string().describe("The expected output of the command"),
624
+ * options: z
625
+ * .array(z.string())
626
+ * .describe("The options you can pass to the command"),
627
+ * }),
628
+ * {
629
+ * method: "jsonSchema",
630
+ * strict: true, // Optional when using the `jsonSchema` method
631
+ * }
632
+ * );
633
+ *
634
+ * const jsonSchemaRes = await llmForJsonSchema.invoke(
635
+ * "What is the command to list files in a directory?"
636
+ * );
637
+ * console.log(jsonSchemaRes);
638
+ * ```
639
+ *
640
+ * ```txt
641
+ * {
642
+ * command: 'ls',
643
+ * expectedOutput: 'A list of files and subdirectories within the specified directory.',
644
+ * options: [
645
+ * '-a: include directory entries whose names begin with a dot (.).',
646
+ * '-l: use a long listing format.',
647
+ * '-h: with -l, print sizes in human readable format (e.g., 1K, 234M, 2G).',
648
+ * '-t: sort by time, newest first.',
649
+ * '-r: reverse order while sorting.',
650
+ * '-S: sort by file size, largest first.',
651
+ * '-R: list subdirectories recursively.'
652
+ * ]
653
+ * }
654
+ * ```
655
+ * </details>
656
+ *
657
+ * <br />
613
658
  */
614
659
  export class ChatOpenAI extends BaseChatModel {
615
660
  static lc_name() {
@@ -850,6 +895,7 @@ export class ChatOpenAI extends BaseChatModel {
850
895
  this.openAIApiKey =
851
896
  fields?.apiKey ??
852
897
  fields?.openAIApiKey ??
898
+ fields?.configuration?.apiKey ??
853
899
  getEnvironmentVariable("OPENAI_API_KEY");
854
900
  this.apiKey = this.openAIApiKey;
855
901
  this.azureOpenAIApiKey =
@@ -1065,7 +1111,7 @@ export class ChatOpenAI extends BaseChatModel {
1065
1111
  }
1066
1112
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
1067
1113
  const generationInfo = { ...newTokenIndices };
1068
- if (choice.finish_reason !== undefined) {
1114
+ if (choice.finish_reason != null) {
1069
1115
  generationInfo.finish_reason = choice.finish_reason;
1070
1116
  // Only include system fingerprint in the last chunk for now
1071
1117
  // to avoid concatenation issues
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/openai",
3
- "version": "0.2.7",
3
+ "version": "0.2.9",
4
4
  "description": "OpenAI integrations for LangChain.js",
5
5
  "type": "module",
6
6
  "engines": {
@@ -15,7 +15,7 @@
15
15
  "homepage": "https://github.com/langchain-ai/langchainjs/tree/main/libs/langchain-openai/",
16
16
  "scripts": {
17
17
  "build": "yarn turbo:command build:internal --filter=@langchain/openai",
18
- "build:internal": "yarn lc_build_v2 --create-entrypoints --pre --tree-shaking",
18
+ "build:internal": "yarn lc_build --create-entrypoints --pre --tree-shaking",
19
19
  "lint:eslint": "NODE_OPTIONS=--max-old-space-size=4096 eslint --cache --ext .ts,.js src/",
20
20
  "lint:dpdm": "dpdm --exit-code circular:1 --no-warning --no-tree src/*.ts src/**/*.ts",
21
21
  "lint": "yarn lint:eslint && yarn lint:dpdm",
@@ -44,7 +44,7 @@
44
44
  "devDependencies": {
45
45
  "@azure/identity": "^4.2.1",
46
46
  "@jest/globals": "^29.5.0",
47
- "@langchain/scripts": "^0.0.21",
47
+ "@langchain/scripts": ">=0.1.0 <0.2.0",
48
48
  "@langchain/standard-tests": "0.0.0",
49
49
  "@swc/core": "^1.3.90",
50
50
  "@swc/jest": "^0.2.29",