Skip to content

Commit 902e7f0

Browse files
committed
Merge branch 'refs/heads/ospp-2024/feat-upload-image' into ospp-2024/optimize-schema-generation
# Conflicts: # app/service/app-center/aiChat.ts
2 parents 2c6c764 + 991b431 commit 902e7f0

File tree

5 files changed

+80
-27
lines changed

5 files changed

+80
-27
lines changed

app/controller/app-center/aiChat.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ export default class AiChatController extends Controller {
2323
const model = foundationModel?.model ?? E_FOUNDATION_MODEL.GPT_35_TURBO;
2424
const token = foundationModel.token;
2525
ctx.body = await ctx.service.appCenter.aiChat.getAnswerFromAi(messages, { model, token });
26+
}
27+
2628

29+
public async uploadFile() {
30+
const { ctx } = this;
31+
const fileStream = await ctx.getFileStream();
32+
const foundationModelObject = JSON.parse(fileStream.fields.foundationModel);
33+
const { model, token } = foundationModelObject.foundationModel;
34+
ctx.body = await ctx.service.appCenter.aiChat.getFileContentFromAi(fileStream, { model, token });
2735
}
2836
}

app/lib/enum.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
/**
2-
* Copyright (c) 2023 - present TinyEngine Authors.
3-
* Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd.
4-
*
5-
* Use of this source code is governed by an MIT-style license.
6-
*
7-
* THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
8-
* BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
9-
* A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
10-
*
11-
*/
2+
* Copyright (c) 2023 - present TinyEngine Authors.
3+
* Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd.
4+
*
5+
* Use of this source code is governed by an MIT-style license.
6+
*
7+
* THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
8+
* BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
9+
* A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
10+
*
11+
*/
1212
// 请求method
1313
export enum E_Method {
1414
Get = 'GET',
@@ -289,5 +289,6 @@ export enum E_Public {
289289
export enum E_FOUNDATION_MODEL {
290290
GPT_35_TURBO = 'gpt-3.5-turbo', // openai
291291
Local_GPT = 'loacl-compatible-gpt-3.5', //本地兼容opanai-api接口的 大语言模型,如chatGLM6b,通义千问 等。
292-
ERNIE_BOT_TURBO = 'ERNIE-Bot-turbo' // 文心一言
292+
ERNIE_BOT_TURBO = 'ERNIE-Bot-turbo', // 文心一言
293+
MOONSHOT_V1_8K = 'moonshot-v1-8k' // kimi
293294
}

app/router/appCenter/base.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
/**
2-
* Copyright (c) 2023 - present TinyEngine Authors.
3-
* Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd.
4-
*
5-
* Use of this source code is governed by an MIT-style license.
6-
*
7-
* THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
8-
* BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
9-
* A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
10-
*
11-
*/
2+
* Copyright (c) 2023 - present TinyEngine Authors.
3+
* Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd.
4+
*
5+
* Use of this source code is governed by an MIT-style license.
6+
*
7+
* THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
8+
* BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
9+
* A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
10+
*
11+
*/
1212
import { Application } from 'egg';
1313

1414
export default (app: Application) => {
@@ -22,7 +22,6 @@ export default (app: Application) => {
2222
const subRouter = router.namespace(ROUTER_PREFIX);
2323

2424
// 应用管理
25-
2625
subRouter.get('/apps/detail/:id', controller.appCenter.apps.detail);
2726
subRouter.post('/apps/update/:id', controller.appCenter.apps.update);
2827

@@ -114,4 +113,5 @@ export default (app: Application) => {
114113

115114
// AI大模型聊天接口
116115
subRouter.post('/ai/chat', controller.appCenter.aiChat.aiChat);
116+
subRouter.post('/ai/files', controller.appCenter.aiChat.uploadFile);
117117
};

app/service/app-center/aiChat.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,8 @@ export default class AiChat extends Service {
6969
this.ctx.logger.debug(httpRequestOption);
7070

7171
res = await ctx.curl(httpRequestUrl, httpRequestOption);
72-
7372
} catch (e: any) {
7473
this.ctx.logger.debug(`调用AI大模型接口失败: ${(e as Error).message}`);
75-
7674
return this.ctx.helper.getResponseData(`调用AI大模型接口失败: ${(e as Error).message}`);
7775
}
7876

config/config.default.ts

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import { EggAppConfig, PowerPartial } from 'egg';
1414
import { I_SchemaConvert } from '../app/lib/interface';
1515
import { E_SchemaFormatFunc, E_FOUNDATION_MODEL } from '../app/lib/enum';
1616

17-
1817
export default (appInfo) => {
1918
const config = {} as PowerPartial<EggAppConfig>;
2019

@@ -246,7 +245,7 @@ export default (appInfo) => {
246245
};
247246

248247
//ai大模型相关配置,请自行替换服务配置
249-
config.aiChat = (messages = [], accessToken: string) => {
248+
config.aiChat = (messages = [], token: string) => {
250249
return {
251250
[E_FOUNDATION_MODEL.GPT_35_TURBO]: {
252251
httpRequestUrl: (process.env.OPENAI_API_URL || 'https://api.openai.com') + '/v1/chat/completions',
@@ -278,7 +277,7 @@ export default (appInfo) => {
278277
manufacturer: '!openai'
279278
},
280279
[E_FOUNDATION_MODEL.ERNIE_BOT_TURBO]: {
281-
httpRequestUrl: `https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/eb-instant?access_token=${accessToken || process.env.WENXIN_ACCESS_TOKEN}`,
280+
httpRequestUrl: `https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/eb-instant?access_token=${token || process.env.WENXIN_ACCESS_TOKEN}`,
282281
httpRequestOption: {
283282
...commonRequestOption,
284283
data: {
@@ -287,6 +286,53 @@ export default (appInfo) => {
287286
}
288287
},
289288
manufacturer: 'baidu'
289+
},
290+
[E_FOUNDATION_MODEL.MOONSHOT_V1_8K]: {
291+
httpRequestUrl: `https://api.moonshot.cn/v1/chat/completions`,
292+
httpRequestOption: {
293+
...commonRequestOption,
294+
data: {
295+
model: E_FOUNDATION_MODEL.MOONSHOT_V1_8K,
296+
messages
297+
},
298+
headers: {
299+
Authorization: `Bearer ${token}`
300+
}
301+
},
302+
manufacturer: 'kimi'
303+
}
304+
};
305+
};
306+
307+
// 文件上传接口
308+
config.uploadFile = (file: any, token: string) => {
309+
return {
310+
[E_FOUNDATION_MODEL.MOONSHOT_V1_8K]: {
311+
httpRequestUrl: `https://api.moonshot.cn/v1/files`,
312+
httpRequestOption: {
313+
data: {
314+
file: file,
315+
purpose: 'file-extract'
316+
},
317+
headers: {
318+
Authorization: `Bearer ${token}`
319+
}
320+
}
321+
}
322+
};
323+
};
324+
325+
// 文件解析接口
326+
config.parsingFile = (fileId: any, token: string) => {
327+
return {
328+
[E_FOUNDATION_MODEL.MOONSHOT_V1_8K]: {
329+
analysisImageHttpRequestUrl: `https://api.moonshot.cn/v1/files/${fileId}/content`,
330+
analysisImageHttpRequestOption: {
331+
method: 'GET',
332+
headers: {
333+
Authorization: `Bearer ${token}`
334+
}
335+
}
290336
}
291337
};
292338
};

0 commit comments

Comments
 (0)