Skip to content

Commit

Permalink
尝试修复乱码问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinlic committed Dec 13, 2024
1 parent 78f9f36 commit c4161df
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "doubao-free-api",
"version": "0.0.2",
"version": "0.0.3",
"description": "Doubao Free API Server",
"type": "module",
"main": "dist/index.js",
Expand Down
41 changes: 23 additions & 18 deletions src/api/controllers/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -635,8 +635,11 @@ async function receiveStream(stream: any): Promise<any> {
if (!message || ![2001, 2008].includes(message.content_type))
return;
const content = JSON.parse(message.content);
if (content.text)
data.choices[0].message.content += content.text;
if (content.text) {
const text = content.text;
const exceptCharIndex = text.indexOf("�");
data.choices[0].message.content += text.substring(0, exceptCharIndex == -1 ? text.length : exceptCharIndex);
}
} catch (err) {
logger.error(err);
reject(err);
Expand All @@ -658,7 +661,6 @@ async function receiveStream(stream: any): Promise<any> {
* @param endCallback 传输结束回调
*/
function createTransStream(stream: any, endCallback?: Function) {
let isEnd = false;
let convId = "";
// 消息创建时间
const created = util.unixTimestamp();
Expand Down Expand Up @@ -691,7 +693,6 @@ function createTransStream(stream: any, endCallback?: Function) {
if (rawResult.code)
throw new APIException(EX.API_REQUEST_FAILED, `[请求doubao失败]: ${rawResult.code}-${rawResult.message}`);
if (rawResult.event_type == 2003) {
isEnd = true;
transStream.write(`data: ${JSON.stringify({
id: convId,
model: MODEL_NAME,
Expand All @@ -717,7 +718,6 @@ function createTransStream(stream: any, endCallback?: Function) {
if (!convId)
convId = result.conversation_id;
if (result.is_finish) {
isEnd = true;
transStream.write(`data: ${JSON.stringify({
id: convId,
model: MODEL_NAME,
Expand All @@ -739,19 +739,24 @@ function createTransStream(stream: any, endCallback?: Function) {
if (!message || ![2001, 2008].includes(message.content_type))
return;
const content = JSON.parse(message.content);
transStream.write(`data: ${JSON.stringify({
id: convId,
model: MODEL_NAME,
object: "chat.completion.chunk",
choices: [
{
index: 0,
delta: { role: "assistant", content: content.text },
finish_reason: null,
},
],
created,
})}\n\n`);
if (content.text) {
const text = content.text;
const exceptCharIndex = text.indexOf("�");
const chunk = text.substring(0, exceptCharIndex == -1 ? text.length : exceptCharIndex);
transStream.write(`data: ${JSON.stringify({
id: convId,
model: MODEL_NAME,
object: "chat.completion.chunk",
choices: [
{
index: 0,
delta: { role: "assistant", content: chunk },
finish_reason: null,
},
],
created,
})}\n\n`);
}
} catch (err) {
logger.error(err);
!transStream.closed && transStream.end("\n\n");
Expand Down

0 comments on commit c4161df

Please sign in to comment.