feat(config): add support for groq AI provider, including config validation and engine implementation (#381)

This commit is contained in:
BILLY Maxime
2024-09-07 14:03:25 +02:00
committed by GitHub
parent a91aa3b4de
commit 2d7e3842d6
4 changed files with 40 additions and 8 deletions
+19 -6
View File
@@ -76,6 +76,16 @@ export const MODEL_LIST = {
'gemini-1.0-pro', 'gemini-1.0-pro',
'gemini-pro-vision', 'gemini-pro-vision',
'text-embedding-004' 'text-embedding-004'
],
groq: [
'llama3-70b-8192', // Meta Llama 3 70B (default one, no daily token limit and 14 400 reqs/day)
'llama3-8b-8192', // Meta Llama 3 8B
'llama-guard-3-8b', // Llama Guard 3 8B
'llama-3.1-8b-instant', // Llama 3.1 8B (Preview)
'llama-3.1-70b-versatile', // Llama 3.1 70B (Preview)
'gemma-7b-it', // Gemma 7B
'gemma2-9b-it' // Gemma 2 9B
] ]
}; };
@@ -87,6 +97,8 @@ const getDefaultModel = (provider: string | undefined): string => {
return MODEL_LIST.anthropic[0]; return MODEL_LIST.anthropic[0];
case 'gemini': case 'gemini':
return MODEL_LIST.gemini[0]; return MODEL_LIST.gemini[0];
case 'groq':
return MODEL_LIST.groq[0];
default: default:
return MODEL_LIST.openai[0]; return MODEL_LIST.openai[0];
} }
@@ -241,7 +253,7 @@ export const configValidators = {
validateConfig( validateConfig(
CONFIG_KEYS.OCO_AI_PROVIDER, CONFIG_KEYS.OCO_AI_PROVIDER,
['openai', 'anthropic', 'gemini', 'azure', 'test', 'flowise'].includes( ['openai', 'anthropic', 'gemini', 'azure', 'test', 'flowise', 'groq'].includes(
value value
) || value.startsWith('ollama'), ) || value.startsWith('ollama'),
`${value} is not supported yet, use 'ollama', 'anthropic', 'azure', 'gemini', 'flowise' or 'openai' (default)` `${value} is not supported yet, use 'ollama', 'anthropic', 'azure', 'gemini', 'flowise' or 'openai' (default)`
@@ -288,7 +300,8 @@ export enum OCO_AI_PROVIDER_ENUM {
GEMINI = 'gemini', GEMINI = 'gemini',
AZURE = 'azure', AZURE = 'azure',
TEST = 'test', TEST = 'test',
FLOWISE = 'flowise' FLOWISE = 'flowise',
GROQ = 'groq',
} }
export type ConfigType = { export type ConfigType = {
@@ -388,7 +401,7 @@ const getEnvConfig = (envPath: string) => {
OCO_EMOJI: parseConfigVarValue(process.env.OCO_EMOJI), OCO_EMOJI: parseConfigVarValue(process.env.OCO_EMOJI),
OCO_LANGUAGE: process.env.OCO_LANGUAGE, OCO_LANGUAGE: process.env.OCO_LANGUAGE,
OCO_MESSAGE_TEMPLATE_PLACEHOLDER: OCO_MESSAGE_TEMPLATE_PLACEHOLDER:
process.env.OCO_MESSAGE_TEMPLATE_PLACEHOLDER, process.env.OCO_MESSAGE_TEMPLATE_PLACEHOLDER,
OCO_PROMPT_MODULE: process.env.OCO_PROMPT_MODULE as OCO_PROMPT_MODULE_ENUM, OCO_PROMPT_MODULE: process.env.OCO_PROMPT_MODULE as OCO_PROMPT_MODULE_ENUM,
OCO_ONE_LINE_COMMIT: parseConfigVarValue(process.env.OCO_ONE_LINE_COMMIT), OCO_ONE_LINE_COMMIT: parseConfigVarValue(process.env.OCO_ONE_LINE_COMMIT),
OCO_TEST_MOCK_TYPE: process.env.OCO_TEST_MOCK_TYPE, OCO_TEST_MOCK_TYPE: process.env.OCO_TEST_MOCK_TYPE,
@@ -445,9 +458,9 @@ interface GetConfigOptions {
} }
export const getConfig = ({ export const getConfig = ({
envPath = defaultEnvPath, envPath = defaultEnvPath,
globalPath = defaultConfigPath globalPath = defaultConfigPath
}: GetConfigOptions = {}): ConfigType => { }: GetConfigOptions = {}): ConfigType => {
const envConfig = getEnvConfig(envPath); const envConfig = getEnvConfig(envPath);
const globalConfig = getGlobalConfig(globalPath); const globalConfig = getGlobalConfig(globalPath);
+10
View File
@@ -0,0 +1,10 @@
import { OpenAiConfig, OpenAiEngine } from './openAi';
interface GroqConfig extends OpenAiConfig {}
export class GroqEngine extends OpenAiEngine {
constructor(config: GroqConfig) {
config.baseURL = 'https://api.groq.com/openai/v1';
super(config);
}
}
+7 -2
View File
@@ -4,7 +4,7 @@ import { GenerateCommitMessageErrorEnum } from '../generateCommitMessageFromGitD
import { tokenCount } from '../utils/tokenCount'; import { tokenCount } from '../utils/tokenCount';
import { AiEngine, AiEngineConfig } from './Engine'; import { AiEngine, AiEngineConfig } from './Engine';
interface OpenAiConfig extends AiEngineConfig {} export interface OpenAiConfig extends AiEngineConfig {}
export class OpenAiEngine implements AiEngine { export class OpenAiEngine implements AiEngine {
config: OpenAiConfig; config: OpenAiConfig;
@@ -12,7 +12,12 @@ export class OpenAiEngine implements AiEngine {
constructor(config: OpenAiConfig) { constructor(config: OpenAiConfig) {
this.config = config; this.config = config;
this.client = new OpenAI({ apiKey: config.apiKey });
if (!config.baseURL) {
this.client = new OpenAI({ apiKey: config.apiKey });
} else {
this.client = new OpenAI({ apiKey: config.apiKey, baseURL: config.baseURL });
}
} }
public generateCommitMessage = async ( public generateCommitMessage = async (
+4
View File
@@ -7,6 +7,7 @@ import { GeminiEngine } from '../engine/gemini';
import { OllamaEngine } from '../engine/ollama'; import { OllamaEngine } from '../engine/ollama';
import { OpenAiEngine } from '../engine/openAi'; import { OpenAiEngine } from '../engine/openAi';
import { TestAi, TestMockType } from '../engine/testAi'; import { TestAi, TestMockType } from '../engine/testAi';
import { GroqEngine } from '../engine/groq';
export function getEngine(): AiEngine { export function getEngine(): AiEngine {
const config = getConfig(); const config = getConfig();
@@ -39,6 +40,9 @@ export function getEngine(): AiEngine {
case OCO_AI_PROVIDER_ENUM.FLOWISE: case OCO_AI_PROVIDER_ENUM.FLOWISE:
return new FlowiseEngine(DEFAULT_CONFIG); return new FlowiseEngine(DEFAULT_CONFIG);
case OCO_AI_PROVIDER_ENUM.GROQ:
return new GroqEngine(DEFAULT_CONFIG);
default: default:
return new OpenAiEngine(DEFAULT_CONFIG); return new OpenAiEngine(DEFAULT_CONFIG);
} }