Skip to content

Commit 387201f

Browse files
committed
fix:删除ai文件上传接口
1 parent f0b23ef commit 387201f

File tree

3 files changed

+0
-80
lines changed

3 files changed

+0
-80
lines changed

app/controller/app-center/aiChat.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,6 @@ export default class AiChatController extends Controller {
4747
});
4848
}
4949

50-
public async uploadFile() {
51-
const { ctx } = this;
52-
const fileStream = await ctx.getFileStream();
53-
const foundationModelObject = JSON.parse(fileStream.fields.foundationModel);
54-
const { model, token } = foundationModelObject.foundationModel;
55-
ctx.body = await ctx.service.appCenter.aiChat.getFileContentFromAi(fileStream, { model, token });
56-
}
57-
5850
public async search() {
5951
const { ctx } = this;
6052
const { content } = ctx.request.body;

app/router/appCenter/base.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,5 @@ export default (app: Application) => {
113113

114114
// AI大模型聊天接口
115115
subRouter.post('/ai/chat', controller.appCenter.aiChat.aiChat);
116-
subRouter.post('/ai/files', controller.appCenter.aiChat.uploadFile);
117116
subRouter.post('/ai/search', controller.appCenter.aiChat.search);
118117
};

app/service/app-center/aiChat.ts

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,20 @@
1010
*
1111
*/
1212
import { Service } from 'egg';
13-
import * as fs from 'fs';
14-
import * as path from 'path';
1513
import OpenApi, * as $OpenApi from '@alicloud/openapi-client';
1614
import OpenApiUtil from '@alicloud/openapi-util';
1715
import * as $Util from '@alicloud/tea-util';
1816
import Credential, { Config } from '@alicloud/credentials';
1917
import OpenAI from 'openai';
2018
import { ChatCompletionMessageParam } from 'openai/resources/chat/completions';
2119

22-
const to = require('await-to-js').default;
23-
2420
export type AiMessage = {
2521
role: string; // 角色
2622
name?: string; // 名称
2723
content: string; // 聊天内容
2824
partial?: boolean;
2925
};
3026

31-
interface ConfigModel {
32-
model: string;
33-
token: string;
34-
}
35-
3627
export default class AiChat extends Service {
3728
/**
3829
* 获取ai的答复
@@ -80,68 +71,6 @@ export default class AiChat extends Service {
8071
}
8172
}
8273

83-
/**
84-
* 文件上传
85-
*
86-
* @param model
87-
* @return
88-
*/
89-
90-
async getFileContentFromAi(fileStream: any, chatConfig: ConfigModel) {
91-
const answer = await this.requestFileContentFromAi(fileStream, chatConfig);
92-
return this.ctx.helper.getResponseData({
93-
originalResponse: answer
94-
});
95-
}
96-
97-
async requestFileContentFromAi(file: any, chatConfig: ConfigModel) {
98-
const { ctx } = this;
99-
const filename = Date.now() + path.extname(file.filename).toLowerCase();
100-
const savePath = path.join(__dirname, filename);
101-
const writeStream = fs.createWriteStream(savePath);
102-
file.pipe(writeStream);
103-
await new Promise((resolve) => writeStream.on('close', resolve));
104-
105-
const client = new OpenAI({
106-
apiKey: chatConfig.token,
107-
baseURL: 'https://api.moonshot.cn/v1'
108-
});
109-
110-
// 上传文件
111-
const [fileError, fileObject] = await to(
112-
client.files.create({
113-
file: fs.createReadStream(savePath),
114-
purpose: 'file-extract'
115-
})
116-
);
117-
118-
if (fileError) {
119-
this.ctx.logger.debug(`调用上传图片接口失败: ${fileError.message}`);
120-
await fs.promises.unlink(savePath).catch((err) => console.error('文件删除失败:', err));
121-
return this.ctx.helper.getResponseData(`调用上传图片接口失败: ${fileError.message}`);
122-
}
123-
124-
// 文件解析
125-
const imageAnalysisConfig = this.config.parsingFile(fileObject.id, chatConfig.token);
126-
const { analysisImageHttpRequestUrl, analysisImageHttpRequestOption } = imageAnalysisConfig[chatConfig.model];
127-
128-
const [analysisError, res] = await to(ctx.curl(analysisImageHttpRequestUrl, analysisImageHttpRequestOption));
129-
130-
if (analysisError) {
131-
this.ctx.logger.debug(`调用解析文件接口失败: ${analysisError.message}`);
132-
await fs.promises.unlink(savePath).catch((err) => console.error('文件删除失败:', err));
133-
return this.ctx.helper.getResponseData(`调用解析文件接口失败: ${analysisError.message}`);
134-
}
135-
136-
// 删除文件
137-
await fs.promises.unlink(savePath).catch((err) => console.error('文件删除失败:', err));
138-
await to(client.files.del(fileObject.id));
139-
140-
// 返回结果
141-
res.data = JSON.parse(res.res.data.toString());
142-
return res.data || this.ctx.helper.getResponseData('调用上传图片接口未返回正确数据.');
143-
}
144-
14574
/**
14675
* 知识库检索
14776
* @remarks

0 commit comments

Comments
 (0)