@langchain/openai 0.0.33 → 0.1.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.
Files changed (32) hide show
  1. package/dist/azure/chat_models.cjs +7 -3
  2. package/dist/azure/chat_models.js +7 -3
  3. package/dist/azure/embeddings.cjs +7 -3
  4. package/dist/azure/embeddings.js +7 -3
  5. package/dist/azure/llms.cjs +7 -3
  6. package/dist/azure/llms.js +7 -3
  7. package/dist/chat_models.cjs +21 -0
  8. package/dist/chat_models.d.ts +3 -0
  9. package/dist/chat_models.js +21 -0
  10. package/dist/utils/azure.cjs +1 -1
  11. package/dist/utils/azure.js +1 -1
  12. package/package.json +6 -5
  13. package/dist/tests/azure/chat_models.int.test.d.ts +0 -1
  14. package/dist/tests/azure/chat_models.int.test.js +0 -659
  15. package/dist/tests/azure/embeddings.int.test.d.ts +0 -1
  16. package/dist/tests/azure/embeddings.int.test.js +0 -67
  17. package/dist/tests/azure/llms.int.test.d.ts +0 -1
  18. package/dist/tests/azure/llms.int.test.js +0 -267
  19. package/dist/tests/chat_models-extended.int.test.d.ts +0 -1
  20. package/dist/tests/chat_models-extended.int.test.js +0 -259
  21. package/dist/tests/chat_models-vision.int.test.d.ts +0 -1
  22. package/dist/tests/chat_models-vision.int.test.js +0 -51
  23. package/dist/tests/chat_models.int.test.d.ts +0 -1
  24. package/dist/tests/chat_models.int.test.js +0 -622
  25. package/dist/tests/chat_models_structured_output.int.test.d.ts +0 -1
  26. package/dist/tests/chat_models_structured_output.int.test.js +0 -232
  27. package/dist/tests/embeddings.int.test.d.ts +0 -1
  28. package/dist/tests/embeddings.int.test.js +0 -84
  29. package/dist/tests/legacy.int.test.d.ts +0 -1
  30. package/dist/tests/legacy.int.test.js +0 -115
  31. package/dist/tests/llms.int.test.d.ts +0 -1
  32. package/dist/tests/llms.int.test.js +0 -260
