@langchain/openai 0.0.32 → 0.0.34
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 +7 -3
- package/dist/azure/chat_models.js +7 -3
- package/dist/azure/embeddings.cjs +7 -3
- package/dist/azure/embeddings.js +7 -3
- package/dist/azure/llms.cjs +7 -3
- package/dist/azure/llms.js +7 -3
- package/dist/chat_models.d.ts +2 -2
- package/dist/utils/azure.cjs +1 -1
- package/dist/utils/azure.js +1 -1
- package/package.json +6 -5
- package/dist/tests/azure/chat_models.int.test.d.ts +0 -1
- package/dist/tests/azure/chat_models.int.test.js +0 -659
- package/dist/tests/azure/embeddings.int.test.d.ts +0 -1
- package/dist/tests/azure/embeddings.int.test.js +0 -67
- package/dist/tests/azure/llms.int.test.d.ts +0 -1
- package/dist/tests/azure/llms.int.test.js +0 -267
- package/dist/tests/chat_models-extended.int.test.d.ts +0 -1
- package/dist/tests/chat_models-extended.int.test.js +0 -259
- package/dist/tests/chat_models-vision.int.test.d.ts +0 -1
- package/dist/tests/chat_models-vision.int.test.js +0 -51
- package/dist/tests/chat_models.int.test.d.ts +0 -1
- package/dist/tests/chat_models.int.test.js +0 -622
- package/dist/tests/chat_models_structured_output.int.test.d.ts +0 -1
- package/dist/tests/chat_models_structured_output.int.test.js +0 -232
- package/dist/tests/embeddings.int.test.d.ts +0 -1
- package/dist/tests/embeddings.int.test.js +0 -84
- package/dist/tests/legacy.int.test.d.ts +0 -1
- package/dist/tests/legacy.int.test.js +0 -115
- package/dist/tests/llms.int.test.d.ts +0 -1
- package/dist/tests/llms.int.test.js +0 -260
|
@@ -1,260 +0,0 @@
|
|
|
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 { OpenAIChat } from "../legacy.js";
|
|
5
|
-
import { OpenAI } from "../llms.js";
|
|
6
|
-
test("Test OpenAI", async () => {
|
|
7
|
-
const model = new OpenAI({
|
|
8
|
-
maxTokens: 5,
|
|
9
|
-
modelName: "gpt-3.5-turbo-instruct",
|
|
10
|
-
});
|
|
11
|
-
const res = await model.invoke("Print hello world");
|
|
12
|
-
console.log({ res });
|
|
13
|
-
});
|
|
14
|
-
test("Test OpenAI with stop", async () => {
|
|
15
|
-
const model = new OpenAI({
|
|
16
|
-
maxTokens: 5,
|
|
17
|
-
modelName: "gpt-3.5-turbo-instruct",
|
|
18
|
-
});
|
|
19
|
-
const res = await model.call("Print hello world", ["world"]);
|
|
20
|
-
console.log({ res });
|
|
21
|
-
});
|
|
22
|
-
test("Test OpenAI with stop in object", async () => {
|
|
23
|
-
const model = new OpenAI({
|
|
24
|
-
maxTokens: 5,
|
|
25
|
-
modelName: "gpt-3.5-turbo-instruct",
|
|
26
|
-
});
|
|
27
|
-
const res = await model.invoke("Print hello world", { stop: ["world"] });
|
|
28
|
-
console.log({ res });
|
|
29
|
-
});
|
|
30
|
-
test("Test OpenAI with timeout in call options", async () => {
|
|
31
|
-
const model = new OpenAI({
|
|
32
|
-
maxTokens: 5,
|
|
33
|
-
maxRetries: 0,
|
|
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 OpenAI with timeout in call options and node adapter", async () => {
|
|
41
|
-
const model = new OpenAI({
|
|
42
|
-
maxTokens: 5,
|
|
43
|
-
maxRetries: 0,
|
|
44
|
-
modelName: "gpt-3.5-turbo-instruct",
|
|
45
|
-
});
|
|
46
|
-
await expect(() => model.invoke("Print hello world", {
|
|
47
|
-
timeout: 10,
|
|
48
|
-
})).rejects.toThrow();
|
|
49
|
-
}, 5000);
|
|
50
|
-
test("Test OpenAI with signal in call options", async () => {
|
|
51
|
-
const model = new OpenAI({
|
|
52
|
-
maxTokens: 5,
|
|
53
|
-
modelName: "gpt-3.5-turbo-instruct",
|
|
54
|
-
});
|
|
55
|
-
const controller = new AbortController();
|
|
56
|
-
await expect(() => {
|
|
57
|
-
const ret = model.invoke("Print hello world", {
|
|
58
|
-
signal: controller.signal,
|
|
59
|
-
});
|
|
60
|
-
controller.abort();
|
|
61
|
-
return ret;
|
|
62
|
-
}).rejects.toThrow();
|
|
63
|
-
}, 5000);
|
|
64
|
-
test("Test OpenAI with signal in call options and node adapter", async () => {
|
|
65
|
-
const model = new OpenAI({
|
|
66
|
-
maxTokens: 5,
|
|
67
|
-
modelName: "gpt-3.5-turbo-instruct",
|
|
68
|
-
});
|
|
69
|
-
const controller = new AbortController();
|
|
70
|
-
await expect(() => {
|
|
71
|
-
const ret = model.invoke("Print hello world", {
|
|
72
|
-
signal: controller.signal,
|
|
73
|
-
});
|
|
74
|
-
controller.abort();
|
|
75
|
-
return ret;
|
|
76
|
-
}).rejects.toThrow();
|
|
77
|
-
}, 5000);
|
|
78
|
-
test("Test OpenAI with concurrency == 1", async () => {
|
|
79
|
-
const model = new OpenAI({
|
|
80
|
-
maxTokens: 5,
|
|
81
|
-
modelName: "gpt-3.5-turbo-instruct",
|
|
82
|
-
maxConcurrency: 1,
|
|
83
|
-
});
|
|
84
|
-
const res = await Promise.all([
|
|
85
|
-
model.invoke("Print hello world"),
|
|
86
|
-
model.invoke("Print hello world"),
|
|
87
|
-
]);
|
|
88
|
-
console.log({ res });
|
|
89
|
-
});
|
|
90
|
-
test("Test OpenAI with maxTokens -1", async () => {
|
|
91
|
-
const model = new OpenAI({
|
|
92
|
-
maxTokens: -1,
|
|
93
|
-
modelName: "gpt-3.5-turbo-instruct",
|
|
94
|
-
});
|
|
95
|
-
const res = await model.call("Print hello world", ["world"]);
|
|
96
|
-
console.log({ res });
|
|
97
|
-
});
|
|
98
|
-
test("Test OpenAI with chat model returns OpenAIChat", async () => {
|
|
99
|
-
const model = new OpenAI({ modelName: "gpt-3.5-turbo" });
|
|
100
|
-
expect(model).toBeInstanceOf(OpenAIChat);
|
|
101
|
-
const res = await model.invoke("Print hello world");
|
|
102
|
-
console.log({ res });
|
|
103
|
-
expect(typeof res).toBe("string");
|
|
104
|
-
});
|
|
105
|
-
test("Test OpenAI with instruct model returns OpenAI", async () => {
|
|
106
|
-
const model = new OpenAI({ modelName: "gpt-3.5-turbo-instruct" });
|
|
107
|
-
expect(model).toBeInstanceOf(OpenAI);
|
|
108
|
-
const res = await model.invoke("Print hello world");
|
|
109
|
-
console.log({ res });
|
|
110
|
-
expect(typeof res).toBe("string");
|
|
111
|
-
});
|
|
112
|
-
test("Test OpenAI with versioned instruct model returns OpenAI", async () => {
|
|
113
|
-
const model = new OpenAI({ modelName: "gpt-3.5-turbo-instruct-0914" });
|
|
114
|
-
expect(model).toBeInstanceOf(OpenAI);
|
|
115
|
-
const res = await model.invoke("Print hello world");
|
|
116
|
-
console.log({ res });
|
|
117
|
-
expect(typeof res).toBe("string");
|
|
118
|
-
});
|
|
119
|
-
test("Test ChatOpenAI tokenUsage", async () => {
|
|
120
|
-
let tokenUsage = {
|
|
121
|
-
completionTokens: 0,
|
|
122
|
-
promptTokens: 0,
|
|
123
|
-
totalTokens: 0,
|
|
124
|
-
};
|
|
125
|
-
const model = new OpenAI({
|
|
126
|
-
maxTokens: 5,
|
|
127
|
-
modelName: "gpt-3.5-turbo-instruct",
|
|
128
|
-
callbackManager: CallbackManager.fromHandlers({
|
|
129
|
-
async handleLLMEnd(output) {
|
|
130
|
-
tokenUsage = output.llmOutput?.tokenUsage;
|
|
131
|
-
},
|
|
132
|
-
}),
|
|
133
|
-
});
|
|
134
|
-
const res = await model.invoke("Hello");
|
|
135
|
-
console.log({ res });
|
|
136
|
-
expect(tokenUsage.promptTokens).toBe(1);
|
|
137
|
-
});
|
|
138
|
-
test("Test OpenAI in streaming mode", async () => {
|
|
139
|
-
let nrNewTokens = 0;
|
|
140
|
-
let streamedCompletion = "";
|
|
141
|
-
const model = new OpenAI({
|
|
142
|
-
maxTokens: 5,
|
|
143
|
-
modelName: "gpt-3.5-turbo-instruct",
|
|
144
|
-
streaming: true,
|
|
145
|
-
callbacks: CallbackManager.fromHandlers({
|
|
146
|
-
async handleLLMNewToken(token) {
|
|
147
|
-
nrNewTokens += 1;
|
|
148
|
-
streamedCompletion += token;
|
|
149
|
-
},
|
|
150
|
-
}),
|
|
151
|
-
});
|
|
152
|
-
const res = await model.invoke("Print hello world");
|
|
153
|
-
console.log({ res });
|
|
154
|
-
expect(nrNewTokens > 0).toBe(true);
|
|
155
|
-
expect(res).toBe(streamedCompletion);
|
|
156
|
-
});
|
|
157
|
-
test("Test OpenAI in streaming mode with multiple prompts", async () => {
|
|
158
|
-
let nrNewTokens = 0;
|
|
159
|
-
const completions = [
|
|
160
|
-
["", ""],
|
|
161
|
-
["", ""],
|
|
162
|
-
];
|
|
163
|
-
const model = new OpenAI({
|
|
164
|
-
maxTokens: 5,
|
|
165
|
-
modelName: "gpt-3.5-turbo-instruct",
|
|
166
|
-
streaming: true,
|
|
167
|
-
n: 2,
|
|
168
|
-
callbacks: CallbackManager.fromHandlers({
|
|
169
|
-
async handleLLMNewToken(token, idx) {
|
|
170
|
-
nrNewTokens += 1;
|
|
171
|
-
completions[idx.prompt][idx.completion] += token;
|
|
172
|
-
},
|
|
173
|
-
}),
|
|
174
|
-
});
|
|
175
|
-
const res = await model.generate(["Print hello world", "print hello sea"]);
|
|
176
|
-
console.log(res.generations, res.generations.map((g) => g[0].generationInfo));
|
|
177
|
-
expect(nrNewTokens > 0).toBe(true);
|
|
178
|
-
expect(res.generations.length).toBe(2);
|
|
179
|
-
expect(res.generations.map((g) => g.map((gg) => gg.text))).toEqual(completions);
|
|
180
|
-
});
|
|
181
|
-
test("Test OpenAIChat in streaming mode with multiple prompts", async () => {
|
|
182
|
-
let nrNewTokens = 0;
|
|
183
|
-
const completions = [[""], [""]];
|
|
184
|
-
const model = new OpenAI({
|
|
185
|
-
maxTokens: 5,
|
|
186
|
-
modelName: "gpt-3.5-turbo",
|
|
187
|
-
streaming: true,
|
|
188
|
-
n: 1,
|
|
189
|
-
callbacks: CallbackManager.fromHandlers({
|
|
190
|
-
async handleLLMNewToken(token, idx) {
|
|
191
|
-
nrNewTokens += 1;
|
|
192
|
-
completions[idx.prompt][idx.completion] += token;
|
|
193
|
-
},
|
|
194
|
-
}),
|
|
195
|
-
});
|
|
196
|
-
const res = await model.generate(["Print hello world", "print hello sea"]);
|
|
197
|
-
console.log(res.generations, res.generations.map((g) => g[0].generationInfo));
|
|
198
|
-
expect(nrNewTokens > 0).toBe(true);
|
|
199
|
-
expect(res.generations.length).toBe(2);
|
|
200
|
-
expect(res.generations.map((g) => g.map((gg) => gg.text))).toEqual(completions);
|
|
201
|
-
});
|
|
202
|
-
test("Test OpenAI prompt value", async () => {
|
|
203
|
-
const model = new OpenAI({
|
|
204
|
-
maxTokens: 5,
|
|
205
|
-
modelName: "gpt-3.5-turbo-instruct",
|
|
206
|
-
});
|
|
207
|
-
const res = await model.generatePrompt([
|
|
208
|
-
new StringPromptValue("Print hello world"),
|
|
209
|
-
]);
|
|
210
|
-
expect(res.generations.length).toBe(1);
|
|
211
|
-
for (const generation of res.generations) {
|
|
212
|
-
expect(generation.length).toBe(1);
|
|
213
|
-
for (const g of generation) {
|
|
214
|
-
console.log(g.text);
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
console.log({ res });
|
|
218
|
-
});
|
|
219
|
-
test("Test OpenAI stream method", async () => {
|
|
220
|
-
const model = new OpenAI({
|
|
221
|
-
maxTokens: 50,
|
|
222
|
-
modelName: "gpt-3.5-turbo-instruct",
|
|
223
|
-
});
|
|
224
|
-
const stream = await model.stream("Print hello world.");
|
|
225
|
-
const chunks = [];
|
|
226
|
-
for await (const chunk of stream) {
|
|
227
|
-
chunks.push(chunk);
|
|
228
|
-
}
|
|
229
|
-
expect(chunks.length).toBeGreaterThan(1);
|
|
230
|
-
});
|
|
231
|
-
test("Test OpenAI stream method with abort", async () => {
|
|
232
|
-
await expect(async () => {
|
|
233
|
-
const model = new OpenAI({
|
|
234
|
-
maxTokens: 250,
|
|
235
|
-
maxRetries: 0,
|
|
236
|
-
modelName: "gpt-3.5-turbo-instruct",
|
|
237
|
-
});
|
|
238
|
-
const stream = await model.stream("How is your day going? Be extremely verbose.", {
|
|
239
|
-
signal: AbortSignal.timeout(1000),
|
|
240
|
-
});
|
|
241
|
-
for await (const chunk of stream) {
|
|
242
|
-
console.log(chunk);
|
|
243
|
-
}
|
|
244
|
-
}).rejects.toThrow();
|
|
245
|
-
});
|
|
246
|
-
test("Test OpenAI stream method with early break", async () => {
|
|
247
|
-
const model = new OpenAI({
|
|
248
|
-
maxTokens: 50,
|
|
249
|
-
modelName: "gpt-3.5-turbo-instruct",
|
|
250
|
-
});
|
|
251
|
-
const stream = await model.stream("How is your day going? Be extremely verbose.");
|
|
252
|
-
let i = 0;
|
|
253
|
-
for await (const chunk of stream) {
|
|
254
|
-
console.log(chunk);
|
|
255
|
-
i += 1;
|
|
256
|
-
if (i > 5) {
|
|
257
|
-
break;
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
});
|