mirror of
https://github.com/RooVetGit/Roo-Code.git
synced 2026-11-02 07:13:07 +00:00
74 lines
2 KiB
TypeScript
74 lines
2 KiB
TypeScript
import { Anthropic } from "@anthropic-ai/sdk"
|
|
import { Mistral } from "@mistralai/mistralai"
|
|
import { ApiHandler } from "../"
|
|
import {
|
|
ApiHandlerOptions,
|
|
mistralDefaultModelId,
|
|
MistralModelId,
|
|
mistralModels,
|
|
ModelInfo,
|
|
openAiNativeDefaultModelId,
|
|
OpenAiNativeModelId,
|
|
openAiNativeModels,
|
|
} from "../../shared/api"
|
|
import { convertToMistralMessages } from "../transform/mistral-format"
|
|
import { ApiStream } from "../transform/stream"
|
|
|
|
export class MistralHandler implements ApiHandler {
|
|
private options: ApiHandlerOptions
|
|
private client: Mistral
|
|
|
|
constructor(options: ApiHandlerOptions) {
|
|
this.options = options
|
|
this.client = new Mistral({
|
|
serverURL: "https://codestral.mistral.ai",
|
|
apiKey: this.options.mistralApiKey,
|
|
})
|
|
}
|
|
|
|
async *createMessage(systemPrompt: string, messages: Anthropic.Messages.MessageParam[]): ApiStream {
|
|
const stream = await this.client.chat.stream({
|
|
model: this.getModel().id,
|
|
// max_completion_tokens: this.getModel().info.maxTokens,
|
|
temperature: this.options.modelTemperature ?? 0,
|
|
messages: [{ role: "system", content: systemPrompt }, ...convertToMistralMessages(messages)],
|
|
stream: true,
|
|
})
|
|
|
|
for await (const chunk of stream) {
|
|
const delta = chunk.data.choices[0]?.delta
|
|
if (delta?.content) {
|
|
let content: string = ""
|
|
if (typeof delta.content === "string") {
|
|
content = delta.content
|
|
} else if (Array.isArray(delta.content)) {
|
|
content = delta.content.map((c) => (c.type === "text" ? c.text : "")).join("")
|
|
}
|
|
yield {
|
|
type: "text",
|
|
text: content,
|
|
}
|
|
}
|
|
|
|
if (chunk.data.usage) {
|
|
yield {
|
|
type: "usage",
|
|
inputTokens: chunk.data.usage.promptTokens || 0,
|
|
outputTokens: chunk.data.usage.completionTokens || 0,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
getModel(): { id: MistralModelId; info: ModelInfo } {
|
|
const modelId = this.options.apiModelId
|
|
if (modelId && modelId in mistralModels) {
|
|
const id = modelId as MistralModelId
|
|
return { id, info: mistralModels[id] }
|
|
}
|
|
return {
|
|
id: mistralDefaultModelId,
|
|
info: mistralModels[mistralDefaultModelId],
|
|
}
|
|
}
|
|
}
|