@langchain/openai 0.0.19 → 0.0.20

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.
@@ -470,10 +470,18 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
470
470
  console.log("[WARNING]: Received non-string content from OpenAI. This is currently not supported.");
471
471
  continue;
472
472
  }
473
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
474
+ const generationInfo = { ...newTokenIndices };
475
+ if (choice.finish_reason !== undefined) {
476
+ generationInfo.finish_reason = choice.finish_reason;
477
+ }
478
+ if (this.logprobs) {
479
+ generationInfo.logprobs = choice.logprobs;
480
+ }
473
481
  const generationChunk = new outputs_1.ChatGenerationChunk({
474
482
  message: chunk,
475
483
  text: chunk.content,
476
- generationInfo: newTokenIndices,
484
+ generationInfo,
477
485
  });
478
486
  yield generationChunk;
479
487
  // eslint-disable-next-line no-void
@@ -499,6 +507,10 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
499
507
  const stream = this._streamResponseChunks(messages, options, runManager);
500
508
  const finalChunks = {};
501
509
  for await (const chunk of stream) {
510
+ chunk.message.response_metadata = {
511
+ ...chunk.generationInfo,
512
+ ...chunk.message.response_metadata,
513
+ };
502
514
  const index = chunk.generationInfo?.completion ?? 0;
503
515
  if (finalChunks[index] === undefined) {
504
516
  finalChunks[index] = chunk;
@@ -466,10 +466,18 @@ export class ChatOpenAI extends BaseChatModel {
466
466
  console.log("[WARNING]: Received non-string content from OpenAI. This is currently not supported.");
467
467
  continue;
468
468
  }
469
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
470
+ const generationInfo = { ...newTokenIndices };
471
+ if (choice.finish_reason !== undefined) {
472
+ generationInfo.finish_reason = choice.finish_reason;
473
+ }
474
+ if (this.logprobs) {
475
+ generationInfo.logprobs = choice.logprobs;
476
+ }
469
477
  const generationChunk = new ChatGenerationChunk({
470
478
  message: chunk,
471
479
  text: chunk.content,
472
- generationInfo: newTokenIndices,
480
+ generationInfo,
473
481
  });
474
482
  yield generationChunk;
475
483
  // eslint-disable-next-line no-void
@@ -495,6 +503,10 @@ export class ChatOpenAI extends BaseChatModel {
495
503
  const stream = this._streamResponseChunks(messages, options, runManager);
496
504
  const finalChunks = {};
497
505
  for await (const chunk of stream) {
506
+ chunk.message.response_metadata = {
507
+ ...chunk.generationInfo,
508
+ ...chunk.message.response_metadata,
509
+ };
498
510
  const index = chunk.generationInfo?.completion ?? 0;
499
511
  if (finalChunks[index] === undefined) {
500
512
  finalChunks[index] = chunk;
package/dist/legacy.cjs CHANGED
@@ -42,7 +42,7 @@ const openai_js_1 = require("./utils/openai.cjs");
42
42
  * maxTokens: 50,
43
43
  * });
44
44
  *
45
- * const res = await model.call(
45
+ * const res = await model.invoke(
46
46
  * "What would be a good company name for a company that makes colorful socks?"
47
47
  * );
48
48
  * console.log({ res });
package/dist/legacy.d.ts CHANGED
@@ -47,7 +47,7 @@ export interface OpenAIChatCallOptions extends OpenAICallOptions {
47
47
  * maxTokens: 50,
48
48
  * });
49
49
  *
50
- * const res = await model.call(
50
+ * const res = await model.invoke(
51
51
  * "What would be a good company name for a company that makes colorful socks?"
52
52
  * );
53
53
  * console.log({ res });
package/dist/legacy.js CHANGED
@@ -39,7 +39,7 @@ import { wrapOpenAIClientError } from "./utils/openai.js";
39
39
  * maxTokens: 50,
40
40
  * });
41
41
  *
42
- * const res = await model.call(
42
+ * const res = await model.invoke(
43
43
  * "What would be a good company name for a company that makes colorful socks?"
44
44
  * );
45
45
  * console.log({ res });
package/dist/llms.cjs CHANGED
@@ -37,7 +37,7 @@ const openai_js_1 = require("./utils/openai.cjs");
37
37
  * maxRetries: 5,
38
38
  * });
39
39
  *
40
- * const res = await model.call(
40
+ * const res = await model.invoke(
41
41
  * "Question: What would be a good company name for a company that makes colorful socks?\nAnswer:"
42
42
  * );
43
43
  * console.log({ res });
package/dist/llms.d.ts CHANGED
@@ -32,7 +32,7 @@ export { OpenAIChat };
32
32
  * maxRetries: 5,
33
33
  * });
