| 75 | } |
| 76 | |
| 77 | export function testChat( |
| 78 | api: BaseLlmApi, |
| 79 | model: string, |
| 80 | options?: TestConfigOptions, |
| 81 | ) { |
| 82 | test("should successfully stream chat", async () => { |
| 83 | const stream = api.chatCompletionStream( |
| 84 | { |
| 85 | model, |
| 86 | messages: [{ role: "user", content: "Hello! Who are you?" }], |
| 87 | stream: true, |
| 88 | }, |
| 89 | new AbortController().signal, |
| 90 | ); |
| 91 | let completion = ""; |
| 92 | let usage: CompletionUsage | undefined = undefined; |
| 93 | for await (const result of stream) { |
| 94 | completion += result.choices[0]?.delta.content ?? ""; |
| 95 | |
| 96 | if (result.usage) { |
| 97 | usage = result.usage; |
| 98 | } else { |
| 99 | // At the end we expect a final message without choices that shares usage |
| 100 | expect(result.choices.length).toBeGreaterThan(0); |
| 101 | } |
| 102 | } |
| 103 | expect(completion.length).toBeGreaterThan(0); |
| 104 | |
| 105 | if (options?.expectUsage === true) { |
| 106 | expect(usage).toBeDefined(); |
| 107 | expect(usage!.completion_tokens).toBeGreaterThan(0); |
| 108 | expect(usage!.prompt_tokens).toBeGreaterThan(0); |
| 109 | // Gemini 2.5 models have thinking tokens, so total_tokens >= prompt + completion |
| 110 | // Other models should have total_tokens = prompt + completion |
| 111 | if (model.includes("gemini-2.5") || model.includes("gemini-2.0")) { |
| 112 | expect(usage!.total_tokens).toBeGreaterThanOrEqual( |
| 113 | usage!.prompt_tokens + usage!.completion_tokens, |
| 114 | ); |
| 115 | } else { |
| 116 | expect(usage!.total_tokens).toEqual( |
| 117 | usage!.prompt_tokens + usage!.completion_tokens, |
| 118 | ); |
| 119 | } |
| 120 | } |
| 121 | }); |
| 122 | |
| 123 | test("should successfully stream multi-part chat with empty text", async () => { |
| 124 | const stream = api.chatCompletionStream( |
| 125 | { |
| 126 | model, |
| 127 | messages: [ |
| 128 | { |
| 129 | role: "user", |
| 130 | content: [ |
| 131 | { |
| 132 | type: "text", |
| 133 | text: "Hello! Who are you?", |
| 134 | }, |