UNPKG

ai-persona-hub

Version:

AI Profile CLI - Create custom AI profiles run against dynamic LLM providers

75 lines 2.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MistralClient = void 0; const mistralai_1 = require("@mistralai/mistralai"); class MistralClient { client; config; constructor(config) { this.config = config; this.client = new mistralai_1.Mistral({ apiKey: config.apiKey }); } async sendMessage(messages, onChunk) { try { const response = await this.client.chat.complete({ model: this.config.model, messages: messages.map(msg => ({ role: msg.role, content: msg.content })), temperature: this.config.temperature, maxTokens: this.config.maxTokens, stream: !!onChunk }); if (onChunk && response.choices) { let fullResponse = ''; for (const choice of response.choices) { if (choice.message?.content) { const content = choice.message.content; const chunk = typeof content === 'string' ? content : JSON.stringify(content); fullResponse += chunk; onChunk(chunk); } } return fullResponse; } const content = response.choices?.[0]?.message?.content; return typeof content === 'string' ? content : (content ? JSON.stringify(content) : ''); } catch (error) { if (error instanceof Error) { throw new Error(`Mistral API error: ${error.message}`); } throw new Error('Unknown error occurred while communicating with Mistral API'); } } async validateConnection() { try { await this.client.chat.complete({ model: this.config.model, messages: [{ role: 'user', content: 'Hello' }], maxTokens: 1 }); return true; } catch (error) { return false; } } static getAvailableModels() { return [ 'mistral-tiny', 'mistral-small', 'mistral-medium', 'mistral-large-latest' ]; } updateConfig(newConfig) { this.config = { ...this.config, ...newConfig }; if (newConfig.apiKey) { this.client = new mistralai_1.Mistral({ apiKey: newConfig.apiKey }); } } } exports.MistralClient = MistralClient; //# sourceMappingURL=mistral-client.js.map