-
Notifications
You must be signed in to change notification settings - Fork 331
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(config): add support for GROQ API key and models
feat(engine): add GroqAi engine implementation for GROQ AI provider
- Loading branch information
Showing
3 changed files
with
166 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import axios from 'axios'; | ||
import chalk from 'chalk'; | ||
import { execa } from 'execa'; | ||
|
||
import { | ||
ChatCompletionRequestMessage, | ||
Configuration as OpenAiApiConfiguration, | ||
OpenAIApi | ||
} from 'openai'; | ||
|
||
import { intro, outro } from '@clack/prompts'; | ||
|
||
import { | ||
CONFIG_MODES, | ||
DEFAULT_TOKEN_LIMITS, | ||
getConfig | ||
} from '../commands/config'; | ||
import { GenerateCommitMessageErrorEnum } from '../generateCommitMessageFromGitDiff'; | ||
import { tokenCount } from '../utils/tokenCount'; | ||
import { AiEngine } from './Engine'; | ||
import { MODEL_LIST } from '../commands/config'; | ||
|
||
const config = getConfig(); | ||
|
||
const MAX_TOKENS_OUTPUT = | ||
config?.OCO_TOKENS_MAX_OUTPUT || | ||
DEFAULT_TOKEN_LIMITS.DEFAULT_MAX_TOKENS_OUTPUT; | ||
const MAX_TOKENS_INPUT = | ||
config?.OCO_TOKENS_MAX_INPUT || DEFAULT_TOKEN_LIMITS.DEFAULT_MAX_TOKENS_INPUT; | ||
let basePath = "https://api.groq.com/openai/v1"; | ||
let apiKey = config?.OCO_GROQ_API_KEY; | ||
|
||
const [command, mode] = process.argv.slice(2); | ||
|
||
const provider = config?.OCO_AI_PROVIDER; | ||
|
||
if ( | ||
provider === 'groq' && | ||
!apiKey && | ||
command !== 'config' && | ||
mode !== CONFIG_MODES.set | ||
) { | ||
intro('opencommit'); | ||
|
||
outro( | ||
'OCO_GROQ_API_KEY is not set, please run `oco config set OCO_GROQ_API_KEY=<your token> .' | ||
); | ||
outro( | ||
'For help look into README https://github.com/di-sukharev/opencommit#setup' | ||
); | ||
|
||
process.exit(1); | ||
} | ||
|
||
const MODEL = config?.OCO_MODEL || 'gpt-3.5-turbo'; | ||
if (provider === 'groq' && | ||
!MODEL_LIST.groq.includes(MODEL) && | ||
command !== 'config' && | ||
mode !== CONFIG_MODES.set) { | ||
outro( | ||
`${chalk.red('✖')} Unsupported model ${MODEL} for Groq AI. Supported models are: ${MODEL_LIST.groq.join( | ||
', ' | ||
)}` | ||
); | ||
|
||
process.exit(1); | ||
} | ||
|
||
export class GroqAi implements AiEngine { | ||
|
||
private groqAiApiConfiguration = new OpenAiApiConfiguration({ | ||
apiKey: apiKey | ||
}); | ||
private groqAI!: OpenAIApi; | ||
|
||
constructor() { | ||
if (basePath) { | ||
this.groqAiApiConfiguration.basePath = basePath; | ||
} | ||
this.groqAI = new OpenAIApi(this.groqAiApiConfiguration); | ||
} | ||
|
||
public generateCommitMessage = async ( | ||
messages: Array<ChatCompletionRequestMessage> | ||
): Promise<string | undefined> => { | ||
const params = { | ||
model: MODEL, | ||
messages, | ||
temperature: 0, | ||
top_p: 0.1, | ||
max_tokens: MAX_TOKENS_OUTPUT | ||
}; | ||
try { | ||
const REQUEST_TOKENS = messages | ||
.map((msg) => tokenCount(msg.content as string) + 4) | ||
.reduce((a, b) => a + b, 0); | ||
|
||
if (REQUEST_TOKENS > MAX_TOKENS_INPUT - MAX_TOKENS_OUTPUT) { | ||
throw new Error(GenerateCommitMessageErrorEnum.tooMuchTokens); | ||
} | ||
|
||
const { data } = await this.groqAI.createChatCompletion(params); | ||
|
||
const message = data.choices[0].message; | ||
|
||
return message?.content; | ||
} catch (error) { | ||
outro(`${chalk.red('✖')} ${JSON.stringify(params)}`); | ||
|
||
const err = error as Error; | ||
outro(`${chalk.red('✖')} ${err?.message || err}`); | ||
|
||
if ( | ||
axios.isAxiosError<{ error?: { message: string } }>(error) && | ||
error.response?.status === 401 | ||
) { | ||
const groqAiError = error.response.data.error; | ||
|
||
if (groqAiError?.message) outro(groqAiError.message); | ||
outro( | ||
'For help look into README https://github.com/di-sukharev/opencommit#setup' | ||
); | ||
} | ||
|
||
throw err; | ||
} | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters