@langchain/openai 0.3.0-rc.0 → 0.3.1-rc.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/README.md +1 -1
- package/dist/chat_models.cjs +16 -4
- package/dist/chat_models.d.ts +1 -0
- package/dist/chat_models.js +15 -4
- package/dist/llms.cjs +1 -1
- package/dist/llms.js +1 -1
- package/dist/utils/tools.d.ts +2 -2
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@ This package contains the LangChain.js integrations for OpenAI through their SDK
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
7
7
|
```bash npm2yarn
|
|
8
|
-
npm install @langchain/openai
|
|
8
|
+
npm install @langchain/openai @langchain/core
|
|
9
9
|
```
|
|
10
10
|
|
|
11
11
|
This package, along with the main LangChain package, depends on [`@langchain/core`](https://npmjs.com/package/@langchain/core/).
|
package/dist/chat_models.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ChatOpenAI = exports.messageToOpenAIRole = void 0;
|
|
3
|
+
exports.ChatOpenAI = exports._convertMessagesToOpenAIParams = exports.messageToOpenAIRole = void 0;
|
|
4
4
|
const openai_1 = require("openai");
|
|
5
5
|
const messages_1 = require("@langchain/core/messages");
|
|
6
6
|
const outputs_1 = require("@langchain/core/outputs");
|
|
@@ -156,7 +156,8 @@ delta, rawResponse, defaultRole, includeRawResponse) {
|
|
|
156
156
|
return new messages_1.ChatMessageChunk({ content, role });
|
|
157
157
|
}
|
|
158
158
|
}
|
|
159
|
-
|
|
159
|
+
// Used in LangSmith, export is important here
|
|
160
|
+
function _convertMessagesToOpenAIParams(messages) {
|
|
160
161
|
// TODO: Function messages do not support array content, fix cast
|
|
161
162
|
return messages.map((message) => {
|
|
162
163
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -186,6 +187,7 @@ function convertMessagesToOpenAIParams(messages) {
|
|
|
186
187
|
return completionParam;
|
|
187
188
|
});
|
|
188
189
|
}
|
|
190
|
+
exports._convertMessagesToOpenAIParams = _convertMessagesToOpenAIParams;
|
|
189
191
|
function _convertChatOpenAIToolTypeToOpenAITool(tool, fields) {
|
|
190
192
|
if ((0, base_1.isOpenAITool)(tool)) {
|
|
191
193
|
if (fields?.strict !== undefined) {
|
|
@@ -1066,7 +1068,17 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
|
|
|
1066
1068
|
};
|
|
1067
1069
|
}
|
|
1068
1070
|
async *_streamResponseChunks(messages, options, runManager) {
|
|
1069
|
-
|
|
1071
|
+
if (this.model.includes("o1-")) {
|
|
1072
|
+
console.warn("[WARNING]: OpenAI o1 models do not yet support token-level streaming. Streaming will yield single chunk.");
|
|
1073
|
+
const result = await this._generate(messages, options, runManager);
|
|
1074
|
+
const messageChunk = (0, messages_1.convertToChunk)(result.generations[0].message);
|
|
1075
|
+
yield new outputs_1.ChatGenerationChunk({
|
|
1076
|
+
message: messageChunk,
|
|
1077
|
+
text: typeof messageChunk.content === "string" ? messageChunk.content : "",
|
|
1078
|
+
});
|
|
1079
|
+
return;
|
|
1080
|
+
}
|
|
1081
|
+
const messagesMapped = _convertMessagesToOpenAIParams(messages);
|
|
1070
1082
|
const params = {
|
|
1071
1083
|
...this.invocationParams(options, {
|
|
1072
1084
|
streaming: true,
|
|
@@ -1161,7 +1173,7 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
|
|
|
1161
1173
|
async _generate(messages, options, runManager) {
|
|
1162
1174
|
const tokenUsage = {};
|
|
1163
1175
|
const params = this.invocationParams(options);
|
|
1164
|
-
const messagesMapped =
|
|
1176
|
+
const messagesMapped = _convertMessagesToOpenAIParams(messages);
|
|
1165
1177
|
if (params.stream) {
|
|
1166
1178
|
const stream = this._streamResponseChunks(messages, options, runManager);
|
|
1167
1179
|
const finalChunks = {};
|
package/dist/chat_models.d.ts
CHANGED
|
@@ -19,6 +19,7 @@ interface OpenAILLMOutput {
|
|
|
19
19
|
}
|
|
20
20
|
type OpenAIRoleEnum = "system" | "assistant" | "user" | "function" | "tool";
|
|
21
21
|
export declare function messageToOpenAIRole(message: BaseMessage): OpenAIRoleEnum;
|
|
22
|
+
export declare function _convertMessagesToOpenAIParams(messages: BaseMessage[]): OpenAIClient.Chat.Completions.ChatCompletionMessageParam[];
|
|
22
23
|
type ChatOpenAIToolType = BindToolsInput | OpenAIClient.ChatCompletionTool;
|
|
23
24
|
export interface ChatOpenAIStructuredOutputMethodOptions<IncludeRaw extends boolean> extends StructuredOutputMethodOptions<IncludeRaw> {
|
|
24
25
|
/**
|
package/dist/chat_models.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { OpenAI as OpenAIClient } from "openai";
|
|
2
|
-
import { AIMessage, AIMessageChunk, ChatMessage, ChatMessageChunk, FunctionMessageChunk, HumanMessageChunk, SystemMessageChunk, ToolMessageChunk, isAIMessage, } from "@langchain/core/messages";
|
|
2
|
+
import { AIMessage, AIMessageChunk, ChatMessage, ChatMessageChunk, FunctionMessageChunk, HumanMessageChunk, SystemMessageChunk, ToolMessageChunk, isAIMessage, convertToChunk, } from "@langchain/core/messages";
|
|
3
3
|
import { ChatGenerationChunk, } from "@langchain/core/outputs";
|
|
4
4
|
import { getEnvironmentVariable } from "@langchain/core/utils/env";
|
|
5
5
|
import { BaseChatModel, } from "@langchain/core/language_models/chat_models";
|
|
@@ -152,7 +152,8 @@ delta, rawResponse, defaultRole, includeRawResponse) {
|
|
|
152
152
|
return new ChatMessageChunk({ content, role });
|
|
153
153
|
}
|
|
154
154
|
}
|
|
155
|
-
|
|
155
|
+
// Used in LangSmith, export is important here
|
|
156
|
+
export function _convertMessagesToOpenAIParams(messages) {
|
|
156
157
|
// TODO: Function messages do not support array content, fix cast
|
|
157
158
|
return messages.map((message) => {
|
|
158
159
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -1062,7 +1063,17 @@ export class ChatOpenAI extends BaseChatModel {
|
|
|
1062
1063
|
};
|
|
1063
1064
|
}
|
|
1064
1065
|
async *_streamResponseChunks(messages, options, runManager) {
|
|
1065
|
-
|
|
1066
|
+
if (this.model.includes("o1-")) {
|
|
1067
|
+
console.warn("[WARNING]: OpenAI o1 models do not yet support token-level streaming. Streaming will yield single chunk.");
|
|
1068
|
+
const result = await this._generate(messages, options, runManager);
|
|
1069
|
+
const messageChunk = convertToChunk(result.generations[0].message);
|
|
1070
|
+
yield new ChatGenerationChunk({
|
|
1071
|
+
message: messageChunk,
|
|
1072
|
+
text: typeof messageChunk.content === "string" ? messageChunk.content : "",
|
|
1073
|
+
});
|
|
1074
|
+
return;
|
|
1075
|
+
}
|
|
1076
|
+
const messagesMapped = _convertMessagesToOpenAIParams(messages);
|
|
1066
1077
|
const params = {
|
|
1067
1078
|
...this.invocationParams(options, {
|
|
1068
1079
|
streaming: true,
|
|
@@ -1157,7 +1168,7 @@ export class ChatOpenAI extends BaseChatModel {
|
|
|
1157
1168
|
async _generate(messages, options, runManager) {
|
|
1158
1169
|
const tokenUsage = {};
|
|
1159
1170
|
const params = this.invocationParams(options);
|
|
1160
|
-
const messagesMapped =
|
|
1171
|
+
const messagesMapped = _convertMessagesToOpenAIParams(messages);
|
|
1161
1172
|
if (params.stream) {
|
|
1162
1173
|
const stream = this._streamResponseChunks(messages, options, runManager);
|
|
1163
1174
|
const finalChunks = {};
|
package/dist/llms.cjs
CHANGED
|
@@ -82,7 +82,7 @@ class OpenAI extends llms_1.BaseLLM {
|
|
|
82
82
|
"",
|
|
83
83
|
`See this page for more information:`,
|
|
84
84
|
"|",
|
|
85
|
-
`└> https://js.langchain.com/
|
|
85
|
+
`└> https://js.langchain.com/docs/integrations/chat/openai`,
|
|
86
86
|
].join("\n"));
|
|
87
87
|
// eslint-disable-next-line no-constructor-return
|
|
88
88
|
return new legacy_js_1.OpenAIChat(fields, configuration);
|
package/dist/llms.js
CHANGED
|
@@ -79,7 +79,7 @@ export class OpenAI extends BaseLLM {
|
|
|
79
79
|
"",
|
|
80
80
|
`See this page for more information:`,
|
|
81
81
|
"|",
|
|
82
|
-
`└> https://js.langchain.com/
|
|
82
|
+
`└> https://js.langchain.com/docs/integrations/chat/openai`,
|
|
83
83
|
].join("\n"));
|
|
84
84
|
// eslint-disable-next-line no-constructor-return
|
|
85
85
|
return new OpenAIChat(fields, configuration);
|
package/dist/utils/tools.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { OpenAI as OpenAIClient } from "openai";
|
|
2
2
|
import { BindToolsInput } from "@langchain/core/language_models/chat_models";
|
|
3
3
|
/**
|
|
4
4
|
* Formats a tool in either OpenAI format, or LangChain structured tool format
|
|
@@ -17,4 +17,4 @@ export declare function _convertToOpenAITool(tool: BindToolsInput, fields?: {
|
|
|
17
17
|
* provided in the function definition.
|
|
18
18
|
*/
|
|
19
19
|
strict?: boolean;
|
|
20
|
-
}):
|
|
20
|
+
}): OpenAIClient.ChatCompletionTool;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@langchain/openai",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.1-rc.0",
|
|
4
4
|
"description": "OpenAI integrations for LangChain.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"zod-to-json-schema": "^3.22.3"
|
|
42
42
|
},
|
|
43
43
|
"peerDependencies": {
|
|
44
|
-
"@langchain/core": ">=0.2.26 <0.4.0
|
|
44
|
+
"@langchain/core": ">=0.2.26 <0.4.0"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@azure/identity": "^4.2.1",
|