修复qwen流出现异常字符时导致流文本分割异常

This commit is contained in:
Vinlic 2024-03-25 10:28:38 +08:00
parent 3b10fabe39
commit 62456b2e52

View File

@ -109,6 +109,7 @@ async function createCompletion(
headers: { headers: {
Cookie: generateCookie(ticket), Cookie: generateCookie(ticket),
...FAKE_HEADERS, ...FAKE_HEADERS,
Accept: 'text/event-stream'
}, },
timeout: 120000, timeout: 120000,
validateStatus: () => true, validateStatus: () => true,
@ -183,6 +184,7 @@ async function createCompletionStream(
headers: { headers: {
Cookie: generateCookie(ticket), Cookie: generateCookie(ticket),
...FAKE_HEADERS, ...FAKE_HEADERS,
Accept: 'text/event-stream'
}, },
timeout: 120000, timeout: 120000,
validateStatus: () => true, validateStatus: () => true,
@ -310,7 +312,6 @@ async function receiveStream(stream: any): Promise<any> {
usage: { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 }, usage: { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 },
created: util.unixTimestamp(), created: util.unixTimestamp(),
}; };
let textOffset = 0;
const parser = createParser((event) => { const parser = createParser((event) => {
try { try {
if (event.type !== "event") return; if (event.type !== "event") return;
@ -325,9 +326,10 @@ async function receiveStream(stream: any): Promise<any> {
if (role != "assistant" && !_.isString(content)) return str; if (role != "assistant" && !_.isString(content)) return str;
return str + content; return str + content;
}, ""); }, "");
const exceptCharIndex = text.indexOf('<27>');
let chunk = text.substring( let chunk = text.substring(
data.choices[0].message.content.length - textOffset, data.choices[0].message.content.length,
text.length exceptCharIndex == -1 ? text.length : exceptCharIndex
); );
if (chunk && result.contentType == "text2image") { if (chunk && result.contentType == "text2image") {
chunk = chunk.replace( chunk = chunk.replace(
@ -406,7 +408,8 @@ function createTransStream(stream: any, endCallback?: Function) {
if (role != "assistant" && !_.isString(content)) return str; if (role != "assistant" && !_.isString(content)) return str;
return str + content; return str + content;
}, ""); }, "");
let chunk = text.substring(content.length, text.length); const exceptCharIndex = text.indexOf('<27>');
let chunk = text.substring(content.length, exceptCharIndex == -1 ? text.length : exceptCharIndex);
if (chunk && result.contentType == "text2image") { if (chunk && result.contentType == "text2image") {
chunk = chunk.replace( chunk = chunk.replace(
/https?:\/\/[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=\,]*)/gi, /https?:\/\/[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=\,]*)/gi,
@ -495,6 +498,11 @@ function generateCookie(ticket: string) {
`login_tongyi_ticket=${ticket}`, `login_tongyi_ticket=${ticket}`,
'_samesite_flag_=true', '_samesite_flag_=true',
`t=${util.uuid(false)}`, `t=${util.uuid(false)}`,
'channel=oug71n2fX3Jd5ualEfKACRvnsceUtpjUC5jHBpfWnSOXKhkvBNuSO8bG3v4HHjCgB722h7LqbHkB6sAxf3OvgA%3D%3D',
'currentRegionId=cn-shenzhen',
'aliyun_country=CN',
'aliyun_lang=zh',
'aliyun_site=CN',
// `login_aliyunid_csrf=_csrf_tk_${util.generateRandomString({ charset: 'numeric', length: 15 })}`, // `login_aliyunid_csrf=_csrf_tk_${util.generateRandomString({ charset: 'numeric', length: 15 })}`,
// `cookie2=${util.uuid(false)}`, // `cookie2=${util.uuid(false)}`,
// `munb=22${util.generateRandomString({ charset: 'numeric', length: 11 })}`, // `munb=22${util.generateRandomString({ charset: 'numeric', length: 11 })}`,