Skip to content

Commit

Permalink
feat: update groq-sdk to 0.7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
tak-bro committed Sep 13, 2024
1 parent be20dd0 commit 025441e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 35 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"copy-paste": "^1.5.3",
"figlet": "^1.7.0",
"formdata-node": "^6.0.3",
"groq-sdk": "^0.5.0",
"groq-sdk": "^0.7.0",
"inquirer": "9.2.8",
"inquirer-reactive-list-prompt": "^1.0.10",
"ollama": "^0.5.6",
Expand Down
14 changes: 4 additions & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 11 additions & 24 deletions src/services/ai/groq.service.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
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 { AIResponse, AIService, AIServiceParams } from './ai.service.js';
import { RequestType, createLogResponse } from '../../utils/log.js';
import { sanitizeMessage } from '../../utils/openai.js';
import { DEFAULT_PROMPT_OPTIONS, PromptOptions, codeReviewPrompt, generatePrompt } from '../../utils/prompt.js';
import { flattenDeep } from '../../utils/utils.js';

export class GroqService extends AIService {
private groq: Groq;
Expand Down Expand Up @@ -71,7 +68,7 @@ export class GroqService extends AIService {
};
const generatedSystemPrompt = requestType === 'review' ? codeReviewPrompt(promptOptions) : generatePrompt(promptOptions);

const chatCompletion = await this.groq.chat.completions.create(
const chatCompletion: Groq.Chat.ChatCompletion = await this.groq.chat.completions.create(
{
messages: [
{
Expand All @@ -93,35 +90,25 @@ export class GroqService extends AIService {
}
);

const fullText = chatCompletion.choices
.filter(choice => choice.message?.content)
.map(choice => sanitizeMessage(choice.message!.content as string))
.join();
logging && createLogResponse('Groq', diff, generatedSystemPrompt, fullText, requestType);

const results = chatCompletion.choices
.filter(choice => choice.message?.content)
.map(choice => sanitizeMessage(choice.message!.content as string));

const result = chatCompletion.choices[0].message.content || '';
logging && createLogResponse('Groq', diff, generatedSystemPrompt, result, requestType);
if (requestType === 'review') {
return flattenDeep(results.map(value => this.sanitizeResponse(value)));
return this.sanitizeResponse(result);
}
return flattenDeep(results.map(value => this.parseMessage(value, type, generate)));
return this.parseMessage(result, type, generate);
} catch (error) {
throw error as any;
}
}

handleError$ = (error: GroqError) => {
handleError$ = (error: Error) => {
let status = 'N/A';
let simpleMessage = 'An error occurred';
const regex = /"message":\s*"([^"]*)"/;
const match = error.message.match(regex);
if (match && match[1]) {
simpleMessage = match[1];
if (error instanceof Groq.APIError) {
status = `${error.status}`;
simpleMessage = error.name;
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
const message = `${error['status']} ${simpleMessage}`;
const message = `${status} ${simpleMessage}`;
return of({
name: `${this.errorPrefix} ${message}`,
value: simpleMessage,
Expand Down

0 comments on commit 025441e

Please sign in to comment.