-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add Cerebras as a provider #6392
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e2af681
Add Cerebras as a provider
kevint-cerebras f9f619b
Add suggested changes
kevint-cerebras 6651213
Add suggested changes
kevint-cerebras a13c75f
Merge branch 'main' into roocode-cerebras
kevint-cerebras e69c8de
Fix Cerebras tests
kevint-cerebras c5712cd
Merge branch 'main' into roocode-cerebras
kevint-cerebras File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import type { ModelInfo } from "../model.js" | ||
|
|
||
| // https://inference-docs.cerebras.ai/api-reference/chat-completions | ||
| export type CerebrasModelId = keyof typeof cerebrasModels | ||
|
|
||
| export const cerebrasDefaultModelId: CerebrasModelId = "qwen-3-235b-a22b-instruct-2507" | ||
|
|
||
| export const cerebrasModels = { | ||
| "llama-3.3-70b": { | ||
| maxTokens: 64000, | ||
| contextWindow: 64000, | ||
| supportsImages: false, | ||
| supportsPromptCache: false, | ||
| inputPrice: 0, | ||
| outputPrice: 0, | ||
| description: "Smart model with ~2600 tokens/s", | ||
| }, | ||
| "qwen-3-32b": { | ||
| maxTokens: 64000, | ||
| contextWindow: 64000, | ||
| supportsImages: false, | ||
| supportsPromptCache: false, | ||
| inputPrice: 0, | ||
| outputPrice: 0, | ||
| description: "SOTA coding performance with ~2500 tokens/s", | ||
| }, | ||
| "qwen-3-235b-a22b": { | ||
| maxTokens: 40000, | ||
| contextWindow: 40000, | ||
| supportsImages: false, | ||
| supportsPromptCache: false, | ||
| inputPrice: 0, | ||
| outputPrice: 0, | ||
| description: "SOTA performance with ~1400 tokens/s", | ||
| }, | ||
| "qwen-3-235b-a22b-instruct-2507": { | ||
| maxTokens: 64000, | ||
| contextWindow: 64000, | ||
| supportsImages: false, | ||
| supportsPromptCache: false, | ||
| inputPrice: 0, | ||
| outputPrice: 0, | ||
| description: "SOTA performance with ~1400 tokens/s", | ||
| supportsReasoningEffort: true, | ||
| }, | ||
| } as const satisfies Record<string, ModelInfo> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest" | ||
| import { CerebrasHandler } from "../cerebras" | ||
| import { cerebrasModels, type CerebrasModelId } from "@roo-code/types" | ||
|
|
||
| // Mock fetch globally | ||
| global.fetch = vi.fn() | ||
|
|
||
| describe("CerebrasHandler", () => { | ||
| let handler: CerebrasHandler | ||
| const mockOptions = { | ||
| cerebrasApiKey: "test-api-key", | ||
| apiModelId: "llama-3.3-70b" as CerebrasModelId, | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| handler = new CerebrasHandler(mockOptions) | ||
| }) | ||
|
|
||
| describe("constructor", () => { | ||
| it("should throw error when API key is missing", () => { | ||
| expect(() => new CerebrasHandler({ cerebrasApiKey: "" })).toThrow("Cerebras API key is required") | ||
| }) | ||
|
|
||
| it("should initialize with valid API key", () => { | ||
| expect(() => new CerebrasHandler(mockOptions)).not.toThrow() | ||
| }) | ||
| }) | ||
|
|
||
| describe("getModel", () => { | ||
| it("should return correct model info", () => { | ||
| const { id, info } = handler.getModel() | ||
| expect(id).toBe("llama-3.3-70b") | ||
| expect(info).toEqual(cerebrasModels["llama-3.3-70b"]) | ||
| }) | ||
|
|
||
| it("should fallback to default model when apiModelId is not provided", () => { | ||
| const handlerWithoutModel = new CerebrasHandler({ cerebrasApiKey: "test" }) | ||
| const { id } = handlerWithoutModel.getModel() | ||
| expect(id).toBe("qwen-3-235b-a22b-instruct-2507") // cerebrasDefaultModelId | ||
| }) | ||
| }) | ||
|
|
||
| describe("message conversion", () => { | ||
| it("should strip thinking tokens from assistant messages", () => { | ||
| // This would test the stripThinkingTokens function | ||
| // Implementation details would test the regex functionality | ||
| }) | ||
|
|
||
| it("should flatten complex message content to strings", () => { | ||
| // This would test the flattenMessageContent function | ||
| // Test various content types: strings, arrays, image objects | ||
| }) | ||
|
|
||
| it("should convert OpenAI messages to Cerebras format", () => { | ||
| // This would test the convertToCerebrasMessages function | ||
| // Ensure all messages have string content and proper role/content structure | ||
| }) | ||
| }) | ||
|
|
||
| describe("createMessage", () => { | ||
| it("should make correct API request", async () => { | ||
| // Mock successful API response | ||
| const mockResponse = { | ||
| ok: true, | ||
| body: { | ||
| getReader: () => ({ | ||
| read: vi.fn().mockResolvedValueOnce({ done: true, value: new Uint8Array() }), | ||
| releaseLock: vi.fn(), | ||
| }), | ||
| }, | ||
| } | ||
| vi.mocked(fetch).mockResolvedValueOnce(mockResponse as any) | ||
|
|
||
| const generator = handler.createMessage("System prompt", []) | ||
| // Test that fetch was called with correct parameters | ||
| expect(fetch).toHaveBeenCalledWith( | ||
| "https://api.cerebras.ai/v1/chat/completions", | ||
| expect.objectContaining({ | ||
| method: "POST", | ||
| headers: expect.objectContaining({ | ||
| "Content-Type": "application/json", | ||
| Authorization: "Bearer test-api-key", | ||
| "User-Agent": "roo-cline/1.0.0", | ||
| }), | ||
| }), | ||
| ) | ||
| }) | ||
|
|
||
| it("should handle API errors properly", async () => { | ||
| const mockErrorResponse = { | ||
| ok: false, | ||
| status: 400, | ||
| text: () => Promise.resolve('{"error": "Bad Request"}'), | ||
| } | ||
| vi.mocked(fetch).mockResolvedValueOnce(mockErrorResponse as any) | ||
|
|
||
| const generator = handler.createMessage("System prompt", []) | ||
| await expect(generator.next()).rejects.toThrow("Cerebras API Error: 400") | ||
| }) | ||
|
|
||
| it("should parse streaming responses correctly", async () => { | ||
| // Test streaming response parsing | ||
| // Mock ReadableStream with various data chunks | ||
| // Verify thinking token extraction and usage tracking | ||
| }) | ||
|
|
||
| it("should handle temperature clamping", async () => { | ||
| const handlerWithTemp = new CerebrasHandler({ | ||
| ...mockOptions, | ||
| modelTemperature: 2.0, // Above Cerebras max of 1.5 | ||
| }) | ||
|
|
||
| vi.mocked(fetch).mockResolvedValueOnce({ | ||
| ok: true, | ||
| body: { getReader: () => ({ read: () => Promise.resolve({ done: true }), releaseLock: vi.fn() }) }, | ||
| } as any) | ||
|
|
||
| await handlerWithTemp.createMessage("test", []).next() | ||
|
|
||
| const requestBody = JSON.parse(vi.mocked(fetch).mock.calls[0][1]?.body as string) | ||
| expect(requestBody.temperature).toBe(1.5) // Should be clamped | ||
| }) | ||
| }) | ||
|
|
||
| describe("completePrompt", () => { | ||
| it("should handle non-streaming completion", async () => { | ||
| const mockResponse = { | ||
| ok: true, | ||
| json: () => | ||
| Promise.resolve({ | ||
| choices: [{ message: { content: "Test response" } }], | ||
| }), | ||
| } | ||
| vi.mocked(fetch).mockResolvedValueOnce(mockResponse as any) | ||
|
|
||
| const result = await handler.completePrompt("Test prompt") | ||
| expect(result).toBe("Test response") | ||
| }) | ||
| }) | ||
|
|
||
| describe("token usage and cost calculation", () => { | ||
| it("should track token usage properly", () => { | ||
| // Test that lastUsage is updated correctly | ||
| // Test getApiCost returns calculated cost based on actual usage | ||
| }) | ||
|
|
||
| it("should provide usage estimates when API doesn't return usage", () => { | ||
| // Test fallback token estimation logic | ||
| }) | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.