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

fix: Fixed the issue that the access_token expired and could not be refreshed #18

Merged
merged 2 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 });

}
}
40 changes: 18 additions & 22 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,9 +50,9 @@ 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) {
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
59 changes: 29 additions & 30 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 = []) => {
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=${process.env.WENXIN_ACCESS_TOKEN}`,
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
Loading