Skip to content

Commit

Permalink
Merge branch 'refs/heads/ospp-2024/feat-upload-image' into ospp-2024/…
Browse files Browse the repository at this point in the history
…optimize-schema-generation

# Conflicts:
#	app/service/app-center/aiChat.ts
  • Loading branch information
Fleurxxx committed Oct 20, 2024
2 parents 2c6c764 + 991b431 commit 902e7f0
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 27 deletions.
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 });
}
}
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
}
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);
};
2 changes: 0 additions & 2 deletions app/service/app-center/aiChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,8 @@ export default class AiChat extends Service {
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}`);
}

Expand Down
52 changes: 49 additions & 3 deletions 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 @@ -246,7 +245,7 @@ export default (appInfo) => {
};

//ai大模型相关配置,请自行替换服务配置
config.aiChat = (messages = [], accessToken: string) => {
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',
Expand Down Expand Up @@ -278,7 +277,7 @@ export default (appInfo) => {
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 || 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: {
Expand All @@ -287,6 +286,53 @@ export default (appInfo) => {
}
},
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.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}`
}
}
}
};
};

// 文件解析接口
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}`
}
}
}
};
};
Expand Down

0 comments on commit 902e7f0

Please sign in to comment.