Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Optimize schema generation #21

Merged
merged 10 commits into from
Oct 30, 2024
8 changes: 8 additions & 0 deletions app/controller/app-center/aiChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ export default class AiChatController extends Controller {
const model = foundationModel?.model ?? E_FOUNDATION_MODEL.GPT_35_TURBO;
const token = foundationModel.token;
ctx.body = await ctx.service.appCenter.aiChat.getAnswerFromAi(messages, { model, token });
}


public async uploadFile() {
const { ctx } = this;
const fileStream = await ctx.getFileStream();
const foundationModelObject = JSON.parse(fileStream.fields.foundationModel);
const { model, token } = foundationModelObject.foundationModel;
ctx.body = await ctx.service.appCenter.aiChat.getFileContentFromAi(fileStream, { model, token });
Fleurxxx marked this conversation as resolved.
Show resolved Hide resolved
}
}
22 changes: 11 additions & 11 deletions app/router/appCenter/base.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/**
* Copyright (c) 2023 - present TinyEngine Authors.
* Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd.
*
* Use of this source code is governed by an MIT-style license.
*
* THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
* BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
* A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
*
*/
* Copyright (c) 2023 - present TinyEngine Authors.
* Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd.
*
* Use of this source code is governed by an MIT-style license.
*
* THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
* BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
* A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
*
*/
import { Application } from 'egg';

