mirror of
https://github.com/RooVetGit/Roo-Code.git
synced 2026-08-28 05:27:24 +00:00
* feat: advanced configuration for OpenAI Compatible Providers * Update .changeset/thirty-eyes-appear.md Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com> * Update webview-ui/src/components/settings/ApiOptions.tsx Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com> * dropdown menu * Show pricing if user entered model info --------- Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com> Co-authored-by: Saoud Rizwan <7799382+saoudrizwan@users.noreply.github.com>
84 lines
2.6 KiB
TypeScript
84 lines
2.6 KiB
TypeScript
import { Anthropic } from "@anthropic-ai/sdk"
|
|
import OpenAI, { AzureOpenAI } from "openai"
|
|
import { withRetry } from "../retry"
|
|
import { ApiHandlerOptions, azureOpenAiDefaultApiVersion, ModelInfo, openAiModelInfoSaneDefaults } from "../../shared/api"
|
|
import { ApiHandler } from "../index"
|
|
import { convertToOpenAiMessages } from "../transform/openai-format"
|
|
import { ApiStream } from "../transform/stream"
|
|
import { convertToR1Format } from "../transform/r1-format"
|
|
|
|
export class OpenAiHandler implements ApiHandler {
|
|
private options: ApiHandlerOptions
|
|
private client: OpenAI
|
|
|
|
constructor(options: ApiHandlerOptions) {
|
|
this.options = options
|
|
// Azure API shape slightly differs from the core API shape: https://github.com/openai/openai-node?tab=readme-ov-file#microsoft-azure-openai
|
|
if (this.options.openAiBaseUrl?.toLowerCase().includes("azure.com")) {
|
|
this.client = new AzureOpenAI({
|
|
baseURL: this.options.openAiBaseUrl,
|
|
apiKey: this.options.openAiApiKey,
|
|
apiVersion: this.options.azureApiVersion || azureOpenAiDefaultApiVersion,
|
|
})
|
|
} else {
|
|
this.client = new OpenAI({
|
|
baseURL: this.options.openAiBaseUrl,
|
|
apiKey: this.options.openAiApiKey,
|
|
})
|
|
}
|
|
}
|
|
|
|
@withRetry()
|
|
async *createMessage(systemPrompt: string, messages: Anthropic.Messages.MessageParam[]): ApiStream {
|
|
const modelId = this.options.openAiModelId ?? ""
|
|
const isDeepseekReasoner = modelId.includes("deepseek-reasoner")
|
|
|
|
let openAiMessages: OpenAI.Chat.ChatCompletionMessageParam[] = [
|
|
{ role: "system", content: systemPrompt },
|
|
...convertToOpenAiMessages(messages),
|
|
]
|
|
|
|
if (isDeepseekReasoner) {
|
|
openAiMessages = convertToR1Format([{ role: "user", content: systemPrompt }, ...messages])
|
|
}
|
|
|
|
const stream = await this.client.chat.completions.create({
|
|
model: modelId,
|
|
messages: openAiMessages,
|
|
temperature: 0,
|
|
stream: true,
|
|
stream_options: { include_usage: true },
|
|
})
|
|
for await (const chunk of stream) {
|
|
const delta = chunk.choices[0]?.delta
|
|
if (delta?.content) {
|
|
yield {
|
|
type: "text",
|
|
text: delta.content,
|
|
}
|
|
}
|
|
|
|
if (delta && "reasoning_content" in delta && delta.reasoning_content) {
|
|
yield {
|
|
type: "reasoning",
|
|
reasoning: (delta.reasoning_content as string | undefined) || "",
|
|
}
|
|
}
|
|
|
|
if (chunk.usage) {
|
|
yield {
|
|
type: "usage",
|
|
inputTokens: chunk.usage.prompt_tokens || 0,
|
|
outputTokens: chunk.usage.completion_tokens || 0,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
getModel(): { id: string; info: ModelInfo } {
|
|
return {
|
|
id: this.options.openAiModelId ?? "",
|
|
info: this.options.openAiModelInfo ?? openAiModelInfoSaneDefaults,
|
|
}
|
|
}
|
|
}
|