| 1 | import { ModelConfig } from "@continuedev/config-yaml"; |
| 2 | import * as dotenv from "dotenv"; |
| 3 | import { vi } from "vitest"; |
| 4 | import { DEEPSEEK_API_BASE } from "../apis/DeepSeek.js"; |
| 5 | import { INCEPTION_API_BASE } from "../apis/Inception.js"; |
| 6 | import { OpenAIApi } from "../apis/OpenAI.js"; |
| 7 | import { constructLlmApi } from "../index.js"; |
| 8 | import { getLlmApi, testChat, testEmbed, testFim, testRerank } from "./util.js"; |
| 9 | |
| 10 | dotenv.config(); |
| 11 | |
| 12 | export interface TestConfigOptions { |
| 13 | skipTools: boolean; |
| 14 | expectUsage?: boolean; |
| 15 | skipSystemMessage?: boolean; |
| 16 | } |
| 17 | |
| 18 | function testConfig(_config: ModelConfig & { options?: TestConfigOptions }) { |
| 19 | const { options, ...config } = _config; |
| 20 | const model = config.model; |
| 21 | const api = getLlmApi({ |
| 22 | provider: config.provider as any, |
| 23 | apiKey: config.apiKey, |
| 24 | apiBase: config.apiBase, |
| 25 | env: config.env, |
| 26 | }); |
| 27 | |
| 28 | if ( |
| 29 | ["chat", "summarize", "edit", "apply"].some((role) => |
| 30 | config.roles?.includes(role as any), |
| 31 | ) |
| 32 | ) { |
| 33 | testChat(api, model, options); |
| 34 | } |
| 35 | |
| 36 | if (config.roles?.includes("embed")) { |
| 37 | testEmbed(api, model); |
| 38 | } |
| 39 | |
| 40 | if (config.roles?.includes("rerank")) { |
| 41 | testRerank(api, model); |
| 42 | } |
| 43 | |
| 44 | if (config.roles?.includes("autocomplete")) { |
| 45 | testFim(api, model); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | const TESTS: Omit<ModelConfig & { options?: TestConfigOptions }, "name">[] = [ |
| 50 | { |
| 51 | provider: "openai", |
| 52 | model: "gpt-4o", |
| 53 | apiKey: process.env.OPENAI_API_KEY!, |
| 54 | roles: ["chat"], |
| 55 | options: { |
| 56 | skipTools: false, |
| 57 | expectUsage: true, |
| 58 | }, |
| 59 | }, |
| 60 | { |
| 61 | provider: "openai", |
| 62 | model: "gpt-4o-mini", |
| 63 | apiKey: process.env.OPENAI_API_KEY!, |
| 64 | roles: ["chat"], |
| 65 | options: { |
| 66 | skipTools: false, |
| 67 | expectUsage: true, |
| 68 | }, |
| 69 | }, |
| 70 | { |
| 71 | provider: "anthropic", |
| 72 | model: "claude-3-5-haiku-latest", |
| 73 | apiKey: process.env.ANTHROPIC_API_KEY!, |
| 74 | roles: ["chat"], |
| 75 | options: { |
| 76 | skipTools: false, |
| 77 | expectUsage: true, |
| 78 | }, |
| 79 | }, |
| 80 | { |
| 81 | provider: "gemini", |
| 82 | model: "gemini-1.5-flash-latest", |
| 83 | apiKey: process.env.GEMINI_API_KEY!, |
| 84 | roles: ["chat"], |
| 85 | options: { |
| 86 | skipTools: false, |
| 87 | expectUsage: true, |
| 88 | }, |
| 89 | }, |
| 90 | { |
| 91 | provider: "gemini", |
| 92 | model: "gemini-2.5-flash", |
| 93 | apiKey: process.env.GEMINI_API_KEY!, |
| 94 | roles: ["chat"], |
| 95 | options: { |
| 96 | skipTools: false, |
| 97 | expectUsage: true, |
| 98 | }, |
| 99 | }, |
| 100 | { |
| 101 | provider: "mistral", |
| 102 | model: "codestral-latest", |
| 103 | apiKey: process.env.MISTRAL_API_KEY!, |
| 104 | roles: ["chat", "autocomplete"], |
| 105 | options: { |
| 106 | skipTools: false, |
| 107 | expectUsage: true, |
| 108 | }, |
| 109 | }, |
| 110 | // { |
| 111 | // provider: "deepseek", |
| 112 | // model: "deepseek-coder", |
| 113 | // apiKey: process.env.DEEPSEEK_API_KEY!, |
| 114 | // roles: ["autocomplete"], |
| 115 | // }, |
| 116 | { |
| 117 | provider: "openai", |
| 118 | model: "text-embedding-3-small", |
| 119 | apiKey: process.env.OPENAI_API_KEY!, |
| 120 | roles: ["embed"], |
| 121 | }, |
| 122 | { |
| 123 | provider: "voyage", |
| 124 | model: "voyage-code-3", |
| 125 | apiKey: process.env.VOYAGE_API_KEY!, |
| 126 | roles: ["embed"], |
| 127 | }, |
| 128 | // { |
| 129 | // provider: "cohere", |
| 130 | // model: "embed-v4.0", |
| 131 | // apiKey: process.env.COHERE_API_KEY!, |
| 132 | // roles: ["embed"], |
| 133 | // }, |
| 134 | // { |
| 135 | // provider: "gemini", |
| 136 | // model: "models/text-embedding-004", |
| 137 | // apiKey: process.env.GEMINI_API_KEY!, |
| 138 | // roles: ["embed"], |
| 139 | // }, |
| 140 | { |
| 141 | provider: "voyage", |
| 142 | model: "rerank-lite-1", |
| 143 | apiKey: process.env.VOYAGE_API_KEY!, |
| 144 | roles: ["rerank"], |
| 145 | }, |
| 146 | { |
| 147 | provider: "relace", |
| 148 | model: "instant-apply", |
| 149 | apiKey: process.env.RELACE_API_KEY!, |
| 150 | roles: ["chat"], |
| 151 | options: { |
| 152 | skipTools: true, |
| 153 | expectUsage: true, |
| 154 | skipSystemMessage: true, |
| 155 | }, |
| 156 | }, |
| 157 | { |
| 158 | provider: "inception", |
| 159 | model: "mercury-coder", |
| 160 | apiKey: process.env.INCEPTION_API_KEY!, |
| 161 | roles: ["chat"], |
| 162 | options: { |
| 163 | skipTools: false, |
| 164 | expectUsage: true, |
| 165 | }, |
| 166 | }, |
| 167 | // { |
| 168 | // provider: "cohere", |
| 169 | // model: "rerank-v3.5", |
| 170 | // apiKey: process.env.COHERE_API_KEY!, |
| 171 | // roles: ["rerank"], |
| 172 | // }, |
| 173 | { |
| 174 | provider: "azure", |
| 175 | model: "gpt-4.1", |
| 176 | apiBase: "https://continue-openai.openai.azure.com", |
| 177 | apiKey: process.env.AZURE_OPENAI_GPT41_API_KEY, |
| 178 | roles: ["chat"], |
| 179 | env: { |
| 180 | deployment: "gpt-4.1", |
| 181 | apiVersion: "2024-02-15-preview", |
| 182 | apiType: "azure-openai", |
| 183 | }, |
| 184 | }, |
| 185 | ]; |
| 186 | |
| 187 | if (process.env.IGNORE_API_KEY_TESTS === "true") { |
| 188 | test("Skipping API key tests", () => { |
| 189 | console.log("Skipping API key tests due to IGNORE_API_KEY_TESTS being set"); |
| 190 | }); |
| 191 | } else { |
| 192 | TESTS.forEach((config) => { |
| 193 | describe(`${config.provider}/${config.model}`, () => { |
| 194 | vi.setConfig({ testTimeout: 30000 }); |
| 195 | testConfig({ name: config.model, ...config }); |
| 196 | }); |
| 197 | }); |
| 198 | } |
| 199 | |
| 200 | describe("Configuration", () => { |
| 201 | it("should configure DeepSeek OpenAI client with correct apiBase and apiKey", () => { |
| 202 | const deepseek = constructLlmApi({ |
| 203 | provider: "deepseek", |
| 204 | apiKey: "sk-xxx", |
| 205 | }); |
| 206 | |
| 207 | expect((deepseek as OpenAIApi).openai.baseURL).toBe(DEEPSEEK_API_BASE); |
| 208 | expect((deepseek as OpenAIApi).openai.apiKey).toBe("sk-xxx"); |
| 209 | |
| 210 | const deepseek2 = constructLlmApi({ |
| 211 | provider: "deepseek", |
| 212 | apiKey: "sk-xxx", |
| 213 | apiBase: "https://api.example.com", |
| 214 | }); |
| 215 | expect((deepseek2 as OpenAIApi).openai.baseURL).toBe( |
| 216 | "https://api.example.com", |
| 217 | ); |
| 218 | }); |
| 219 | |
| 220 | it("should configure Inception OpenAI client with correct apiBase and apiKey", () => { |
| 221 | const inception = constructLlmApi({ |
| 222 | provider: "inception", |
| 223 | apiKey: "sk-xxx", |
| 224 | }); |
| 225 | |
| 226 | expect((inception as OpenAIApi).openai.baseURL).toBe(INCEPTION_API_BASE); |
| 227 | expect((inception as OpenAIApi).openai.apiKey).toBe("sk-xxx"); |
| 228 | |
| 229 | const inception2 = constructLlmApi({ |
| 230 | provider: "inception", |
| 231 | apiKey: "sk-xxx", |
| 232 | apiBase: "https://api.example.com", |
| 233 | }); |
| 234 | expect((inception2 as OpenAIApi).openai.baseURL).toBe( |
| 235 | "https://api.example.com", |
| 236 | ); |
| 237 | }); |
| 238 | |
| 239 | it("should configure Azure OpenAI client with root URL and trailing slash", () => { |
| 240 | const azure = constructLlmApi({ |
| 241 | provider: "azure", |
| 242 | apiKey: "sk-xxx", |
| 243 | apiBase: "https://test-azure-openai.azure.com/", |
| 244 | env: { |
| 245 | deployment: "gpt-4.1", |
| 246 | apiType: "azure-openai", |
| 247 | apiVersion: "2023-05-15", |
| 248 | }, |
| 249 | }); |
| 250 | |
| 251 | // The Azure client modifies the baseURL to include the deployment |
| 252 | expect((azure as OpenAIApi).openai.baseURL).toBe( |
| 253 | "https://test-azure-openai.azure.com/openai/deployments/gpt-4.1", |
| 254 | ); |
| 255 | expect((azure as OpenAIApi).openai.apiKey).toBe("sk-xxx"); |
| 256 | }); |
| 257 | |
| 258 | it("should configure Azure OpenAI client with path and trailing slash", () => { |
| 259 | const azure = constructLlmApi({ |
| 260 | provider: "azure", |
| 261 | apiKey: "sk-xxx", |
| 262 | apiBase: "https://test-azure-openai.azure.com/v1/", |
| 263 | env: { |
| 264 | deployment: "gpt-4.1", |
| 265 | apiType: "azure-openai", |
| 266 | apiVersion: "2023-05-15", |
| 267 | }, |
| 268 | }); |
| 269 | |
| 270 | // The Azure client modifies the baseURL to include the deployment |
| 271 | expect((azure as OpenAIApi).openai.baseURL).toBe( |
| 272 | "https://test-azure-openai.azure.com/v1/openai/deployments/gpt-4.1", |
| 273 | ); |
| 274 | expect((azure as OpenAIApi).openai.apiKey).toBe("sk-xxx"); |
| 275 | }); |
| 276 | |
| 277 | it("should configure Azure OpenAI client with root URL and no trailing slash", () => { |
| 278 | const azure = constructLlmApi({ |
| 279 | provider: "azure", |
| 280 | apiKey: "sk-xxx", |
| 281 | apiBase: "https://test-azure-openai.azure.com", |
| 282 | env: { |
| 283 | deployment: "gpt-4.1", |
| 284 | apiType: "azure-openai", |
| 285 | apiVersion: "2023-05-15", |
| 286 | }, |
| 287 | }); |
| 288 | |
| 289 | // The Azure client modifies the baseURL to include the deployment |
| 290 | expect((azure as OpenAIApi).openai.baseURL).toBe( |
| 291 | "https://test-azure-openai.azure.com/openai/deployments/gpt-4.1", |
| 292 | ); |
| 293 | expect((azure as OpenAIApi).openai.apiKey).toBe("sk-xxx"); |
| 294 | }); |
| 295 | |
| 296 | it("should configure Azure OpenAI client with path and no trailing slash", () => { |
| 297 | const azure = constructLlmApi({ |
| 298 | provider: "azure", |
| 299 | apiKey: "sk-xxx", |
| 300 | apiBase: "https://test-azure-openai.azure.com/v1", |
| 301 | env: { |
| 302 | deployment: "gpt-4.1", |
| 303 | apiType: "azure-openai", |
| 304 | apiVersion: "2023-05-15", |
| 305 | }, |
| 306 | }); |
| 307 | |
| 308 | // The Azure client modifies the baseURL to include the deployment |
| 309 | expect((azure as OpenAIApi).openai.baseURL).toBe( |
| 310 | "https://test-azure-openai.azure.com/v1/openai/deployments/gpt-4.1", |
| 311 | ); |
| 312 | expect((azure as OpenAIApi).openai.apiKey).toBe("sk-xxx"); |
| 313 | }); |
| 314 | |
| 315 | describe("ollama api base", () => { |
| 316 | it('should have correct default API base for "ollama"', () => { |
| 317 | const ollama = constructLlmApi({ |
| 318 | provider: "ollama", |
| 319 | }); |
| 320 | |
| 321 | expect((ollama as OpenAIApi).openai.baseURL).toBe( |
| 322 | "http://localhost:11434/v1/", |
| 323 | ); |
| 324 | }); |
| 325 | it('should append /v1 to apiBase for "ollama"', () => { |
| 326 | const ollama = constructLlmApi({ |
| 327 | provider: "ollama", |
| 328 | apiBase: "http://localhost:123", |
| 329 | }); |
| 330 | |
| 331 | expect((ollama as OpenAIApi).openai.baseURL).toBe( |
| 332 | "http://localhost:123/v1/", |
| 333 | ); |
| 334 | }); |
| 335 | it("should not reappend /v1 to apibase for ollama if it is already present", () => { |
| 336 | const ollama = constructLlmApi({ |
| 337 | provider: "ollama", |
| 338 | apiBase: "http://localhost:123/v1/", |
| 339 | }); |
| 340 | |
| 341 | expect((ollama as OpenAIApi).openai.baseURL).toBe( |
| 342 | "http://localhost:123/v1/", |
| 343 | ); |
| 344 | }); |
| 345 | it("should append v1 if apiBase is like myhostv1/", () => { |
| 346 | const ollama = constructLlmApi({ |
| 347 | provider: "ollama", |
| 348 | apiBase: "https://myhostv1/", |
| 349 | }); |
| 350 | expect((ollama as OpenAIApi).openai.baseURL).toBe("https://myhostv1/v1/"); |
| 351 | }); |
| 352 | it("should preserve query params and append v1 in apibase", () => { |
| 353 | const ollama = constructLlmApi({ |
| 354 | provider: "ollama", |
| 355 | apiBase: "https://test.com:123/ollama-server?x=1", |
| 356 | }); |
| 357 | expect((ollama as OpenAIApi).openai.baseURL).toBe( |
| 358 | "https://test.com:123/ollama-server/v1/?x=1", |
| 359 | ); |
| 360 | }); |
| 361 | }); |
| 362 | }); |