@langchain/openai 0.0.28 → 0.0.29
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/azure/chat_models.cjs +47 -7
- package/dist/azure/chat_models.d.ts +2 -1
- package/dist/azure/chat_models.js +47 -7
- package/dist/azure/embeddings.cjs +70 -0
- package/dist/azure/embeddings.d.ts +14 -0
- package/dist/azure/embeddings.js +66 -0
- package/dist/azure/llms.cjs +46 -7
- package/dist/azure/llms.d.ts +2 -1
- package/dist/azure/llms.js +46 -7
- package/dist/chat_models.cjs +10 -3
- package/dist/chat_models.d.ts +4 -3
- package/dist/chat_models.js +10 -3
- package/dist/embeddings.cjs +10 -3
- package/dist/embeddings.d.ts +5 -4
- package/dist/embeddings.js +10 -3
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/llms.cjs +10 -3
- package/dist/llms.d.ts +4 -3
- package/dist/llms.js +10 -3
- package/dist/tests/azure/chat_models.int.test.d.ts +1 -0
- package/dist/tests/azure/chat_models.int.test.js +659 -0
- package/dist/tests/azure/embeddings.int.test.d.ts +1 -0
- package/dist/tests/azure/embeddings.int.test.js +67 -0
- package/dist/tests/azure/llms.int.test.d.ts +1 -0
- package/dist/tests/azure/llms.int.test.js +267 -0
- package/dist/types.d.ts +5 -0
- package/package.json +3 -2
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { test, expect } from "@jest/globals";
|
|
2
|
+
import { AzureOpenAIEmbeddings as OpenAIEmbeddings } from "../../azure/embeddings.js";
|
|
3
|
+
test("Test AzureOpenAIEmbeddings.embedQuery", async () => {
|
|
4
|
+
const embeddings = new OpenAIEmbeddings();
|
|
5
|
+
const res = await embeddings.embedQuery("Hello world");
|
|
6
|
+
expect(typeof res[0]).toBe("number");
|
|
7
|
+
});
|
|
8
|
+
test("Test AzureOpenAIEmbeddings.embedDocuments", async () => {
|
|
9
|
+
const embeddings = new OpenAIEmbeddings();
|
|
10
|
+
const res = await embeddings.embedDocuments(["Hello world", "Bye bye"]);
|
|
11
|
+
expect(res).toHaveLength(2);
|
|
12
|
+
expect(typeof res[0][0]).toBe("number");
|
|
13
|
+
expect(typeof res[1][0]).toBe("number");
|
|
14
|
+
});
|
|
15
|
+
test("Test AzureOpenAIEmbeddings concurrency", async () => {
|
|
16
|
+
const embeddings = new OpenAIEmbeddings({
|
|
17
|
+
batchSize: 1,
|
|
18
|
+
maxConcurrency: 2,
|
|
19
|
+
});
|
|
20
|
+
const res = await embeddings.embedDocuments([
|
|
21
|
+
"Hello world",
|
|
22
|
+
"Bye bye",
|
|
23
|
+
"Hello world",
|
|
24
|
+
"Bye bye",
|
|
25
|
+
"Hello world",
|
|
26
|
+
"Bye bye",
|
|
27
|
+
]);
|
|
28
|
+
expect(res).toHaveLength(6);
|
|
29
|
+
expect(res.find((embedding) => typeof embedding[0] !== "number")).toBe(undefined);
|
|
30
|
+
});
|
|
31
|
+
test("Test timeout error thrown from SDK", async () => {
|
|
32
|
+
await expect(async () => {
|
|
33
|
+
const model = new OpenAIEmbeddings({
|
|
34
|
+
timeout: 1,
|
|
35
|
+
maxRetries: 0,
|
|
36
|
+
});
|
|
37
|
+
await model.embedDocuments([
|
|
38
|
+
"Hello world",
|
|
39
|
+
"Bye bye",
|
|
40
|
+
"Hello world",
|
|
41
|
+
"Bye bye",
|
|
42
|
+
"Hello world",
|
|
43
|
+
"Bye bye",
|
|
44
|
+
]);
|
|
45
|
+
}).rejects.toThrow();
|
|
46
|
+
});
|
|
47
|
+
test("Test AzureOpenAIEmbeddings.embedQuery with v3 and dimensions", async () => {
|
|
48
|
+
const embeddings = new OpenAIEmbeddings({
|
|
49
|
+
modelName: "text-embedding-3-small",
|
|
50
|
+
dimensions: 127,
|
|
51
|
+
});
|
|
52
|
+
const res = await embeddings.embedQuery("Hello world");
|
|
53
|
+
expect(typeof res[0]).toBe("number");
|
|
54
|
+
expect(res.length).toBe(127);
|
|
55
|
+
});
|
|
56
|
+
test("Test AzureOpenAIEmbeddings.embedDocuments with v3 and dimensions", async () => {
|
|
57
|
+
const embeddings = new OpenAIEmbeddings({
|
|
58
|
+
modelName: "text-embedding-3-small",
|
|
59
|
+
dimensions: 127,
|
|
60
|
+
});
|
|
61
|
+
const res = await embeddings.embedDocuments(["Hello world", "Bye bye"]);
|
|
62
|
+
expect(res).toHaveLength(2);
|
|
63
|
+
expect(typeof res[0][0]).toBe("number");
|
|
64
|
+
expect(typeof res[1][0]).toBe("number");
|
|
65
|
+
expect(res[0].length).toBe(127);
|
|
66
|
+
expect(res[1].length).toBe(127);
|
|
67
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { test, expect } from "@jest/globals";
|
|
2
|
+
import { StringPromptValue } from "@langchain/core/prompt_values";
|
|
3
|
+
import { CallbackManager } from "@langchain/core/callbacks/manager";
|
|
4
|
+
import { ClientSecretCredential, getBearerTokenProvider, } from "@azure/identity";
|
|
5
|
+
import { getEnvironmentVariable } from "@langchain/core/utils/env";
|
|
6
|
+
import { AzureOpenAI } from "../../azure/llms.js";
|
|
7
|
+
test("Test Azure OpenAI invoke", async () => {
|
|
8
|
+
const model = new AzureOpenAI({
|
|
9
|
+
maxTokens: 5,
|
|
10
|
+
modelName: "gpt-3.5-turbo-instruct",
|
|
11
|
+
});
|
|
12
|
+
const res = await model.invoke("Print hello world");
|
|
13
|
+
console.log({ res });
|
|
14
|
+
});
|
|
15
|
+
test("Test Azure OpenAI call", async () => {
|
|
16
|
+
const model = new AzureOpenAI({
|
|
17
|
+
maxTokens: 5,
|
|
18
|
+
modelName: "gpt-3.5-turbo-instruct",
|
|
19
|
+
});
|
|
20
|
+
const res = await model.call("Print hello world", ["world"]);
|
|
21
|
+
console.log({ res });
|
|
22
|
+
});
|
|
23
|
+
test("Test Azure OpenAI with stop in object", async () => {
|
|
24
|
+
const model = new AzureOpenAI({
|
|
25
|
+
maxTokens: 5,
|
|
26
|
+
modelName: "gpt-3.5-turbo-instruct",
|
|
27
|
+
});
|
|
28
|
+
const res = await model.invoke("Print hello world", { stop: ["world"] });
|
|
29
|
+
console.log({ res });
|
|
30
|
+
});
|
|
31
|
+
test("Test Azure OpenAI with timeout in call options", async () => {
|
|
32
|
+
const model = new AzureOpenAI({
|
|
33
|
+
maxTokens: 5,
|
|
34
|
+
modelName: "gpt-3.5-turbo-instruct",
|
|
35
|
+
});
|
|
36
|
+
await expect(() => model.invoke("Print hello world", {
|
|
37
|
+
timeout: 10,
|
|
38
|
+
})).rejects.toThrow();
|
|
39
|
+
}, 5000);
|
|
40
|
+
test("Test Azure OpenAI with timeout in call options and node adapter", async () => {
|
|
41
|
+
const model = new AzureOpenAI({
|
|
42
|
+
maxTokens: 5,
|
|
43
|
+
modelName: "gpt-3.5-turbo-instruct",
|
|
44
|
+
});
|
|
45
|
+
await expect(() => model.invoke("Print hello world", {
|
|
46
|
+
timeout: 10,
|
|
47
|
+
})).rejects.toThrow();
|
|
48
|
+
}, 5000);
|
|
49
|
+
test("Test Azure OpenAI with signal in call options", async () => {
|
|
50
|
+
const model = new AzureOpenAI({
|
|
51
|
+
maxTokens: 5,
|
|
52
|
+
modelName: "gpt-3.5-turbo-instruct",
|
|
53
|
+
});
|
|
54
|
+
const controller = new AbortController();
|
|
55
|
+
await expect(() => {
|
|
56
|
+
const ret = model.invoke("Print hello world", {
|
|
57
|
+
signal: controller.signal,
|
|
58
|
+
});
|
|
59
|
+
controller.abort();
|
|
60
|
+
return ret;
|
|
61
|
+
}).rejects.toThrow();
|
|
62
|
+
}, 5000);
|
|
63
|
+
test("Test Azure OpenAI with signal in call options and node adapter", async () => {
|
|
64
|
+
const model = new AzureOpenAI({
|
|
65
|
+
maxTokens: 5,
|
|
66
|
+
modelName: "gpt-3.5-turbo-instruct",
|
|
67
|
+
});
|
|
68
|
+
const controller = new AbortController();
|
|
69
|
+
await expect(() => {
|
|
70
|
+
const ret = model.invoke("Print hello world", {
|
|
71
|
+
signal: controller.signal,
|
|
72
|
+
});
|
|
73
|
+
controller.abort();
|
|
74
|
+
return ret;
|
|
75
|
+
}).rejects.toThrow();
|
|
76
|
+
}, 5000);
|
|
77
|
+
test("Test Azure OpenAI with concurrency == 1", async () => {
|
|
78
|
+
const model = new AzureOpenAI({
|
|
79
|
+
maxTokens: 5,
|
|
80
|
+
modelName: "gpt-3.5-turbo-instruct",
|
|
81
|
+
maxConcurrency: 1,
|
|
82
|
+
});
|
|
83
|
+
const res = await Promise.all([
|
|
84
|
+
model.invoke("Print hello world"),
|
|
85
|
+
model.invoke("Print hello world"),
|
|
86
|
+
]);
|
|
87
|
+
console.log({ res });
|
|
88
|
+
});
|
|
89
|
+
test("Test Azure OpenAI with maxTokens -1", async () => {
|
|
90
|
+
const model = new AzureOpenAI({
|
|
91
|
+
maxTokens: -1,
|
|
92
|
+
modelName: "gpt-3.5-turbo-instruct",
|
|
93
|
+
});
|
|
94
|
+
const res = await model.call("Print hello world", ["world"]);
|
|
95
|
+
console.log({ res });
|
|
96
|
+
});
|
|
97
|
+
test("Test Azure OpenAI with model name", async () => {
|
|
98
|
+
const model = new AzureOpenAI({ modelName: "gpt-3.5-turbo-instruct" });
|
|
99
|
+
expect(model).toBeInstanceOf(AzureOpenAI);
|
|
100
|
+
const res = await model.invoke("Print hello world");
|
|
101
|
+
console.log({ res });
|
|
102
|
+
expect(typeof res).toBe("string");
|
|
103
|
+
});
|
|
104
|
+
test("Test Azure OpenAI with versioned instruct model returns Azure OpenAI", async () => {
|
|
105
|
+
const model = new AzureOpenAI({
|
|
106
|
+
modelName: "gpt-3.5-turbo-instruct-0914",
|
|
107
|
+
});
|
|
108
|
+
expect(model).toBeInstanceOf(AzureOpenAI);
|
|
109
|
+
const res = await model.invoke("Print hello world");
|
|
110
|
+
console.log({ res });
|
|
111
|
+
expect(typeof res).toBe("string");
|
|
112
|
+
});
|
|
113
|
+
test("Test Azure OpenAI tokenUsage", async () => {
|
|
114
|
+
let tokenUsage = {
|
|
115
|
+
completionTokens: 0,
|
|
116
|
+
promptTokens: 0,
|
|
117
|
+
totalTokens: 0,
|
|
118
|
+
};
|
|
119
|
+
const model = new AzureOpenAI({
|
|
120
|
+
maxTokens: 5,
|
|
121
|
+
modelName: "gpt-3.5-turbo-instruct",
|
|
122
|
+
callbackManager: CallbackManager.fromHandlers({
|
|
123
|
+
async handleLLMEnd(output) {
|
|
124
|
+
tokenUsage = output.llmOutput?.tokenUsage;
|
|
125
|
+
},
|
|
126
|
+
}),
|
|
127
|
+
});
|
|
128
|
+
const res = await model.invoke("Hello");
|
|
129
|
+
console.log({ res });
|
|
130
|
+
expect(tokenUsage.promptTokens).toBe(1);
|
|
131
|
+
});
|
|
132
|
+
test("Test Azure OpenAI in streaming mode", async () => {
|
|
133
|
+
let nrNewTokens = 0;
|
|
134
|
+
let streamedCompletion = "";
|
|
135
|
+
const model = new AzureOpenAI({
|
|
136
|
+
maxTokens: 5,
|
|
137
|
+
modelName: "gpt-3.5-turbo-instruct",
|
|
138
|
+
streaming: true,
|
|
139
|
+
callbacks: CallbackManager.fromHandlers({
|
|
140
|
+
async handleLLMNewToken(token) {
|
|
141
|
+
nrNewTokens += 1;
|
|
142
|
+
streamedCompletion += token;
|
|
143
|
+
},
|
|
144
|
+
}),
|
|
145
|
+
});
|
|
146
|
+
const res = await model.invoke("Print hello world");
|
|
147
|
+
console.log({ res });
|
|
148
|
+
expect(nrNewTokens > 0).toBe(true);
|
|
149
|
+
expect(res).toBe(streamedCompletion);
|
|
150
|
+
});
|
|
151
|
+
test("Test Azure OpenAI in streaming mode with multiple prompts", async () => {
|
|
152
|
+
let nrNewTokens = 0;
|
|
153
|
+
const completions = [
|
|
154
|
+
["", ""],
|
|
155
|
+
["", ""],
|
|
156
|
+
];
|
|
157
|
+
const model = new AzureOpenAI({
|
|
158
|
+
maxTokens: 5,
|
|
159
|
+
modelName: "gpt-3.5-turbo-instruct",
|
|
160
|
+
streaming: true,
|
|
161
|
+
n: 2,
|
|
162
|
+
callbacks: CallbackManager.fromHandlers({
|
|
163
|
+
async handleLLMNewToken(token, idx) {
|
|
164
|
+
nrNewTokens += 1;
|
|
165
|
+
completions[idx.prompt][idx.completion] += token;
|
|
166
|
+
},
|
|
167
|
+
}),
|
|
168
|
+
});
|
|
169
|
+
const res = await model.generate(["Print hello world", "print hello sea"]);
|
|
170
|
+
console.log(res.generations, res.generations.map((g) => g[0].generationInfo));
|
|
171
|
+
expect(nrNewTokens > 0).toBe(true);
|
|
172
|
+
expect(res.generations.length).toBe(2);
|
|
173
|
+
expect(res.generations.map((g) => g.map((gg) => gg.text))).toEqual(completions);
|
|
174
|
+
});
|
|
175
|
+
test("Test Azure OpenAI in streaming mode with multiple prompts", async () => {
|
|
176
|
+
let nrNewTokens = 0;
|
|
177
|
+
const completions = [[""], [""]];
|
|
178
|
+
const model = new AzureOpenAI({
|
|
179
|
+
maxTokens: 5,
|
|
180
|
+
modelName: "gpt-3.5-turbo",
|
|
181
|
+
streaming: true,
|
|
182
|
+
n: 1,
|
|
183
|
+
callbacks: CallbackManager.fromHandlers({
|
|
184
|
+
async handleLLMNewToken(token, idx) {
|
|
185
|
+
nrNewTokens += 1;
|
|
186
|
+
completions[idx.prompt][idx.completion] += token;
|
|
187
|
+
},
|
|
188
|
+
}),
|
|
189
|
+
});
|
|
190
|
+
const res = await model.generate(["Print hello world", "print hello sea"]);
|
|
191
|
+
console.log(res.generations, res.generations.map((g) => g[0].generationInfo));
|
|
192
|
+
expect(nrNewTokens > 0).toBe(true);
|
|
193
|
+
expect(res.generations.length).toBe(2);
|
|
194
|
+
expect(res.generations.map((g) => g.map((gg) => gg.text))).toEqual(completions);
|
|
195
|
+
});
|
|
196
|
+
test("Test Azure OpenAI prompt value", async () => {
|
|
197
|
+
const model = new AzureOpenAI({
|
|
198
|
+
maxTokens: 5,
|
|
199
|
+
modelName: "gpt-3.5-turbo-instruct",
|
|
200
|
+
});
|
|
201
|
+
const res = await model.generatePrompt([
|
|
202
|
+
new StringPromptValue("Print hello world"),
|
|
203
|
+
]);
|
|
204
|
+
expect(res.generations.length).toBe(1);
|
|
205
|
+
for (const generation of res.generations) {
|
|
206
|
+
expect(generation.length).toBe(1);
|
|
207
|
+
for (const g of generation) {
|
|
208
|
+
console.log(g.text);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
console.log({ res });
|
|
212
|
+
});
|
|
213
|
+
test("Test Azure OpenAI stream method", async () => {
|
|
214
|
+
const model = new AzureOpenAI({
|
|
215
|
+
maxTokens: 50,
|
|
216
|
+
modelName: "gpt-3.5-turbo-instruct",
|
|
217
|
+
});
|
|
218
|
+
const stream = await model.stream("Print hello world.");
|
|
219
|
+
const chunks = [];
|
|
220
|
+
for await (const chunk of stream) {
|
|
221
|
+
chunks.push(chunk);
|
|
222
|
+
}
|
|
223
|
+
expect(chunks.length).toBeGreaterThan(1);
|
|
224
|
+
});
|
|
225
|
+
test("Test Azure OpenAI stream method with abort", async () => {
|
|
226
|
+
await expect(async () => {
|
|
227
|
+
const model = new AzureOpenAI({
|
|
228
|
+
maxTokens: 250,
|
|
229
|
+
modelName: "gpt-3.5-turbo-instruct",
|
|
230
|
+
});
|
|
231
|
+
const stream = await model.stream("How is your day going? Be extremely verbose.", {
|
|
232
|
+
signal: AbortSignal.timeout(1000),
|
|
233
|
+
});
|
|
234
|
+
for await (const chunk of stream) {
|
|
235
|
+
console.log(chunk);
|
|
236
|
+
}
|
|
237
|
+
}).rejects.toThrow();
|
|
238
|
+
});
|
|
239
|
+
test("Test Azure OpenAI stream method with early break", async () => {
|
|
240
|
+
const model = new AzureOpenAI({
|
|
241
|
+
maxTokens: 50,
|
|
242
|
+
modelName: "gpt-3.5-turbo-instruct",
|
|
243
|
+
});
|
|
244
|
+
const stream = await model.stream("How is your day going? Be extremely verbose.");
|
|
245
|
+
let i = 0;
|
|
246
|
+
for await (const chunk of stream) {
|
|
247
|
+
console.log(chunk);
|
|
248
|
+
i += 1;
|
|
249
|
+
if (i > 5) {
|
|
250
|
+
break;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
test("Test Azure OpenAI with bearer token credentials", async () => {
|
|
255
|
+
const tenantId = getEnvironmentVariable("AZURE_TENANT_ID") ?? "";
|
|
256
|
+
const clientId = getEnvironmentVariable("AZURE_CLIENT_ID") ?? "";
|
|
257
|
+
const clientSecret = getEnvironmentVariable("AZURE_CLIENT_SECRET") ?? "";
|
|
258
|
+
const credentials = new ClientSecretCredential(tenantId, clientId, clientSecret);
|
|
259
|
+
const azureADTokenProvider = getBearerTokenProvider(credentials, "https://cognitiveservices.azure.com/.default");
|
|
260
|
+
const model = new AzureOpenAI({
|
|
261
|
+
maxTokens: 5,
|
|
262
|
+
modelName: "davinci-002",
|
|
263
|
+
azureADTokenProvider,
|
|
264
|
+
});
|
|
265
|
+
const res = await model.invoke("Print hello world");
|
|
266
|
+
console.log({ res });
|
|
267
|
+
});
|
package/dist/types.d.ts
CHANGED
|
@@ -158,4 +158,9 @@ export declare interface AzureOpenAIInput {
|
|
|
158
158
|
* will be result in the endpoint URL: https://westeurope.api.cognitive.microsoft.com/openai/deployments/{DeploymentName}/
|
|
159
159
|
*/
|
|
160
160
|
azureOpenAIBasePath?: string;
|
|
161
|
+
/**
|
|
162
|
+
* A function that returns an access token for Microsoft Entra (formerly known as Azure Active Directory),
|
|
163
|
+
* which will be invoked on every request.
|
|
164
|
+
*/
|
|
165
|
+
azureADTokenProvider?: () => Promise<string>;
|
|
161
166
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@langchain/openai",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.29",
|
|
4
4
|
"description": "OpenAI integrations for LangChain.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -41,11 +41,12 @@
|
|
|
41
41
|
"dependencies": {
|
|
42
42
|
"@langchain/core": "~0.1.56",
|
|
43
43
|
"js-tiktoken": "^1.0.7",
|
|
44
|
-
"openai": "^4.
|
|
44
|
+
"openai": "^4.41.1",
|
|
45
45
|
"zod": "^3.22.4",
|
|
46
46
|
"zod-to-json-schema": "^3.22.3"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
|
+
"@azure/identity": "^4.2.0",
|
|
49
50
|
"@jest/globals": "^29.5.0",
|
|
50
51
|
"@langchain/scripts": "~0.0",
|
|
51
52
|
"@swc/core": "^1.3.90",
|