Skip to content

Commit

Permalink
Merge pull request #59 from nowscott/development
Browse files Browse the repository at this point in the history
update:更新配置文件名称,使用ky代替原生fetch
  • Loading branch information
nowscott authored Jul 27, 2024
2 parents 805a6e5 + 88fa63f commit 6bcaff1
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 59 deletions.
13 changes: 13 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"copy-to-clipboard": "^3.3.3",
"file-saver": "^2.0.5",
"groq-sdk": "^0.5.0",
"ky": "^1.5.0",
"moment": "^2.30.1",
"react": "^18.3.1",
"react-dom": "^18.3.1",
Expand Down
2 changes: 1 addition & 1 deletion src/components/Settings.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// src/components/Settings.js
import React, { useState, useEffect } from 'react';
import SettingInput from './SettingInput';
import { models, settingsConfig } from 'settingsConfig';
import { models, settingsConfig } from 'config';
import { useSettings } from 'contexts/SettingsContext';

const Settings = () => {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/contexts/SettingsContext.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { createContext, useContext, useState } from 'react';
import { models, initialSettings } from '../settingsConfig';
import { models, initialSettings } from 'config';

const SettingsContext = createContext();

Expand Down
2 changes: 1 addition & 1 deletion src/pages/ChatPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const ChatPage = () => {
setAiMessageMid(newAiMessageId);
const formatMessage = (msg) => `${msg.role === 'user' ? 'user' : 'AI'}: ${msg.content}`;
const chatHistory = messages.map(formatMessage).join('\n');
const fullPrompt = `history: ${chatHistory}\nuser: ${prompt}`;
const fullPrompt = `chatHistory: ${chatHistory}\nuser: ${prompt}`;
setIsMessageComplete(false);
setSubmittedPrompt(fullPrompt);
setShouldSubmit(true);
Expand Down
64 changes: 8 additions & 56 deletions src/service/silicon.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import ky from 'ky';
import { handleStreamResponse } from 'utils/streamUtils';

const apiKey = process.env.REACT_APP_SILICON_API_KEY;
if (!apiKey) {
throw new Error("Silicon API Key 缺失");
Expand All @@ -18,16 +21,14 @@ export const sendSiliconMessage = ({
}) => {
const controller = new AbortController();
const { signal } = controller;
let shouldStop = false;

const options = {
method: 'POST',
headers: {
accept: 'application/json',
'content-type': 'application/json',
authorization: `Bearer ${apiKey}`,
},
body: JSON.stringify({
json: {
model,
messages: [
{ role: 'system', content: systemPrompt },
Expand All @@ -39,65 +40,16 @@ export const sendSiliconMessage = ({
top_k: topK,
frequency_penalty: frequencyPenalty,
stream: true,
}),
},
signal,
};

fetch('https://api.siliconflow.cn/v1/chat/completions', options)
ky.post('https://api.siliconflow.cn/v1/chat/completions', options)
.then(response => {
if (!response.ok) {
throw new Error('网络响应不正常');
}
return response.body.getReader();
})
.then(reader => {
const decoder = new TextDecoder('utf-8');
const read = async () => {
if (shouldStop) return;

try {
const { done, value } = await reader.read();
if (done) {
onCompletion && onCompletion();
return;
}

const text = decoder.decode(value, { stream: true });
text.split('\n').forEach(line => {
if (line.trim() && line.trim() !== 'data: [DONE]') {
try {
const jsonResponse = JSON.parse(line.trim().replace(/^data: /, ''));
const delta = jsonResponse.choices[0].delta;
if (delta && delta.content) {
onContentUpdate(delta.content);
}
if (jsonResponse.usage) {
onTokenUpdate(jsonResponse.usage.total_tokens);
}
if (jsonResponse.choices[0].finish_reason === "stop") {
shouldStop = true;
return;
}
} catch (error) {
console.error('解析 JSON 时出错:', error, '行:', line);
}
}
});

if (!shouldStop) {
read();
}
} catch (err) {
if (err.name === 'AbortError') {
console.log('请求被中止');
} else {
console.error('读取流时出错:', err);
}
}
};
read();
handleStreamResponse(response.body.getReader(), onContentUpdate, onTokenUpdate, onCompletion);
})
.catch(err => {
onContentUpdate('在当前网络环境下该模型服务异常,请检查网络或切换其他模型');
console.error('API 请求失败:', err);
});

Expand Down
52 changes: 52 additions & 0 deletions src/utils/streamUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// utils/streamUtils.js

export const handleStreamResponse = async (reader, onContentUpdate, onTokenUpdate, onCompletion) => {
const decoder = new TextDecoder('utf-8');
let shouldStop = false;

const read = async () => {
if (shouldStop) return;

try {
const { done, value } = await reader.read();
if (done) {
onCompletion && onCompletion();
return;
}

const text = decoder.decode(value, { stream: true });
text.split('\n').forEach(line => {
if (line.trim() && line.trim() !== 'data: [DONE]') {
try {
const jsonResponse = JSON.parse(line.trim().replace(/^data: /, ''));
const delta = jsonResponse.choices[0].delta;
if (delta && delta.content) {
onContentUpdate(delta.content);
}
if (jsonResponse.usage) {
onTokenUpdate(jsonResponse.usage.total_tokens);
}
if (jsonResponse.choices[0].finish_reason === "stop") {
shouldStop = true;
return;
}
} catch (error) {
console.error('解析 JSON 时出错:', error, '行:', line);
}
}
});

if (!shouldStop) {
read();
}
} catch (err) {
if (err.name === 'AbortError') {
console.log('请求被中止');
} else {
console.error('读取流时出错:', err);
}
}
};

read();
};

0 comments on commit 6bcaff1

Please sign in to comment.