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

feat: Invoke the image upload and image recognition interface to real… #20

Merged
merged 5 commits into from
Oct 22, 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
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;
Fleurxxx marked this conversation as resolved.
Show resolved Hide resolved
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;
Fleurxxx marked this conversation as resolved.
Show resolved Hide resolved
// 文件解析
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 { 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 @@ -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}`
}
}
}
};
};
Fleurxxx marked this conversation as resolved.
Show resolved Hide resolved

// 文件解析接口
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}`
}
}
}
};
};
Fleurxxx marked this conversation as resolved.
Show resolved Hide resolved

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