@langchain/openai 0.0.33 → 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.
Files changed (32) hide show
  1. package/dist/azure/chat_models.cjs +7 -3
  2. package/dist/azure/chat_models.js +7 -3
  3. package/dist/azure/embeddings.cjs +7 -3
  4. package/dist/azure/embeddings.js +7 -3
  5. package/dist/azure/llms.cjs +7 -3
  6. package/dist/azure/llms.js +7 -3
  7. package/dist/chat_models.cjs +21 -0
  8. package/dist/chat_models.d.ts +3 -0
  9. package/dist/chat_models.js +21 -0
  10. package/dist/utils/azure.cjs +1 -1
  11. package/dist/utils/azure.js +1 -1
  12. package/package.json +6 -5
  13. package/dist/tests/azure/chat_models.int.test.d.ts +0 -1
  14. package/dist/tests/azure/chat_models.int.test.js +0 -659
  15. package/dist/tests/azure/embeddings.int.test.d.ts +0 -1
  16. package/dist/tests/azure/embeddings.int.test.js +0 -67
  17. package/dist/tests/azure/llms.int.test.d.ts +0 -1
  18. package/dist/tests/azure/llms.int.test.js +0 -267
  19. package/dist/tests/chat_models-extended.int.test.d.ts +0 -1
  20. package/dist/tests/chat_models-extended.int.test.js +0 -259
  21. package/dist/tests/chat_models-vision.int.test.d.ts +0 -1
  22. package/dist/tests/chat_models-vision.int.test.js +0 -51
  23. package/dist/tests/chat_models.int.test.d.ts +0 -1
  24. package/dist/tests/chat_models.int.test.js +0 -622
  25. package/dist/tests/chat_models_structured_output.int.test.d.ts +0 -1
  26. package/dist/tests/chat_models_structured_output.int.test.js +0 -232
  27. package/dist/tests/embeddings.int.test.d.ts +0 -1
  28. package/dist/tests/embeddings.int.test.js +0 -84
  29. package/dist/tests/legacy.int.test.d.ts +0 -1
  30. package/dist/tests/legacy.int.test.js +0 -115
  31. package/dist/tests/llms.int.test.d.ts +0 -1
  32. package/dist/tests/llms.int.test.js +0 -260
