| 1 | import { afterEach, describe, expect, test, vi } from "vitest"; |
| 2 | import { ILLM } from "../../index.js"; |
| 3 | import Anthropic from "./Anthropic.js"; |
| 4 | |
| 5 | interface LlmTestCase { |
| 6 | llm: ILLM; |
| 7 | methodToTest: keyof ILLM; |
| 8 | params: any[]; |
| 9 | expectedRequest: { |
| 10 | url: string; |
| 11 | method: string; |
| 12 | headers?: Record<string, string>; |
| 13 | body?: Record<string, any>; |
| 14 | }; |
| 15 | mockResponse?: any; |
| 16 | mockStream?: any[]; |
| 17 | } |
| 18 | |
| 19 | function createMockStream(mockStream: any[]) { |
| 20 | const encoder = new TextEncoder(); |
| 21 | return new ReadableStream({ |
| 22 | start(controller) { |
| 23 | for (const chunk of mockStream) { |
| 24 | controller.enqueue( |
| 25 | encoder.encode( |
| 26 | `data: ${ |
| 27 | typeof chunk === "string" ? chunk : JSON.stringify(chunk) |
| 28 | }\n\n`, |
| 29 | ), |
| 30 | ); |
| 31 | } |
| 32 | controller.close(); |
| 33 | }, |
| 34 | }); |
| 35 | } |
| 36 | |
| 37 | function setupMockFetch(mockResponse?: any, mockStream?: any[]) { |
| 38 | const mockFetch = vi.fn(); |
| 39 | |
| 40 | if (mockStream) { |
| 41 | const stream = createMockStream(mockStream); |
| 42 | mockFetch.mockResolvedValue( |
| 43 | new Response(stream, { |
| 44 | headers: { |
| 45 | "Content-Type": "text/event-stream", |
| 46 | }, |
| 47 | }), |
| 48 | ); |
| 49 | } else { |
| 50 | mockFetch.mockResolvedValue( |
| 51 | new Response(JSON.stringify(mockResponse), { |
| 52 | headers: { "Content-Type": "application/json" }, |
| 53 | }), |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | return mockFetch; |
| 58 | } |
| 59 | |
| 60 | function setupReadableStreamPolyfill() { |
| 61 | // This can be removed if https://github.com/nodejs/undici/issues/2888 is resolved |
| 62 | // @ts-ignore |
| 63 | const originalFrom = ReadableStream.from; |
| 64 | // @ts-ignore |
| 65 | ReadableStream.from = (body) => { |
| 66 | if (body?.source) { |
| 67 | return body; |
| 68 | } |
| 69 | return originalFrom(body); |
| 70 | }; |
| 71 | } |
| 72 | |
| 73 | async function executeLlmMethod( |
| 74 | llm: ILLM, |
| 75 | methodToTest: keyof ILLM, |
| 76 | params: any[], |
| 77 | ) { |
| 78 | if (typeof (llm as any)[methodToTest] !== "function") { |
| 79 | throw new Error( |
| 80 | `Method ${String(methodToTest)} does not exist on the LLM instance.`, |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | const result = await (llm as any)[methodToTest](...params); |
| 85 | if (result?.next) { |
| 86 | for await (const _ of result) { |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | function assertFetchCall(mockFetch: any, expectedRequest: any) { |
| 92 | expect(mockFetch).toHaveBeenCalledTimes(1); |
| 93 | const [url, options] = mockFetch.mock.calls[0]; |
| 94 | |
| 95 | expect(url.toString()).toBe(expectedRequest.url); |
| 96 | expect(options.method).toBe(expectedRequest.method); |
| 97 | |
| 98 | if (expectedRequest.headers) { |
| 99 | expect(options.headers).toEqual( |
| 100 | expect.objectContaining(expectedRequest.headers), |
| 101 | ); |
| 102 | } |
| 103 | |
| 104 | if (expectedRequest.body) { |
| 105 | const actualBody = JSON.parse(options.body as string); |
| 106 | expect(actualBody).toEqual(expectedRequest.body); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | async function runLlmTest(testCase: LlmTestCase) { |
| 111 | const { |
| 112 | llm, |
| 113 | methodToTest, |
| 114 | params, |
| 115 | expectedRequest, |
| 116 | mockResponse, |
| 117 | mockStream, |
| 118 | } = testCase; |
| 119 | |
| 120 | const mockFetch = setupMockFetch(mockResponse, mockStream); |
| 121 | setupReadableStreamPolyfill(); |
| 122 | |
| 123 | (llm as any).fetch = mockFetch; |
| 124 | |
| 125 | await executeLlmMethod(llm, methodToTest, params); |
| 126 | assertFetchCall(mockFetch, expectedRequest); |
| 127 | } |
| 128 | |
| 129 | describe("Anthropic", () => { |
| 130 | afterEach(() => { |
| 131 | vi.clearAllMocks(); |
| 132 | }); |
| 133 | |
| 134 | test("streamChat should send a valid request", async () => { |
| 135 | const anthropic = new Anthropic({ |
| 136 | apiKey: "test-api-key", |
| 137 | model: "claude-3-5-sonnet-latest", |
| 138 | apiBase: "https://api.anthropic.com/v1/", |
| 139 | }); |
| 140 | |
| 141 | await runLlmTest({ |
| 142 | llm: anthropic, |
| 143 | methodToTest: "streamChat", |
| 144 | params: [ |
| 145 | [{ role: "user", content: "hello" }], |
| 146 | new AbortController().signal, |
| 147 | ], |
| 148 | expectedRequest: { |
| 149 | url: "https://api.anthropic.com/v1/messages", |
| 150 | method: "POST", |
| 151 | headers: { |
| 152 | "Content-Type": "application/json", |
| 153 | Accept: "application/json", |
| 154 | "anthropic-version": "2023-06-01", |
| 155 | "x-api-key": "test-api-key", |
| 156 | }, |
| 157 | body: { |
| 158 | model: "claude-3-5-sonnet-latest", |
| 159 | max_tokens: 8192, |
| 160 | stream: true, |
| 161 | messages: [ |
| 162 | { |
| 163 | role: "user", |
| 164 | content: [{ type: "text", text: "hello" }], |
| 165 | }, |
| 166 | ], |
| 167 | system: "", |
| 168 | }, |
| 169 | }, |
| 170 | mockStream: [ |
| 171 | '{"type": "content_block_delta", "delta": {"type": "text_delta", "text": "Hello! How can I help you today?"}}', |
| 172 | '{"type": "content_block_stop"}', |
| 173 | ], |
| 174 | }); |
| 175 | }); |
| 176 | |
| 177 | test("chat should send a valid request", async () => { |
| 178 | const anthropic = new Anthropic({ |
| 179 | apiKey: "test-api-key", |
| 180 | model: "claude-3-5-sonnet-latest", |
| 181 | apiBase: "https://api.anthropic.com/v1/", |
| 182 | }); |
| 183 | |
| 184 | await runLlmTest({ |
| 185 | llm: anthropic, |
| 186 | methodToTest: "chat", |
| 187 | params: [ |
| 188 | [{ role: "user", content: "hello" }], |
| 189 | new AbortController().signal, |
| 190 | ], |
| 191 | expectedRequest: { |
| 192 | url: "https://api.anthropic.com/v1/messages", |
| 193 | method: "POST", |
| 194 | headers: { |
| 195 | "Content-Type": "application/json", |
| 196 | Accept: "application/json", |
| 197 | "anthropic-version": "2023-06-01", |
| 198 | "x-api-key": "test-api-key", |
| 199 | }, |
| 200 | body: { |
| 201 | model: "claude-3-5-sonnet-latest", |
| 202 | max_tokens: 8192, |
| 203 | stream: true, |
| 204 | messages: [ |
| 205 | { |
| 206 | role: "user", |
| 207 | content: [{ type: "text", text: "hello" }], |
| 208 | }, |
| 209 | ], |
| 210 | system: "", |
| 211 | }, |
| 212 | }, |
| 213 | mockStream: [ |
| 214 | '{"type": "content_block_delta", "delta": {"type": "text_delta", "text": "Hello! How can I help you today?"}}', |
| 215 | '{"type": "content_block_stop"}', |
| 216 | ], |
| 217 | }); |
| 218 | }); |
| 219 | |
| 220 | test("streamComplete should send a valid request", async () => { |
| 221 | const anthropic = new Anthropic({ |
| 222 | apiKey: "test-api-key", |
| 223 | model: "claude-3-5-sonnet-latest", |
| 224 | apiBase: "https://api.anthropic.com/v1/", |
| 225 | }); |
| 226 | |
| 227 | await runLlmTest({ |
| 228 | llm: anthropic, |
| 229 | methodToTest: "streamComplete", |
| 230 | params: ["Complete this: Hello", new AbortController().signal], |
| 231 | expectedRequest: { |
| 232 | url: "https://api.anthropic.com/v1/messages", |
| 233 | method: "POST", |
| 234 | headers: { |
| 235 | "Content-Type": "application/json", |
| 236 | Accept: "application/json", |
| 237 | "anthropic-version": "2023-06-01", |
| 238 | "x-api-key": "test-api-key", |
| 239 | }, |
| 240 | body: { |
| 241 | model: "claude-3-5-sonnet-latest", |
| 242 | max_tokens: 8192, |
| 243 | stream: true, |
| 244 | messages: [ |
| 245 | { |
| 246 | role: "user", |
| 247 | content: [{ type: "text", text: "Complete this: Hello" }], |
| 248 | }, |
| 249 | ], |
| 250 | system: "", |
| 251 | }, |
| 252 | }, |
| 253 | mockStream: [ |
| 254 | '{"type": "content_block_delta", "delta": {"type": "text_delta", "text": " world!"}}', |
| 255 | '{"type": "content_block_stop"}', |
| 256 | ], |
| 257 | }); |
| 258 | }); |
| 259 | |
| 260 | test("complete should send a valid request", async () => { |
| 261 | const anthropic = new Anthropic({ |
| 262 | apiKey: "test-api-key", |
| 263 | model: "claude-3-5-sonnet-latest", |
| 264 | apiBase: "https://api.anthropic.com/v1/", |
| 265 | }); |
| 266 | |
| 267 | await runLlmTest({ |
| 268 | llm: anthropic, |
| 269 | methodToTest: "complete", |
| 270 | params: ["Complete this: Hello", new AbortController().signal], |
| 271 | expectedRequest: { |
| 272 | url: "https://api.anthropic.com/v1/messages", |
| 273 | method: "POST", |
| 274 | headers: { |
| 275 | "Content-Type": "application/json", |
| 276 | Accept: "application/json", |
| 277 | "anthropic-version": "2023-06-01", |
| 278 | "x-api-key": "test-api-key", |
| 279 | }, |
| 280 | body: { |
| 281 | model: "claude-3-5-sonnet-latest", |
| 282 | max_tokens: 8192, |
| 283 | stream: true, |
| 284 | messages: [ |
| 285 | { |
| 286 | role: "user", |
| 287 | content: [{ type: "text", text: "Complete this: Hello" }], |
| 288 | }, |
| 289 | ], |
| 290 | system: "", |
| 291 | }, |
| 292 | }, |
| 293 | mockStream: [ |
| 294 | '{"type": "content_block_delta", "delta": {"type": "text_delta", "text": " world!"}}', |
| 295 | '{"type": "content_block_stop"}', |
| 296 | ], |
| 297 | }); |
| 298 | }); |
| 299 | |
| 300 | describe("Different configurations", () => { |
| 301 | test("should handle system message", async () => { |
| 302 | const anthropic = new Anthropic({ |
| 303 | apiKey: "test-api-key", |
| 304 | model: "claude-3-5-sonnet-latest", |
| 305 | apiBase: "https://api.anthropic.com/v1/", |
| 306 | }); |
| 307 | |
| 308 | await runLlmTest({ |
| 309 | llm: anthropic, |
| 310 | methodToTest: "streamChat", |
| 311 | params: [ |
| 312 | [ |
| 313 | { role: "system", content: "You are a helpful assistant." }, |
| 314 | { role: "user", content: "Hello!" }, |
| 315 | ], |
| 316 | new AbortController().signal, |
| 317 | ], |
| 318 | expectedRequest: { |
| 319 | url: "https://api.anthropic.com/v1/messages", |
| 320 | method: "POST", |
| 321 | headers: { |
| 322 | "Content-Type": "application/json", |
| 323 | Accept: "application/json", |
| 324 | "anthropic-version": "2023-06-01", |
| 325 | "x-api-key": "test-api-key", |
| 326 | }, |
| 327 | body: { |
| 328 | model: "claude-3-5-sonnet-latest", |
| 329 | max_tokens: 8192, |
| 330 | stream: true, |
| 331 | messages: [ |
| 332 | { |
| 333 | role: "user", |
| 334 | content: [{ type: "text", text: "Hello!" }], |
| 335 | }, |
| 336 | ], |
| 337 | system: "You are a helpful assistant.", |
| 338 | }, |
| 339 | }, |
| 340 | mockStream: [ |
| 341 | '{"type": "content_block_delta", "delta": {"type": "text_delta", "text": "Hello! How can I help you today?"}}', |
| 342 | '{"type": "content_block_stop"}', |
| 343 | ], |
| 344 | }); |
| 345 | }); |
| 346 | |
| 347 | test("should handle tool calls", async () => { |
| 348 | const anthropic = new Anthropic({ |
| 349 | apiKey: "test-api-key", |
| 350 | model: "claude-3-5-sonnet-latest", |
| 351 | apiBase: "https://api.anthropic.com/v1/", |
| 352 | }); |
| 353 | |
| 354 | const tools = [ |
| 355 | { |
| 356 | type: "function" as const, |
| 357 | function: { |
| 358 | name: "get_weather", |
| 359 | description: "Get the current weather", |
| 360 | parameters: { |
| 361 | type: "object", |
| 362 | properties: { |
| 363 | location: { type: "string" }, |
| 364 | }, |
| 365 | required: ["location"], |
| 366 | }, |
| 367 | }, |
| 368 | }, |
| 369 | ]; |
| 370 | |
| 371 | await runLlmTest({ |
| 372 | llm: anthropic, |
| 373 | methodToTest: "streamChat", |
| 374 | params: [ |
| 375 | [{ role: "user", content: "What's the weather in New York?" }], |
| 376 | new AbortController().signal, |
| 377 | { tools }, |
| 378 | ], |
| 379 | expectedRequest: { |
| 380 | url: "https://api.anthropic.com/v1/messages", |
| 381 | method: "POST", |
| 382 | headers: { |
| 383 | "Content-Type": "application/json", |
| 384 | Accept: "application/json", |
| 385 | "anthropic-version": "2023-06-01", |
| 386 | "x-api-key": "test-api-key", |
| 387 | }, |
| 388 | body: { |
| 389 | model: "claude-3-5-sonnet-latest", |
| 390 | max_tokens: 8192, |
| 391 | stream: true, |
| 392 | messages: [ |
| 393 | { |
| 394 | role: "user", |
| 395 | content: [ |
| 396 | { type: "text", text: "What's the weather in New York?" }, |
| 397 | ], |
| 398 | }, |
| 399 | ], |
| 400 | tools: [ |
| 401 | { |
| 402 | name: "get_weather", |
| 403 | description: "Get the current weather", |
| 404 | input_schema: { |
| 405 | type: "object", |
| 406 | properties: { |
| 407 | location: { type: "string" }, |
| 408 | }, |
| 409 | required: ["location"], |
| 410 | }, |
| 411 | }, |
| 412 | ], |
| 413 | system: "", |
| 414 | }, |
| 415 | }, |
| 416 | mockStream: [ |
| 417 | '{"type": "content_block_start", "content_block": {"type": "tool_use", "id": "toolu_123", "name": "get_weather"}}', |
| 418 | '{"type": "content_block_delta", "delta": {"type": "input_json_delta", "partial_json": "{\\"location\\": \\"New York\\""}}', |
| 419 | '{"type": "content_block_stop"}', |
| 420 | ], |
| 421 | }); |
| 422 | }); |
| 423 | |
| 424 | test("should handle custom max tokens", async () => { |
| 425 | const anthropic = new Anthropic({ |
| 426 | apiKey: "test-api-key", |
| 427 | model: "claude-3-5-sonnet-latest", |
| 428 | apiBase: "https://api.anthropic.com/v1/", |
| 429 | }); |
| 430 | |
| 431 | await runLlmTest({ |
| 432 | llm: anthropic, |
| 433 | methodToTest: "streamChat", |
| 434 | params: [ |
| 435 | [{ role: "user", content: "hello" }], |
| 436 | new AbortController().signal, |
| 437 | { maxTokens: 1000 }, |
| 438 | ], |
| 439 | expectedRequest: { |
| 440 | url: "https://api.anthropic.com/v1/messages", |
| 441 | method: "POST", |
| 442 | headers: { |
| 443 | "Content-Type": "application/json", |
| 444 | Accept: "application/json", |
| 445 | "anthropic-version": "2023-06-01", |
| 446 | "x-api-key": "test-api-key", |
| 447 | }, |
| 448 | body: { |
| 449 | model: "claude-3-5-sonnet-latest", |
| 450 | max_tokens: 1000, |
| 451 | stream: true, |
| 452 | messages: [ |
| 453 | { |
| 454 | role: "user", |
| 455 | content: [{ type: "text", text: "hello" }], |
| 456 | }, |
| 457 | ], |
| 458 | system: "", |
| 459 | }, |
| 460 | }, |
| 461 | mockStream: [ |
| 462 | '{"type": "content_block_delta", "delta": {"type": "text_delta", "text": "Hello!"}}', |
| 463 | '{"type": "content_block_stop"}', |
| 464 | ], |
| 465 | }); |
| 466 | }); |
| 467 | }); |
| 468 | |
| 469 | describe("Error handling", () => { |
| 470 | test("should throw error when API key is missing", async () => { |
| 471 | const anthropic = new Anthropic({ |
| 472 | apiKey: "", |
| 473 | model: "claude-3-5-sonnet-latest", |
| 474 | apiBase: "https://api.anthropic.com/v1/", |
| 475 | }); |
| 476 | |
| 477 | await expect( |
| 478 | runLlmTest({ |
| 479 | llm: anthropic, |
| 480 | methodToTest: "streamChat", |
| 481 | params: [ |
| 482 | [{ role: "user", content: "hello" }], |
| 483 | new AbortController().signal, |
| 484 | ], |
| 485 | expectedRequest: { |
| 486 | url: "https://api.anthropic.com/v1/messages", |
| 487 | method: "POST", |
| 488 | }, |
| 489 | }), |
| 490 | ).rejects.toThrow( |
| 491 | "Request not sent. You have an Anthropic model configured in your config.json, but the API key is not set.", |
| 492 | ); |
| 493 | }); |
| 494 | }); |
| 495 | }); |