@langchain/openai 0.1.2 → 0.2.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.
- package/dist/chat_models.cjs +28 -11
- package/dist/chat_models.d.ts +18 -4
- package/dist/chat_models.js +28 -11
- package/dist/legacy.cjs +3 -1
- package/dist/legacy.js +3 -1
- package/dist/types.d.ts +5 -0
- package/package.json +5 -4
package/dist/chat_models.cjs
CHANGED
|
@@ -47,7 +47,7 @@ function messageToOpenAIRole(message) {
|
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
49
|
exports.messageToOpenAIRole = messageToOpenAIRole;
|
|
50
|
-
function openAIResponseToChatMessage(message) {
|
|
50
|
+
function openAIResponseToChatMessage(message, messageId) {
|
|
51
51
|
const rawToolCalls = message.tool_calls;
|
|
52
52
|
switch (message.role) {
|
|
53
53
|
case "assistant": {
|
|
@@ -70,6 +70,7 @@ function openAIResponseToChatMessage(message) {
|
|
|
70
70
|
function_call: message.function_call,
|
|
71
71
|
tool_calls: rawToolCalls,
|
|
72
72
|
},
|
|
73
|
+
id: messageId,
|
|
73
74
|
});
|
|
74
75
|
}
|
|
75
76
|
default:
|
|
@@ -78,7 +79,7 @@ function openAIResponseToChatMessage(message) {
|
|
|
78
79
|
}
|
|
79
80
|
function _convertDeltaToMessageChunk(
|
|
80
81
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
81
|
-
delta, defaultRole) {
|
|
82
|
+
delta, messageId, defaultRole) {
|
|
82
83
|
const role = delta.role ?? defaultRole;
|
|
83
84
|
const content = delta.content ?? "";
|
|
84
85
|
let additional_kwargs;
|
|
@@ -114,6 +115,7 @@ delta, defaultRole) {
|
|
|
114
115
|
content,
|
|
115
116
|
tool_call_chunks: toolCallChunks,
|
|
116
117
|
additional_kwargs,
|
|
118
|
+
id: messageId,
|
|
117
119
|
});
|
|
118
120
|
}
|
|
119
121
|
else if (role === "system") {
|
|
@@ -150,9 +152,11 @@ function convertMessagesToOpenAIParams(messages) {
|
|
|
150
152
|
}
|
|
151
153
|
if (message.additional_kwargs.function_call != null) {
|
|
152
154
|
completionParam.function_call = message.additional_kwargs.function_call;
|
|
155
|
+
completionParam.content = null;
|
|
153
156
|
}
|
|
154
157
|
if ((0, messages_1.isAIMessage)(message) && !!message.tool_calls?.length) {
|
|
155
158
|
completionParam.tool_calls = message.tool_calls.map(openai_tools_1.convertLangChainToolCallToOpenAI);
|
|
159
|
+
completionParam.content = null;
|
|
156
160
|
}
|
|
157
161
|
else {
|
|
158
162
|
if (message.additional_kwargs.tool_calls != null) {
|
|
@@ -168,14 +172,13 @@ function convertMessagesToOpenAIParams(messages) {
|
|
|
168
172
|
/**
|
|
169
173
|
* Wrapper around OpenAI large language models that use the Chat endpoint.
|
|
170
174
|
*
|
|
171
|
-
* To use you should have the `
|
|
172
|
-
* `OPENAI_API_KEY` environment variable set.
|
|
175
|
+
* To use you should have the `OPENAI_API_KEY` environment variable set.
|
|
173
176
|
*
|
|
174
|
-
* To use with Azure you should have the
|
|
177
|
+
* To use with Azure you should have the:
|
|
175
178
|
* `AZURE_OPENAI_API_KEY`,
|
|
176
179
|
* `AZURE_OPENAI_API_INSTANCE_NAME`,
|
|
177
180
|
* `AZURE_OPENAI_API_DEPLOYMENT_NAME`
|
|
178
|
-
* and `AZURE_OPENAI_API_VERSION` environment
|
|
181
|
+
* and `AZURE_OPENAI_API_VERSION` environment variables set.
|
|
179
182
|
* `AZURE_OPENAI_BASE_PATH` is optional and will override `AZURE_OPENAI_API_INSTANCE_NAME` if you need to use a custom endpoint.
|
|
180
183
|
*
|
|
181
184
|
* @remarks
|
|
@@ -329,6 +332,12 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
|
|
|
329
332
|
writable: true,
|
|
330
333
|
value: false
|
|
331
334
|
});
|
|
335
|
+
Object.defineProperty(this, "streamUsage", {
|
|
336
|
+
enumerable: true,
|
|
337
|
+
configurable: true,
|
|
338
|
+
writable: true,
|
|
339
|
+
value: true
|
|
340
|
+
});
|
|
332
341
|
Object.defineProperty(this, "maxTokens", {
|
|
333
342
|
enumerable: true,
|
|
334
343
|
configurable: true,
|
|
@@ -457,6 +466,7 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
|
|
|
457
466
|
this.stopSequences = this?.stop;
|
|
458
467
|
this.user = fields?.user;
|
|
459
468
|
this.streaming = fields?.streaming ?? false;
|
|
469
|
+
this.streamUsage = fields?.streamUsage ?? this.streamUsage;
|
|
460
470
|
if (this.azureOpenAIApiKey || this.azureADTokenProvider) {
|
|
461
471
|
if (!this.azureOpenAIApiInstanceName && !this.azureOpenAIBasePath) {
|
|
462
472
|
throw new Error("Azure OpenAI API instance name not found");
|
|
@@ -507,6 +517,13 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
|
|
|
507
517
|
return (tools !== undefined &&
|
|
508
518
|
tools.every((tool) => Array.isArray(tool.lc_namespace)));
|
|
509
519
|
}
|
|
520
|
+
let streamOptionsConfig = {};
|
|
521
|
+
if (options?.stream_options !== undefined) {
|
|
522
|
+
streamOptionsConfig = { stream_options: options.stream_options };
|
|
523
|
+
}
|
|
524
|
+
else if (this.streamUsage && this.streaming) {
|
|
525
|
+
streamOptionsConfig = { stream_options: { include_usage: true } };
|
|
526
|
+
}
|
|
510
527
|
const params = {
|
|
511
528
|
model: this.model,
|
|
512
529
|
temperature: this.temperature,
|
|
@@ -520,6 +537,7 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
|
|
|
520
537
|
logit_bias: this.logitBias,
|
|
521
538
|
stop: options?.stop ?? this.stopSequences,
|
|
522
539
|
user: this.user,
|
|
540
|
+
// if include_usage is set or streamUsage then stream must be set to true.
|
|
523
541
|
stream: this.streaming,
|
|
524
542
|
functions: options?.functions,
|
|
525
543
|
function_call: options?.function_call,
|
|
@@ -529,9 +547,8 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
|
|
|
529
547
|
tool_choice: options?.tool_choice,
|
|
530
548
|
response_format: options?.response_format,
|
|
531
549
|
seed: options?.seed,
|
|
532
|
-
...
|
|
533
|
-
|
|
534
|
-
: {}),
|
|
550
|
+
...streamOptionsConfig,
|
|
551
|
+
parallel_tool_calls: options?.parallel_tool_calls,
|
|
535
552
|
...this.modelKwargs,
|
|
536
553
|
};
|
|
537
554
|
return params;
|
|
@@ -566,7 +583,7 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
|
|
|
566
583
|
if (!delta) {
|
|
567
584
|
continue;
|
|
568
585
|
}
|
|
569
|
-
const chunk = _convertDeltaToMessageChunk(delta, defaultRole);
|
|
586
|
+
const chunk = _convertDeltaToMessageChunk(delta, data.id, defaultRole);
|
|
570
587
|
defaultRole = delta.role ?? defaultRole;
|
|
571
588
|
const newTokenIndices = {
|
|
572
589
|
prompt: options.promptIndex ?? 0,
|
|
@@ -677,7 +694,7 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
|
|
|
677
694
|
const text = part.message?.content ?? "";
|
|
678
695
|
const generation = {
|
|
679
696
|
text,
|
|
680
|
-
message: openAIResponseToChatMessage(part.message ?? { role: "assistant" }),
|
|
697
|
+
message: openAIResponseToChatMessage(part.message ?? { role: "assistant" }, data.id),
|
|
681
698
|
};
|
|
682
699
|
generation.generationInfo = {
|
|
683
700
|
...(part.finish_reason ? { finish_reason: part.finish_reason } : {}),
|
package/dist/chat_models.d.ts
CHANGED
|
@@ -27,21 +27,34 @@ export interface ChatOpenAICallOptions extends OpenAICallOptions, BaseFunctionCa
|
|
|
27
27
|
type: "json_object";
|
|
28
28
|
};
|
|
29
29
|
seed?: number;
|
|
30
|
+
/**
|
|
31
|
+
* Additional options to pass to streamed completions.
|
|
32
|
+
* If provided takes precedence over "streamUsage" set at initialization time.
|
|
33
|
+
*/
|
|
30
34
|
stream_options?: {
|
|
35
|
+
/**
|
|
36
|
+
* Whether or not to include token usage in the stream.
|
|
37
|
+
* If set to `true`, this will include an additional
|
|
38
|
+
* chunk at the end of the stream with the token usage.
|
|
39
|
+
*/
|
|
31
40
|
include_usage: boolean;
|
|
32
41
|
};
|
|
42
|
+
/**
|
|
43
|
+
* Whether or not to restrict the ability to
|
|
44
|
+
* call multiple tools in one response.
|
|
45
|
+
*/
|
|
46
|
+
parallel_tool_calls?: boolean;
|
|
33
47
|
}
|
|
34
48
|
/**
|
|
35
49
|
* Wrapper around OpenAI large language models that use the Chat endpoint.
|
|
36
50
|
*
|
|
37
|
-
* To use you should have the `
|
|
38
|
-
* `OPENAI_API_KEY` environment variable set.
|
|
51
|
+
* To use you should have the `OPENAI_API_KEY` environment variable set.
|
|
39
52
|
*
|
|
40
|
-
* To use with Azure you should have the
|
|
53
|
+
* To use with Azure you should have the:
|
|
41
54
|
* `AZURE_OPENAI_API_KEY`,
|
|
42
55
|
* `AZURE_OPENAI_API_INSTANCE_NAME`,
|
|
43
56
|
* `AZURE_OPENAI_API_DEPLOYMENT_NAME`
|
|
44
|
-
* and `AZURE_OPENAI_API_VERSION` environment
|
|
57
|
+
* and `AZURE_OPENAI_API_VERSION` environment variables set.
|
|
45
58
|
* `AZURE_OPENAI_BASE_PATH` is optional and will override `AZURE_OPENAI_API_INSTANCE_NAME` if you need to use a custom endpoint.
|
|
46
59
|
*
|
|
47
60
|
* @remarks
|
|
@@ -87,6 +100,7 @@ export declare class ChatOpenAI<CallOptions extends ChatOpenAICallOptions = Chat
|
|
|
87
100
|
user?: string;
|
|
88
101
|
timeout?: number;
|
|
89
102
|
streaming: boolean;
|
|
103
|
+
streamUsage: boolean;
|
|
90
104
|
maxTokens?: number;
|
|
91
105
|
logprobs?: boolean;
|
|
92
106
|
topLogprobs?: number;
|
package/dist/chat_models.js
CHANGED
|
@@ -43,7 +43,7 @@ export function messageToOpenAIRole(message) {
|
|
|
43
43
|
throw new Error(`Unknown message type: ${type}`);
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
|
-
function openAIResponseToChatMessage(message) {
|
|
46
|
+
function openAIResponseToChatMessage(message, messageId) {
|
|
47
47
|
const rawToolCalls = message.tool_calls;
|
|
48
48
|
switch (message.role) {
|
|
49
49
|
case "assistant": {
|
|
@@ -66,6 +66,7 @@ function openAIResponseToChatMessage(message) {
|
|
|
66
66
|
function_call: message.function_call,
|
|
67
67
|
tool_calls: rawToolCalls,
|
|
68
68
|
},
|
|
69
|
+
id: messageId,
|
|
69
70
|
});
|
|
70
71
|
}
|
|
71
72
|
default:
|
|
@@ -74,7 +75,7 @@ function openAIResponseToChatMessage(message) {
|
|
|
74
75
|
}
|
|
75
76
|
function _convertDeltaToMessageChunk(
|
|
76
77
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
77
|
-
delta, defaultRole) {
|
|
78
|
+
delta, messageId, defaultRole) {
|
|
78
79
|
const role = delta.role ?? defaultRole;
|
|
79
80
|
const content = delta.content ?? "";
|
|
80
81
|
let additional_kwargs;
|
|
@@ -110,6 +111,7 @@ delta, defaultRole) {
|
|
|
110
111
|
content,
|
|
111
112
|
tool_call_chunks: toolCallChunks,
|
|
112
113
|
additional_kwargs,
|
|
114
|
+
id: messageId,
|
|
113
115
|
});
|
|
114
116
|
}
|
|
115
117
|
else if (role === "system") {
|
|
@@ -146,9 +148,11 @@ function convertMessagesToOpenAIParams(messages) {
|
|
|
146
148
|
}
|
|
147
149
|
if (message.additional_kwargs.function_call != null) {
|
|
148
150
|
completionParam.function_call = message.additional_kwargs.function_call;
|
|
151
|
+
completionParam.content = null;
|
|
149
152
|
}
|
|
150
153
|
if (isAIMessage(message) && !!message.tool_calls?.length) {
|
|
151
154
|
completionParam.tool_calls = message.tool_calls.map(convertLangChainToolCallToOpenAI);
|
|
155
|
+
completionParam.content = null;
|
|
152
156
|
}
|
|
153
157
|
else {
|
|
154
158
|
if (message.additional_kwargs.tool_calls != null) {
|
|
@@ -164,14 +168,13 @@ function convertMessagesToOpenAIParams(messages) {
|
|
|
164
168
|
/**
|
|
165
169
|
* Wrapper around OpenAI large language models that use the Chat endpoint.
|
|
166
170
|
*
|
|
167
|
-
* To use you should have the `
|
|
168
|
-
* `OPENAI_API_KEY` environment variable set.
|
|
171
|
+
* To use you should have the `OPENAI_API_KEY` environment variable set.
|
|
169
172
|
*
|
|
170
|
-
* To use with Azure you should have the
|
|
173
|
+
* To use with Azure you should have the:
|
|
171
174
|
* `AZURE_OPENAI_API_KEY`,
|
|
172
175
|
* `AZURE_OPENAI_API_INSTANCE_NAME`,
|
|
173
176
|
* `AZURE_OPENAI_API_DEPLOYMENT_NAME`
|
|
174
|
-
* and `AZURE_OPENAI_API_VERSION` environment
|
|
177
|
+
* and `AZURE_OPENAI_API_VERSION` environment variables set.
|
|
175
178
|
* `AZURE_OPENAI_BASE_PATH` is optional and will override `AZURE_OPENAI_API_INSTANCE_NAME` if you need to use a custom endpoint.
|
|
176
179
|
*
|
|
177
180
|
* @remarks
|
|
@@ -325,6 +328,12 @@ export class ChatOpenAI extends BaseChatModel {
|
|
|
325
328
|
writable: true,
|
|
326
329
|
value: false
|
|
327
330
|
});
|
|
331
|
+
Object.defineProperty(this, "streamUsage", {
|
|
332
|
+
enumerable: true,
|
|
333
|
+
configurable: true,
|
|
334
|
+
writable: true,
|
|
335
|
+
value: true
|
|
336
|
+
});
|
|
328
337
|
Object.defineProperty(this, "maxTokens", {
|
|
329
338
|
enumerable: true,
|
|
330
339
|
configurable: true,
|
|
@@ -453,6 +462,7 @@ export class ChatOpenAI extends BaseChatModel {
|
|
|
453
462
|
this.stopSequences = this?.stop;
|
|
454
463
|
this.user = fields?.user;
|
|
455
464
|
this.streaming = fields?.streaming ?? false;
|
|
465
|
+
this.streamUsage = fields?.streamUsage ?? this.streamUsage;
|
|
456
466
|
if (this.azureOpenAIApiKey || this.azureADTokenProvider) {
|
|
457
467
|
if (!this.azureOpenAIApiInstanceName && !this.azureOpenAIBasePath) {
|
|
458
468
|
throw new Error("Azure OpenAI API instance name not found");
|
|
@@ -503,6 +513,13 @@ export class ChatOpenAI extends BaseChatModel {
|
|
|
503
513
|
return (tools !== undefined &&
|
|
504
514
|
tools.every((tool) => Array.isArray(tool.lc_namespace)));
|
|
505
515
|
}
|
|
516
|
+
let streamOptionsConfig = {};
|
|
517
|
+
if (options?.stream_options !== undefined) {
|
|
518
|
+
streamOptionsConfig = { stream_options: options.stream_options };
|
|
519
|
+
}
|
|
520
|
+
else if (this.streamUsage && this.streaming) {
|
|
521
|
+
streamOptionsConfig = { stream_options: { include_usage: true } };
|
|
522
|
+
}
|
|
506
523
|
const params = {
|
|
507
524
|
model: this.model,
|
|
508
525
|
temperature: this.temperature,
|
|
@@ -516,6 +533,7 @@ export class ChatOpenAI extends BaseChatModel {
|
|
|
516
533
|
logit_bias: this.logitBias,
|
|
517
534
|
stop: options?.stop ?? this.stopSequences,
|
|
518
535
|
user: this.user,
|
|
536
|
+
// if include_usage is set or streamUsage then stream must be set to true.
|
|
519
537
|
stream: this.streaming,
|
|
520
538
|
functions: options?.functions,
|
|
521
539
|
function_call: options?.function_call,
|
|
@@ -525,9 +543,8 @@ export class ChatOpenAI extends BaseChatModel {
|
|
|
525
543
|
tool_choice: options?.tool_choice,
|
|
526
544
|
response_format: options?.response_format,
|
|
527
545
|
seed: options?.seed,
|
|
528
|
-
...
|
|
529
|
-
|
|
530
|
-
: {}),
|
|
546
|
+
...streamOptionsConfig,
|
|
547
|
+
parallel_tool_calls: options?.parallel_tool_calls,
|
|
531
548
|
...this.modelKwargs,
|
|
532
549
|
};
|
|
533
550
|
return params;
|
|
@@ -562,7 +579,7 @@ export class ChatOpenAI extends BaseChatModel {
|
|
|
562
579
|
if (!delta) {
|
|
563
580
|
continue;
|
|
564
581
|
}
|
|
565
|
-
const chunk = _convertDeltaToMessageChunk(delta, defaultRole);
|
|
582
|
+
const chunk = _convertDeltaToMessageChunk(delta, data.id, defaultRole);
|
|
566
583
|
defaultRole = delta.role ?? defaultRole;
|
|
567
584
|
const newTokenIndices = {
|
|
568
585
|
prompt: options.promptIndex ?? 0,
|
|
@@ -673,7 +690,7 @@ export class ChatOpenAI extends BaseChatModel {
|
|
|
673
690
|
const text = part.message?.content ?? "";
|
|
674
691
|
const generation = {
|
|
675
692
|
text,
|
|
676
|
-
message: openAIResponseToChatMessage(part.message ?? { role: "assistant" }),
|
|
693
|
+
message: openAIResponseToChatMessage(part.message ?? { role: "assistant" }, data.id),
|
|
677
694
|
};
|
|
678
695
|
generation.generationInfo = {
|
|
679
696
|
...(part.finish_reason ? { finish_reason: part.finish_reason } : {}),
|
package/dist/legacy.cjs
CHANGED
|
@@ -227,7 +227,9 @@ class OpenAIChat extends llms_1.LLM {
|
|
|
227
227
|
value: void 0
|
|
228
228
|
});
|
|
229
229
|
this.openAIApiKey =
|
|
230
|
-
fields?.
|
|
230
|
+
fields?.apiKey ??
|
|
231
|
+
fields?.openAIApiKey ??
|
|
232
|
+
(0, env_1.getEnvironmentVariable)("OPENAI_API_KEY");
|
|
231
233
|
this.azureOpenAIApiKey =
|
|
232
234
|
fields?.azureOpenAIApiKey ??
|
|
233
235
|
(0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_KEY");
|
package/dist/legacy.js
CHANGED
|
@@ -224,7 +224,9 @@ export class OpenAIChat extends LLM {
|
|
|
224
224
|
value: void 0
|
|
225
225
|
});
|
|
226
226
|
this.openAIApiKey =
|
|
227
|
-
fields?.
|
|
227
|
+
fields?.apiKey ??
|
|
228
|
+
fields?.openAIApiKey ??
|
|
229
|
+
getEnvironmentVariable("OPENAI_API_KEY");
|
|
228
230
|
this.azureOpenAIApiKey =
|
|
229
231
|
fields?.azureOpenAIApiKey ??
|
|
230
232
|
getEnvironmentVariable("AZURE_OPENAI_API_KEY");
|
package/dist/types.d.ts
CHANGED
|
@@ -24,6 +24,11 @@ export declare interface OpenAIBaseInput {
|
|
|
24
24
|
user?: string;
|
|
25
25
|
/** Whether to stream the results or not. Enabling disables tokenUsage reporting */
|
|
26
26
|
streaming: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Whether or not to include token usage data in streamed chunks.
|
|
29
|
+
* @default true
|
|
30
|
+
*/
|
|
31
|
+
streamUsage?: boolean;
|
|
27
32
|
/**
|
|
28
33
|
* Model name to use
|
|
29
34
|
* Alias for `model`
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@langchain/openai",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "OpenAI integrations for LangChain.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -35,9 +35,9 @@
|
|
|
35
35
|
"author": "LangChain",
|
|
36
36
|
"license": "MIT",
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@langchain/core": ">=0.2.
|
|
38
|
+
"@langchain/core": ">=0.2.8 <0.3.0",
|
|
39
39
|
"js-tiktoken": "^1.0.12",
|
|
40
|
-
"openai": "^4.
|
|
40
|
+
"openai": "^4.49.1",
|
|
41
41
|
"zod": "^3.22.4",
|
|
42
42
|
"zod-to-json-schema": "^3.22.3"
|
|
43
43
|
},
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"@azure/identity": "^4.2.0",
|
|
46
46
|
"@jest/globals": "^29.5.0",
|
|
47
47
|
"@langchain/scripts": "~0.0.14",
|
|
48
|
-
"@langchain/standard-tests": "
|
|
48
|
+
"@langchain/standard-tests": "0.0.0",
|
|
49
49
|
"@swc/core": "^1.3.90",
|
|
50
50
|
"@swc/jest": "^0.2.29",
|
|
51
51
|
"dpdm": "^3.12.0",
|
|
@@ -61,6 +61,7 @@
|
|
|
61
61
|
"prettier": "^2.8.3",
|
|
62
62
|
"release-it": "^15.10.1",
|
|
63
63
|
"rimraf": "^5.0.1",
|
|
64
|
+
"ts-jest": "^29.1.0",
|
|
64
65
|
"typescript": "~5.1.6"
|
|
65
66
|
},
|
|
66
67
|
"publishConfig": {
|