Skip to content

Commit

Permalink
Merge pull request #50 from nowscott/development
Browse files Browse the repository at this point in the history
add:添加网络异常时的反馈
  • Loading branch information
nowscott authored Jul 23, 2024
2 parents 9c38876 + e94a15c commit 94b8316
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
.env.development.local
.env.test.local
.env.production.local
.env

npm-debug.log*
yarn-debug.log*
Expand Down
11 changes: 2 additions & 9 deletions src/service/groq.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export function sendGroqMessage({

const fetchChatCompletion = async () => {
try {
console.log(model, 'API 请求中...');
const chatCompletion = await groq.chat.completions.create({
messages: [
{ role: 'system', content: systemPrompt },
Expand All @@ -43,22 +42,16 @@ export function sendGroqMessage({
stop: null,
});

let buffer = '';

for await (const chunk of chatCompletion) {
const content = chunk.choices[0]?.delta?.content || '';
buffer += content;
onContentUpdate(buffer);
buffer = '';
onContentUpdate(content);
if (chunk.x_groq && chunk.x_groq.usage) {
onTokenUpdate(chunk.x_groq.usage.total_tokens);
console.log(chunk.x_groq.usage.total_tokens, 'API 请求成功');
}
}
onCompletion();
console.log(model, 'API 请求成功');
} catch (error) {
console.error('API 请求失败:', error);
onContentUpdate('在当前网络环境下该模型服务异常,请检查网络或切换其他模型');
onCompletion({ error: '请求失败,请稍后重试。' });
}
};
Expand Down

0 comments on commit 94b8316

Please sign in to comment.