| 1 | import { createOpenAISubclassTests } from "./test-utils/openai-test-utils.js"; |
| 2 | |
| 3 | // Import all OpenAI-compatible providers |
| 4 | import OpenAI from "./OpenAI.js"; |
| 5 | import Groq from "./Groq.js"; |
| 6 | import Fireworks from "./Fireworks.js"; |
| 7 | import Together from "./Together.js"; |
| 8 | import Deepseek from "./Deepseek.js"; |
| 9 | import OpenRouter from "./OpenRouter.js"; |
| 10 | import xAI from "./xAI.js"; |
| 11 | import Mistral from "./Mistral.js"; |
| 12 | import LMStudio from "./LMStudio.js"; |
| 13 | import Cerebras from "./Cerebras.js"; |
| 14 | import DeepInfra from "./DeepInfra.js"; |
| 15 | import Nvidia from "./Nvidia.js"; |
| 16 | import SambaNova from "./SambaNova.js"; |
| 17 | import Scaleway from "./Scaleway.js"; |
| 18 | import Venice from "./Venice.js"; |
| 19 | import Moonshot from "./Moonshot.js"; |
| 20 | import Novita from "./Novita.js"; |
| 21 | import SiliconFlow from "./SiliconFlow.js"; |
| 22 | import Kindo from "./Kindo.js"; |
| 23 | import Azure from "./Azure.js"; |
| 24 | import Inception from "./Inception.js"; |
| 25 | import Docker from "./Docker.js"; |
| 26 | import Voyage from "./Voyage.js"; |
| 27 | import Vllm from "./Vllm.js"; |
| 28 | import TextGenWebUI from "./TextGenWebUI.js"; |
| 29 | // import Relace from "./Relace.js"; // Skip - causing import issues |
| 30 | import FunctionNetwork from "./FunctionNetwork.js"; |
| 31 | import NCompass from "./NCompass.js"; |
| 32 | import LlamaStack from "./LlamaStack.js"; |
| 33 | import Nebius from "./Nebius.js"; |
| 34 | import OVHcloud from "./OVHcloud.js"; |
| 35 | |
| 36 | // Base OpenAI tests |
| 37 | import { afterEach, describe, expect, test, vi } from "vitest"; |
| 38 | import { ILLM } from "../../index.js"; |
| 39 | |
| 40 | interface LlmTestCase { |
| 41 | llm: ILLM; |
| 42 | methodToTest: keyof ILLM; |
| 43 | params: any[]; |
| 44 | expectedRequest: { |
| 45 | url: string; |
| 46 | method: string; |
| 47 | headers?: Record<string, string>; |
| 48 | body?: Record<string, any>; |
| 49 | }; |
| 50 | mockResponse?: any; |
| 51 | mockStream?: any[]; |
| 52 | } |
| 53 | |
| 54 | function createMockStream(mockStream: any[]) { |
| 55 | const encoder = new TextEncoder(); |
| 56 | return new ReadableStream({ |
| 57 | start(controller) { |
| 58 | for (const chunk of mockStream) { |
| 59 | controller.enqueue( |
| 60 | encoder.encode( |
| 61 | `data: ${ |
| 62 | typeof chunk === "string" ? chunk : JSON.stringify(chunk) |
| 63 | }\n\n`, |
| 64 | ), |
| 65 | ); |
| 66 | } |
| 67 | controller.close(); |
| 68 | }, |
| 69 | }); |
| 70 | } |
| 71 | |
| 72 | function setupMockFetch(mockResponse?: any, mockStream?: any[]) { |
| 73 | const mockFetch = vi.fn(); |
| 74 | |
| 75 | if (mockStream) { |
| 76 | const stream = createMockStream(mockStream); |
| 77 | mockFetch.mockResolvedValue( |
| 78 | new Response(stream, { |
| 79 | headers: { |
| 80 | "Content-Type": "text/event-stream", |
| 81 | }, |
| 82 | }), |
| 83 | ); |
| 84 | } else { |
| 85 | mockFetch.mockResolvedValue( |
| 86 | new Response(JSON.stringify(mockResponse), { |
| 87 | headers: { "Content-Type": "application/json" }, |
| 88 | }), |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | return mockFetch; |
| 93 | } |
| 94 | |
| 95 | function setupReadableStreamPolyfill() { |
| 96 | // This can be removed if https://github.com/nodejs/undici/issues/2888 is resolved |
| 97 | // @ts-ignore |
| 98 | const originalFrom = ReadableStream.from; |
| 99 | // @ts-ignore |
| 100 | ReadableStream.from = (body) => { |
| 101 | if (body?.source) { |
| 102 | return body; |
| 103 | } |
| 104 | return originalFrom(body); |
| 105 | }; |
| 106 | } |
| 107 | |
| 108 | async function executeLlmMethod( |
| 109 | llm: ILLM, |
| 110 | methodToTest: keyof ILLM, |
| 111 | params: any[], |
| 112 | ) { |
| 113 | if (typeof (llm as any)[methodToTest] !== "function") { |
| 114 | throw new Error( |
| 115 | `Method ${String(methodToTest)} does not exist on the LLM instance.`, |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | const result = await (llm as any)[methodToTest](...params); |
| 120 | if (result?.next) { |
| 121 | for await (const _ of result) { |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | function assertFetchCall(mockFetch: any, expectedRequest: any) { |
| 127 | expect(mockFetch).toHaveBeenCalledTimes(1); |
| 128 | const [url, options] = mockFetch.mock.calls[0]; |
| 129 | |
| 130 | expect(url.toString()).toBe(expectedRequest.url); |
| 131 | expect(options.method).toBe(expectedRequest.method); |
| 132 | |
| 133 | if (expectedRequest.headers) { |
| 134 | expect(options.headers).toEqual( |
| 135 | expect.objectContaining(expectedRequest.headers), |
| 136 | ); |
| 137 | } |
| 138 | |
| 139 | if (expectedRequest.body) { |
| 140 | const actualBody = JSON.parse(options.body as string); |
| 141 | expect(actualBody).toEqual(expectedRequest.body); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | async function runLlmTest(testCase: LlmTestCase) { |
| 146 | const { |
| 147 | llm, |
| 148 | methodToTest, |
| 149 | params, |
| 150 | expectedRequest, |
| 151 | mockResponse, |
| 152 | mockStream, |
| 153 | } = testCase; |
| 154 | |
| 155 | const mockFetch = setupMockFetch(mockResponse, mockStream); |
| 156 | setupReadableStreamPolyfill(); |
| 157 | |
| 158 | (llm as any).fetch = mockFetch; |
| 159 | |
| 160 | // Disable OpenAI adapter to use our custom fetch mock |
| 161 | (llm as any).useOpenAIAdapterFor = []; |
| 162 | |
| 163 | await executeLlmMethod(llm, methodToTest, params); |
| 164 | assertFetchCall(mockFetch, expectedRequest); |
| 165 | } |
| 166 | |
| 167 | describe("OpenAI", () => { |
| 168 | afterEach(() => { |
| 169 | vi.clearAllMocks(); |
| 170 | }); |
| 171 | |
| 172 | test("streamChat should send a valid request", async () => { |
| 173 | const openai = new OpenAI({ |
| 174 | apiKey: "test-api-key", |
| 175 | model: "gpt-4", |
| 176 | apiBase: "https://api.openai.com/v1/", |
| 177 | }); |
| 178 | |
| 179 | await runLlmTest({ |
| 180 | llm: openai, |
| 181 | methodToTest: "streamChat", |
| 182 | params: [ |
| 183 | [{ role: "user", content: "hello" }], |
| 184 | new AbortController().signal, |
| 185 | ], |
| 186 | expectedRequest: { |
| 187 | url: "https://api.openai.com/v1/chat/completions", |
| 188 | method: "POST", |
| 189 | headers: { |
| 190 | "Content-Type": "application/json", |
| 191 | Authorization: "Bearer test-api-key", |
| 192 | "api-key": "test-api-key", |
| 193 | }, |
| 194 | body: { |
| 195 | model: "gpt-4", |
| 196 | messages: [{ role: "user", content: "hello" }], |
| 197 | stream: true, |
| 198 | max_tokens: 2048, |
| 199 | }, |
| 200 | }, |
| 201 | mockStream: [ |
| 202 | { choices: [{ delta: { content: "Hello" } }] }, |
| 203 | { choices: [{ delta: { content: " world" } }] }, |
| 204 | ], |
| 205 | }); |
| 206 | }); |
| 207 | |
| 208 | test("should handle embeddings", async () => { |
| 209 | const openai = new OpenAI({ |
| 210 | apiKey: "test-api-key", |
| 211 | model: "text-embedding-ada-002", |
| 212 | apiBase: "https://api.openai.com/v1/", |
| 213 | }); |
| 214 | |
| 215 | await runLlmTest({ |
| 216 | llm: openai, |
| 217 | methodToTest: "embed", |
| 218 | params: [["Hello", "World"]], |
| 219 | expectedRequest: { |
| 220 | url: "https://api.openai.com/v1/embeddings", |
| 221 | method: "POST", |
| 222 | headers: { |
| 223 | Authorization: "Bearer test-api-key", |
| 224 | "Content-Type": "application/json", |
| 225 | "api-key": "test-api-key", |
| 226 | }, |
| 227 | body: { |
| 228 | input: ["Hello", "World"], |
| 229 | model: "text-embedding-ada-002", |
| 230 | }, |
| 231 | }, |
| 232 | mockResponse: { |
| 233 | data: [{ embedding: [0.1, 0.2, 0.3] }, { embedding: [0.4, 0.5, 0.6] }], |
| 234 | }, |
| 235 | }); |
| 236 | }); |
| 237 | }); |
| 238 | |
| 239 | // OpenAI-compatible providers |
| 240 | createOpenAISubclassTests(Groq, { |
| 241 | providerName: "groq", |
| 242 | defaultApiBase: "https://api.groq.com/openai/v1/", |
| 243 | modelConversions: { |
| 244 | "mistral-8x7b": "mixtral-8x7b-32768", |
| 245 | "llama3-8b": "llama3-8b-8192", |
| 246 | "llama3-70b": "llama3-70b-8192", |
| 247 | }, |
| 248 | modelConversionContent: "[INST] hello [/INST]", |
| 249 | modelConversionMaxTokens: 4096, |
| 250 | }); |
| 251 | |
| 252 | createOpenAISubclassTests(Fireworks, { |
| 253 | providerName: "fireworks", |
| 254 | defaultApiBase: "https://api.fireworks.ai/inference/v1/", |
| 255 | modelConversions: { |
| 256 | "starcoder-7b": "accounts/fireworks/models/starcoder-7b", |
| 257 | }, |
| 258 | modelConversionContent: |
| 259 | "<|im_start|>user\nhello<|im_end|>\n<|im_start|>assistant\n", |
| 260 | }); |
| 261 | |
| 262 | createOpenAISubclassTests(Together, { |
| 263 | providerName: "together", |
| 264 | defaultApiBase: "https://api.together.xyz/v1/", |
| 265 | modelConversions: { |
| 266 | "codellama-7b": "codellama-7b", |
| 267 | "llama3-8b": "meta-llama/Llama-3-8b-chat-hf", |
| 268 | "mistral-8x7b": "mistralai/Mixtral-8x7B-Instruct-v0.1", |
| 269 | }, |
| 270 | modelConversionContent: "hello", |
| 271 | customStreamCompleteEndpoint: "completions", |
| 272 | }); |
| 273 | |
| 274 | createOpenAISubclassTests(Deepseek, { |
| 275 | providerName: "deepseek", |
| 276 | defaultApiBase: "https://api.deepseek.com/", |
| 277 | }); |
| 278 | |
| 279 | createOpenAISubclassTests(OpenRouter, { |
| 280 | providerName: "openrouter", |
| 281 | defaultApiBase: "https://openrouter.ai/api/v1/", |
| 282 | }); |
| 283 | |
| 284 | createOpenAISubclassTests(xAI, { |
| 285 | providerName: "xAI", |
| 286 | defaultApiBase: "https://api.x.ai/v1/", |
| 287 | modelConversions: { |
| 288 | "grok-beta": "grok-beta", |
| 289 | }, |
| 290 | modelConversionContent: "hello", |
| 291 | }); |
| 292 | |
| 293 | createOpenAISubclassTests(Mistral, { |
| 294 | providerName: "mistral", |
| 295 | defaultApiBase: "https://api.mistral.ai/v1/", |
| 296 | modelConversions: { |
| 297 | "mistral-7b": "mistral-7b", |
| 298 | "mistral-8x7b": "open-mixtral-8x7b", |
| 299 | }, |
| 300 | modelConversionContent: "hello", |
| 301 | }); |
| 302 | |
| 303 | createOpenAISubclassTests(LMStudio, { |
| 304 | providerName: "lmstudio", |
| 305 | defaultApiBase: "http://localhost:1234/v1/", |
| 306 | }); |
| 307 | |
| 308 | createOpenAISubclassTests(Cerebras, { |
| 309 | providerName: "cerebras", |
| 310 | defaultApiBase: "https://api.cerebras.ai/v1/", |
| 311 | modelConversions: { |
| 312 | "llama3.1-8b": "llama3.1-8b", |
| 313 | "llama3.1-70b": "llama3.1-70b", |
| 314 | }, |
| 315 | modelConversionContent: |
| 316 | "<|start_header_id|>user<|end_header_id|>\nhello<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n", |
| 317 | }); |
| 318 | |
| 319 | createOpenAISubclassTests(DeepInfra, { |
| 320 | providerName: "deepinfra", |
| 321 | defaultApiBase: "https://api.deepinfra.com/v1/openai/", |
| 322 | customEmbeddingsUrl: |
| 323 | "https://api.deepinfra.com/v1/inference/text-embedding-ada-002", |
| 324 | customEmbeddingsHeaders: { |
| 325 | Authorization: "bearer test-api-key", |
| 326 | }, |
| 327 | customEmbeddingsBody: { |
| 328 | inputs: ["Hello", "World"], |
| 329 | }, |
| 330 | }); |
| 331 | |
| 332 | createOpenAISubclassTests(Nvidia, { |
| 333 | providerName: "nvidia", |
| 334 | defaultApiBase: "https://integrate.api.nvidia.com/v1/", |
| 335 | customEmbeddingsHeaders: { |
| 336 | Authorization: "Bearer test-api-key", |
| 337 | "Content-Type": "application/json", |
| 338 | }, |
| 339 | customEmbeddingsBody: { |
| 340 | input: ["Hello", "World"], |
| 341 | model: "text-embedding-ada-002", |
| 342 | input_type: "passage", |
| 343 | truncate: "END", |
| 344 | }, |
| 345 | }); |
| 346 | |
| 347 | createOpenAISubclassTests(SambaNova, { |
| 348 | providerName: "sambanova", |
| 349 | defaultApiBase: "https://api.sambanova.ai/v1/", |
| 350 | }); |
| 351 | |
| 352 | createOpenAISubclassTests(Scaleway, { |
| 353 | providerName: "scaleway", |
| 354 | defaultApiBase: "https://api.scaleway.ai/v1/", |
| 355 | }); |
| 356 | |
| 357 | createOpenAISubclassTests(Venice, { |
| 358 | providerName: "venice", |
| 359 | defaultApiBase: "https://api.venice.ai/api/v1/", |
| 360 | }); |
| 361 | |
| 362 | createOpenAISubclassTests(Moonshot, { |
| 363 | providerName: "moonshot", |
| 364 | defaultApiBase: "https://api.moonshot.cn/v1/", |
| 365 | }); |
| 366 | |
| 367 | createOpenAISubclassTests(Novita, { |
| 368 | providerName: "novita", |
| 369 | defaultApiBase: "https://api.novita.ai/v3/openai/", |
| 370 | customStreamCompleteEndpoint: "completions", |
| 371 | }); |
| 372 | |
| 373 | createOpenAISubclassTests(SiliconFlow, { |
| 374 | providerName: "siliconflow", |
| 375 | defaultApiBase: "https://api.siliconflow.cn/v1/", |
| 376 | }); |
| 377 | |
| 378 | createOpenAISubclassTests(Kindo, { |
| 379 | providerName: "kindo", |
| 380 | defaultApiBase: "https://llm.kindo.ai/v1/", |
| 381 | }); |
| 382 | |
| 383 | createOpenAISubclassTests(Azure, { |
| 384 | providerName: "azure", |
| 385 | }); |
| 386 | |
| 387 | createOpenAISubclassTests(Inception, { |
| 388 | providerName: "inception", |
| 389 | defaultApiBase: "https://api.inceptionlabs.ai/v1/", |
| 390 | customBodyOptions: { |
| 391 | temperature: 0.0, |
| 392 | presence_penalty: 1.5, |
| 393 | stop: ["<|endoftext|>"], |
| 394 | }, |
| 395 | }); |
| 396 | |
| 397 | createOpenAISubclassTests(Docker, { |
| 398 | providerName: "docker", |
| 399 | defaultApiBase: "http://localhost:12434/engines/v1/", |
| 400 | }); |
| 401 | |
| 402 | createOpenAISubclassTests(Voyage, { |
| 403 | providerName: "voyage", |
| 404 | defaultApiBase: "https://api.voyageai.com/v1/", |
| 405 | }); |
| 406 | |
| 407 | createOpenAISubclassTests(Vllm, { |
| 408 | providerName: "vllm", |
| 409 | defaultApiBase: "https://api.openai.com/v1/", |
| 410 | }); |
| 411 | |
| 412 | createOpenAISubclassTests(TextGenWebUI, { |
| 413 | providerName: "text-gen-webui", |
| 414 | defaultApiBase: "http://localhost:5000/v1/", |
| 415 | }); |
| 416 | |
| 417 | createOpenAISubclassTests(FunctionNetwork, { |
| 418 | providerName: "function-network", |
| 419 | defaultApiBase: "https://api.function.network/v1/", |
| 420 | customEmbeddingsHeaders: { |
| 421 | Authorization: "Bearer test-api-key", |
| 422 | "Content-Type": "application/json", |
| 423 | }, |
| 424 | }); |
| 425 | |
| 426 | createOpenAISubclassTests(NCompass, { |
| 427 | providerName: "ncompass", |
| 428 | defaultApiBase: "https://api.ncompass.tech/v1/", |
| 429 | customEmbeddingsUrl: "https://api.gcp.ncompass.tech/v1/embeddings", |
| 430 | customEmbeddingsHeaders: { |
| 431 | Authorization: "Bearer test-api-key", |
| 432 | "Content-Type": "application/json", |
| 433 | }, |
| 434 | customEmbeddingsBody: { |
| 435 | input: ["Hello", "World"], |
| 436 | model: "text-embedding-ada-002", |
| 437 | }, |
| 438 | }); |
| 439 | |
| 440 | createOpenAISubclassTests(LlamaStack, { |
| 441 | providerName: "llamastack", |
| 442 | defaultApiBase: "http://localhost:8321/v1/openai/v1/", |
| 443 | }); |
| 444 | |
| 445 | createOpenAISubclassTests(Nebius, { |
| 446 | providerName: "nebius", |
| 447 | defaultApiBase: "https://api.studio.nebius.ai/v1/", |
| 448 | }); |
| 449 | |
| 450 | createOpenAISubclassTests(OVHcloud, { |
| 451 | providerName: "ovhcloud", |
| 452 | defaultApiBase: "https://oai.endpoints.kepler.ai.cloud.ovh.net/v1/", |
| 453 | }); |