-
Notifications
You must be signed in to change notification settings - Fork 2.6k
feat(custom-openai-compatible-api-provider): extended api provider to… #2450
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
Closed
Closed
Changes from 1 commit
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import { CustomOpenAiHandler } from "../custom-openai" | ||
| import { openAiModelInfoSaneDefaults } from "../../../shared/api" | ||
|
|
||
| describe("CustomOpenAiHandler", () => { | ||
| it("should construct with required options", () => { | ||
| const handler = new CustomOpenAiHandler({ | ||
| customBaseUrl: "https://api.example.com", | ||
| customApiKey: "test-key", | ||
| customAuthHeaderName: "X-API-Key", | ||
| customAuthHeaderPrefix: "", | ||
| }) | ||
|
|
||
| expect(handler).toBeDefined() | ||
| }) | ||
|
|
||
| it("should throw error if customBaseUrl is not provided", () => { | ||
| expect(() => { | ||
| new CustomOpenAiHandler({ | ||
| customApiKey: "test-key", | ||
| }) | ||
| }).toThrow("Custom OpenAI provider requires 'customBaseUrl' to be set.") | ||
| }) | ||
|
|
||
| it("should use model in path when useModelInPath is true", async () => { | ||
| const handler = new CustomOpenAiHandler({ | ||
| customBaseUrl: "https://api.example.com", | ||
| customApiKey: "test-key", | ||
| useModelInPath: true, | ||
| customPathPrefix: "/api/v1/chat/", | ||
| openAiModelId: "gpt-3.5-turbo", | ||
| openAiCustomModelInfo: openAiModelInfoSaneDefaults, | ||
| }) | ||
|
|
||
| // Mock the client.post method | ||
| const mockPost = jest.fn().mockResolvedValue({ | ||
| data: { | ||
| choices: [{ message: { content: "Test response" } }], | ||
| usage: { prompt_tokens: 10, completion_tokens: 20 }, | ||
| }, | ||
| }) | ||
|
|
||
| // @ts-ignore - Replace the client with our mock | ||
| handler.client = { post: mockPost } | ||
|
|
||
| // Call createMessage to trigger the endpoint construction | ||
| const stream = handler.createMessage("Test system prompt", [{ role: "user", content: "Test message" }]) | ||
|
|
||
| // Consume the stream to ensure the post method is called | ||
| for await (const _ of stream) { | ||
| // Just consume the stream | ||
| } | ||
|
|
||
| // Verify the endpoint used in the post call | ||
| expect(mockPost).toHaveBeenCalledWith("/api/v1/chat/gpt-3.5-turbo", expect.any(Object), expect.any(Object)) | ||
| }) | ||
|
|
||
| it("should use standard endpoint when useModelInPath is false", async () => { | ||
| const handler = new CustomOpenAiHandler({ | ||
| customBaseUrl: "https://api.example.com", | ||
| customApiKey: "test-key", | ||
| useModelInPath: false, | ||
| openAiModelId: "gpt-3.5-turbo", | ||
| openAiCustomModelInfo: openAiModelInfoSaneDefaults, | ||
| }) | ||
|
|
||
| // Mock the client.post method | ||
| const mockPost = jest.fn().mockResolvedValue({ | ||
| data: { | ||
| choices: [{ message: { content: "Test response" } }], | ||
| usage: { prompt_tokens: 10, completion_tokens: 20 }, | ||
| }, | ||
| }) | ||
|
|
||
| // @ts-ignore - Replace the client with our mock | ||
| handler.client = { post: mockPost } | ||
|
|
||
| // Call createMessage to trigger the endpoint construction | ||
| const stream = handler.createMessage("Test system prompt", [{ role: "user", content: "Test message" }]) | ||
|
|
||
| // Consume the stream to ensure the post method is called | ||
| for await (const _ of stream) { | ||
| // Just consume the stream | ||
| } | ||
|
|
||
| // Verify the endpoint used in the post call | ||
| expect(mockPost).toHaveBeenCalledWith("/chat/completions", expect.any(Object), expect.any(Object)) | ||
| }) | ||
| }) |
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,227 @@ | ||
| // src/api/providers/custom-openai.ts | ||
| import { Anthropic } from "@anthropic-ai/sdk" | ||
| import axios, { AxiosInstance, AxiosRequestConfig } from "axios" // Use axios for custom requests | ||
|
|
||
| import { | ||
| ApiHandlerOptions, | ||
| ModelInfo, | ||
| openAiModelInfoSaneDefaults, // Use sane defaults initially | ||
| } from "../../shared/api" | ||
| import { SingleCompletionHandler } from "../index" | ||
| import { convertToOpenAiMessages } from "../transform/openai-format" // Reuse message formatting | ||
| import { ApiStream, ApiStreamChunk, ApiStreamUsageChunk } from "../transform/stream" | ||
| import { BaseProvider } from "./base-provider" | ||
| import { XmlMatcher } from "../../utils/xml-matcher" // For potential reasoning tags | ||
|
|
||
| // Define specific options for the custom provider | ||
| export interface CustomOpenAiHandlerOptions extends ApiHandlerOptions { | ||
| customBaseUrl?: string | ||
| customApiKey?: string | ||
| customAuthHeaderName?: string // e.g., 'X-API-Key' | ||
| customAuthHeaderPrefix?: string // e.g., 'Bearer ' or '' | ||
| // URL path options | ||
| useModelInPath?: boolean // Whether to include model in URL path (e.g., /api/v1/chat/model-name) | ||
| customPathPrefix?: string // Custom path prefix (e.g., /api/v1/chat/) | ||
| // Potentially add other OpenAI-compatible options if needed later | ||
| modelTemperature?: number | null // Allow null to match schema | ||
| includeMaxTokens?: boolean | ||
| openAiStreamingEnabled?: boolean // Reuse existing streaming flag? | ||
| openAiModelId?: string // Reuse model ID field | ||
| openAiCustomModelInfo?: ModelInfo | null // Allow null to match schema | ||
| } | ||
|
|
||
| // Default headers - maybe keep these? | ||
| export const defaultHeaders = { | ||
| "HTTP-Referer": "https://github.com/RooVetGit/Roo-Cline", | ||
| "X-Title": "Roo Code", | ||
| } | ||
|
|
||
| export class CustomOpenAiHandler extends BaseProvider implements SingleCompletionHandler { | ||
| protected options: CustomOpenAiHandlerOptions | ||
| private client: AxiosInstance // Use an axios instance | ||
|
|
||
| constructor(options: CustomOpenAiHandlerOptions) { | ||
| super() | ||
| this.options = options | ||
|
|
||
| const baseURL = this.options.customBaseUrl | ||
| if (!baseURL) { | ||
| throw new Error("Custom OpenAI provider requires 'customBaseUrl' to be set.") | ||
| } | ||
| if (!this.options.customApiKey) { | ||
| console.warn("Custom OpenAI provider initialized without 'customApiKey'.") | ||
| } | ||
|
|
||
| // Prepare authentication header | ||
| const authHeaderName = this.options.customAuthHeaderName || "Authorization" // Default to Authorization | ||
| const authHeaderPrefix = | ||
| this.options.customAuthHeaderPrefix !== undefined ? this.options.customAuthHeaderPrefix : "Bearer " // Default to Bearer prefix | ||
| const apiKey = this.options.customApiKey || "not-provided" | ||
| const authHeaderValue = `${authHeaderPrefix}${apiKey}`.trim() // Handle empty prefix | ||
|
|
||
| this.client = axios.create({ | ||
| baseURL, | ||
| headers: { | ||
| ...defaultHeaders, // Include default Roo headers | ||
| [authHeaderName]: authHeaderValue, // Add the custom auth header | ||
| "Content-Type": "application/json", | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| // --- Implementation using axios --- | ||
|
|
||
| override async *createMessage(systemPrompt: string, messages: Anthropic.Messages.MessageParam[]): ApiStream { | ||
| const modelInfo = this.getModel().info | ||
| const modelId = this.options.openAiModelId ?? "custom-model" // Get model ID from options | ||
| const streamingEnabled = this.options.openAiStreamingEnabled ?? true // Default to streaming | ||
|
|
||
| // Convert messages to OpenAI format | ||
| // Need to import OpenAI types for this | ||
| const systemMessage: { role: "system"; content: string } = { | ||
| role: "system", | ||
| content: systemPrompt, | ||
| } | ||
| const convertedMessages = [systemMessage, ...convertToOpenAiMessages(messages)] | ||
|
|
||
| // Construct the common payload parts | ||
| const payload: Record<string, any> = { | ||
| model: modelId, | ||
| messages: convertedMessages, | ||
| temperature: this.options.modelTemperature ?? 0, // Default temperature | ||
| stream: streamingEnabled, | ||
| } | ||
|
|
||
| if (streamingEnabled && modelInfo.supportsUsageStream) { | ||
| payload.stream_options = { include_usage: true } | ||
| } | ||
|
|
||
| if (this.options.includeMaxTokens && modelInfo.maxTokens) { | ||
| payload.max_tokens = modelInfo.maxTokens | ||
| } | ||
| // Determine the endpoint based on configuration | ||
| let endpoint = "/chat/completions" // Default OpenAI-compatible endpoint | ||
|
|
||
| // If useModelInPath is true, construct the endpoint with the model in the path | ||
| if (this.options.useModelInPath && modelId) { | ||
| const pathPrefix = this.options.customPathPrefix || "/api/v1/chat/" | ||
| endpoint = `${pathPrefix}${modelId}` | ||
| } | ||
|
|
||
| try { | ||
| if (streamingEnabled) { | ||
| const response = await this.client.post(endpoint, payload, { | ||
| responseType: "stream", | ||
| }) | ||
|
|
||
| const stream = response.data as NodeJS.ReadableStream | ||
| let buffer = "" | ||
| let lastUsage: any = null | ||
| const matcher = new XmlMatcher( | ||
| "think", | ||
| (chunk) => ({ type: chunk.matched ? "reasoning" : "text", text: chunk.data }) as const, | ||
| ) | ||
|
|
||
| for await (const chunk of stream) { | ||
| buffer += chunk.toString() | ||
|
|
||
| // Process buffer line by line (SSE format) | ||
| let EOL | ||
| while ((EOL = buffer.indexOf("\n")) >= 0) { | ||
| const line = buffer.substring(0, EOL).trim() | ||
| buffer = buffer.substring(EOL + 1) | ||
|
|
||
| if (line.startsWith("data:")) { | ||
| const data = line.substring(5).trim() | ||
| if (data === "[DONE]") { | ||
| break // Stream finished | ||
| } | ||
| try { | ||
| const parsed = JSON.parse(data) | ||
| const delta = parsed.choices?.[0]?.delta ?? {} | ||
|
|
||
| if (delta.content) { | ||
| for (const contentChunk of matcher.update(delta.content)) { | ||
| yield contentChunk | ||
| } | ||
| } | ||
| // Handle potential reasoning content if supported by the custom model | ||
| if ("reasoning_content" in delta && delta.reasoning_content) { | ||
| yield { | ||
| type: "reasoning", | ||
| text: (delta.reasoning_content as string | undefined) || "", | ||
| } | ||
| } | ||
|
|
||
| if (parsed.usage) { | ||
| lastUsage = parsed.usage | ||
| } | ||
| } catch (e) { | ||
| console.error("Error parsing stream data:", e, "Data:", data) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| // Yield any remaining text from the matcher | ||
| for (const contentChunk of matcher.final()) { | ||
| yield contentChunk | ||
| } | ||
|
|
||
| if (lastUsage) { | ||
| yield this.processUsageMetrics(lastUsage, modelInfo) | ||
| } | ||
| } else { | ||
| // Non-streaming case | ||
| const response = await this.client.post(endpoint, payload) | ||
| const completion = response.data | ||
|
|
||
| yield { | ||
| type: "text", | ||
| text: completion.choices?.[0]?.message?.content || "", | ||
| } | ||
| if (completion.usage) { | ||
| yield this.processUsageMetrics(completion.usage, modelInfo) | ||
| } | ||
| } | ||
| } catch (error: any) { | ||
| console.error("Custom OpenAI API request failed:", error) | ||
| let errorMessage = "Custom OpenAI API request failed." | ||
| if (axios.isAxiosError(error) && error.response) { | ||
| errorMessage += ` Status: ${error.response.status}. Data: ${JSON.stringify(error.response.data)}` | ||
| } else if (error instanceof Error) { | ||
| errorMessage += ` Error: ${error.message}` | ||
| } | ||
| // Yield an error chunk or throw? For now, yield text. | ||
| yield { type: "text", text: `[ERROR: ${errorMessage}]` } | ||
| // Consider throwing an error instead if that's preferred for handling failures | ||
| // throw new Error(errorMessage); | ||
| } | ||
| } | ||
|
|
||
| override getModel(): { id: string; info: ModelInfo } { | ||
| // Reuse existing fields if they make sense for custom providers | ||
| return { | ||
| id: this.options.openAiModelId ?? "custom-model", // Default or configured ID | ||
| info: this.options.openAiCustomModelInfo ?? openAiModelInfoSaneDefaults, | ||
| } | ||
| } | ||
|
|
||
| async completePrompt(prompt: string): Promise<string> { | ||
| // TODO: Implement non-streaming completion if needed (optional for Roo?) | ||
| console.log("Prompt:", prompt) | ||
| return "[Placeholder: CustomOpenAiHandler.completePrompt not implemented]" | ||
| } | ||
|
|
||
| // --- Helper methods (potentially reuse/adapt from OpenAiHandler) --- | ||
| protected processUsageMetrics(usage: any, modelInfo?: ModelInfo): ApiStreamUsageChunk { | ||
| // Adapt if usage stats format differs | ||
| return { | ||
| type: "usage", | ||
| inputTokens: usage?.prompt_tokens || 0, | ||
| outputTokens: usage?.completion_tokens || 0, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // TODO: Add function to fetch models if the custom endpoint supports a /models route | ||
| // export async function getCustomOpenAiModels(...) { ... } | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider refining error handling. Instead of yielding an error chunk with potential internal details (e.g. from
JSON.stringifyonerror.response.data), it might be safer to throw an error or return a sanitized message so sensitive data isn’t inadvertently exposed.