|
1 | | -// npx jest src/api/providers/__tests__/openai-native.test.ts |
| 1 | +// npx vitest run api/providers/__tests__/openai-native.spec.ts |
2 | 2 |
|
| 3 | +import { vitest } from "vitest" |
3 | 4 | import { Anthropic } from "@anthropic-ai/sdk" |
4 | 5 |
|
5 | 6 | import { OpenAiNativeHandler } from "../openai-native" |
6 | 7 | import { ApiHandlerOptions } from "../../../shared/api" |
7 | 8 |
|
8 | 9 | // Mock OpenAI client |
9 | | -const mockCreate = jest.fn() |
| 10 | +const mockCreate = vitest.fn() |
10 | 11 |
|
11 | | -jest.mock("openai", () => { |
| 12 | +vitest.mock("openai", () => { |
12 | 13 | return { |
13 | 14 | __esModule: true, |
14 | | - default: jest.fn().mockImplementation(() => ({ |
| 15 | + default: vitest.fn().mockImplementation(() => ({ |
15 | 16 | chat: { |
16 | 17 | completions: { |
17 | 18 | create: mockCreate.mockImplementation(async (options) => { |
@@ -372,6 +373,75 @@ describe("OpenAiNativeHandler", () => { |
372 | 373 | }) |
373 | 374 | }) |
374 | 375 |
|
| 376 | + describe("temperature parameter handling", () => { |
| 377 | + it("should include temperature for models that support it", async () => { |
| 378 | + // Test with gpt-4.1 which supports temperature |
| 379 | + handler = new OpenAiNativeHandler({ |
| 380 | + apiModelId: "gpt-4.1", |
| 381 | + openAiNativeApiKey: "test-api-key", |
| 382 | + }) |
| 383 | + |
| 384 | + await handler.completePrompt("Test prompt") |
| 385 | + expect(mockCreate).toHaveBeenCalledWith({ |
| 386 | + model: "gpt-4.1", |
| 387 | + messages: [{ role: "user", content: "Test prompt" }], |
| 388 | + temperature: 0, |
| 389 | + }) |
| 390 | + }) |
| 391 | + |
| 392 | + it("should strip temperature for o1 family models", async () => { |
| 393 | + const o1Models = ["o1", "o1-preview", "o1-mini"] |
| 394 | + |
| 395 | + for (const modelId of o1Models) { |
| 396 | + handler = new OpenAiNativeHandler({ |
| 397 | + apiModelId: modelId, |
| 398 | + openAiNativeApiKey: "test-api-key", |
| 399 | + }) |
| 400 | + |
| 401 | + mockCreate.mockClear() |
| 402 | + await handler.completePrompt("Test prompt") |
| 403 | + |
| 404 | + const callArgs = mockCreate.mock.calls[0][0] |
| 405 | + // Temperature should be undefined for o1 models |
| 406 | + expect(callArgs.temperature).toBeUndefined() |
| 407 | + expect(callArgs.model).toBe(modelId) |
| 408 | + } |
| 409 | + }) |
| 410 | + |
| 411 | + it("should strip temperature for o3-mini model", async () => { |
| 412 | + handler = new OpenAiNativeHandler({ |
| 413 | + apiModelId: "o3-mini", |
| 414 | + openAiNativeApiKey: "test-api-key", |
| 415 | + }) |
| 416 | + |
| 417 | + await handler.completePrompt("Test prompt") |
| 418 | + |
| 419 | + const callArgs = mockCreate.mock.calls[0][0] |
| 420 | + // Temperature should be undefined for o3-mini models |
| 421 | + expect(callArgs.temperature).toBeUndefined() |
| 422 | + expect(callArgs.model).toBe("o3-mini") |
| 423 | + expect(callArgs.reasoning_effort).toBe("medium") |
| 424 | + }) |
| 425 | + |
| 426 | + it("should strip temperature in streaming mode for unsupported models", async () => { |
| 427 | + handler = new OpenAiNativeHandler({ |
| 428 | + apiModelId: "o1", |
| 429 | + openAiNativeApiKey: "test-api-key", |
| 430 | + }) |
| 431 | + |
| 432 | + const stream = handler.createMessage(systemPrompt, messages) |
| 433 | + // Consume the stream |
| 434 | + for await (const _chunk of stream) { |
| 435 | + // Just consume the stream |
| 436 | + } |
| 437 | + |
| 438 | + const callArgs = mockCreate.mock.calls[0][0] |
| 439 | + expect(callArgs).not.toHaveProperty("temperature") |
| 440 | + expect(callArgs.model).toBe("o1") |
| 441 | + expect(callArgs.stream).toBe(true) |
| 442 | + }) |
| 443 | + }) |
| 444 | + |
375 | 445 | describe("getModel", () => { |
376 | 446 | it("should return model info", () => { |
377 | 447 | const modelInfo = handler.getModel() |
|
0 commit comments