| 1 | import { createOpenAISubclassTests } from "./test-utils/openai-test-utils.js"; |
| 2 | |
| 3 | // Import core 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 CometAPI from "./CometAPI.js"; |
| 17 | |
| 18 | // Base OpenAI tests |
| 19 | import { afterEach, describe, expect, test, vi } from "vitest"; |
| 20 | import { ILLM } from "../../index.js"; |
| 21 | |
| 22 | interface LlmTestCase { |
| 23 | llm: ILLM; |
| 24 | methodToTest: keyof ILLM; |
| 25 | params: any[]; |
| 26 | expectedRequest: { |
| 27 | url: string; |
| 28 | method: string; |
| 29 | headers?: Record<string, string>; |
| 30 | body?: Record<string, any>; |
| 31 | }; |
| 32 | mockResponse?: any; |
| 33 | mockStream?: any[]; |
| 34 | } |
| 35 | |
| 36 | function createMockStream(mockStream: any[]) { |
| 37 | const encoder = new TextEncoder(); |
| 38 | return new ReadableStream({ |
| 39 | start(controller) { |
| 40 | for (const chunk of mockStream) { |
| 41 | controller.enqueue( |
| 42 | encoder.encode( |
| 43 | `data: ${ |
| 44 | typeof chunk === "string" ? chunk : JSON.stringify(chunk) |
| 45 | }\n\n`, |
| 46 | ), |
| 47 | ); |
| 48 | } |
| 49 | controller.close(); |
| 50 | }, |
| 51 | }); |
| 52 | } |
| 53 | |
| 54 | function setupMockFetch(mockResponse?: any, mockStream?: any[]) { |
| 55 | const mockFetch = vi.fn(); |
| 56 | |
| 57 | if (mockStream) { |
| 58 | const stream = createMockStream(mockStream); |
| 59 | mockFetch.mockResolvedValue( |
| 60 | new Response(stream, { |
| 61 | headers: { |
| 62 | "Content-Type": "text/event-stream", |
| 63 | }, |
| 64 | }), |
| 65 | ); |
| 66 | } else { |
| 67 | mockFetch.mockResolvedValue( |
| 68 | new Response(JSON.stringify(mockResponse), { |
| 69 | headers: { "Content-Type": "application/json" }, |
| 70 | }), |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | return mockFetch; |
| 75 | } |
| 76 | |
| 77 | function setupReadableStreamPolyfill() { |
| 78 | // This can be removed if https://github.com/nodejs/undici/issues/2888 is resolved |
| 79 | // @ts-ignore |
| 80 | const originalFrom = ReadableStream.from; |
| 81 | // @ts-ignore |
| 82 | ReadableStream.from = (body) => { |
| 83 | if (body?.source) { |
| 84 | return body; |
| 85 | } |
| 86 | return originalFrom(body); |
| 87 | }; |
| 88 | } |
| 89 | |
| 90 | async function executeLlmMethod( |
| 91 | llm: ILLM, |
| 92 | methodToTest: keyof ILLM, |
| 93 | params: any[], |
| 94 | ) { |
| 95 | if (typeof (llm as any)[methodToTest] !== "function") { |
| 96 | throw new Error( |
| 97 | `Method ${String(methodToTest)} does not exist on the LLM instance.`, |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | const result = await (llm as any)[methodToTest](...params); |
| 102 | if (result?.next) { |
| 103 | for await (const _ of result) { |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | function assertFetchCall(mockFetch: any, expectedRequest: any) { |
| 109 | expect(mockFetch).toHaveBeenCalledTimes(1); |
| 110 | const [url, options] = mockFetch.mock.calls[0]; |
| 111 | |
| 112 | expect(url.toString()).toBe(expectedRequest.url); |
| 113 | expect(options.method).toBe(expectedRequest.method); |
| 114 | |
| 115 | if (expectedRequest.headers) { |
| 116 | expect(options.headers).toEqual( |
| 117 | expect.objectContaining(expectedRequest.headers), |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | if (expectedRequest.body) { |
| 122 | const actualBody = JSON.parse(options.body as string); |
| 123 | expect(actualBody).toEqual(expectedRequest.body); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | async function runLlmTest(testCase: LlmTestCase) { |
| 128 | const { |
| 129 | llm, |
| 130 | methodToTest, |
| 131 | params, |
| 132 | expectedRequest, |
| 133 | mockResponse, |
| 134 | mockStream, |
| 135 | } = testCase; |
| 136 | |
| 137 | const mockFetch = setupMockFetch(mockResponse, mockStream); |
| 138 | setupReadableStreamPolyfill(); |
| 139 | |
| 140 | (llm as any).fetch = mockFetch; |
| 141 | |
| 142 | // Disable OpenAI adapter to use our custom fetch mock |
| 143 | (llm as any).useOpenAIAdapterFor = []; |
| 144 | |
| 145 | await executeLlmMethod(llm, methodToTest, params); |
| 146 | assertFetchCall(mockFetch, expectedRequest); |
| 147 | } |
| 148 | |
| 149 | describe("OpenAI", () => { |
| 150 | afterEach(() => { |
| 151 | vi.clearAllMocks(); |
| 152 | }); |
| 153 | |
| 154 | test("streamChat should send a valid request", async () => { |
| 155 | const openai = new OpenAI({ |
| 156 | apiKey: "test-api-key", |
| 157 | model: "gpt-4", |
| 158 | apiBase: "https://api.openai.com/v1/", |
| 159 | }); |
| 160 | |
| 161 | await runLlmTest({ |
| 162 | llm: openai, |
| 163 | methodToTest: "streamChat", |
| 164 | params: [ |
| 165 | [{ role: "user", content: "hello" }], |
| 166 | new AbortController().signal, |
| 167 | ], |
| 168 | expectedRequest: { |
| 169 | url: "https://api.openai.com/v1/chat/completions", |
| 170 | method: "POST", |
| 171 | headers: { |
| 172 | "Content-Type": "application/json", |
| 173 | Authorization: "Bearer test-api-key", |
| 174 | "api-key": "test-api-key", |
| 175 | }, |
| 176 | body: { |
| 177 | model: "gpt-4", |
| 178 | messages: [{ role: "user", content: "hello" }], |
| 179 | stream: true, |
| 180 | max_tokens: 2048, |
| 181 | }, |
| 182 | }, |
| 183 | mockStream: [ |
| 184 | { choices: [{ delta: { content: "Hello" } }] }, |
| 185 | { choices: [{ delta: { content: " world" } }] }, |
| 186 | ], |
| 187 | }); |
| 188 | }); |
| 189 | }); |
| 190 | |
| 191 | // Core OpenAI-compatible providers |
| 192 | createOpenAISubclassTests(Groq, { |
| 193 | providerName: "groq", |
| 194 | defaultApiBase: "https://api.groq.com/openai/v1/", |
| 195 | modelConversions: { |
| 196 | "mistral-8x7b": "mistral-8x7b", |
| 197 | "llama3-8b": "llama3-8b-8192", |
| 198 | }, |
| 199 | modelConversionContent: "[INST] hello [/INST]", |
| 200 | modelConversionMaxTokens: 4096, |
| 201 | }); |
| 202 | |
| 203 | createOpenAISubclassTests(Fireworks, { |
| 204 | providerName: "fireworks", |
| 205 | defaultApiBase: "https://api.fireworks.ai/inference/v1/", |
| 206 | modelConversions: { |
| 207 | "starcoder-7b": "starcoder-7b", |
| 208 | }, |
| 209 | modelConversionContent: |
| 210 | "<|im_start|>user\nhello<|im_end|>\n<|im_start|>assistant\n", |
| 211 | }); |
| 212 | |
| 213 | createOpenAISubclassTests(Together, { |
| 214 | providerName: "together", |
| 215 | defaultApiBase: "https://api.together.xyz/v1/", |
| 216 | modelConversions: { |
| 217 | "codellama-7b": "codellama-7b", |
| 218 | "llama3-8b": "meta-llama/Llama-3-8b-chat-hf", |
| 219 | }, |
| 220 | modelConversionContent: "hello", |
| 221 | customStreamCompleteEndpoint: "completions", |
| 222 | }); |
| 223 | |
| 224 | createOpenAISubclassTests(Deepseek, { |
| 225 | providerName: "deepseek", |
| 226 | defaultApiBase: "https://api.deepseek.com/", |
| 227 | }); |
| 228 | |
| 229 | createOpenAISubclassTests(OpenRouter, { |
| 230 | providerName: "openrouter", |
| 231 | defaultApiBase: "https://openrouter.ai/api/v1/", |
| 232 | }); |
| 233 | |
| 234 | createOpenAISubclassTests(xAI, { |
| 235 | providerName: "xAI", |
| 236 | defaultApiBase: "https://api.x.ai/v1/", |
| 237 | modelConversions: { |
| 238 | "grok-beta": "grok-beta", |
| 239 | }, |
| 240 | modelConversionContent: "hello", |
| 241 | }); |
| 242 | |
| 243 | createOpenAISubclassTests(Mistral, { |
| 244 | providerName: "mistral", |
| 245 | defaultApiBase: "https://api.mistral.ai/v1/", |
| 246 | modelConversions: { |
| 247 | "mistral-7b": "mistral-7b", |
| 248 | "mistral-8x7b": "open-mixtral-8x7b", |
| 249 | }, |
| 250 | modelConversionContent: "hello", |
| 251 | }); |
| 252 | |
| 253 | createOpenAISubclassTests(LMStudio, { |
| 254 | providerName: "lmstudio", |
| 255 | defaultApiBase: "http://localhost:1234/v1/", |
| 256 | }); |
| 257 | |
| 258 | createOpenAISubclassTests(Cerebras, { |
| 259 | providerName: "cerebras", |
| 260 | defaultApiBase: "https://api.cerebras.ai/v1/", |
| 261 | modelConversions: { |
| 262 | "llama3.1-8b": "llama3.1-8b", |
| 263 | "llama3.1-70b": "llama3.1-70b", |
| 264 | }, |
| 265 | modelConversionContent: |
| 266 | "<|start_header_id|>user<|end_header_id|>\nhello<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n", |
| 267 | }); |
| 268 | |
| 269 | createOpenAISubclassTests(DeepInfra, { |
| 270 | providerName: "deepinfra", |
| 271 | defaultApiBase: "https://api.deepinfra.com/v1/openai/", |
| 272 | customEmbeddingsUrl: |
| 273 | "https://api.deepinfra.com/v1/inference/text-embedding-ada-002", |
| 274 | customEmbeddingsHeaders: { |
| 275 | Authorization: "bearer test-api-key", |
| 276 | }, |
| 277 | customEmbeddingsBody: { |
| 278 | inputs: ["Hello", "World"], |
| 279 | }, |
| 280 | }); |
| 281 | |
| 282 | createOpenAISubclassTests(Nvidia, { |
| 283 | providerName: "nvidia", |
| 284 | defaultApiBase: "https://integrate.api.nvidia.com/v1/", |
| 285 | customEmbeddingsHeaders: { |
| 286 | Authorization: "Bearer test-api-key", |
| 287 | "Content-Type": "application/json", |
| 288 | }, |
| 289 | customEmbeddingsBody: { |
| 290 | input: ["Hello", "World"], |
| 291 | model: "text-embedding-ada-002", |
| 292 | input_type: "passage", |
| 293 | truncate: "END", |
| 294 | }, |
| 295 | }); |
| 296 | |
| 297 | createOpenAISubclassTests(CometAPI, { |
| 298 | providerName: "cometapi", |
| 299 | defaultApiBase: "https://api.cometapi.com/v1/", |
| 300 | modelConversions: { |
| 301 | "gpt-5-mini": "gpt-5-mini", |
| 302 | "claude-4-sonnet": "claude-sonnet-4-20250514", |
| 303 | }, |
| 304 | modelConversionContent: "hello", |
| 305 | }); |