This commit is contained in:
Vinlic 2024-03-25 04:18:52 +08:00
commit cea74b948b
7 changed files with 63 additions and 25 deletions

View File

@ -87,7 +87,7 @@ https://udify.app/chat/Pe89TtaX3rKXM8NS
### 代码调用
![代码调用](./doc/example-12.jpg)
![代码调用](./doc/example-12.png)
### 图像解析

Binary file not shown.

Before

Width:  |  Height:  |  Size: 110 KiB

BIN
doc/example-12.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 89 KiB

View File

@ -1,6 +1,6 @@
{
"name": "glm-free-api",
"version": "0.0.7",
"version": "0.0.11",
"description": "GLM Free API Server",
"type": "module",
"main": "dist/index.js",

View File

@ -479,7 +479,9 @@ async function receiveStream(stream: any): Promise<any> {
created: util.unixTimestamp()
};
let toolCall = false;
// let codeGenerating = false;
let codeGenerating = false;
let textChunkLength = 0;
let codeTemp = '';
let lastExecutionOutput = '';
let textOffset = 0;
const parser = createParser(event => {
@ -498,12 +500,19 @@ async function receiveStream(stream: any): Promise<any> {
return str;
const partText = content.reduce((innerStr, value) => {
const { status: partStatus, type, text, image, code, content } = value;
if(partStatus == 'init' && textChunkLength > 0) {
textOffset += textChunkLength + 1;
textChunkLength = 0;
innerStr += '\n';
}
if(type == 'text') {
if(toolCall) {
innerStr += '\n';
textOffset++;
toolCall = false;
}
if(partStatus == 'finish')
textChunkLength = text.length;
return innerStr + text;
}
else if(type == 'quote_result' && status == 'finish' && meta_data && _.isArray(meta_data.metadata_list)) {
@ -518,18 +527,24 @@ async function receiveStream(stream: any): Promise<any> {
toolCall = true;
return innerStr + imageText;
}
// else if(type == 'code' && partStatus == 'init' && !codeGenerating) {
// codeGenerating = true;
// const label = '代码生成中...\n';
// textOffset += label.length;
// return innerStr + label;
// }
// else if(type == 'code' && partStatus == 'finish' && codeGenerating) {
// codeGenerating = false;
// const label = '代码执行中...\n';
// textOffset += label.length;
// return innerStr + label;
// }
else if(type == 'code' && partStatus == 'init') {
let codeHead = '';
if(!codeGenerating) {
codeGenerating = true;
codeHead = '```python\n';
}
const chunk = code.substring(codeTemp.length, code.length);
codeTemp += chunk;
textOffset += codeHead.length + chunk.length;
return innerStr + codeHead + chunk;
}
else if(type == 'code' && partStatus == 'finish' && codeGenerating) {
const codeFooter = '\n```\n';
codeGenerating = false;
codeTemp = '';
textOffset += codeFooter.length;
return innerStr + codeFooter;
}
else if(type == 'execution_output' && _.isString(content) && partStatus == 'done' && lastExecutionOutput != content) {
lastExecutionOutput = content;
const _content = content.replace(/^\n/, '');
@ -576,6 +591,8 @@ function createTransStream(stream: any, endCallback?: Function) {
let content = '';
let toolCall = false;
let codeGenerating = false;
let textChunkLength = 0;
let codeTemp = '';
let lastExecutionOutput = '';
let textOffset = 0;
!transStream.closed && transStream.write(`data: ${JSON.stringify({
@ -601,13 +618,20 @@ function createTransStream(stream: any, endCallback?: Function) {
return str;
const partText = content.reduce((innerStr, value) => {
const { status: partStatus, type, text, image, code, content } = value;
if(partStatus == 'init' && textChunkLength > 0) {
textOffset += textChunkLength + 1;
textChunkLength = 0;
innerStr += '\n';
}
if(type == 'text') {
if(toolCall) {
innerStr += '\n';
textOffset++;
toolCall = false;
}
return innerStr + text.replace(/【\d+†source】/g, '');
if(partStatus == 'finish')
textChunkLength = text.length;
return innerStr + text;
}
else if(type == 'quote_result' && status == 'finish' && meta_data && _.isArray(meta_data.metadata_list)) {
const searchText = meta_data.metadata_list.reduce((meta, v) => meta + `检索 ${v.title}(${v.url}) ...`, '') + '\n';
@ -621,17 +645,23 @@ function createTransStream(stream: any, endCallback?: Function) {
toolCall = true;
return innerStr + imageText;
}
else if(type == 'code' && partStatus == 'init' && !codeGenerating) {
codeGenerating = true;
const label = '代码生成中...\n';
textOffset += label.length;
return innerStr + label;
else if(type == 'code' && partStatus == 'init') {
let codeHead = '';
if(!codeGenerating) {
codeGenerating = true;
codeHead = '```python\n';
}
const chunk = code.substring(codeTemp.length, code.length);
codeTemp += chunk;
textOffset += codeHead.length + chunk.length;
return innerStr + codeHead + chunk;
}
else if(type == 'code' && partStatus == 'finish' && codeGenerating) {
const codeFooter = '\n```\n';
codeGenerating = false;
const label = '代码执行中...\n';
textOffset += label.length;
return innerStr + label;
codeTemp = '';
textOffset += codeFooter.length;
return innerStr + codeFooter;
}
else if(type == 'execution_output' && _.isString(content) && partStatus == 'done' && lastExecutionOutput != content) {
lastExecutionOutput = content;

View File

@ -1,5 +1,7 @@
import chat from "./chat.ts";
import ping from "./ping.ts";
export default [
chat
chat,
ping
];

6
src/api/routes/ping.ts Normal file
View File

@ -0,0 +1,6 @@
export default {
prefix: '/ping',
get: {
'': async () => "pong"
}
}