@@ -1,67 +0,0 @@
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
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,267 +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 { 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
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,259 +0,0 @@
1
- import { test, expect, jest } from "@jest/globals";
2
- import { AIMessage, HumanMessage, ToolMessage } from "@langchain/core/messages";
3
- import { InMemoryCache } from "@langchain/core/caches";
4
- import { ChatOpenAI } from "../chat_models.js";
5
- test("Test ChatOpenAI JSON mode", async () => {
6
- const chat = new ChatOpenAI({
7
- modelName: "gpt-3.5-turbo-1106",
8
- maxTokens: 128,
9
- }).bind({
10
- response_format: {
11
- type: "json_object",
12
- },
13
- });
14
- const message = new HumanMessage("Hello!");
15
- const res = await chat.invoke([["system", "Only return JSON"], message]);
16
- console.log(JSON.stringify(res));
17
- });
18
- test("Test ChatOpenAI seed", async () => {
19
- const chat = new ChatOpenAI({
20
- modelName: "gpt-3.5-turbo-1106",
21
- maxTokens: 128,
22
- temperature: 1,
23
- }).bind({
24
- seed: 123454930394983,
25
- });
26
- const message = new HumanMessage("Say something random!");
27
- const res = await chat.invoke([message]);
28
- console.log(JSON.stringify(res));
29
- const res2 = await chat.invoke([message]);
30
- expect(res).toEqual(res2);
31
- });
32
- test("Test ChatOpenAI tool calling", async () => {
33
- const chat = new ChatOpenAI({
34
- modelName: "gpt-3.5-turbo-1106",
35
- maxTokens: 128,
36
- }).bind({
37
- tools: [
38
- {
39
- type: "function",
40
- function: {
41
- name: "get_current_weather",
42
- description: "Get the current weather in a given location",
43
- parameters: {
44
- type: "object",
45
- properties: {
46
- location: {
47
- type: "string",
48
- description: "The city and state, e.g. San Francisco, CA",
49
- },
50
- unit: { type: "string", enum: ["celsius", "fahrenheit"] },
51
- },
52
- required: ["location"],
53
- },
54
- },
55
- },
56
- ],
57
- tool_choice: "auto",
58
- });
59
- const res = await chat.invoke([
60
- ["human", "What's the weather like in San Francisco, Tokyo, and Paris?"],
61
- ]);
62
- console.log(JSON.stringify(res));
63
- expect(res.additional_kwargs.tool_calls?.length).toEqual(3);
64
- expect(res.tool_calls?.[0].args).toEqual(JSON.parse(res.additional_kwargs.tool_calls?.[0].function.arguments ?? "{}"));
65
- expect(res.tool_calls?.[1].args).toEqual(JSON.parse(res.additional_kwargs.tool_calls?.[1].function.arguments ?? "{}"));
66
- expect(res.tool_calls?.[2].args).toEqual(JSON.parse(res.additional_kwargs.tool_calls?.[2].function.arguments ?? "{}"));
67
- });
68
- test("Test ChatOpenAI streaming logprobs", async () => {
69
- const model = new ChatOpenAI({
70
- maxTokens: 50,
71
- modelName: "gpt-3.5-turbo",
72
- streaming: true,
73
- logprobs: true,
74
- });
75
- const res = await model.invoke("Print hello world.");
76
- console.log(res.response_metadata.logprobs.content);
77
- expect(res.response_metadata.logprobs.content.length).toBeGreaterThan(0);
78
- });
79
- test("Test ChatOpenAI tool calling with ToolMessages", async () => {
80
- function getCurrentWeather(location) {
81
- if (location.toLowerCase().includes("tokyo")) {
82
- return JSON.stringify({ location, temperature: "10", unit: "celsius" });
83
- }
84
- else if (location.toLowerCase().includes("san francisco")) {
85
- return JSON.stringify({
86
- location,
87
- temperature: "72",
88
- unit: "fahrenheit",
89
- });
90
- }
91
- else {
92
- return JSON.stringify({ location, temperature: "22", unit: "celsius" });
93
- }
94
- }
95
- const chat = new ChatOpenAI({
96
- modelName: "gpt-3.5-turbo-1106",
97
- maxTokens: 128,
98
- }).bind({
99
- tools: [
100
- {
101
- type: "function",
102
- function: {
103
- name: "get_current_weather",
104
- description: "Get the current weather in a given location",
105
- parameters: {
106
- type: "object",
107
- properties: {
108
- location: {
109
- type: "string",
110
- description: "The city and state, e.g. San Francisco, CA",
111
- },
112
- unit: { type: "string", enum: ["celsius", "fahrenheit"] },
113
- },
114
- required: ["location"],
115
- },
116
- },
117
- },
118
- ],
119
- tool_choice: "auto",
120
- });
121
- const res = await chat.invoke([
122
- ["human", "What's the weather like in San Francisco, Tokyo, and Paris?"],
123
- ]);
124
- console.log(JSON.stringify(res));
125
- expect(res.additional_kwargs.tool_calls?.length).toBeGreaterThan(1);
126
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
127
- const toolMessages = res.additional_kwargs.tool_calls.map((toolCall) => new ToolMessage({
128
- tool_call_id: toolCall.id,
129
- name: toolCall.function.name,
130
- content: getCurrentWeather(JSON.parse(toolCall.function.arguments).location),
131
- }));
132
- const finalResponse = await chat.invoke([
133
- ["human", "What's the weather like in San Francisco, Tokyo, and Paris?"],
134
- res,
135
- ...toolMessages,
136
- ]);
137
- console.log(finalResponse);
138
- });
139
- test("Test ChatOpenAI tool calling with streaming", async () => {
140
- const chat = new ChatOpenAI({
141
- modelName: "gpt-3.5-turbo-1106",
142
- maxTokens: 256,
143
- }).bind({
144
- tools: [
145
- {
146
- type: "function",
147
- function: {
148
- name: "get_current_weather",
149
- description: "Get the current weather in a given location",
150
- parameters: {
151
- type: "object",
152
- properties: {
153
- location: {
154
- type: "string",
155
- description: "The city and state, e.g. San Francisco, CA",
156
- },
157
- unit: { type: "string", enum: ["celsius", "fahrenheit"] },
158
- },
159
- required: ["location"],
160
- },
161
- },
162
- },
163
- ],
164
- tool_choice: "auto",
165
- });
166
- const stream = await chat.stream([
167
- ["human", "What's the weather like in San Francisco, Tokyo, and Paris?"],
168
- ]);
169
- let finalChunk;
170
- const chunks = [];
171
- for await (const chunk of stream) {
172
- console.log(chunk.additional_kwargs.tool_calls);
173
- chunks.push(chunk);
174
- if (!finalChunk) {
175
- finalChunk = chunk;
176
- }
177
- else {
178
- finalChunk = finalChunk.concat(chunk);
179
- }
180
- }
181
- expect(chunks.length).toBeGreaterThan(1);
182
- console.log(finalChunk?.additional_kwargs.tool_calls);
183
- expect(finalChunk?.additional_kwargs.tool_calls?.length).toBeGreaterThan(1);
184
- });
185
- test("ChatOpenAI in JSON mode can cache generations", async () => {
186
- const memoryCache = new InMemoryCache();
187
- const lookupSpy = jest.spyOn(memoryCache, "lookup");
188
- const updateSpy = jest.spyOn(memoryCache, "update");
189
- const chat = new ChatOpenAI({
190
- modelName: "gpt-3.5-turbo-1106",
191
- temperature: 1,
192
- cache: memoryCache,
193
- }).bind({
194
- response_format: {
195
- type: "json_object",
196
- },
197
- });
198
- const message = new HumanMessage("Respond with a JSON object containing arbitrary fields.");
199
- const res = await chat.invoke([message]);
200
- console.log(res);
201
- const res2 = await chat.invoke([message]);
202
- console.log(res2);
203
- expect(res).toEqual(res2);
204
- expect(lookupSpy).toHaveBeenCalledTimes(2);
205
- expect(updateSpy).toHaveBeenCalledTimes(1);
206
- lookupSpy.mockRestore();
207
- updateSpy.mockRestore();
208
- });
209
- test("Few shotting with tool calls", async () => {
210
- const chat = new ChatOpenAI({
211
- modelName: "gpt-3.5-turbo-1106",
212
- temperature: 1,
213
- }).bind({
214
- tools: [
215
- {
216
- type: "function",
217
- function: {
218
- name: "get_current_weather",
219
- description: "Get the current weather in a given location",
220
- parameters: {
221
- type: "object",
222
- properties: {
223
- location: {
224
- type: "string",
225
- description: "The city and state, e.g. San Francisco, CA",
226
- },
227
- unit: { type: "string", enum: ["celsius", "fahrenheit"] },
228
- },
229
- required: ["location"],
230
- },
231
- },
232
- },
233
- ],
234
- tool_choice: "auto",
235
- });
236
- const res = await chat.invoke([
237
- new HumanMessage("What is the weather in SF?"),
238
- new AIMessage({
239
- content: "",
240
- tool_calls: [
241
- {
242
- id: "12345",
243
- name: "get_current_weather",
244
- args: {
245
- location: "SF",
246
- },
247
- },
248
- ],
249
- }),
250
- new ToolMessage({
251
- tool_call_id: "12345",
252
- content: "It is currently 24 degrees with hail in SF.",
253
- }),
254
- new AIMessage("It is currently 24 degrees in SF with hail in SF."),
255
- new HumanMessage("What did you say the weather was?"),
256
- ]);
257
- console.log(res);
258
- expect(res.content).toContain("24");
259
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,51 +0,0 @@
1
- import { test } from "@jest/globals";
2
- import { HumanMessage } from "@langchain/core/messages";
3
- import * as fs from "node:fs/promises";
4
- import { fileURLToPath } from "node:url";
5
- import * as path from "node:path";
6
- import { ChatOpenAI } from "../chat_models.js";
7
- test("Test ChatOpenAI with a file", async () => {
8
- const __filename = fileURLToPath(import.meta.url);
9
- const __dirname = path.dirname(__filename);
10
- const imageData = await fs.readFile(path.join(__dirname, "/data/hotdog.jpg"));
11
- const chat = new ChatOpenAI({
12
- modelName: "gpt-4-vision-preview",
13
- maxTokens: 1024,
14
- });
15
- const message = new HumanMessage({
16
- content: [
17
- {
18
- type: "text",
19
- text: "What's in this image?",
20
- },
21
- {
22
- type: "image_url",
23
- image_url: {
24
- url: `data:image/jpeg;base64,${imageData.toString("base64")}`,
25
- },
26
- },
27
- ],
28
- });
29
- const res = await chat.invoke([message]);
30
- console.log({ res });
31
- });
32
- test("Test ChatOpenAI with a URL", async () => {
33
- const chat = new ChatOpenAI({
34
- modelName: "gpt-4-vision-preview",
35
- maxTokens: 1024,
36
- });
37
- const message = new HumanMessage({
38
- content: [
39
- {
40
- type: "text",
41
- text: "What does this image say?",
42
- },
43
- {
44
- type: "image_url",
45
- image_url: "https://www.freecodecamp.org/news/content/images/2023/05/Screenshot-2023-05-29-at-5.40.38-PM.png",
46
- },
47
- ],
48
- });
49
- const res = await chat.invoke([message]);
50
- console.log({ res });
51
- });
@@ -1 +0,0 @@
1
- export {};