mirror of
https://github.com/di-sukharev/opencommit.git
synced 2026-04-20 03:02:51 -04:00
- Move hardcoded baseURL before ...config spread in constructor - This allows user config to override the default DeepSeek API URL - Fixes issue #539 where OCO_API_URL was ignored by DeepSeek engine
53 lines
1.7 KiB
TypeScript
53 lines
1.7 KiB
TypeScript
import { OpenAI } from 'openai';
|
|
import { GenerateCommitMessageErrorEnum } from '../generateCommitMessageFromGitDiff';
|
|
import { normalizeEngineError } from '../utils/engineErrorHandler';
|
|
import { removeContentTags } from '../utils/removeContentTags';
|
|
import { tokenCount } from '../utils/tokenCount';
|
|
import { OpenAiEngine, OpenAiConfig } from './openAi';
|
|
|
|
export interface DeepseekConfig extends OpenAiConfig {}
|
|
|
|
export class DeepseekEngine extends OpenAiEngine {
|
|
constructor(config: DeepseekConfig) {
|
|
// Call OpenAIEngine constructor with forced Deepseek baseURL
|
|
// Put baseURL first so user config can override it
|
|
super({
|
|
baseURL: 'https://api.deepseek.com/v1',
|
|
...config
|
|
});
|
|
}
|
|
|
|
// Identical method from OpenAiEngine, re-implemented here
|
|
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;
|
|
let content = message?.content;
|
|
return removeContentTags(content, 'think');
|
|
} catch (error) {
|
|
throw normalizeEngineError(error, 'deepseek', this.config.model);
|
|
}
|
|
};
|
|
}
|