Skip to content

Commit

Permalink
fix: Tweak code details
Browse files Browse the repository at this point in the history
  • Loading branch information
Fleurxxx committed Jul 30, 2024
1 parent 1665c01 commit e305ec2
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 66 deletions.
25 changes: 13 additions & 12 deletions app/controller/app-center/aiChat.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 { Controller } from 'egg';
import { E_FOUNDATION_MODEL } from '../../lib/enum';

Expand All @@ -22,12 +22,13 @@ export default class AiChatController extends Controller {
*/
public async aiChat() {
const { ctx } = this;
const { foundationModel, messages, accessToken } = ctx.request.body;
const { foundationModel, messages } = ctx.request.body;
this.ctx.logger.info('ai接口请求参参数 model选型:', foundationModel);
if (!messages || !Array.isArray(messages)) {
return this.ctx.helper.getResponseData('Not passing the correct message parameter');
}
const model = foundationModel?.model ?? E_FOUNDATION_MODEL.GPT_35_TURBO;
ctx.body = await ctx.service.appCenter.aiChat.getAnswerFromAi(messages, { model }, accessToken);
const token = foundationModel.token;
ctx.body = await ctx.service.appCenter.aiChat.getAnswerFromAi(messages, { model, token });
}
}
46 changes: 21 additions & 25 deletions app/service/app-center/aiChat.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 { Service } from 'egg';
import Transformer from '@opentiny/tiny-engine-transform';
import { E_FOUNDATION_MODEL } from '../../lib/enum';
Expand All @@ -30,30 +30,29 @@ export default class AiChat extends Service {
* @return
*/

async getAnswerFromAi(messages: Array<AiMessage>, chatConfig: any, accessToken: string) {
const answer = await this.requestAnswerFromAi(messages, chatConfig, accessToken);
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;
const replyWithoutCode = this.removeCode(answerContent);

return this.ctx.helper.getResponseData({
originalResponse: answer,
replyWithoutCode,
schema,
schema
});
}

async requestAnswerFromAi(messages: Array<AiMessage>, chatConfig: any,accessToken: string) {
async requestAnswerFromAi(messages: Array<AiMessage>, chatConfig: any) {
const { ctx } = this;
this.formatMessage(messages);
let res: any = null;
try {
// 根据大模型的不同匹配不同的配置
const aiChatConfig = this.config.aiChat(messages,accessToken);
const aiChatConfig = this.config.aiChat(messages, chatConfig.token);
const { httpRequestUrl, httpRequestOption } = aiChatConfig[chatConfig.model];
this.ctx.logger.debug(httpRequestOption)
this.ctx.logger.debug(httpRequestOption);
res = await ctx.curl(httpRequestUrl, httpRequestOption);

} catch (e: any) {
Expand All @@ -63,19 +62,16 @@ export default class AiChat extends Service {
}

if (!res) {

return this.ctx.helper.getResponseData(`调用AI大模型接口未返回正确数据.`);
}

// 适配文心一言的响应数据结构,文心的部分异常情况status也是200,需要转为400,以免前端无所适从
if (res.data?.error_code) {

return this.ctx.helper.getResponseData(res.data?.error_msg);
}

// 适配chatgpt的响应数据结构
if (res.status !== 200) {

return this.ctx.helper.getResponseData(res.data?.error?.message, res.status);
}

Expand All @@ -87,10 +83,10 @@ export default class AiChat extends Service {
{
message: {
role: 'assistant',
content: res.data.result,
},
},
],
content: res.data.result
}
}
]
};
}

Expand Down Expand Up @@ -157,7 +153,7 @@ export default class AiChat extends Service {
4. 回复中只能有一个代码块
5. 不要加任何注释
6. el-table标签内不得出现el-table-column
###`,
###`
};
const reg = /.*\u7f16\u7801\u65f6\u9075\u4ece\u4ee5\u4e0b\u51e0\u6761\u8981\u6c42.*/;
const { role, content } = messages[0];
Expand Down
57 changes: 28 additions & 29 deletions config/config.default.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 * as path from 'path';
import { EggAppConfig, PowerPartial } from 'egg';
import { I_SchemaConvert } from '../app/lib/interface';
Expand Down Expand Up @@ -59,13 +59,12 @@ export default (appInfo) => {
url: process.env.OBS_ACCESS_URL,
serviceUrl: process.env.OBS_SERVICE_URL,
subFolder: 'app-preview/source-code',
bucket: 'tiny-engine',
bucket: 'tiny-engine'
};

config.queueName = 'tinyengine.build.platform'; // 构建设计器 rabbitMq 队列名称



config.security = {
csrf: {
enable: false,
Expand Down Expand Up @@ -243,57 +242,57 @@ export default (appInfo) => {
method: 'POST',
dataType: 'json',
contentType: 'json',
timeout: 10 * 60 * 1000, // 这里与当前大模型接口的最大响应时长保持一致
timeout: 10 * 60 * 1000 // 这里与当前大模型接口的最大响应时长保持一致
};

//ai大模型相关配置,请自行替换服务配置
config.aiChat = (messages = [], accessToken: string) => {
return {
[E_FOUNDATION_MODEL.GPT_35_TURBO]: {
httpRequestUrl: (process.env.OPENAI_API_URL || 'https://api.openai.com')+'/v1/chat/completions',
httpRequestUrl: (process.env.OPENAI_API_URL || 'https://api.openai.com') + '/v1/chat/completions',
httpRequestOption: {
...commonRequestOption,
data: {
model: E_FOUNDATION_MODEL.GPT_35_TURBO,
messages,
messages
},
headers: {
Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
},
Authorization: `Bearer ${process.env.OPENAI_API_KEY}`
}
},
manufacturer: 'openai',
manufacturer: 'openai'
},
////本地兼容opanai-api接口的 大语言模型,如chatGLM6b,通义千问 等。你也可以分开成多个
[E_FOUNDATION_MODEL.Local_GPT]: {
httpRequestUrl: (process.env.Local_GPT_API_URL || 'http://127.0.0.1:8000')+'/v1/chat/completions',
httpRequestUrl: (process.env.Local_GPT_API_URL || 'http://127.0.0.1:8000') + '/v1/chat/completions',
httpRequestOption: {
...commonRequestOption,
data: {
model: E_FOUNDATION_MODEL.Local_GPT,
messages,
messages
},
headers: {
Authorization: `Bearer ${process.env.Local_GPT_API_KEY}`,
},
Authorization: `Bearer ${process.env.Local_GPT_API_KEY}`
}
},
manufacturer: '!openai',
manufacturer: '!openai'
},
[E_FOUNDATION_MODEL.ERNIE_BOT_TURBO]: {
httpRequestUrl: `https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/eb-instant?access_token=`+accessToken,
httpRequestUrl: `https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/eb-instant?access_token=${accessToken || process.env.WENXIN_ACCESS_TOKEN}`,
httpRequestOption: {
...commonRequestOption,
data: {
model: E_FOUNDATION_MODEL.ERNIE_BOT_TURBO,
messages,
},
messages
}
},
manufacturer: 'baidu',
},
manufacturer: 'baidu'
}
};
};

config.npmRegistryOptions = [
'--registry=https://registry.npmjs.org/',
'--registry=https://registry.npmjs.org/'
];

config.buildground = '/tmp/buildground';
Expand Down

0 comments on commit e305ec2

Please sign in to comment.