export default (app: Application) => {
Expand All @@ -22,7 +22,6 @@ export default (app: Application) => {
const subRouter = router.namespace(ROUTER_PREFIX);

// 应用管理

subRouter.get('/apps/detail/:id', controller.appCenter.apps.detail);
subRouter.post('/apps/update/:id', controller.appCenter.apps.update);

Expand Down Expand Up @@ -114,4 +113,5 @@ export default (app: Application) => {

// AI大模型聊天接口
subRouter.post('/ai/chat', controller.appCenter.aiChat.aiChat);
subRouter.post('/ai/files', controller.appCenter.aiChat.uploadFile);
Fleurxxx marked this conversation as resolved.
Show resolved Hide resolved
};
118 changes: 112 additions & 6 deletions app/service/app-center/aiChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,25 @@
*
*/
import { Service } from 'egg';
import Transformer from '@opentiny/tiny-engine-transform';
import { E_FOUNDATION_MODEL } from '../../lib/enum';
import * as fs from 'fs';
import * as path from 'path';

const OpenAI = require('openai');
Fleurxxx marked this conversation as resolved.
Show resolved Hide resolved


export type AiMessage = {
role: string; // 角色
name?: string; // 名称
content: string; // 聊天内容
partial?: boolean;
};

interface ConfigModel {
model: string;
token: string;
}
Fleurxxx marked this conversation as resolved.
Show resolved Hide resolved

export default class AiChat extends Service {
/**
* 获取ai的答复
Expand All @@ -31,11 +41,35 @@ export default class AiChat extends Service {
*/

async getAnswerFromAi(messages: Array<AiMessage>, chatConfig: any) {
const answer = await this.requestAnswerFromAi(messages, chatConfig);
const answerContent = answer.choices[0]?.message.content;
// 从ai回复中提取页面的代码
const codes = this.extractCode(answerContent);
const schema = codes ? Transformer.translate(codes) : null;
let res = await this.requestAnswerFromAi(messages, chatConfig);
let answerContent = '';
let isFinish = res.choices[0].finish_reason;

if (isFinish !== 'length') {
answerContent = res.choices[0]?.message.content;
}

// 若内容过长被截断,继续回复
while (isFinish === 'length') {
const prefix = res.choices[0].message.content;
answerContent += prefix;
messages.push({
role: 'assistant',
content: prefix,
partial: true
});

res = await this.requestAnswerFromAi(messages, chatConfig);
answerContent += res.choices[0].message.content;
isFinish = res.choices[0].finish_reason;
}
Fleurxxx marked this conversation as resolved.
Show resolved Hide resolved

const code = this.extractCode(answerContent);
const schema = this.extractSchemaCode(code);
Fleurxxx marked this conversation as resolved.
Show resolved Hide resolved
const answer = {
role: res.choices[0].message.role,
content: answerContent
};
const replyWithoutCode = this.removeCode(answerContent);
return this.ctx.helper.getResponseData({
originalResponse: answer,
Expand Down Expand Up @@ -123,6 +157,20 @@ export default class AiChat extends Service {
return content.substring(0, start) + '<代码在画布中展示>' + content.substring(end);
}

private extractSchemaCode(content) {
const startMarker = /```json/;
const endMarker = /```/;

const start = content.search(startMarker);
const end = content.slice(start + 7).search(endMarker) + start + 7;

if (start >= 0 && end >= 0) {
return JSON.parse(content.substring(start + 7, end).trim());
}

return null;
}
Fleurxxx marked this conversation as resolved.
Show resolved Hide resolved

private getStartAndEnd(str: string) {
const start = str.search(/```|<template>/);

Expand Down Expand Up @@ -162,5 +210,63 @@ export default class AiChat extends Service {
}
return messages;
}

/**
* 文件上传
*
* @param model
* @return
*/

async getFileContentFromAi(fileStream: any, chatConfig: ConfigModel) {
const answer = await this.requestFileContentFromAi(fileStream, chatConfig);
return this.ctx.helper.getResponseData({
originalResponse: answer
});
}
Fleurxxx marked this conversation as resolved.
Show resolved Hide resolved

async requestFileContentFromAi(file: any, chatConfig: ConfigModel) {
const { ctx } = this;
const filename = Math.random().toString(36).substr(2) + new Date().getTime() + path.extname(file.filename).toLocaleLowerCase();
Fleurxxx marked this conversation as resolved.
Show resolved Hide resolved
Fleurxxx marked this conversation as resolved.
Show resolved Hide resolved
const savePath = path.join(__dirname, filename);
const writeStream = fs.createWriteStream(savePath);
file.pipe(writeStream);
await new Promise(resolve => writeStream.on('close', resolve));
Fleurxxx marked this conversation as resolved.
Show resolved Hide resolved

const client = new OpenAI({
apiKey: chatConfig.token,
baseURL: 'https://api.moonshot.cn/v1'
});
Fleurxxx marked this conversation as resolved.
Show resolved Hide resolved
let res: any = null;
try {
//上传文件
const fileObject = await client.files.create({
file: fs.createReadStream(savePath),
purpose: 'file-extract'
});

// 文件解析
const imageAnalysisConfig = this.config.parsingFile(fileObject.id, chatConfig.token);
const { analysisImageHttpRequestUrl, analysisImageHttpRequestOption } = imageAnalysisConfig[chatConfig.model];
res = await ctx.curl(analysisImageHttpRequestUrl, analysisImageHttpRequestOption);
res.data = JSON.parse(res.res.data.toString());
} catch (e: any) {
this.ctx.logger.debug(`调用上传图片接口失败: ${(e as Error).message}`);
return this.ctx.helper.getResponseData(`调用上传图片接口失败: ${(e as Error).message}`);
Fleurxxx marked this conversation as resolved.
Show resolved Hide resolved
} finally {
try {
Fleurxxx marked this conversation as resolved.
Show resolved Hide resolved
await fs.promises.unlink(savePath);
console.log('文件已删除:', savePath);
} catch (err) {
console.error('文件删除失败:', err);
Fleurxxx marked this conversation as resolved.
Show resolved Hide resolved
}
}

if (!res) {
return this.ctx.helper.getResponseData(`调用上传图片接口未返回正确数据.`);
}
Fleurxxx marked this conversation as resolved.
Show resolved Hide resolved

return res.data;
}
}

34 changes: 33 additions & 1 deletion config/config.default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { EggAppConfig, PowerPartial } from 'egg';
import { I_SchemaConvert } from '../app/lib/interface';
import { E_SchemaFormatFunc, E_FOUNDATION_MODEL } from '../app/lib/enum';


export default (appInfo) => {
const config = {} as PowerPartial<EggAppConfig>;

Expand Down Expand Up @@ -305,6 +304,39 @@ export default (appInfo) => {
};
};

// 文件上传接口
config.uploadFile = (file: any, token: string) => {
return {
[E_FOUNDATION_MODEL.MOONSHOT_V1_8K]: {
httpRequestUrl: `https://api.moonshot.cn/v1/files`,
httpRequestOption: {
data: {
file: file,
purpose: 'file-extract'
},
headers: {
Authorization: `Bearer ${token}`
}
}
}
};
};
Fleurxxx marked this conversation as resolved.
Show resolved Hide resolved

// 文件解析接口
config.parsingFile = (fileId: any, token: string) => {
return {
[E_FOUNDATION_MODEL.MOONSHOT_V1_8K]: {
analysisImageHttpRequestUrl: `https://api.moonshot.cn/v1/files/${fileId}/content`,
analysisImageHttpRequestOption: {
method: 'GET',
headers: {
Authorization: `Bearer ${token}`
}
}
}
};
};
Fleurxxx marked this conversation as resolved.
Show resolved Hide resolved

config.npmRegistryOptions = [
'--registry=https://registry.npmjs.org/'
];
Expand Down
Loading