Skip to content

Commit

Permalink
fix: 临时修复llma一直换行的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark committed Nov 18, 2023
1 parent f545e25 commit c86718a
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 67 deletions.
2 changes: 1 addition & 1 deletion dist/buildinfo.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"sha": "365ccff", "timestamp": 1700277776}
{"sha": "f545e25", "timestamp": 1700280157}
38 changes: 8 additions & 30 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ var Environment = class {
// 检查更新的分支
UPDATE_BRANCH = "master";
// 当前版本
BUILD_TIMESTAMP = 1700277776;
BUILD_TIMESTAMP = 1700280157;
// 当前版本 commit id
BUILD_VERSION = "365ccff";
BUILD_VERSION = "f545e25";
// 使用流模式
STREAM_MODE = true;
// 安全模式
Expand Down Expand Up @@ -590,7 +590,7 @@ var Stream = class {
throw new Error(`Attempted to iterate over a response with no body`);
}
const lineDecoder = new LineDecoder();
const iter = readableStreamAsyncIterable(this.response.body);
const iter = this.response.body;
for await (const chunk of iter) {
for (const line of lineDecoder.decode(chunk)) {
const sse = this.decoder.decode(line);
Expand Down Expand Up @@ -750,33 +750,6 @@ function partition(str, delimiter) {
}
return [str, "", ""];
}
function readableStreamAsyncIterable(stream) {
if (stream[Symbol.asyncIterator])
return stream;
const reader = stream.getReader();
return {
async next() {
try {
const result = await reader.read();
if (result === null || result === void 0 ? void 0 : result.done)
reader.releaseLock();
return result;
} catch (e) {
reader.releaseLock();
throw e;
}
},
async return() {
const cancelPromise = reader.cancel();
reader.releaseLock();
await cancelPromise;
return { done: true, value: void 0 };
},
[Symbol.asyncIterator]() {
return this;
}
};
}

// src/openai.js
function isOpenAIEnable(context) {
Expand Down Expand Up @@ -1219,6 +1192,11 @@ async function requestCompletionsFromWorkersAI(message, history, context, onStre
const c = chunk?.response || "";
lengthDelta += c.length;
contentFull = contentFull + c;
if (contentFull.endsWith("\n\n\n\n")) {
contentFull = contentFull.replace(/\n+$/, "");
controller.abort();
break;
}
if (lengthDelta > updateStep) {
lengthDelta = 0;
updateStep += 5;
Expand Down
2 changes: 1 addition & 1 deletion dist/timestamp
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1700277776
1700280157
36 changes: 1 addition & 35 deletions src/vendors/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class Stream {
throw new Error(`Attempted to iterate over a response with no body`);
}
const lineDecoder = new LineDecoder();
const iter = readableStreamAsyncIterable(this.response.body);
const iter = this.response.body;
for await (const chunk of iter) {
for (const line of lineDecoder.decode(chunk)) {
const sse = this.decoder.decode(line);
Expand Down Expand Up @@ -189,37 +189,3 @@ function partition(str, delimiter) {
}
return [str, '', ''];
}
/**
* Most browsers don't yet have async iterable support for ReadableStream,
* and Node has a very different way of reading bytes from its "ReadableStream".
*
* This polyfill was pulled from https://github.com/MattiasBuelens/web-streams-polyfill/pull/122#issuecomment-1627354490
*/
function readableStreamAsyncIterable(stream) {
if (stream[Symbol.asyncIterator])
return stream;
const reader = stream.getReader();
return {
async next() {
try {
const result = await reader.read();
if (result === null || result === void 0 ? void 0 : result.done)
reader.releaseLock(); // release lock when stream becomes closed
return result;
}
catch (e) {
reader.releaseLock(); // release lock when stream becomes errored
throw e;
}
},
async return() {
const cancelPromise = reader.cancel();
reader.releaseLock();
await cancelPromise;
return { done: true, value: undefined };
},
[Symbol.asyncIterator]() {
return this;
},
};
}
6 changes: 6 additions & 0 deletions src/workers-ai.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ export async function requestCompletionsFromWorkersAI(message, history, context,
const c = chunk?.response || '';
lengthDelta += c.length;
contentFull = contentFull + c;
// 临时修复llma一直换行的问题
if (contentFull.endsWith('\n\n\n\n')) {
contentFull = contentFull.replace(/\n+$/, '');
controller.abort();
break;
}
if (lengthDelta > updateStep) {
lengthDelta = 0;
updateStep += 5;
Expand Down

0 comments on commit c86718a

Please sign in to comment.