mirror of
https://github.com/LLM-Red-Team/kimi-free-api.git
synced 2024-12-23 07:49:19 +08:00
增加搜索监听
This commit is contained in:
parent
c1c5ebea4b
commit
1e08df14ea
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "kimi-free-api",
|
"name": "kimi-free-api",
|
||||||
"version": "0.0.1",
|
"version": "0.0.2",
|
||||||
"description": "Kimi Free Server",
|
"description": "Kimi Free Server",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
|
@ -150,6 +150,7 @@ async function receiveStream(convId: string, stream: any) {
|
|||||||
],
|
],
|
||||||
created: parseInt(performance.now() as any)
|
created: parseInt(performance.now() as any)
|
||||||
};
|
};
|
||||||
|
let refContent = '';
|
||||||
const parser = createParser(event => {
|
const parser = createParser(event => {
|
||||||
try {
|
try {
|
||||||
if (event.type !== "event") return;
|
if (event.type !== "event") return;
|
||||||
@ -159,9 +160,16 @@ async function receiveStream(convId: string, stream: any) {
|
|||||||
if (result.event == 'cmpl') {
|
if (result.event == 'cmpl') {
|
||||||
data.choices[0].message.content += result.text;
|
data.choices[0].message.content += result.text;
|
||||||
}
|
}
|
||||||
else if (result.event == 'all_done')
|
else if (result.event == 'all_done' || result.event == 'error') {
|
||||||
|
data.choices[0].message.content += '[以下内容由于不合规被停止生成,我们换个话题吧]' + (refContent ? `\n\n搜索结果来自:\n${refContent}` : '');
|
||||||
|
refContent = '';
|
||||||
resolve(data);
|
resolve(data);
|
||||||
}
|
}
|
||||||
|
else if(result.event == 'search_plus' && result.msg && result.msg.type == 'get_res')
|
||||||
|
refContent += `${result.msg.title}(${result.msg.url})\n`;
|
||||||
|
// else
|
||||||
|
// logger.warn(result.event, result);
|
||||||
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
logger.error(err);
|
logger.error(err);
|
||||||
reject(err);
|
reject(err);
|
||||||
@ -176,6 +184,7 @@ async function receiveStream(convId: string, stream: any) {
|
|||||||
function createTransStream(convId: string, stream: any, endCallback?: Function) {
|
function createTransStream(convId: string, stream: any, endCallback?: Function) {
|
||||||
const created = parseInt(performance.now() as any);
|
const created = parseInt(performance.now() as any);
|
||||||
const transStream = new PassThrough();
|
const transStream = new PassThrough();
|
||||||
|
let searchFlag = false;
|
||||||
!transStream.closed && transStream.write(`data: ${JSON.stringify({
|
!transStream.closed && transStream.write(`data: ${JSON.stringify({
|
||||||
id: convId,
|
id: convId,
|
||||||
model: 'kimi',
|
model: 'kimi',
|
||||||
@ -197,19 +206,23 @@ function createTransStream(convId: string, stream: any, endCallback?: Function)
|
|||||||
model: 'kimi',
|
model: 'kimi',
|
||||||
object: 'chat.completion.chunk',
|
object: 'chat.completion.chunk',
|
||||||
choices: [
|
choices: [
|
||||||
{ index: 0, delta: { content: result.text }, finish_reason: null }
|
{ index: 0, delta: { content: (searchFlag ? '\n' : '') + result.text }, finish_reason: null }
|
||||||
],
|
],
|
||||||
created
|
created
|
||||||
})}\n\n`;
|
})}\n\n`;
|
||||||
|
if(searchFlag)
|
||||||
|
searchFlag = false;
|
||||||
!transStream.closed && transStream.write(data);
|
!transStream.closed && transStream.write(data);
|
||||||
}
|
}
|
||||||
else if (result.event == 'all_done') {
|
else if (result.event == 'all_done' || result.event == 'error') {
|
||||||
const data = `data: ${JSON.stringify({
|
const data = `data: ${JSON.stringify({
|
||||||
id: convId,
|
id: convId,
|
||||||
model: 'kimi',
|
model: 'kimi',
|
||||||
object: 'chat.completion.chunk',
|
object: 'chat.completion.chunk',
|
||||||
choices: [
|
choices: [
|
||||||
{ index: 0, delta: {}, finish_reason: 'stop' }
|
{ index: 0, delta: result.event == 'error' ? {
|
||||||
|
content: '[以下内容由于不合规被停止生成,我们换个话题吧]'
|
||||||
|
} : {}, finish_reason: 'stop' }
|
||||||
],
|
],
|
||||||
created
|
created
|
||||||
})}\n\n`;
|
})}\n\n`;
|
||||||
@ -217,6 +230,24 @@ function createTransStream(convId: string, stream: any, endCallback?: Function)
|
|||||||
!transStream.closed && transStream.end('data: [DONE]\n\n');
|
!transStream.closed && transStream.end('data: [DONE]\n\n');
|
||||||
endCallback && endCallback();
|
endCallback && endCallback();
|
||||||
}
|
}
|
||||||
|
else if(result.event == 'search_plus' && result.msg && result.msg.type == 'get_res') {
|
||||||
|
if(!searchFlag)
|
||||||
|
searchFlag = true;
|
||||||
|
const data = `data: ${JSON.stringify({
|
||||||
|
id: convId,
|
||||||
|
model: 'kimi',
|
||||||
|
object: 'chat.completion.chunk',
|
||||||
|
choices: [
|
||||||
|
{ index: 0, delta: {
|
||||||
|
content: `检索 ${result.msg.title}(${result.msg.url}) ...\n`
|
||||||
|
}, finish_reason: null }
|
||||||
|
],
|
||||||
|
created
|
||||||
|
})}\n\n`;
|
||||||
|
!transStream.closed && transStream.write(data);
|
||||||
|
}
|
||||||
|
// else
|
||||||
|
// logger.warn(result.event, result);
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
logger.error(err);
|
logger.error(err);
|
||||||
|
Loading…
Reference in New Issue
Block a user