@@ -18,9 +18,13 @@ class AzureChatOpenAI extends chat_models_js_1.ChatOpenAI {
18
18
  constructor(fields) {
19
19
  const newFields = fields ? { ...fields } : fields;
20
20
  if (newFields) {
21
- newFields.azureOpenAIApiDeploymentName = newFields.deploymentName;
22
- newFields.azureOpenAIApiKey = newFields.openAIApiKey;
23
- newFields.azureOpenAIApiVersion = newFields.openAIApiVersion;
21
+ // don't rewrite the fields if they are already set
22
+ newFields.azureOpenAIApiDeploymentName =
23
+ newFields.azureOpenAIApiDeploymentName ?? newFields.deploymentName;
24
+ newFields.azureOpenAIApiKey =
25
+ newFields.azureOpenAIApiKey ?? newFields.openAIApiKey;
26
+ newFields.azureOpenAIApiVersion =
27
+ newFields.azureOpenAIApiVersion ?? newFields.openAIApiVersion;
24
28
  }
25
29
  super(newFields);
26
30
  }
@@ -15,9 +15,13 @@ export class AzureChatOpenAI extends ChatOpenAI {
15
15
  constructor(fields) {
16
16
  const newFields = fields ? { ...fields } : fields;
17
17
  if (newFields) {
18
- newFields.azureOpenAIApiDeploymentName = newFields.deploymentName;
19
- newFields.azureOpenAIApiKey = newFields.openAIApiKey;
20
- newFields.azureOpenAIApiVersion = newFields.openAIApiVersion;
18
+ // don't rewrite the fields if they are already set
19
+ newFields.azureOpenAIApiDeploymentName =
20
+ newFields.azureOpenAIApiDeploymentName ?? newFields.deploymentName;
21
+ newFields.azureOpenAIApiKey =
22
+ newFields.azureOpenAIApiKey ?? newFields.openAIApiKey;
23
+ newFields.azureOpenAIApiVersion =
24
+ newFields.azureOpenAIApiVersion ?? newFields.openAIApiVersion;
21
25
  }
22
26
  super(newFields);
23
27
  }
@@ -9,9 +9,13 @@ class AzureOpenAIEmbeddings extends embeddings_js_1.OpenAIEmbeddings {
9
9
  constructor(fields, configuration) {
10
10
  const newFields = { ...fields };
11
11
  if (Object.entries(newFields).length) {
12
- newFields.azureOpenAIApiDeploymentName = newFields.deploymentName;
13
- newFields.azureOpenAIApiKey = newFields.apiKey;
14
- newFields.azureOpenAIApiVersion = newFields.openAIApiVersion;
12
+ // don't rewrite the fields if they are already set
13
+ newFields.azureOpenAIApiDeploymentName =
14
+ newFields.azureOpenAIApiDeploymentName ?? newFields.deploymentName;
15
+ newFields.azureOpenAIApiKey =
16
+ newFields.azureOpenAIApiKey ?? newFields.apiKey;
17
+ newFields.azureOpenAIApiVersion =
18
+ newFields.azureOpenAIApiVersion ?? newFields.openAIApiVersion;
15
19
  }
16
20
  super(newFields, configuration);
17
21
  }
@@ -6,9 +6,13 @@ export class AzureOpenAIEmbeddings extends OpenAIEmbeddings {
6
6
  constructor(fields, configuration) {
7
7
  const newFields = { ...fields };
8
8
  if (Object.entries(newFields).length) {
9
- newFields.azureOpenAIApiDeploymentName = newFields.deploymentName;
10
- newFields.azureOpenAIApiKey = newFields.apiKey;
11
- newFields.azureOpenAIApiVersion = newFields.openAIApiVersion;
9
+ // don't rewrite the fields if they are already set
10
+ newFields.azureOpenAIApiDeploymentName =
11
+ newFields.azureOpenAIApiDeploymentName ?? newFields.deploymentName;
12
+ newFields.azureOpenAIApiKey =
13
+ newFields.azureOpenAIApiKey ?? newFields.apiKey;
14
+ newFields.azureOpenAIApiVersion =
15
+ newFields.azureOpenAIApiVersion ?? newFields.openAIApiVersion;
12
16
  }
13
17
  super(newFields, configuration);
14
18
  }
@@ -15,9 +15,13 @@ class AzureOpenAI extends llms_js_1.OpenAI {
15
15
  constructor(fields) {
16
16
  const newFields = fields ? { ...fields } : fields;
17
17
  if (newFields) {
18
- newFields.azureOpenAIApiDeploymentName = newFields.deploymentName;
19
- newFields.azureOpenAIApiKey = newFields.openAIApiKey;
20
- newFields.azureOpenAIApiVersion = newFields.openAIApiVersion;
18
+ // don't rewrite the fields if they are already set
19
+ newFields.azureOpenAIApiDeploymentName =
20
+ newFields.azureOpenAIApiDeploymentName ?? newFields.deploymentName;
21
+ newFields.azureOpenAIApiKey =
22
+ newFields.azureOpenAIApiKey ?? newFields.openAIApiKey;
23
+ newFields.azureOpenAIApiVersion =
24
+ newFields.azureOpenAIApiVersion ?? newFields.openAIApiVersion;
21
25
  }
22
26
  super(newFields);
23
27
  }
@@ -12,9 +12,13 @@ export class AzureOpenAI extends OpenAI {
12
12
  constructor(fields) {
13
13
  const newFields = fields ? { ...fields } : fields;
14
14
  if (newFields) {
15
- newFields.azureOpenAIApiDeploymentName = newFields.deploymentName;
16
- newFields.azureOpenAIApiKey = newFields.openAIApiKey;
17
- newFields.azureOpenAIApiVersion = newFields.openAIApiVersion;
15
+ // don't rewrite the fields if they are already set
16
+ newFields.azureOpenAIApiDeploymentName =
17
+ newFields.azureOpenAIApiDeploymentName ?? newFields.deploymentName;
18
+ newFields.azureOpenAIApiKey =
19
+ newFields.azureOpenAIApiKey ?? newFields.openAIApiKey;
20
+ newFields.azureOpenAIApiVersion =
21
+ newFields.azureOpenAIApiVersion ?? newFields.openAIApiVersion;
18
22
  }
19
23
  super(newFields);
20
24
  }
@@ -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
  }
@@ -25,7 +25,7 @@ exports.getEndpoint = void 0;
25
25
  */
26
26
  function getEndpoint(config) {
27
27
  const { azureOpenAIApiDeploymentName, azureOpenAIApiInstanceName, azureOpenAIApiKey, azureOpenAIBasePath, baseURL, azureADTokenProvider, } = config;
28
- if (azureOpenAIApiKey &&
28
+ if ((azureOpenAIApiKey || azureADTokenProvider) &&
29
29
  azureOpenAIBasePath &&
30
30
  azureOpenAIApiDeploymentName) {
31
31
  return `${azureOpenAIBasePath}/${azureOpenAIApiDeploymentName}`;
@@ -22,7 +22,7 @@
22
22
  */
23
23
  export function getEndpoint(config) {
24
24
  const { azureOpenAIApiDeploymentName, azureOpenAIApiInstanceName, azureOpenAIApiKey, azureOpenAIBasePath, baseURL, azureADTokenProvider, } = config;
25
- if (azureOpenAIApiKey &&
25
+ if ((azureOpenAIApiKey || azureADTokenProvider) &&
26
26
  azureOpenAIBasePath &&
27
27
  azureOpenAIApiDeploymentName) {
28
28
  return `${azureOpenAIBasePath}/${azureOpenAIApiDeploymentName}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/openai",
3
- "version": "0.0.33",
3
+ "version": "0.1.0",
4
4
  "description": "OpenAI integrations for LangChain.js",
5
5
  "type": "module",
6
6
  "engines": {
@@ -14,7 +14,8 @@
14
14
  },
15
15
  "homepage": "https://github.com/langchain-ai/langchainjs/tree/main/libs/langchain-openai/",
16
16
  "scripts": {
17
- "build": "yarn run build:deps && yarn clean && yarn build:esm && yarn build:cjs && yarn build:scripts",
17
+ "build": "yarn turbo:command build:internal --filter=@langchain/openai",
18
+ "build:internal": "yarn lc-build:v2 --create-entrypoints --pre --tree-shaking",
18
19
  "build:deps": "yarn run turbo:command build --filter=@langchain/core",
19
20
  "build:esm": "NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist/",
20
21
  "build:cjs": "NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist-cjs/ -p tsconfig.cjs.json && yarn move-cjs-to-dist && rimraf dist-cjs",
@@ -24,7 +25,7 @@
24
25
  "lint:dpdm": "dpdm --exit-code circular:1 --no-warning --no-tree src/*.ts src/**/*.ts",
25
26
  "lint": "yarn lint:eslint && yarn lint:dpdm",
26
27
  "lint:fix": "yarn lint:eslint --fix && yarn lint:dpdm",
27
- "clean": "rimraf .turbo/ dist/ && NODE_OPTIONS=--max-old-space-size=4096 yarn lc-build --config ./langchain.config.js --create-entrypoints --pre",
28
+ "clean": "rm -rf .turbo dist/",
28
29
  "prepack": "yarn build",
29
30
  "test": "yarn run build:deps && NODE_OPTIONS=--experimental-vm-modules jest --testPathIgnorePatterns=\\.int\\.test.ts --testTimeout 30000 --maxWorkers=50%",
30
31
  "test:watch": "yarn run build:deps && NODE_OPTIONS=--experimental-vm-modules jest --watch --testPathIgnorePatterns=\\.int\\.test.ts",
@@ -39,7 +40,7 @@
39
40
  "author": "LangChain",
40
41
  "license": "MIT",
41
42
  "dependencies": {
42
- "@langchain/core": ">0.1.56 <0.3.0",
43
+ "@langchain/core": ">=0.2.5 <0.3.0",
43
44
  "js-tiktoken": "^1.0.12",
44
45
  "openai": "^4.41.1",
45
46
  "zod": "^3.22.4",
@@ -48,7 +49,7 @@
48
49
  "devDependencies": {
49
50
  "@azure/identity": "^4.2.0",
50
51
  "@jest/globals": "^29.5.0",
51
- "@langchain/scripts": "~0.0",
52
+ "@langchain/scripts": "~0.0.14",
52
53
  "@swc/core": "^1.3.90",
53
54
  "@swc/jest": "^0.2.29",
54
55
  "dpdm": "^3.12.0",
@@ -1 +0,0 @@
1
- export {};