mirror of
https://github.com/di-sukharev/opencommit.git
synced 2026-04-20 03:02:51 -04:00
* feat(config): add support for groq AI provider, including config validation and engine implementation (#381) * fix migrations (#414) --------- Co-authored-by: Takanori Matsumoto <matscube@gmail.com> Co-authored-by: BILLY Maxime <ozeliurs@gmail.com>
65 lines
1.8 KiB
TypeScript
65 lines
1.8 KiB
TypeScript
import axios from 'axios';
|
|
import { OpenAI } from 'openai';
|
|
import { GenerateCommitMessageErrorEnum } from '../generateCommitMessageFromGitDiff';
|
|
import { tokenCount } from '../utils/tokenCount';
|
|
import { AiEngine, AiEngineConfig } from './Engine';
|
|
|
|
export interface OpenAiConfig extends AiEngineConfig {}
|
|
|
|
export class OpenAiEngine implements AiEngine {
|
|
config: OpenAiConfig;
|
|
client: OpenAI;
|
|
|
|
constructor(config: OpenAiConfig) {
|
|
this.config = config;
|
|
|
|
if (!config.baseURL) {
|
|
this.client = new OpenAI({ apiKey: config.apiKey });
|
|
} else {
|
|
this.client = new OpenAI({ apiKey: config.apiKey, baseURL: config.baseURL });
|
|
}
|
|
}
|
|
|
|
public generateCommitMessage = async (
|
|
messages: Array<OpenAI.Chat.Completions.ChatCompletionMessageParam>
|
|
): Promise<string | null> => {
|
|
const params = {
|
|
model: this.config.model,
|
|
messages,
|
|
temperature: 0,
|
|
top_p: 0.1,
|
|
max_tokens: 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.completions.create(params);
|
|
|
|
const message = completion.choices[0].message;
|
|
|
|
return message?.content;
|
|
} catch (error) {
|
|
const err = error as Error;
|
|
if (
|
|
axios.isAxiosError<{ error?: { message: string } }>(error) &&
|
|
error.response?.status === 401
|
|
) {
|
|
const openAiError = error.response.data.error;
|
|
|
|
if (openAiError) throw new Error(openAiError.message);
|
|
}
|
|
|
|
throw err;
|
|
}
|
|
};
|
|
}
|