-
Notifications
You must be signed in to change notification settings - Fork 406
feat(config, engine): add support for Mistral AI provider and engine #436
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
Changes from all commits
edead9f
ae64625
bfdc0b6
f4032af
c34cdee
52d11b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import axios from 'axios'; | ||
| import { Mistral } from '@mistralai/mistralai'; | ||
| import { OpenAI } from 'openai'; | ||
| import { GenerateCommitMessageErrorEnum } from '../generateCommitMessageFromGitDiff'; | ||
| import { tokenCount } from '../utils/tokenCount'; | ||
| import { AiEngine, AiEngineConfig } from './Engine'; | ||
| import { | ||
| AssistantMessage as MistralAssistantMessage, | ||
| SystemMessage as MistralSystemMessage, | ||
| ToolMessage as MistralToolMessage, | ||
| UserMessage as MistralUserMessage | ||
| } from '@mistralai/mistralai/models/components'; | ||
|
|
||
| export interface MistralAiConfig extends AiEngineConfig {} | ||
| export type MistralCompletionMessageParam = Array< | ||
| | (MistralSystemMessage & { role: "system" }) | ||
| | (MistralUserMessage & { role: "user" }) | ||
| | (MistralAssistantMessage & { role: "assistant" }) | ||
| | (MistralToolMessage & { role: "tool" }) | ||
| > | ||
|
|
||
| export class MistralAiEngine implements AiEngine { | ||
| config: MistralAiConfig; | ||
| client: Mistral; | ||
|
|
||
| constructor(config: MistralAiConfig) { | ||
| this.config = config; | ||
|
|
||
| if (!config.baseURL) { | ||
| this.client = new Mistral({ apiKey: config.apiKey }); | ||
| } else { | ||
| this.client = new Mistral({ apiKey: config.apiKey, serverURL: config.baseURL }); | ||
| } | ||
| } | ||
|
|
||
| public generateCommitMessage = async ( | ||
| messages: Array<OpenAI.Chat.Completions.ChatCompletionMessageParam> | ||
| ): Promise<string | null> => { | ||
| const params = { | ||
| model: this.config.model, | ||
| messages: messages as MistralCompletionMessageParam, | ||
| topP: 0.1, | ||
| maxTokens: this.config.maxTokensOutput | ||
| }; | ||
|
|
||
| try { | ||
| const REQUEST_TOKENS = messages | ||
| .map((msg) => tokenCount(msg.content as string) + 4) | ||
| .reduce((a, b) => a + b, 0); | ||
|
|
||
| if ( | ||
| REQUEST_TOKENS > | ||
| this.config.maxTokensInput - this.config.maxTokensOutput | ||
| ) | ||
| throw new Error(GenerateCommitMessageErrorEnum.tooMuchTokens); | ||
|
|
||
| const completion = await this.client.chat.complete(params); | ||
|
|
||
| if (!completion.choices) | ||
| throw Error('No completion choice available.') | ||
|
|
||
| const message = completion.choices[0].message; | ||
|
|
||
| if (!message || !message.content) | ||
| throw Error('No completion choice available.') | ||
|
|
||
| return message.content as string; | ||
| } catch (error) { | ||
| const err = error as Error; | ||
| if ( | ||
| axios.isAxiosError<{ error?: { message: string } }>(error) && | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this wont be hit
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we need to remove the try/catch from the engines and prob do one try/catch in the Engine |
||
| error.response?.status === 401 | ||
| ) { | ||
| const mistralError = error.response.data.error; | ||
|
|
||
| if (mistralError) throw new Error(mistralError.message); | ||
| } | ||
|
|
||
| throw err; | ||
| } | ||
| }; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.