| 1 | import { afterEach, describe, expect, test, vi } from "vitest"; |
| 2 | import { ILLM } from "../../index.js"; |
| 3 | import OpenAI from "./OpenAI.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 | // Disable OpenAI adapter to use our custom fetch mock |
| 126 | (llm as any).useOpenAIAdapterFor = []; |
| 127 | |
| 128 | await executeLlmMethod(llm, methodToTest, params); |
| 129 | assertFetchCall(mockFetch, expectedRequest); |
| 130 | } |
| 131 | |
| 132 | describe("OpenAI", () => { |
| 133 | afterEach(() => { |
| 134 | vi.clearAllMocks(); |
| 135 | }); |
| 136 | |
| 137 | test("streamChat should send a valid request", async () => { |
| 138 | const openai = new OpenAI({ |
| 139 | apiKey: "test-api-key", |
| 140 | model: "gpt-4", |
| 141 | apiBase: "https://api.openai.com/v1/", |
| 142 | }); |
| 143 | |
| 144 | await runLlmTest({ |
| 145 | llm: openai, |
| 146 | methodToTest: "streamChat", |
| 147 | params: [ |
| 148 | [{ role: "user", content: "hello" }], |
| 149 | new AbortController().signal, |
| 150 | ], |
| 151 | expectedRequest: { |
| 152 | url: "https://api.openai.com/v1/chat/completions", |
| 153 | method: "POST", |
| 154 | headers: { |
| 155 | "Content-Type": "application/json", |
| 156 | Authorization: "Bearer test-api-key", |
| 157 | "api-key": "test-api-key", |
| 158 | }, |
| 159 | body: { |
| 160 | model: "gpt-4", |
| 161 | messages: [{ role: "user", content: "hello" }], |
| 162 | stream: true, |
| 163 | max_tokens: 2048, |
| 164 | }, |
| 165 | }, |
| 166 | mockStream: [ |
| 167 | { choices: [{ delta: { content: "Hello" } }] }, |
| 168 | { choices: [{ delta: { content: " world" } }] }, |
| 169 | ], |
| 170 | }); |
| 171 | }); |
| 172 | |
| 173 | test("chat should send a valid request", async () => { |
| 174 | const openai = new OpenAI({ |
| 175 | apiKey: "test-api-key", |
| 176 | model: "gpt-4", |
| 177 | apiBase: "https://api.openai.com/v1/", |
| 178 | }); |
| 179 | |
| 180 | await runLlmTest({ |
| 181 | llm: openai, |
| 182 | methodToTest: "chat", |
| 183 | params: [ |
| 184 | [{ role: "user", content: "hello" }], |
| 185 | new AbortController().signal, |
| 186 | ], |
| 187 | expectedRequest: { |
| 188 | url: "https://api.openai.com/v1/chat/completions", |
| 189 | method: "POST", |
| 190 | headers: { |
| 191 | "Content-Type": "application/json", |
| 192 | Authorization: "Bearer test-api-key", |
| 193 | "api-key": "test-api-key", |
| 194 | }, |
| 195 | body: { |
| 196 | model: "gpt-4", |
| 197 | messages: [{ role: "user", content: "hello" }], |
| 198 | stream: true, |
| 199 | max_tokens: 2048, |
| 200 | }, |
| 201 | }, |
| 202 | mockStream: [ |
| 203 | { choices: [{ delta: { content: "Hello" } }] }, |
| 204 | { choices: [{ delta: { content: " world" } }] }, |
| 205 | ], |
| 206 | }); |
| 207 | }); |
| 208 | |
| 209 | test("streamComplete should send a valid request", async () => { |
| 210 | const openai = new OpenAI({ |
| 211 | apiKey: "test-api-key", |
| 212 | model: "gpt-4", |
| 213 | apiBase: "https://api.openai.com/v1/", |
| 214 | }); |
| 215 | |
| 216 | await runLlmTest({ |
| 217 | llm: openai, |
| 218 | methodToTest: "streamComplete", |
| 219 | params: ["Hello", new AbortController().signal], |
| 220 | expectedRequest: { |
| 221 | url: "https://api.openai.com/v1/chat/completions", |
| 222 | method: "POST", |
| 223 | headers: { |
| 224 | "Content-Type": "application/json", |
| 225 | Authorization: "Bearer test-api-key", |
| 226 | "api-key": "test-api-key", |
| 227 | }, |
| 228 | body: { |
| 229 | model: "gpt-4", |
| 230 | messages: [{ role: "user", content: "Hello" }], |
| 231 | stream: true, |
| 232 | max_tokens: 2048, |
| 233 | }, |
| 234 | }, |
| 235 | mockStream: [ |
| 236 | { choices: [{ delta: { content: "Hello" } }] }, |
| 237 | { choices: [{ delta: { content: " world" } }] }, |
| 238 | ], |
| 239 | }); |
| 240 | }); |
| 241 | |
| 242 | test("complete should send a valid request", async () => { |
| 243 | const openai = new OpenAI({ |
| 244 | apiKey: "test-api-key", |
| 245 | model: "gpt-4", |
| 246 | apiBase: "https://api.openai.com/v1/", |
| 247 | }); |
| 248 | |
| 249 | await runLlmTest({ |
| 250 | llm: openai, |
| 251 | methodToTest: "complete", |
| 252 | params: ["Hello", new AbortController().signal], |
| 253 | expectedRequest: { |
| 254 | url: "https://api.openai.com/v1/chat/completions", |
| 255 | method: "POST", |
| 256 | headers: { |
| 257 | "Content-Type": "application/json", |
| 258 | Authorization: "Bearer test-api-key", |
| 259 | "api-key": "test-api-key", |
| 260 | }, |
| 261 | body: { |
| 262 | model: "gpt-4", |
| 263 | messages: [{ role: "user", content: "Hello" }], |
| 264 | stream: true, |
| 265 | max_tokens: 2048, |
| 266 | }, |
| 267 | }, |
| 268 | mockStream: [ |
| 269 | { choices: [{ delta: { content: "Hello" } }] }, |
| 270 | { choices: [{ delta: { content: " world" } }] }, |
| 271 | ], |
| 272 | }); |
| 273 | }); |
| 274 | |
| 275 | test("should handle O1 models correctly", async () => { |
| 276 | const openai = new OpenAI({ |
| 277 | apiKey: "test-api-key", |
| 278 | model: "o3-mini", |
| 279 | apiBase: "https://api.openai.com/v1/", |
| 280 | }); |
| 281 | |
| 282 | await runLlmTest({ |
| 283 | llm: openai, |
| 284 | methodToTest: "chat", |
| 285 | params: [ |
| 286 | [{ role: "user", content: "hello" }], |
| 287 | new AbortController().signal, |
| 288 | { maxTokens: 100 }, |
| 289 | ], |
| 290 | expectedRequest: { |
| 291 | url: "https://api.openai.com/v1/chat/completions", |
| 292 | method: "POST", |
| 293 | headers: { |
| 294 | "Content-Type": "application/json", |
| 295 | Authorization: "Bearer test-api-key", |
| 296 | "api-key": "test-api-key", |
| 297 | }, |
| 298 | body: { |
| 299 | model: "o3-mini", |
| 300 | messages: [{ role: "user", content: "hello" }], |
| 301 | stream: true, |
| 302 | max_completion_tokens: 100, |
| 303 | }, |
| 304 | }, |
| 305 | mockStream: [{ choices: [{ delta: { content: "Hello world" } }] }], |
| 306 | }); |
| 307 | }); |
| 308 | |
| 309 | test("should handle tools", async () => { |
| 310 | const openai = new OpenAI({ |
| 311 | apiKey: "test-api-key", |
| 312 | model: "gpt-4", |
| 313 | apiBase: "https://api.openai.com/v1/", |
| 314 | }); |
| 315 | |
| 316 | const tools = [ |
| 317 | { |
| 318 | type: "function" as const, |
| 319 | function: { |
| 320 | name: "test_function", |
| 321 | description: "Test function", |
| 322 | parameters: { type: "object", properties: {} }, |
| 323 | }, |
| 324 | }, |
| 325 | ]; |
| 326 | |
| 327 | await runLlmTest({ |
| 328 | llm: openai, |
| 329 | methodToTest: "chat", |
| 330 | params: [ |
| 331 | [{ role: "user", content: "hello" }], |
| 332 | new AbortController().signal, |
| 333 | { tools }, |
| 334 | ], |
| 335 | expectedRequest: { |
| 336 | url: "https://api.openai.com/v1/chat/completions", |
| 337 | method: "POST", |
| 338 | headers: { |
| 339 | "Content-Type": "application/json", |
| 340 | Authorization: "Bearer test-api-key", |
| 341 | "api-key": "test-api-key", |
| 342 | }, |
| 343 | body: { |
| 344 | model: "gpt-4", |
| 345 | messages: [{ role: "user", content: "hello" }], |
| 346 | stream: true, |
| 347 | max_tokens: 2048, |
| 348 | tools: [ |
| 349 | { |
| 350 | type: "function", |
| 351 | function: { |
| 352 | name: "test_function", |
| 353 | description: "Test function", |
| 354 | parameters: { type: "object", properties: {} }, |
| 355 | }, |
| 356 | }, |
| 357 | ], |
| 358 | }, |
| 359 | }, |
| 360 | mockStream: [{ choices: [{ delta: { content: "I'll help you" } }] }], |
| 361 | }); |
| 362 | }); |
| 363 | |
| 364 | test("should handle custom max tokens", async () => { |
| 365 | const openai = new OpenAI({ |
| 366 | apiKey: "test-api-key", |
| 367 | model: "gpt-4", |
| 368 | apiBase: "https://api.openai.com/v1/", |
| 369 | }); |
| 370 | |
| 371 | await runLlmTest({ |
| 372 | llm: openai, |
| 373 | methodToTest: "chat", |
| 374 | params: [ |
| 375 | [{ role: "user", content: "hello" }], |
| 376 | new AbortController().signal, |
| 377 | { maxTokens: 500 }, |
| 378 | ], |
| 379 | expectedRequest: { |
| 380 | url: "https://api.openai.com/v1/chat/completions", |
| 381 | method: "POST", |
| 382 | headers: { |
| 383 | "Content-Type": "application/json", |
| 384 | Authorization: "Bearer test-api-key", |
| 385 | "api-key": "test-api-key", |
| 386 | }, |
| 387 | body: { |
| 388 | model: "gpt-4", |
| 389 | messages: [{ role: "user", content: "hello" }], |
| 390 | stream: true, |
| 391 | max_tokens: 500, |
| 392 | }, |
| 393 | }, |
| 394 | mockStream: [{ choices: [{ delta: { content: "Hello" } }] }], |
| 395 | }); |
| 396 | }); |
| 397 | |
| 398 | test("should handle embeddings", async () => { |
| 399 | const openai = new OpenAI({ |
| 400 | apiKey: "test-api-key", |
| 401 | model: "text-embedding-ada-002", |
| 402 | apiBase: "https://api.openai.com/v1/", |
| 403 | }); |
| 404 | |
| 405 | await runLlmTest({ |
| 406 | llm: openai, |
| 407 | methodToTest: "embed", |
| 408 | params: [["Hello", "World"]], |
| 409 | expectedRequest: { |
| 410 | url: "https://api.openai.com/v1/embeddings", |
| 411 | method: "POST", |
| 412 | headers: { |
| 413 | Authorization: "Bearer test-api-key", |
| 414 | "Content-Type": "application/json", |
| 415 | "api-key": "test-api-key", |
| 416 | }, |
| 417 | body: { |
| 418 | input: ["Hello", "World"], |
| 419 | model: "text-embedding-ada-002", |
| 420 | }, |
| 421 | }, |
| 422 | mockResponse: { |
| 423 | data: [{ embedding: [0.1, 0.2, 0.3] }, { embedding: [0.4, 0.5, 0.6] }], |
| 424 | }, |
| 425 | }); |
| 426 | }); |
| 427 | }); |