Skip to content

Commit

Permalink
Merge branch 'main' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
lu-yg authored Oct 12, 2024
2 parents c866632 + fbf50b5 commit abbada5
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 74 deletions.
24 changes: 13 additions & 11 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 @@ -21,6 +21,8 @@ export default class AiChatController extends Controller {
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 });
const token = foundationModel.token;
ctx.body = await ctx.service.appCenter.aiChat.getAnswerFromAi(messages, { model, token });

}
}
23 changes: 12 additions & 11 deletions app/lib/enum.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.
*
*/
// 请求method
export enum E_Method {
Get = 'GET',
Expand Down Expand Up @@ -289,5 +289,6 @@ export enum E_Public {
export enum E_FOUNDATION_MODEL {
GPT_35_TURBO = 'gpt-3.5-turbo', // openai
Local_GPT = 'loacl-compatible-gpt-3.5', //本地兼容opanai-api接口的 大语言模型,如chatGLM6b,通义千问 等。
ERNIE_BOT_TURBO = 'ERNIE-Bot-turbo' // 文心一言
ERNIE_BOT_TURBO = 'ERNIE-Bot-turbo', // 文心一言
MOONSHOT_V1_8K = 'moonshot-v1-8k' // kimi
}
42 changes: 18 additions & 24 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 Down Expand Up @@ -37,11 +37,10 @@ export default class AiChat extends Service {
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
});
}

Expand All @@ -51,31 +50,26 @@ export default class AiChat extends Service {
let res: any = null;
try {
// 根据大模型的不同匹配不同的配置
const aiChatConfig = this.config.aiChat(messages);
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) {
this.ctx.logger.debug(`调用AI大模型接口失败: ${(e as Error).message}`);

return this.ctx.helper.getResponseData(`调用AI大模型接口失败: ${(e as Error).message}`);
}

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 +81,10 @@ export default class AiChat extends Service {
{
message: {
role: 'assistant',
content: res.data.result,
},
},
],
content: res.data.result
}
}
]
};
}

Expand Down Expand Up @@ -157,7 +151,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
69 changes: 41 additions & 28 deletions config/config.default.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/**
* 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 { EggAppConfig, PowerPartial } from 'egg';
* 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 { E_FOUNDATION_MODEL, E_SchemaFormatFunc } from '../app/lib/enum';
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,25 +242,25 @@ export default (appInfo) => {
method: 'POST',
dataType: 'json',
contentType: 'json',
timeout: 10 * 60 * 1000, // 这里与当前大模型接口的最大响应时长保持一致
timeout: 10 * 60 * 1000 // 这里与当前大模型接口的最大响应时长保持一致
};

//ai大模型相关配置,请自行替换服务配置
config.aiChat = (messages = []) => {
config.aiChat = (messages = [], token: string) => {
return {
[E_FOUNDATION_MODEL.GPT_35_TURBO]: {
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]: {
Expand All @@ -270,30 +269,44 @@ export default (appInfo) => {
...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=${process.env.WENXIN_ACCESS_TOKEN}`,
httpRequestUrl: `https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/eb-instant?access_token=${token || process.env.WENXIN_ACCESS_TOKEN}`,
httpRequestOption: {
...commonRequestOption,
data: {
model: E_FOUNDATION_MODEL.ERNIE_BOT_TURBO,
messages,
},
messages
}
},
manufacturer: 'baidu',
manufacturer: 'baidu'
},
[E_FOUNDATION_MODEL.MOONSHOT_V1_8K]: {
httpRequestUrl: `https://api.moonshot.cn/v1/chat/completions`,
httpRequestOption: {
...commonRequestOption,
data: {
model: E_FOUNDATION_MODEL.MOONSHOT_V1_8K,
messages
},
headers: {
Authorization: `Bearer ${token}`
}
},
manufacturer: 'kimi'
}
};
};

config.npmRegistryOptions = [
'--registry=https://registry.npmjs.org/',
'--registry=https://registry.npmjs.org/'
];
// 国内镜像
config.cnpmRegistryOptions = [
Expand Down

0 comments on commit abbada5

Please sign in to comment.