Skip to content

Commit

Permalink
Merge pull request #20 from Fleurxxx/ospp-2024/feat-upload-image
Browse files Browse the repository at this point in the history
feat: Invoke the image upload and image recognition interface to real…
  • Loading branch information
wenmine authored Oct 22, 2024
2 parents 4b124c0 + 991b431 commit f744e45
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 12 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 });
}
}
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);
};
47 changes: 47 additions & 0 deletions app/service/app-center/aiChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export type AiMessage = {
content: string; // 聊天内容
};

interface ConfigModel {
model: string;
token: string;
}

export default class AiChat extends Service {
/**
* 获取ai的答复
Expand Down Expand Up @@ -162,5 +167,47 @@ export default class AiChat extends Service {
}
return messages;
}

/**
* 文件上传
*
* @param model
* @return
*/

async getFileContentFromAi(fileStream: any, chatConfig: ConfigModel) {
const answer = await this.requestFileContentFromAi(fileStream, chatConfig);
return this.ctx.helper.getResponseData({
originalResponse: answer
});
}

async requestFileContentFromAi(file: any, chatConfig: ConfigModel) {
const { ctx } = this;
let res: any = null;
try {
// 文件上传
const aiUploadConfig = this.config.uploadFile(file, chatConfig.token);
const { httpRequestUrl, httpRequestOption } = aiUploadConfig[chatConfig.model];
this.ctx.logger.debug(httpRequestOption);
res = await ctx.curl(httpRequestUrl, httpRequestOption);
const imageObject = JSON.parse(res.res.data.toString());
const fileObject = imageObject.data[0].id;
// 文件解析
const imageAnalysisConfig = this.config.parsingFile(fileObject, chatConfig.token);
const { analysisImageHttpRequestUrl, analysisImageHttpRequestOption } = imageAnalysisConfig[chatConfig.model];
res = await ctx.curl(analysisImageHttpRequestUrl, analysisImageHttpRequestOption);
res.data = JSON.parse(res.res.data.toString());
} catch (e: any) {
this.ctx.logger.debug(`调用上传图片接口失败: ${(e as Error).message}`);
return this.ctx.helper.getResponseData(`调用上传图片接口失败: ${(e as Error).message}`);
}

if (!res) {
return this.ctx.helper.getResponseData(`调用上传图片接口未返回正确数据.`);
}

return res.data;
}
}

34 changes: 33 additions & 1 deletion 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 { E_FOUNDATION_MODEL, E_SchemaFormatFunc } from '../app/lib/enum';
import { I_SchemaConvert } from '../app/lib/interface';


export default (appInfo) => {
const config = {} as PowerPartial<EggAppConfig>;

Expand Down Expand Up @@ -305,6 +304,39 @@ export default (appInfo) => {
};
};

// 文件上传接口
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}`
}
}
}
};
};

config.npmRegistryOptions = [
'--registry=https://registry.npmjs.org/'
];
Expand Down

0 comments on commit f744e45

Please sign in to comment.