Skip to content

Commit

Permalink
perf: 添加流解析失败错误信息
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark committed Oct 8, 2023
1 parent 12fa595 commit 25774cb
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 22 deletions.
2 changes: 1 addition & 1 deletion dist/buildinfo.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"sha": "9d714ee", "timestamp": 1696745505}
{"sha": "12fa595", "timestamp": 1696745907}
25 changes: 15 additions & 10 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ var ENV = {
// 检查更新的分支
UPDATE_BRANCH: "master",
// 当前版本
BUILD_TIMESTAMP: 1696745505,
BUILD_TIMESTAMP: 1696745907,
// 当前版本 commit id
BUILD_VERSION: "9d714ee",
BUILD_VERSION: "12fa595",
I18N: null,
LANGUAGE: "zh-cn",
// 使用流模式
Expand Down Expand Up @@ -756,16 +756,21 @@ async function requestCompletionsFromOpenAI(message, history, context, onStream)
let contentFull = "";
let lengthDelta = 0;
let updateStep = 20;
for await (const data of stream) {
const c = data.choices[0].delta?.content || "";
lengthDelta += c.length;
contentFull = contentFull + c;
if (lengthDelta > updateStep) {
lengthDelta = 0;
updateStep += 5;
await onStream(`${contentFull}
try {
for await (const data of stream) {
const c = data.choices[0].delta?.content || "";
lengthDelta += c.length;
contentFull = contentFull + c;
if (lengthDelta > updateStep) {
lengthDelta = 0;
updateStep += 5;
await onStream(`${contentFull}
${ENV.I18N.message.loading}...`);
}
}
} catch (e) {
contentFull += `
ERROR: ${e.message}`;
}
return contentFull;
}
Expand Down
2 changes: 1 addition & 1 deletion dist/timestamp
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1696745505
1696745907
22 changes: 13 additions & 9 deletions src/openai.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-unused-vars */
import {Context} from './context.js';
import {DATABASE, ENV} from './env.js';
import {Stream} from "./vendors/stream.js";
import {Stream} from './vendors/stream.js';


/**
Expand Down Expand Up @@ -55,19 +55,23 @@ export async function requestCompletionsFromOpenAI(message, history, context, on
signal,
});
if (onStream && resp.ok && resp.headers.get('content-type').indexOf('text/event-stream') !== -1) {
const stream = new Stream(resp, controller)
const stream = new Stream(resp, controller);
let contentFull = '';
let lengthDelta = 0;
let updateStep = 20;
for await (const data of stream) {
const c = data.choices[0].delta?.content || ''
lengthDelta += c.length;
contentFull = contentFull + c;
try {
for await (const data of stream) {
const c = data.choices[0].delta?.content || '';
lengthDelta += c.length;
contentFull = contentFull + c;
if (lengthDelta > updateStep) {
lengthDelta = 0;
updateStep += 5;
await onStream(`${contentFull}\n${ENV.I18N.message.loading}...`);
lengthDelta = 0;
updateStep += 5;
await onStream(`${contentFull}\n${ENV.I18N.message.loading}...`);
}
}
} catch (e) {
contentFull += `\nERROR: ${e.message}`;
}
return contentFull;
}
Expand Down
1 change: 1 addition & 0 deletions src/vendors/stream.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable */
// https://github.com/openai/openai-node/blob/master/src/streaming.ts

export class Stream {
constructor(response, controller) {
Expand Down
2 changes: 1 addition & 1 deletion src/workers-ai.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {Ai} from './vendors/cloudflare-ai.js';
* @return {boolean}
*/
export function isWorkersAIEnable(context) {
return AI && AI.fetch
return AI && AI.fetch;
}


Expand Down

0 comments on commit 25774cb

Please sign in to comment.