From 023f4613ebf2064395e7b8652c84249d33381f1a Mon Sep 17 00:00:00 2001 From: Fleurxxx <984209872@qq.com> Date: Wed, 31 Jul 2024 01:56:04 +0800 Subject: [PATCH] fix: Tweak code details --- app/controller/app-center/aiChat.ts | 33 +++++++---------- app/service/app-center/aiChat.ts | 46 +++++++++++------------ config/config.default.ts | 57 ++++++++++++++--------------- 3 files changed, 63 insertions(+), 73 deletions(-) diff --git a/app/controller/app-center/aiChat.ts b/app/controller/app-center/aiChat.ts index bb6bca9..9527c80 100644 --- a/app/controller/app-center/aiChat.ts +++ b/app/controller/app-center/aiChat.ts @@ -1,33 +1,28 @@ /** -* 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'; export default class AiChatController extends Controller { - - - /** - * @router post /api/ai/chat 路径 - * @summary AI大模型聊天 - * @description 根据角色和提问信息返回AI答复 - */ 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 }); + } } diff --git a/app/service/app-center/aiChat.ts b/app/service/app-center/aiChat.ts index 7b5d759..972499a 100644 --- a/app/service/app-center/aiChat.ts +++ b/app/service/app-center/aiChat.ts @@ -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'; @@ -30,30 +30,29 @@ export default class AiChat extends Service { * @return */ - async getAnswerFromAi(messages: Array, chatConfig: any, accessToken: string) { - const answer = await this.requestAnswerFromAi(messages, chatConfig, accessToken); + async getAnswerFromAi(messages: Array, 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, chatConfig: any,accessToken: string) { + async requestAnswerFromAi(messages: Array, 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) { @@ -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); } @@ -87,10 +83,10 @@ export default class AiChat extends Service { { message: { role: 'assistant', - content: res.data.result, - }, - }, - ], + content: res.data.result + } + } + ] }; } @@ -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]; diff --git a/config/config.default.ts b/config/config.default.ts index 7f1c1d0..c54f7ba 100644 --- a/config/config.default.ts +++ b/config/config.default.ts @@ -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'; @@ -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, @@ -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';