34
34
  *
35
- * const res = await model.call(
35
+ * const res = await model.invoke(
36
36
  * "Question: What would be a good company name for a company that makes colorful socks?\nAnswer:"
37
37
  * );
38
38
  * console.log({ res });
package/dist/llms.js CHANGED
@@ -34,7 +34,7 @@ export { OpenAIChat };
34
34
  * maxRetries: 5,
35
35
  * });
36
36
  *
37
- * const res = await model.call(
37
+ * const res = await model.invoke(
38
38
  * "Question: What would be a good company name for a company that makes colorful socks?\nAnswer:"
39
39
  * );
40
40
  * console.log({ res });
@@ -62,6 +62,17 @@ test("Test ChatOpenAI tool calling", async () => {
62
62
  console.log(JSON.stringify(res));
63
63
  expect(res.additional_kwargs.tool_calls?.length).toBeGreaterThan(1);
64
64
  });
65
+ test("Test ChatOpenAI streaming logprobs", async () => {
66
+ const model = new ChatOpenAI({
67
+ maxTokens: 50,
68
+ modelName: "gpt-3.5-turbo",
69
+ streaming: true,
70
+ logprobs: true,
71
+ });
72
+ const res = await model.invoke("Print hello world.");
73
+ console.log(res.response_metadata.logprobs.content);
74
+ expect(res.response_metadata.logprobs.content.length).toBeGreaterThan(0);
75
+ });
65
76
  test("Test ChatOpenAI tool calling with ToolMessages", async () => {
66
77
  function getCurrentWeather(location) {
67
78
  if (location.toLowerCase().includes("tokyo")) {
@@ -3,7 +3,7 @@ import { CallbackManager } from "@langchain/core/callbacks/manager";
3
3
  import { OpenAIChat } from "../legacy.js";
4
4
  test("Test OpenAI", async () => {
5
5
  const model = new OpenAIChat({ modelName: "gpt-3.5-turbo", maxTokens: 10 });
6
- const res = await model.call("Print hello world");
6
+ const res = await model.invoke("Print hello world");
7
7
  console.log({ res });
8
8
  });
9
9
  test("Test OpenAI with prefix messages", async () => {
@@ -14,7 +14,7 @@ test("Test OpenAI with prefix messages", async () => {
14
14
  ],
15
15
  maxTokens: 10,
16
16
  });
17
- const res = await model.call("What is my name");
17
+ const res = await model.invoke("What is my name");
18
18
  console.log({ res });
19
19
  });
20
20
  test("Test OpenAI in streaming mode", async () => {
@@ -31,7 +31,7 @@ test("Test OpenAI in streaming mode", async () => {
31
31
  },
32
32
  }),
33
33
  });
34
- const res = await model.call("Print hello world");
34
+ const res = await model.invoke("Print hello world");
35
35
  console.log({ res });
36
36
  expect(nrNewTokens > 0).toBe(true);
37
37
  expect(res).toBe(streamedCompletion);
@@ -43,18 +43,18 @@ test("Test OpenAI with stop", async () => {
43
43
  });
44
44
  test("Test OpenAI with stop in object", async () => {
45
45
  const model = new OpenAIChat({ maxTokens: 5 });
46
- const res = await model.call("Print hello world", { stop: ["world"] });
46
+ const res = await model.invoke("Print hello world", { stop: ["world"] });
47
47
  console.log({ res });
48
48
  });
49
49
  test("Test OpenAI with timeout in call options", async () => {
50
50
  const model = new OpenAIChat({ maxTokens: 5 });
51
- await expect(() => model.call("Print hello world", {
51
+ await expect(() => model.invoke("Print hello world", {
52
52
  timeout: 10,
53
53
  })).rejects.toThrow();
54
54
  }, 5000);
55
55
  test("Test OpenAI with timeout in call options and node adapter", async () => {
56
56
  const model = new OpenAIChat({ maxTokens: 5 });
57
- await expect(() => model.call("Print hello world", {
57
+ await expect(() => model.invoke("Print hello world", {
58
58
  timeout: 10,
59
59
  })).rejects.toThrow();
60
60
  }, 5000);
@@ -62,7 +62,7 @@ test("Test OpenAI with signal in call options", async () => {
62
62
  const model = new OpenAIChat({ maxTokens: 5 });
63
63
  const controller = new AbortController();
64
64
  await expect(() => {
65
- const ret = model.call("Print hello world", {
65
+ const ret = model.invoke("Print hello world", {
66
66
  signal: controller.signal,
67
67
  });
68
68
  controller.abort();
@@ -73,7 +73,7 @@ test("Test OpenAI with signal in call options and node adapter", async () => {
73
73
  const model = new OpenAIChat({ maxTokens: 5 });
74
74
  const controller = new AbortController();
75
75
  await expect(() => {
76
- const ret = model.call("Print hello world", {
76
+ const ret = model.invoke("Print hello world", {
77
77
  signal: controller.signal,
78
78
  });
79
79
  controller.abort();
@@ -8,7 +8,7 @@ test("Test OpenAI", async () => {
8
8
  maxTokens: 5,
9
9
  modelName: "gpt-3.5-turbo-instruct",
10
10
  });
11
- const res = await model.call("Print hello world");
11
+ const res = await model.invoke("Print hello world");
12
12
  console.log({ res });
13
13
  });
14
14
  test("Test OpenAI with stop", async () => {
@@ -24,7 +24,7 @@ test("Test OpenAI with stop in object", async () => {
24
24
  maxTokens: 5,
25
25
  modelName: "gpt-3.5-turbo-instruct",
26
26
  });
27
- const res = await model.call("Print hello world", { stop: ["world"] });
27
+ const res = await model.invoke("Print hello world", { stop: ["world"] });
28
28
  console.log({ res });
29
29
  });
30
30
  test("Test OpenAI with timeout in call options", async () => {
@@ -33,7 +33,7 @@ test("Test OpenAI with timeout in call options", async () => {
33
33
  maxRetries: 0,
34
34
  modelName: "gpt-3.5-turbo-instruct",
35
35
  });
36
- await expect(() => model.call("Print hello world", {
36
+ await expect(() => model.invoke("Print hello world", {
37
37
  timeout: 10,
38
38
  })).rejects.toThrow();
39
39
  }, 5000);
@@ -43,7 +43,7 @@ test("Test OpenAI with timeout in call options and node adapter", async () => {
43
43
  maxRetries: 0,
44
44
  modelName: "gpt-3.5-turbo-instruct",
45
45
  });
46
- await expect(() => model.call("Print hello world", {
46
+ await expect(() => model.invoke("Print hello world", {
47
47
  timeout: 10,
48
48
  })).rejects.toThrow();
49
49
  }, 5000);
@@ -54,7 +54,7 @@ test("Test OpenAI with signal in call options", async () => {
54
54
  });
55
55
  const controller = new AbortController();
56
56
  await expect(() => {
57
- const ret = model.call("Print hello world", {
57
+ const ret = model.invoke("Print hello world", {
58
58
  signal: controller.signal,
59
59
  });
60
60
  controller.abort();
@@ -68,7 +68,7 @@ test("Test OpenAI with signal in call options and node adapter", async () => {
68
68
  });
69
69
  const controller = new AbortController();
70
70
  await expect(() => {
71
- const ret = model.call("Print hello world", {
71
+ const ret = model.invoke("Print hello world", {
72
72
  signal: controller.signal,
73
73
  });
74
74
  controller.abort();
@@ -82,8 +82,8 @@ test("Test OpenAI with concurrency == 1", async () => {
82
82
  maxConcurrency: 1,
83
83
  });
84
84
  const res = await Promise.all([
85
- model.call("Print hello world"),
86
- model.call("Print hello world"),
85
+ model.invoke("Print hello world"),
86
+ model.invoke("Print hello world"),
87
87
  ]);
88
88
  console.log({ res });
89
89
  });
@@ -98,21 +98,21 @@ test("Test OpenAI with maxTokens -1", async () => {
98
98
  test("Test OpenAI with chat model returns OpenAIChat", async () => {
99
99
  const model = new OpenAI({ modelName: "gpt-3.5-turbo" });
100
100
  expect(model).toBeInstanceOf(OpenAIChat);
101
- const res = await model.call("Print hello world");
101
+ const res = await model.invoke("Print hello world");
102
102
  console.log({ res });
103
103
  expect(typeof res).toBe("string");
104
104
  });
105
105
  test("Test OpenAI with instruct model returns OpenAI", async () => {
106
106
  const model = new OpenAI({ modelName: "gpt-3.5-turbo-instruct" });
107
107
  expect(model).toBeInstanceOf(OpenAI);
108
- const res = await model.call("Print hello world");
108
+ const res = await model.invoke("Print hello world");
109
109
  console.log({ res });
110
110
  expect(typeof res).toBe("string");
111
111
  });
112
112
  test("Test OpenAI with versioned instruct model returns OpenAI", async () => {
113
113
  const model = new OpenAI({ modelName: "gpt-3.5-turbo-instruct-0914" });
114
114
  expect(model).toBeInstanceOf(OpenAI);
115
- const res = await model.call("Print hello world");
115
+ const res = await model.invoke("Print hello world");
116
116
  console.log({ res });
117
117
  expect(typeof res).toBe("string");
118
118
  });
@@ -131,7 +131,7 @@ test("Test ChatOpenAI tokenUsage", async () => {
131
131
  },
132
132
  }),
133
133
  });
134
- const res = await model.call("Hello");
134
+ const res = await model.invoke("Hello");
135
135
  console.log({ res });
136
136
  expect(tokenUsage.promptTokens).toBe(1);
137
137
  });
@@ -149,7 +149,7 @@ test("Test OpenAI in streaming mode", async () => {
149
149
  },
150
150
  }),
151
151
  });
152
- const res = await model.call("Print hello world");
152
+ const res = await model.invoke("Print hello world");
153
153
  console.log({ res });
154
154
  expect(nrNewTokens > 0).toBe(true);
155
155
  expect(res).toBe(streamedCompletion);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/openai",
3
- "version": "0.0.19",
3
+ "version": "0.0.20",
4
4
  "description": "OpenAI integrations for LangChain.js",
5
5
  "type": "module",
6
6
  "engines": {
@@ -39,7 +39,7 @@
39
39
  "author": "LangChain",
40
40
  "license": "MIT",
41
41
  "dependencies": {
42
- "@langchain/core": "~0.1.44",
42
+ "@langchain/core": "~0.1.45",
43
43
  "js-tiktoken": "^1.0.7",
44
44
  "openai": "^4.26.0",
45
45
  "zod": "^3.22.4",