Browse Source

add mistral latest api models

d.o.it 11 months ago
parent
commit
95078a6bc9

+ 16 - 7
src/api/providers/mistral.ts

@@ -22,22 +22,31 @@ export class MistralHandler implements ApiHandler {
 
 	constructor(options: ApiHandlerOptions) {
 		this.options = options
+		const baseUrl = this.getBaseUrl()
+		console.log("MistralHandler: baseUrl", baseUrl)
 		this.client = new Mistral({
-			serverURL: "https://codestral.mistral.ai",
+			serverURL: baseUrl,
 			apiKey: this.options.mistralApiKey,
 		})
 	}
 
+	private getBaseUrl(): string {
+		const modelId = this.options.apiModelId
+		if (modelId?.startsWith("codestral-")) {
+			return this.options.mistralCodestralUrl || "https://codestral.mistral.ai"
+		}
+		return "https://api.mistral.ai"
+	}
+
 	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,
+		const response = await this.client.chat.stream({
+			model: this.options.apiModelId || mistralDefaultModelId,
+			messages: convertToMistralMessages(messages),
+			maxTokens: this.options.includeMaxTokens ? this.getModel().info.maxTokens : undefined,
 			temperature: this.options.modelTemperature ?? MISTRAL_DEFAULT_TEMPERATURE,
-			messages: [{ role: "system", content: systemPrompt }, ...convertToMistralMessages(messages)],
-			stream: true,
 		})
 
-		for await (const chunk of stream) {
+		for await (const chunk of response) {
 			const delta = chunk.data.choices[0]?.delta
 			if (delta?.content) {
 				let content: string = ""

+ 1 - 0
src/shared/api.ts

@@ -52,6 +52,7 @@ export interface ApiHandlerOptions {
 	geminiApiKey?: string
 	openAiNativeApiKey?: string
 	mistralApiKey?: string
+	mistralCodestralUrl?: string // New option for Codestral URL
 	azureApiVersion?: string
 	openRouterUseMiddleOutTransform?: boolean
 	openAiStreamingEnabled?: boolean

+ 25 - 2
webview-ui/src/components/settings/ApiOptions.tsx

@@ -314,6 +314,7 @@ const ApiOptions = ({ apiErrorMessage, modelIdErrorMessage, fromWelcomeView }: A
 						placeholder="Enter API Key...">
 						<span style={{ fontWeight: 500 }}>Mistral API Key</span>
 					</VSCodeTextField>
+
 					<p
 						style={{
 							fontSize: "12px",
@@ -323,15 +324,37 @@ const ApiOptions = ({ apiErrorMessage, modelIdErrorMessage, fromWelcomeView }: A
 						This key is stored locally and only used to make API requests from this extension.
 						{!apiConfiguration?.mistralApiKey && (
 							<VSCodeLink
-								href="https://console.mistral.ai/codestral/"
+								href="https://console.mistral.ai/"
 								style={{
 									display: "inline",
 									fontSize: "inherit",
 								}}>
-								You can get a Mistral API key by signing up here.
+								You can get a La Plateforme (api.mistral.ai) / Codestral (codestral.mistral.ai) API key
+								by signing up here.
 							</VSCodeLink>
 						)}
 					</p>
+
+					{apiConfiguration?.apiModelId?.startsWith("codestral-") && (
+						<div>
+							<VSCodeTextField
+								value={apiConfiguration?.mistralCodestralUrl || ""}
+								style={{ width: "100%", marginTop: "10px" }}
+								type="url"
+								onBlur={handleInputChange("mistralCodestralUrl")}
+								placeholder="Default: https://codestral.mistral.ai">
+								<span style={{ fontWeight: 500 }}>Codestral Base URL (Optional)</span>
+							</VSCodeTextField>
+							<p
+								style={{
+									fontSize: "12px",
+									marginTop: 3,
+									color: "var(--vscode-descriptionForeground)",
+								}}>
+								Set alternative URL for Codestral model: https://api.mistral.ai
+							</p>
+						</div>
+					)}
 				</div>
 			)}