-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from tak-bro/feature/add-groq
Feature/add groq
- Loading branch information
Showing
7 changed files
with
155 additions
and
3 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,88 @@ | ||
import chalk from 'chalk'; | ||
import Groq from 'groq-sdk'; | ||
import { GroqError } from 'groq-sdk/error'; | ||
import { ReactiveListChoice } from 'inquirer-reactive-list-prompt'; | ||
import { Observable, catchError, concatMap, from, map, of } from 'rxjs'; | ||
import { fromPromise } from 'rxjs/internal/observable/innerFrom'; | ||
|
||
import { AIService, AIServiceParams } from './ai.service.js'; | ||
import { createLogResponse } from '../../utils/log.js'; | ||
import { deduplicateMessages } from '../../utils/openai.js'; | ||
import { extraPrompt, generateDefaultPrompt } from '../../utils/prompt.js'; | ||
|
||
export class GroqService extends AIService { | ||
private groq: Groq; | ||
|
||
constructor(private readonly params: AIServiceParams) { | ||
super(params); | ||
this.colors = { | ||
primary: '#f55036', | ||
secondary: '#fff', | ||
}; | ||
this.serviceName = chalk.bgHex(this.colors.primary).hex(this.colors.secondary).bold('[Groq]'); | ||
this.errorPrefix = chalk.red.bold(`[Groq]`); | ||
this.groq = new Groq({ apiKey: this.params.config.GROQ_KEY }); | ||
} | ||
|
||
generateCommitMessage$(): Observable<ReactiveListChoice> { | ||
return fromPromise(this.generateMessage()).pipe( | ||
concatMap(messages => from(messages)), | ||
map(message => ({ | ||
name: `${this.serviceName} ${message}`, | ||
value: message, | ||
isError: false, | ||
})), | ||
catchError(this.handleError$) | ||
); | ||
} | ||
|
||
private async generateMessage(): Promise<string[]> { | ||
try { | ||
const diff = this.params.stagedDiff.diff; | ||
const { locale, generate, type, prompt: userPrompt, logging } = this.params.config; | ||
const maxLength = this.params.config['max-length']; | ||
const defaultPrompt = generateDefaultPrompt(locale, maxLength, type, userPrompt); | ||
const systemPrompt = `${defaultPrompt}\n${extraPrompt(generate)}`; | ||
|
||
const chatCompletion = await this.groq.chat.completions.create( | ||
{ | ||
messages: [ | ||
{ role: 'system', content: systemPrompt }, | ||
{ | ||
role: 'user', | ||
content: `Here are diff: ${diff}`, | ||
}, | ||
], | ||
model: this.params.config.GROQ_MODEL, | ||
}, | ||
{ | ||
timeout: this.params.config.timeout, | ||
} | ||
); | ||
|
||
const result = chatCompletion.choices[0].message.content || ''; | ||
logging && createLogResponse('Groq', diff, systemPrompt, result); | ||
return deduplicateMessages(this.sanitizeMessage(result, this.params.config.type, generate)); | ||
} catch (error) { | ||
throw error as any; | ||
} | ||
} | ||
|
||
handleError$ = (error: GroqError) => { | ||
let simpleMessage = 'An error occurred'; | ||
const regex = /"message":\s*"([^"]*)"/; | ||
const match = error.message.match(regex); | ||
if (match && match[1]) { | ||
simpleMessage = match[1]; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
const message = `${error['status']} ${simpleMessage}`; | ||
return of({ | ||
name: `${this.errorPrefix} ${message}`, | ||
value: simpleMessage, | ||
isError: true, | ||
disabled: true, | ||
}); | ||
}; | ||
} |
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