From 5db3e3a661ecbdb6cfdcf55ea9957df02aa522c0 Mon Sep 17 00:00:00 2001 From: Fleurxxx <984209872@qq.com> Date: Sat, 17 Aug 2024 19:32:54 +0800 Subject: [PATCH 1/4] feat: Invoke the image upload and image recognition interface to realize the function of carrying picture dialogue --- app/controller/app-center/aiChat.ts | 8 ++++++ app/router/appCenter/base.ts | 22 +++++++-------- app/service/app-center/aiChat.ts | 42 +++++++++++++++++++++++++++++ config/config.default.ts | 34 ++++++++++++++++++++++- 4 files changed, 94 insertions(+), 12 deletions(-) diff --git a/app/controller/app-center/aiChat.ts b/app/controller/app-center/aiChat.ts index 9527c80..350af76 100644 --- a/app/controller/app-center/aiChat.ts +++ b/app/controller/app-center/aiChat.ts @@ -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 }); } } diff --git a/app/router/appCenter/base.ts b/app/router/appCenter/base.ts index c406442..2b7c273 100644 --- a/app/router/appCenter/base.ts +++ b/app/router/appCenter/base.ts @@ -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) => { @@ -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); @@ -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); }; diff --git a/app/service/app-center/aiChat.ts b/app/service/app-center/aiChat.ts index 85d3edf..61f7a8c 100644 --- a/app/service/app-center/aiChat.ts +++ b/app/service/app-center/aiChat.ts @@ -162,5 +162,47 @@ export default class AiChat extends Service { } return messages; } + + /** + * 文件上传 + * + * @param model + * @return + */ + + async getFileContentFromAi(fileStream: any, chatConfig: any) { + const answer = await this.requestFileContentFromAi(fileStream, chatConfig); + return this.ctx.helper.getResponseData({ + originalResponse: answer + }); + } + + async requestFileContentFromAi(file: any, chatConfig: any) { + 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; + } } diff --git a/config/config.default.ts b/config/config.default.ts index ac0d902..d078264 100644 --- a/config/config.default.ts +++ b/config/config.default.ts @@ -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; @@ -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/' ]; From 290ed484b919b75b805dbfe1fd1e751a0ba8dc49 Mon Sep 17 00:00:00 2001 From: wangjunyue <984209872@qq.com> Date: Sun, 20 Oct 2024 12:35:38 +0800 Subject: [PATCH 2/4] =?UTF-8?q?feat=EF=BC=9AAdd=20type?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/service/app-center/aiChat.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/app/service/app-center/aiChat.ts b/app/service/app-center/aiChat.ts index 61f7a8c..71487a3 100644 --- a/app/service/app-center/aiChat.ts +++ b/app/service/app-center/aiChat.ts @@ -12,6 +12,8 @@ import { Service } from 'egg'; import Transformer from '@opentiny/tiny-engine-transform'; import { E_FOUNDATION_MODEL } from '../../lib/enum'; +import { gzip } from 'compressing'; +import FileStream = gzip.FileStream; export type AiMessage = { role: string; // 角色 @@ -19,6 +21,11 @@ export type AiMessage = { content: string; // 聊天内容 }; +interface ConfigModel { + model: string, + token: string +} + export default class AiChat extends Service { /** * 获取ai的答复 @@ -170,14 +177,14 @@ export default class AiChat extends Service { * @return */ - async getFileContentFromAi(fileStream: any, chatConfig: any) { + async getFileContentFromAi(fileStream: FileStream, chatConfig: ConfigModel) { const answer = await this.requestFileContentFromAi(fileStream, chatConfig); return this.ctx.helper.getResponseData({ originalResponse: answer }); } - async requestFileContentFromAi(file: any, chatConfig: any) { + async requestFileContentFromAi(file: FileStream, chatConfig: ConfigModel) { const { ctx } = this; let res: any = null; try { From 69f950138b842f30fb672e8639f34376f4ec62fd Mon Sep 17 00:00:00 2001 From: wangjunyue <984209872@qq.com> Date: Sun, 20 Oct 2024 12:42:42 +0800 Subject: [PATCH 3/4] =?UTF-8?q?feat=EF=BC=9AAdd=20type?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/service/app-center/aiChat.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/service/app-center/aiChat.ts b/app/service/app-center/aiChat.ts index 71487a3..2649f5a 100644 --- a/app/service/app-center/aiChat.ts +++ b/app/service/app-center/aiChat.ts @@ -22,8 +22,8 @@ export type AiMessage = { }; interface ConfigModel { - model: string, - token: string + model: string; + token: string; } export default class AiChat extends Service { From 991b431a4a73442c36c8342fe345a8b2bf67ba1f Mon Sep 17 00:00:00 2001 From: wangjunyue <984209872@qq.com> Date: Mon, 21 Oct 2024 02:22:06 +0800 Subject: [PATCH 4/4] fix: fix type problem --- app/service/app-center/aiChat.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/app/service/app-center/aiChat.ts b/app/service/app-center/aiChat.ts index 2649f5a..23a268d 100644 --- a/app/service/app-center/aiChat.ts +++ b/app/service/app-center/aiChat.ts @@ -12,8 +12,6 @@ import { Service } from 'egg'; import Transformer from '@opentiny/tiny-engine-transform'; import { E_FOUNDATION_MODEL } from '../../lib/enum'; -import { gzip } from 'compressing'; -import FileStream = gzip.FileStream; export type AiMessage = { role: string; // 角色 @@ -177,14 +175,14 @@ export default class AiChat extends Service { * @return */ - async getFileContentFromAi(fileStream: FileStream, chatConfig: ConfigModel) { + async getFileContentFromAi(fileStream: any, chatConfig: ConfigModel) { const answer = await this.requestFileContentFromAi(fileStream, chatConfig); return this.ctx.helper.getResponseData({ originalResponse: answer }); } - async requestFileContentFromAi(file: FileStream, chatConfig: ConfigModel) { + async requestFileContentFromAi(file: any, chatConfig: ConfigModel) { const { ctx } = this; let res: any = null; try {