mirror of
https://github.com/LLM-Red-Team/kimi-free-api.git
synced 2024-11-01 20:09:20 +08:00
处理有几率遇到�输出的情况
This commit is contained in:
parent
980b506e94
commit
ae541f533e
@ -299,7 +299,7 @@ async function createCompletionStream(model = MODEL_NAME, messages: any[], refre
|
|||||||
*/
|
*/
|
||||||
async function fakeRequest(refreshToken: string) {
|
async function fakeRequest(refreshToken: string) {
|
||||||
const token = await acquireToken(refreshToken);
|
const token = await acquireToken(refreshToken);
|
||||||
const options = {
|
const options = {
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${token}`,
|
Authorization: `Bearer ${token}`,
|
||||||
Referer: `https://kimi.moonshot.cn/`,
|
Referer: `https://kimi.moonshot.cn/`,
|
||||||
@ -570,8 +570,9 @@ async function receiveStream(model: string, convId: string, stream: any) {
|
|||||||
if (_.isError(result))
|
if (_.isError(result))
|
||||||
throw new Error(`Stream response invalid: ${event.data}`);
|
throw new Error(`Stream response invalid: ${event.data}`);
|
||||||
// 处理消息
|
// 处理消息
|
||||||
if (result.event == 'cmpl') {
|
if (result.event == 'cmpl' && result.text) {
|
||||||
data.choices[0].message.content += result.text;
|
const exceptCharIndex = result.text.indexOf("<22>");
|
||||||
|
data.choices[0].message.content += result.text.substring(0, exceptCharIndex == -1 ? result.text.length : exceptCharIndex);
|
||||||
}
|
}
|
||||||
// 处理结束或错误
|
// 处理结束或错误
|
||||||
else if (result.event == 'all_done' || result.event == 'error') {
|
else if (result.event == 'all_done' || result.event == 'error') {
|
||||||
@ -632,12 +633,14 @@ function createTransStream(model: string, convId: string, stream: any, endCallba
|
|||||||
throw new Error(`Stream response invalid: ${event.data}`);
|
throw new Error(`Stream response invalid: ${event.data}`);
|
||||||
// 处理消息
|
// 处理消息
|
||||||
if (result.event == 'cmpl') {
|
if (result.event == 'cmpl') {
|
||||||
|
const exceptCharIndex = result.text.indexOf("<22>");
|
||||||
|
const chunk = result.text.substring(0, exceptCharIndex == -1 ? result.text.length : exceptCharIndex);
|
||||||
const data = `data: ${JSON.stringify({
|
const data = `data: ${JSON.stringify({
|
||||||
id: convId,
|
id: convId,
|
||||||
model,
|
model,
|
||||||
object: 'chat.completion.chunk',
|
object: 'chat.completion.chunk',
|
||||||
choices: [
|
choices: [
|
||||||
{ index: 0, delta: { content: (searchFlag ? '\n' : '') + result.text }, finish_reason: null }
|
{ index: 0, delta: { content: (searchFlag ? '\n' : '') + chunk }, finish_reason: null }
|
||||||
],
|
],
|
||||||
created
|
created
|
||||||
})}\n\n`;
|
})}\n\n`;
|
||||||
|
Loading…
Reference in New Issue
Block a user