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", "name": "glm-free-api",
"version": "0.0.7", "version": "0.0.11",
"description": "GLM Free API Server", "description": "GLM Free API Server",
"type": "module", "type": "module",
"main": "dist/index.js", "main": "dist/index.js",

View File

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

View File

@ -1,5 +1,7 @@
import chat from "./chat.ts"; import chat from "./chat.ts";
import ping from "./ping.ts";
export default [ 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"
}
}