@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,659 +0,0 @@
1
- import { test, jest, expect } from "@jest/globals";
2
- import { ChatMessage, HumanMessage, SystemMessage, } from "@langchain/core/messages";
3
- import { ChatPromptValue } from "@langchain/core/prompt_values";
4
- import { PromptTemplate, ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate, } from "@langchain/core/prompts";
5
- import { CallbackManager } from "@langchain/core/callbacks/manager";
6
- import { InMemoryCache } from "@langchain/core/caches";
7
- import { getEnvironmentVariable } from "@langchain/core/utils/env";
8
- import { ClientSecretCredential, getBearerTokenProvider, } from "@azure/identity";
9
- import { AzureChatOpenAI } from "../../azure/chat_models.js";
10
- test("Test Azure ChatOpenAI call method", async () => {
11
- const chat = new AzureChatOpenAI({
12
- modelName: "gpt-3.5-turbo",
13
- maxTokens: 10,
14
- });
15
- const message = new HumanMessage("Hello!");
16
- const res = await chat.call([message]);
17
- console.log({ res });
18
- });
19
- test("Test Azure ChatOpenAI with SystemChatMessage", async () => {
20
- const chat = new AzureChatOpenAI({
21
- modelName: "gpt-3.5-turbo",
22
- maxTokens: 10,
23
- });
24
- const system_message = new SystemMessage("You are to chat with a user.");
25
- const message = new HumanMessage("Hello!");
26
- const res = await chat.call([system_message, message]);
27
- console.log({ res });
28
- });
29
- test("Test Azure ChatOpenAI Generate", async () => {
30
- const chat = new AzureChatOpenAI({
31
- modelName: "gpt-3.5-turbo",
32
- maxTokens: 10,
33
- n: 2,
34
- });
35
- const message = new HumanMessage("Hello!");
36
- const res = await chat.generate([[message], [message]]);
37
- expect(res.generations.length).toBe(2);
38
- for (const generation of res.generations) {
39
- expect(generation.length).toBe(2);
40
- for (const message of generation) {
41
- console.log(message.text);
42
- expect(typeof message.text).toBe("string");
43
- }
44
- }
45
- console.log({ res });
46
- });
47
- test("Test Azure ChatOpenAI Generate throws when one of the calls fails", async () => {
48
- const chat = new AzureChatOpenAI({
49
- modelName: "gpt-3.5-turbo",
50
- maxTokens: 10,
51
- n: 2,
52
- });
53
- const message = new HumanMessage("Hello!");
54
- await expect(() => chat.generate([[message], [message]], {
55
- signal: AbortSignal.timeout(10),
56
- })).rejects.toThrow();
57
- });
58
- test("Test Azure ChatOpenAI tokenUsage", async () => {
59
- let tokenUsage = {
60
- completionTokens: 0,
61
- promptTokens: 0,
62
- totalTokens: 0,
63
- };
64
- const model = new AzureChatOpenAI({
65
- modelName: "gpt-3.5-turbo",
66
- maxTokens: 10,
67
- callbackManager: CallbackManager.fromHandlers({
68
- async handleLLMEnd(output) {
69
- console.log(output);
70
- tokenUsage = output.llmOutput?.tokenUsage;
71
- },
72
- }),
73
- });
74
- const message = new HumanMessage("Hello");
75
- const res = await model.invoke([message]);
76
- console.log({ res });
77
- expect(tokenUsage.promptTokens).toBeGreaterThan(0);
78
- });
79
- test("Test Azure ChatOpenAI tokenUsage with a batch", async () => {
80
- let tokenUsage = {
81
- completionTokens: 0,
82
- promptTokens: 0,
83
- totalTokens: 0,
84
- };
85
- const model = new AzureChatOpenAI({
86
- temperature: 0,
87
- modelName: "gpt-3.5-turbo",
88
- callbackManager: CallbackManager.fromHandlers({
89
- async handleLLMEnd(output) {
90
- tokenUsage = output.llmOutput?.tokenUsage;
91
- },
92
- }),
93
- });
94
- const res = await model.generate([
95
- [new HumanMessage("Hello")],
96
- [new HumanMessage("Hi")],
97
- ]);
98
- console.log(res);
99
- expect(tokenUsage.promptTokens).toBeGreaterThan(0);
100
- });
101
- test("Test Azure ChatOpenAI in streaming mode", async () => {
102
- let nrNewTokens = 0;
103
- let streamedCompletion = "";
104
- const model = new AzureChatOpenAI({
105
- modelName: "gpt-3.5-turbo",
106
- streaming: true,
107
- maxTokens: 10,
108
- callbacks: [
109
- {
110
- async handleLLMNewToken(token) {
111
- nrNewTokens += 1;
112
- streamedCompletion += token;
113
- },
114
- },
115
- ],
116
- });
117
- const message = new HumanMessage("Hello!");
118
- const result = await model.invoke([message]);
119
- expect(nrNewTokens > 0).toBe(true);
120
- expect(result.content).toBe(streamedCompletion);
121
- }, 10000);
122
- test("Test Azure ChatOpenAI in streaming mode with n > 1 and multiple prompts", async () => {
123
- let nrNewTokens = 0;
124
- const streamedCompletions = [
125
- ["", ""],
126
- ["", ""],
127
- ];
128
- const model = new AzureChatOpenAI({
129
- modelName: "gpt-3.5-turbo",
130
- streaming: true,
131
- maxTokens: 10,
132
- n: 2,
133
- callbacks: [
134
- {
135
- async handleLLMNewToken(token, idx) {
136
- nrNewTokens += 1;
137
- streamedCompletions[idx.prompt][idx.completion] += token;
138
- },
139
- },
140
- ],
141
- });
142
- const message1 = new HumanMessage("Hello!");
143
- const message2 = new HumanMessage("Bye!");
144
- const result = await model.generate([[message1], [message2]]);
145
- expect(nrNewTokens > 0).toBe(true);
146
- expect(result.generations.map((g) => g.map((gg) => gg.text))).toEqual(streamedCompletions);
147
- }, 10000);
148
- test("Test Azure ChatOpenAI prompt value", async () => {
149
- const chat = new AzureChatOpenAI({
150
- modelName: "gpt-3.5-turbo",
151
- maxTokens: 10,
152
- n: 2,
153
- });
154
- const message = new HumanMessage("Hello!");
155
- const res = await chat.generatePrompt([new ChatPromptValue([message])]);
156
- expect(res.generations.length).toBe(1);
157
- for (const generation of res.generations) {
158
- expect(generation.length).toBe(2);
159
- for (const g of generation) {
160
- console.log(g.text);
161
- }
162
- }
163
- console.log({ res });
164
- });
165
- test("Test Azure OpenAI Chat, docs, prompt templates", async () => {
166
- const chat = new AzureChatOpenAI({ temperature: 0, maxTokens: 10 });
167
- const systemPrompt = PromptTemplate.fromTemplate("You are a helpful assistant that translates {input_language} to {output_language}.");
168
- const chatPrompt = ChatPromptTemplate.fromMessages([
169
- new SystemMessagePromptTemplate(systemPrompt),
170
- HumanMessagePromptTemplate.fromTemplate("{text}"),
171
- ]);
172
- const responseA = await chat.generatePrompt([
173
- await chatPrompt.formatPromptValue({
174
- input_language: "English",
175
- output_language: "French",
176
- text: "I love programming.",
177
- }),
178
- ]);
179
- console.log(responseA.generations);
180
- }, 5000);
181
- test("Test Azure ChatOpenAI with stop", async () => {
182
- const model = new AzureChatOpenAI({ maxTokens: 5 });
183
- const res = await model.call([new HumanMessage("Print hello world")], ["world"]);
184
- console.log({ res });
185
- });
186
- test("Test Azure ChatOpenAI with stop in object", async () => {
187
- const model = new AzureChatOpenAI({ maxTokens: 5 });
188
- const res = await model.invoke([new HumanMessage("Print hello world")], {
189
- stop: ["world"],
190
- });
191
- console.log({ res });
192
- });
193
- test("Test Azure ChatOpenAI with timeout in call options", async () => {
194
- const model = new AzureChatOpenAI({ maxTokens: 5 });
195
- await expect(() => model.invoke([new HumanMessage("Print hello world")], { timeout: 10 })).rejects.toThrow();
196
- }, 5000);
197
- test("Test Azure ChatOpenAI with timeout in call options and node adapter", async () => {
198
- const model = new AzureChatOpenAI({ maxTokens: 5 });
199
- await expect(() => model.invoke([new HumanMessage("Print hello world")], { timeout: 10 })).rejects.toThrow();
200
- }, 5000);
201
- test("Test Azure ChatOpenAI with signal in call options", async () => {
202
- const model = new AzureChatOpenAI({ maxTokens: 5 });
203
- const controller = new AbortController();
204
- await expect(() => {
205
- const ret = model.invoke([new HumanMessage("Print hello world")], {
206
- signal: controller.signal,
207
- });
208
- controller.abort();
209
- return ret;
210
- }).rejects.toThrow();
211
- }, 5000);
212
- test("Test Azure ChatOpenAI with signal in call options and node adapter", async () => {
213
- const model = new AzureChatOpenAI({
214
- maxTokens: 5,
215
- modelName: "gpt-3.5-turbo-instruct",
216
- });
217
- const controller = new AbortController();
218
- await expect(() => {
219
- const ret = model.invoke([new HumanMessage("Print hello world")], {
220
- signal: controller.signal,
221
- });
222
- controller.abort();
223
- return ret;
224
- }).rejects.toThrow();
225
- }, 5000);
226
- test("Test Azure ChatOpenAI with specific roles in ChatMessage", async () => {
227
- const chat = new AzureChatOpenAI({
228
- modelName: "gpt-3.5-turbo",
229
- maxTokens: 10,
230
- });
231
- const system_message = new ChatMessage("You are to chat with a user.", "system");
232
- const user_message = new ChatMessage("Hello!", "user");
233
- const res = await chat.call([system_message, user_message]);
234
- console.log({ res });
235
- });
236
- test("Test Azure ChatOpenAI stream method", async () => {
237
- const model = new AzureChatOpenAI({
238
- maxTokens: 50,
239
- modelName: "gpt-3.5-turbo",
240
- });
241
- const stream = await model.stream("Print hello world.");
242
- const chunks = [];
243
- for await (const chunk of stream) {
244
- console.log(chunk);
245
- chunks.push(chunk);
246
- }
247
- expect(chunks.length).toBeGreaterThan(1);
248
- });
249
- test("Test Azure ChatOpenAI stream method with abort", async () => {
250
- await expect(async () => {
251
- const model = new AzureChatOpenAI({
252
- maxTokens: 100,
253
- modelName: "gpt-3.5-turbo",
254
- });
255
- const stream = await model.stream("How is your day going? Be extremely verbose.", {
256
- signal: AbortSignal.timeout(500),
257
- });
258
- for await (const chunk of stream) {
259
- console.log(chunk);
260
- }
261
- }).rejects.toThrow();
262
- });
263
- test("Test Azure ChatOpenAI stream method with early break", async () => {
264
- const model = new AzureChatOpenAI({
265
- maxTokens: 50,
266
- modelName: "gpt-3.5-turbo",
267
- });
268
- const stream = await model.stream("How is your day going? Be extremely verbose.");
269
- let i = 0;
270
- for await (const chunk of stream) {
271
- console.log(chunk);
272
- i += 1;
273
- if (i > 10) {
274
- break;
275
- }
276
- }
277
- });
278
- test("Test Azure ChatOpenAI stream method, timeout error thrown from SDK", async () => {
279
- await expect(async () => {
280
- const model = new AzureChatOpenAI({
281
- maxTokens: 50,
282
- modelName: "gpt-3.5-turbo",
283
- timeout: 1,
284
- });
285
- const stream = await model.stream("How is your day going? Be extremely verbose.");
286
- for await (const chunk of stream) {
287
- console.log(chunk);
288
- }
289
- }).rejects.toThrow();
290
- });
291
- test("Test Azure ChatOpenAI Function calling with streaming", async () => {
292
- let finalResult;
293
- const modelForFunctionCalling = new AzureChatOpenAI({
294
- modelName: "gpt-3.5-turbo",
295
- temperature: 0,
296
- callbacks: [
297
- {
298
- handleLLMEnd(output) {
299
- finalResult = output.generations[0][0].message;
300
- },
301
- },
302
- ],
303
- });
304
- const stream = await modelForFunctionCalling.stream("What is the weather in New York?", {
305
- functions: [
306
- {
307
- name: "get_current_weather",
308
- description: "Get the current weather in a given location",
309
- parameters: {
310
- type: "object",
311
- properties: {
312
- location: {
313
- type: "string",
314
- description: "The city and state, e.g. San Francisco, CA",
315
- },
316
- unit: { type: "string", enum: ["celsius", "fahrenheit"] },
317
- },
318
- required: ["location"],
319
- },
320
- },
321
- ],
322
- function_call: {
323
- name: "get_current_weather",
324
- },
325
- });
326
- const chunks = [];
327
- let streamedOutput;
328
- for await (const chunk of stream) {
329
- chunks.push(chunk);
330
- if (!streamedOutput) {
331
- streamedOutput = chunk;
332
- }
333
- else if (chunk) {
334
- streamedOutput = streamedOutput.concat(chunk);
335
- }
336
- }
337
- expect(finalResult).toEqual(streamedOutput);
338
- expect(chunks.length).toBeGreaterThan(1);
339
- expect(finalResult?.additional_kwargs?.function_call?.name).toBe("get_current_weather");
340
- console.log(JSON.parse(finalResult?.additional_kwargs?.function_call?.arguments ?? "")
341
- .location);
342
- });
343
- test("Test Azure ChatOpenAI can cache generations", async () => {
344
- const memoryCache = new InMemoryCache();
345
- const lookupSpy = jest.spyOn(memoryCache, "lookup");
346
- const updateSpy = jest.spyOn(memoryCache, "update");
347
- const chat = new AzureChatOpenAI({
348
- modelName: "gpt-3.5-turbo",
349
- maxTokens: 10,
350
- n: 2,
351
- cache: memoryCache,
352
- });
353
- const message = new HumanMessage("Hello");
354
- const res = await chat.generate([[message], [message]]);
355
- expect(res.generations.length).toBe(2);
356
- expect(lookupSpy).toHaveBeenCalledTimes(2);
357
- expect(updateSpy).toHaveBeenCalledTimes(2);
358
- lookupSpy.mockRestore();
359
- updateSpy.mockRestore();
360
- });
361
- test("Test Azure ChatOpenAI can write and read cached generations", async () => {
362
- const memoryCache = new InMemoryCache();
363
- const lookupSpy = jest.spyOn(memoryCache, "lookup");
364
- const updateSpy = jest.spyOn(memoryCache, "update");
365
- const chat = new AzureChatOpenAI({
366
- modelName: "gpt-3.5-turbo",
367
- maxTokens: 100,
368
- n: 1,
369
- cache: memoryCache,
370
- });
371
- const generateUncachedSpy = jest.spyOn(chat, "_generateUncached");
372
- const messages = [
373
- [
374
- new HumanMessage("what color is the sky?"),
375
- new HumanMessage("what color is the ocean?"),
376
- ],
377
- [new HumanMessage("hello")],
378
- ];
379
- const response1 = await chat.generate(messages);
380
- expect(generateUncachedSpy).toHaveBeenCalledTimes(1);
381
- generateUncachedSpy.mockRestore();
382
- const response2 = await chat.generate(messages);
383
- expect(generateUncachedSpy).toHaveBeenCalledTimes(0); // Request should be cached, no need to generate.
384
- generateUncachedSpy.mockRestore();
385
- expect(response1.generations.length).toBe(2);
386
- expect(response2.generations).toEqual(response1.generations);
387
- expect(lookupSpy).toHaveBeenCalledTimes(4);
388
- expect(updateSpy).toHaveBeenCalledTimes(2);
389
- lookupSpy.mockRestore();
390
- updateSpy.mockRestore();
391
- });
392
- test("Test Azure ChatOpenAI should not reuse cache if function call args have changed", async () => {
393
- const memoryCache = new InMemoryCache();
394
- const lookupSpy = jest.spyOn(memoryCache, "lookup");
395
- const updateSpy = jest.spyOn(memoryCache, "update");
396
- const chat = new AzureChatOpenAI({
397
- modelName: "gpt-3.5-turbo",
398
- maxTokens: 100,
399
- n: 1,
400
- cache: memoryCache,
401
- });
402
- const generateUncachedSpy = jest.spyOn(chat, "_generateUncached");
403
- const messages = [
404
- [
405
- new HumanMessage("what color is the sky?"),
406
- new HumanMessage("what color is the ocean?"),
407
- ],
408
- [new HumanMessage("hello")],
409
- ];
410
- const response1 = await chat.generate(messages);
411
- expect(generateUncachedSpy).toHaveBeenCalledTimes(1);
412
- generateUncachedSpy.mockRestore();
413
- const response2 = await chat.generate(messages, {
414
- functions: [
415
- {
416
- name: "extractor",
417
- description: "Extract fields from the input",
418
- parameters: {
419
- type: "object",
420
- properties: {
421
- tone: {
422
- type: "string",
423
- description: "the tone of the input",
424
- },
425
- },
426
- required: ["tone"],
427
- },
428
- },
429
- ],
430
- function_call: {
431
- name: "extractor",
432
- },
433
- });
434
- expect(generateUncachedSpy).toHaveBeenCalledTimes(0); // Request should not be cached since it's being called with different function call args
435
- expect(response1.generations.length).toBe(2);
436
- expect(response2.generations[0][0].message.additional_kwargs
437
- .function_call?.name ?? "").toEqual("extractor");
438
- const response3 = await chat.generate(messages, {
439
- functions: [
440
- {
441
- name: "extractor",
442
- description: "Extract fields from the input",
443
- parameters: {
444
- type: "object",
445
- properties: {
446
- tone: {
447
- type: "string",
448
- description: "the tone of the input",
449
- },
450
- },
451
- required: ["tone"],
452
- },
453
- },
454
- ],
455
- function_call: {
456
- name: "extractor",
457
- },
458
- });
459
- expect(response2.generations).toEqual(response3.generations);
460
- expect(lookupSpy).toHaveBeenCalledTimes(6);
461
- expect(updateSpy).toHaveBeenCalledTimes(4);
462
- lookupSpy.mockRestore();
463
- updateSpy.mockRestore();
464
- });
465
- function createSampleMessages() {
466
- // same example as in https://github.com/openai/openai-cookbook/blob/main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb
467
- return [
468
- createSystemChatMessage("You are a helpful, pattern-following assistant that translates corporate jargon into plain English."),
469
- createSystemChatMessage("New synergies will help drive top-line growth.", "example_user"),
470
- createSystemChatMessage("Things working well together will increase revenue.", "example_assistant"),
471
- createSystemChatMessage("Let's circle back when we have more bandwidth to touch base on opportunities for increased leverage.", "example_user"),
472
- createSystemChatMessage("Let's talk later when we're less busy about how to do better.", "example_assistant"),
473
- new HumanMessage("This late pivot means we don't have time to boil the ocean for the client deliverable."),
474
- ];
475
- }
476
- function createSystemChatMessage(text, name) {
477
- const msg = new SystemMessage(text);
478
- msg.name = name;
479
- return msg;
480
- }
481
- test("Test Azure ChatOpenAI getNumTokensFromMessages gpt-3.5-turbo-0301 model for sample input", async () => {
482
- const messages = createSampleMessages();
483
- const chat = new AzureChatOpenAI({
484
- azureOpenAIApiKey: "dummy",
485
- modelName: "gpt-3.5-turbo-0301",
486
- });
487
- const { totalCount } = await chat.getNumTokensFromMessages(messages);
488
- expect(totalCount).toBe(127);
489
- });
490
- test("Test Azure ChatOpenAI getNumTokensFromMessages gpt-4-0314 model for sample input", async () => {
491
- const messages = createSampleMessages();
492
- const chat = new AzureChatOpenAI({
493
- azureOpenAIApiKey: "dummy",
494
- modelName: "gpt-4-0314",
495
- });
496
- const { totalCount } = await chat.getNumTokensFromMessages(messages);
497
- expect(totalCount).toBe(129);
498
- });
499
- test("Test Azure ChatOpenAI token usage reporting for streaming function calls", async () => {
500
- let streamingTokenUsed = -1;
501
- let nonStreamingTokenUsed = -1;
502
- const humanMessage = "What a beautiful day!";
503
- const extractionFunctionSchema = {
504
- name: "extractor",
505
- description: "Extracts fields from the input.",
506
- parameters: {
507
- type: "object",
508
- properties: {
509
- tone: {
510
- type: "string",
511
- enum: ["positive", "negative"],
512
- description: "The overall tone of the input",
513
- },
514
- word_count: {
515
- type: "number",
516
- description: "The number of words in the input",
517
- },
518
- chat_response: {
519
- type: "string",
520
- description: "A response to the human's input",
521
- },
522
- },
523
- required: ["tone", "word_count", "chat_response"],
524
- },
525
- };
526
- const streamingModel = new AzureChatOpenAI({
527
- modelName: "gpt-3.5-turbo",
528
- streaming: true,
529
- maxRetries: 10,
530
- maxConcurrency: 10,
531
- temperature: 0,
532
- topP: 0,
533
- callbacks: [
534
- {
535
- handleLLMEnd: async (output) => {
536
- streamingTokenUsed =
537
- output.llmOutput?.estimatedTokenUsage?.totalTokens;
538
- console.log("streaming usage", output.llmOutput?.estimatedTokenUsage);
539
- },
540
- handleLLMError: async (err) => {
541
- console.error(err);
542
- },
543
- },
544
- ],
545
- }).bind({
546
- seed: 42,
547
- functions: [extractionFunctionSchema],
548
- function_call: { name: "extractor" },
549
- });
550
- const nonStreamingModel = new AzureChatOpenAI({
551
- modelName: "gpt-3.5-turbo",
552
- streaming: false,
553
- maxRetries: 10,
554
- maxConcurrency: 10,
555
- temperature: 0,
556
- topP: 0,
557
- callbacks: [
558
- {
559
- handleLLMEnd: async (output) => {
560
- nonStreamingTokenUsed = output.llmOutput?.tokenUsage?.totalTokens;
561
- console.log("non-streaming usage", output.llmOutput?.tokenUsage);
562
- },
563
- handleLLMError: async (err) => {
564
- console.error(err);
565
- },
566
- },
567
- ],
568
- }).bind({
569
- functions: [extractionFunctionSchema],
570
- function_call: { name: "extractor" },
571
- });
572
- const [nonStreamingResult, streamingResult] = await Promise.all([
573
- nonStreamingModel.invoke([new HumanMessage(humanMessage)]),
574
- streamingModel.invoke([new HumanMessage(humanMessage)]),
575
- ]);
576
- if (nonStreamingResult.additional_kwargs.function_call?.arguments &&
577
- streamingResult.additional_kwargs.function_call?.arguments) {
578
- console.log(`Function Call: ${JSON.stringify(nonStreamingResult.additional_kwargs.function_call)}`);
579
- const nonStreamingArguments = JSON.stringify(JSON.parse(nonStreamingResult.additional_kwargs.function_call.arguments));
580
- const streamingArguments = JSON.stringify(JSON.parse(streamingResult.additional_kwargs.function_call.arguments));
581
- if (nonStreamingArguments === streamingArguments) {
582
- expect(streamingTokenUsed).toEqual(nonStreamingTokenUsed);
583
- }
584
- }
585
- expect(streamingTokenUsed).toBeGreaterThan(-1);
586
- });
587
- test("Test Azure ChatOpenAI token usage reporting for streaming calls", async () => {
588
- let streamingTokenUsed = -1;
589
- let nonStreamingTokenUsed = -1;
590
- const systemPrompt = "You are a helpful assistant";
591
- const question = "What is the color of the night sky?";
592
- const streamingModel = new AzureChatOpenAI({
593
- modelName: "gpt-3.5-turbo",
594
- streaming: true,
595
- maxRetries: 10,
596
- maxConcurrency: 10,
597
- temperature: 0,
598
- topP: 0,
599
- callbacks: [
600
- {
601
- handleLLMEnd: async (output) => {
602
- streamingTokenUsed =
603
- output.llmOutput?.estimatedTokenUsage?.totalTokens;
604
- console.log("streaming usage", output.llmOutput?.estimatedTokenUsage);
605
- },
606
- handleLLMError: async (err) => {
607
- console.error(err);
608
- },
609
- },
610
- ],
611
- });
612
- const nonStreamingModel = new AzureChatOpenAI({
613
- modelName: "gpt-3.5-turbo",
614
- streaming: false,
615
- maxRetries: 10,
616
- maxConcurrency: 10,
617
- temperature: 0,
618
- topP: 0,
619
- callbacks: [
620
- {
621
- handleLLMEnd: async (output) => {
622
- nonStreamingTokenUsed = output.llmOutput?.tokenUsage?.totalTokens;
623
- console.log("non-streaming usage", output.llmOutput?.estimated);
624
- },
625
- handleLLMError: async (err) => {
626
- console.error(err);
627
- },
628
- },
629
- ],
630
- });
631
- const [nonStreamingResult, streamingResult] = await Promise.all([
632
- nonStreamingModel.generate([
633
- [new SystemMessage(systemPrompt), new HumanMessage(question)],
634
- ]),
635
- streamingModel.generate([
636
- [new SystemMessage(systemPrompt), new HumanMessage(question)],
637
- ]),
638
- ]);
639
- expect(streamingTokenUsed).toBeGreaterThan(-1);
640
- if (nonStreamingResult.generations[0][0].text ===
641
- streamingResult.generations[0][0].text) {
642
- expect(streamingTokenUsed).toEqual(nonStreamingTokenUsed);
643
- }
644
- });
645
- test("Test Azure ChatOpenAI with bearer token provider", async () => {
646
- const tenantId = getEnvironmentVariable("AZURE_TENANT_ID") ?? "";
647
- const clientId = getEnvironmentVariable("AZURE_CLIENT_ID") ?? "";
648
- const clientSecret = getEnvironmentVariable("AZURE_CLIENT_SECRET") ?? "";
649
- const credentials = new ClientSecretCredential(tenantId, clientId, clientSecret);
650
- const azureADTokenProvider = getBearerTokenProvider(credentials, "https://cognitiveservices.azure.com/.default");
651
- const chat = new AzureChatOpenAI({
652
- modelName: "gpt-3.5-turbo",
653
- maxTokens: 5,
654
- azureADTokenProvider,
655
- });
656
- const message = new HumanMessage("Hello!");
657
- const res = await chat.invoke([["system", "Say hi"], message]);
658
- console.log(res);
659
- });
@@ -1 +0,0 @@
1
- export {};