|
5 | 5 | convertReadableStreamToArray,
|
6 | 6 | } from '@ai-sdk/provider-utils/test';
|
7 | 7 | import { createOpenAICompatible } from './openai-compatible-provider';
|
| 8 | +import { OpenAICompatibleChatLanguageModel } from './openai-compatible-chat-language-model'; |
8 | 9 |
|
9 | 10 | const TEST_PROMPT: LanguageModelV1Prompt = [
|
10 | 11 | { role: 'user', content: [{ type: 'text', text: 'Hello' }] },
|
@@ -386,6 +387,364 @@ describe('doGenerate', () => {
|
386 | 387 | ]);
|
387 | 388 | });
|
388 | 389 |
|
| 390 | + describe('response format', () => { |
| 391 | + it('should not send a response_format when response format is text', async () => { |
| 392 | + prepareJsonResponse({ content: '{"value":"Spark"}' }); |
| 393 | + |
| 394 | + const model = new OpenAICompatibleChatLanguageModel( |
| 395 | + 'gpt-4o-2024-08-06', |
| 396 | + {}, |
| 397 | + { |
| 398 | + provider: 'test-provider', |
| 399 | + url: () => 'https://my.api.com/v1/chat/completions', |
| 400 | + headers: () => ({}), |
| 401 | + supportsStructuredOutputs: false, |
| 402 | + }, |
| 403 | + ); |
| 404 | + |
| 405 | + await model.doGenerate({ |
| 406 | + inputFormat: 'prompt', |
| 407 | + mode: { type: 'regular' }, |
| 408 | + prompt: TEST_PROMPT, |
| 409 | + responseFormat: { type: 'text' }, |
| 410 | + }); |
| 411 | + |
| 412 | + expect(await server.getRequestBodyJson()).toStrictEqual({ |
| 413 | + model: 'gpt-4o-2024-08-06', |
| 414 | + messages: [{ role: 'user', content: 'Hello' }], |
| 415 | + }); |
| 416 | + }); |
| 417 | + |
| 418 | + it('should forward json response format as "json_object" without schema', async () => { |
| 419 | + prepareJsonResponse({ content: '{"value":"Spark"}' }); |
| 420 | + |
| 421 | + const model = provider('gpt-4o-2024-08-06'); |
| 422 | + |
| 423 | + await model.doGenerate({ |
| 424 | + inputFormat: 'prompt', |
| 425 | + mode: { type: 'regular' }, |
| 426 | + prompt: TEST_PROMPT, |
| 427 | + responseFormat: { type: 'json' }, |
| 428 | + }); |
| 429 | + |
| 430 | + expect(await server.getRequestBodyJson()).toStrictEqual({ |
| 431 | + model: 'gpt-4o-2024-08-06', |
| 432 | + messages: [{ role: 'user', content: 'Hello' }], |
| 433 | + response_format: { type: 'json_object' }, |
| 434 | + }); |
| 435 | + }); |
| 436 | + |
| 437 | + it('should forward json response format as "json_object" and omit schema when structuredOutputs are disabled', async () => { |
| 438 | + prepareJsonResponse({ content: '{"value":"Spark"}' }); |
| 439 | + |
| 440 | + const model = new OpenAICompatibleChatLanguageModel( |
| 441 | + 'gpt-4o-2024-08-06', |
| 442 | + {}, |
| 443 | + { |
| 444 | + provider: 'test-provider', |
| 445 | + url: () => 'https://my.api.com/v1/chat/completions', |
| 446 | + headers: () => ({}), |
| 447 | + supportsStructuredOutputs: false, |
| 448 | + }, |
| 449 | + ); |
| 450 | + |
| 451 | + const { warnings } = await model.doGenerate({ |
| 452 | + inputFormat: 'prompt', |
| 453 | + mode: { type: 'regular' }, |
| 454 | + prompt: TEST_PROMPT, |
| 455 | + responseFormat: { |
| 456 | + type: 'json', |
| 457 | + schema: { |
| 458 | + type: 'object', |
| 459 | + properties: { value: { type: 'string' } }, |
| 460 | + required: ['value'], |
| 461 | + additionalProperties: false, |
| 462 | + $schema: 'http://json-schema.org/draft-07/schema#', |
| 463 | + }, |
| 464 | + }, |
| 465 | + }); |
| 466 | + |
| 467 | + expect(await server.getRequestBodyJson()).toStrictEqual({ |
| 468 | + model: 'gpt-4o-2024-08-06', |
| 469 | + messages: [{ role: 'user', content: 'Hello' }], |
| 470 | + response_format: { type: 'json_object' }, |
| 471 | + }); |
| 472 | + |
| 473 | + expect(warnings).toEqual([ |
| 474 | + { |
| 475 | + details: |
| 476 | + 'JSON response format schema is only supported with structuredOutputs', |
| 477 | + setting: 'responseFormat', |
| 478 | + type: 'unsupported-setting', |
| 479 | + }, |
| 480 | + ]); |
| 481 | + }); |
| 482 | + |
| 483 | + it('should forward json response format as "json_object" and include schema when structuredOutputs are enabled', async () => { |
| 484 | + prepareJsonResponse({ content: '{"value":"Spark"}' }); |
| 485 | + |
| 486 | + const model = new OpenAICompatibleChatLanguageModel( |
| 487 | + 'gpt-4o-2024-08-06', |
| 488 | + {}, |
| 489 | + { |
| 490 | + provider: 'test-provider', |
| 491 | + url: () => 'https://my.api.com/v1/chat/completions', |
| 492 | + headers: () => ({}), |
| 493 | + supportsStructuredOutputs: true, |
| 494 | + }, |
| 495 | + ); |
| 496 | + |
| 497 | + const { warnings } = await model.doGenerate({ |
| 498 | + inputFormat: 'prompt', |
| 499 | + mode: { type: 'regular' }, |
| 500 | + prompt: TEST_PROMPT, |
| 501 | + responseFormat: { |
| 502 | + type: 'json', |
| 503 | + schema: { |
| 504 | + type: 'object', |
| 505 | + properties: { value: { type: 'string' } }, |
| 506 | + required: ['value'], |
| 507 | + additionalProperties: false, |
| 508 | + $schema: 'http://json-schema.org/draft-07/schema#', |
| 509 | + }, |
| 510 | + }, |
| 511 | + }); |
| 512 | + |
| 513 | + expect(await server.getRequestBodyJson()).toStrictEqual({ |
| 514 | + model: 'gpt-4o-2024-08-06', |
| 515 | + messages: [{ role: 'user', content: 'Hello' }], |
| 516 | + response_format: { |
| 517 | + type: 'json_schema', |
| 518 | + json_schema: { |
| 519 | + name: 'response', |
| 520 | + strict: true, |
| 521 | + schema: { |
| 522 | + type: 'object', |
| 523 | + properties: { value: { type: 'string' } }, |
| 524 | + required: ['value'], |
| 525 | + additionalProperties: false, |
| 526 | + $schema: 'http://json-schema.org/draft-07/schema#', |
| 527 | + }, |
| 528 | + }, |
| 529 | + }, |
| 530 | + }); |
| 531 | + |
| 532 | + expect(warnings).toEqual([]); |
| 533 | + }); |
| 534 | + |
| 535 | + it('should use json_schema & strict in object-json mode when structuredOutputs are enabled', async () => { |
| 536 | + prepareJsonResponse({ content: '{"value":"Spark"}' }); |
| 537 | + |
| 538 | + const model = new OpenAICompatibleChatLanguageModel( |
| 539 | + 'gpt-4o-2024-08-06', |
| 540 | + {}, |
| 541 | + { |
| 542 | + provider: 'test-provider', |
| 543 | + url: () => 'https://my.api.com/v1/chat/completions', |
| 544 | + headers: () => ({}), |
| 545 | + supportsStructuredOutputs: true, |
| 546 | + }, |
| 547 | + ); |
| 548 | + |
| 549 | + await model.doGenerate({ |
| 550 | + inputFormat: 'prompt', |
| 551 | + mode: { |
| 552 | + type: 'object-json', |
| 553 | + schema: { |
| 554 | + type: 'object', |
| 555 | + properties: { value: { type: 'string' } }, |
| 556 | + required: ['value'], |
| 557 | + additionalProperties: false, |
| 558 | + $schema: 'http://json-schema.org/draft-07/schema#', |
| 559 | + }, |
| 560 | + }, |
| 561 | + prompt: TEST_PROMPT, |
| 562 | + }); |
| 563 | + |
| 564 | + expect(await server.getRequestBodyJson()).toStrictEqual({ |
| 565 | + model: 'gpt-4o-2024-08-06', |
| 566 | + messages: [{ role: 'user', content: 'Hello' }], |
| 567 | + response_format: { |
| 568 | + type: 'json_schema', |
| 569 | + json_schema: { |
| 570 | + name: 'response', |
| 571 | + strict: true, |
| 572 | + schema: { |
| 573 | + type: 'object', |
| 574 | + properties: { value: { type: 'string' } }, |
| 575 | + required: ['value'], |
| 576 | + additionalProperties: false, |
| 577 | + $schema: 'http://json-schema.org/draft-07/schema#', |
| 578 | + }, |
| 579 | + }, |
| 580 | + }, |
| 581 | + }); |
| 582 | + }); |
| 583 | + |
| 584 | + it('should set name & description in object-json mode when structuredOutputs are enabled', async () => { |
| 585 | + prepareJsonResponse({ content: '{"value":"Spark"}' }); |
| 586 | + |
| 587 | + const model = new OpenAICompatibleChatLanguageModel( |
| 588 | + 'gpt-4o-2024-08-06', |
| 589 | + {}, |
| 590 | + { |
| 591 | + provider: 'test-provider', |
| 592 | + url: () => 'https://my.api.com/v1/chat/completions', |
| 593 | + headers: () => ({}), |
| 594 | + supportsStructuredOutputs: true, |
| 595 | + }, |
| 596 | + ); |
| 597 | + |
| 598 | + await model.doGenerate({ |
| 599 | + inputFormat: 'prompt', |
| 600 | + mode: { |
| 601 | + type: 'object-json', |
| 602 | + name: 'test-name', |
| 603 | + description: 'test description', |
| 604 | + schema: { |
| 605 | + type: 'object', |
| 606 | + properties: { value: { type: 'string' } }, |
| 607 | + required: ['value'], |
| 608 | + additionalProperties: false, |
| 609 | + $schema: 'http://json-schema.org/draft-07/schema#', |
| 610 | + }, |
| 611 | + }, |
| 612 | + prompt: TEST_PROMPT, |
| 613 | + }); |
| 614 | + |
| 615 | + expect(await server.getRequestBodyJson()).toStrictEqual({ |
| 616 | + model: 'gpt-4o-2024-08-06', |
| 617 | + messages: [{ role: 'user', content: 'Hello' }], |
| 618 | + response_format: { |
| 619 | + type: 'json_schema', |
| 620 | + json_schema: { |
| 621 | + name: 'test-name', |
| 622 | + description: 'test description', |
| 623 | + strict: true, |
| 624 | + schema: { |
| 625 | + type: 'object', |
| 626 | + properties: { value: { type: 'string' } }, |
| 627 | + required: ['value'], |
| 628 | + additionalProperties: false, |
| 629 | + $schema: 'http://json-schema.org/draft-07/schema#', |
| 630 | + }, |
| 631 | + }, |
| 632 | + }, |
| 633 | + }); |
| 634 | + }); |
| 635 | + |
| 636 | + it('should allow for undefined schema in object-json mode when structuredOutputs are enabled', async () => { |
| 637 | + prepareJsonResponse({ content: '{"value":"Spark"}' }); |
| 638 | + |
| 639 | + const model = new OpenAICompatibleChatLanguageModel( |
| 640 | + 'gpt-4o-2024-08-06', |
| 641 | + {}, |
| 642 | + { |
| 643 | + provider: 'test-provider', |
| 644 | + url: () => 'https://my.api.com/v1/chat/completions', |
| 645 | + headers: () => ({}), |
| 646 | + supportsStructuredOutputs: true, |
| 647 | + }, |
| 648 | + ); |
| 649 | + |
| 650 | + await model.doGenerate({ |
| 651 | + inputFormat: 'prompt', |
| 652 | + mode: { |
| 653 | + type: 'object-json', |
| 654 | + name: 'test-name', |
| 655 | + description: 'test description', |
| 656 | + }, |
| 657 | + prompt: TEST_PROMPT, |
| 658 | + }); |
| 659 | + |
| 660 | + expect(await server.getRequestBodyJson()).toStrictEqual({ |
| 661 | + model: 'gpt-4o-2024-08-06', |
| 662 | + messages: [{ role: 'user', content: 'Hello' }], |
| 663 | + response_format: { |
| 664 | + type: 'json_object', |
| 665 | + }, |
| 666 | + }); |
| 667 | + }); |
| 668 | + |
| 669 | + it('should set strict in object-tool mode when structuredOutputs are enabled', async () => { |
| 670 | + prepareJsonResponse({ |
| 671 | + tool_calls: [ |
| 672 | + { |
| 673 | + id: 'call_O17Uplv4lJvD6DVdIvFFeRMw', |
| 674 | + type: 'function', |
| 675 | + function: { |
| 676 | + name: 'test-tool', |
| 677 | + arguments: '{"value":"Spark"}', |
| 678 | + }, |
| 679 | + }, |
| 680 | + ], |
| 681 | + }); |
| 682 | + |
| 683 | + const model = new OpenAICompatibleChatLanguageModel( |
| 684 | + 'gpt-4o-2024-08-06', |
| 685 | + {}, |
| 686 | + { |
| 687 | + provider: 'test-provider', |
| 688 | + url: () => 'https://my.api.com/v1/chat/completions', |
| 689 | + headers: () => ({}), |
| 690 | + supportsStructuredOutputs: true, |
| 691 | + }, |
| 692 | + ); |
| 693 | + |
| 694 | + const result = await model.doGenerate({ |
| 695 | + inputFormat: 'prompt', |
| 696 | + mode: { |
| 697 | + type: 'object-tool', |
| 698 | + tool: { |
| 699 | + type: 'function', |
| 700 | + name: 'test-tool', |
| 701 | + description: 'test description', |
| 702 | + parameters: { |
| 703 | + type: 'object', |
| 704 | + properties: { value: { type: 'string' } }, |
| 705 | + required: ['value'], |
| 706 | + additionalProperties: false, |
| 707 | + $schema: 'http://json-schema.org/draft-07/schema#', |
| 708 | + }, |
| 709 | + }, |
| 710 | + }, |
| 711 | + prompt: TEST_PROMPT, |
| 712 | + }); |
| 713 | + |
| 714 | + expect(await server.getRequestBodyJson()).toStrictEqual({ |
| 715 | + model: 'gpt-4o-2024-08-06', |
| 716 | + messages: [{ role: 'user', content: 'Hello' }], |
| 717 | + tool_choice: { type: 'function', function: { name: 'test-tool' } }, |
| 718 | + tools: [ |
| 719 | + { |
| 720 | + type: 'function', |
| 721 | + function: { |
| 722 | + name: 'test-tool', |
| 723 | + description: 'test description', |
| 724 | + parameters: { |
| 725 | + type: 'object', |
| 726 | + properties: { value: { type: 'string' } }, |
| 727 | + required: ['value'], |
| 728 | + additionalProperties: false, |
| 729 | + $schema: 'http://json-schema.org/draft-07/schema#', |
| 730 | + }, |
| 731 | + strict: true, |
| 732 | + }, |
| 733 | + }, |
| 734 | + ], |
| 735 | + }); |
| 736 | + |
| 737 | + expect(result.toolCalls).toStrictEqual([ |
| 738 | + { |
| 739 | + args: '{"value":"Spark"}', |
| 740 | + toolCallId: 'call_O17Uplv4lJvD6DVdIvFFeRMw', |
| 741 | + toolCallType: 'function', |
| 742 | + toolName: 'test-tool', |
| 743 | + }, |
| 744 | + ]); |
| 745 | + }); |
| 746 | + }); |
| 747 | + |
389 | 748 | it('should send request body', async () => {
|
390 | 749 | prepareJsonResponse({ content: '' });
|
391 | 750 |
|
|
0 commit comments