|
10 | 10 | * |
11 | 11 | */ |
12 | 12 | import { Service } from 'egg'; |
13 | | -import * as fs from 'fs'; |
14 | | -import * as path from 'path'; |
15 | 13 | import OpenApi, * as $OpenApi from '@alicloud/openapi-client'; |
16 | 14 | import OpenApiUtil from '@alicloud/openapi-util'; |
17 | 15 | import * as $Util from '@alicloud/tea-util'; |
18 | 16 | import Credential, { Config } from '@alicloud/credentials'; |
19 | 17 | import OpenAI from 'openai'; |
20 | 18 | import { ChatCompletionMessageParam } from 'openai/resources/chat/completions'; |
21 | 19 |
|
22 | | -const to = require('await-to-js').default; |
23 | | - |
24 | 20 | export type AiMessage = { |
25 | 21 | role: string; // 角色 |
26 | 22 | name?: string; // 名称 |
27 | 23 | content: string; // 聊天内容 |
28 | 24 | partial?: boolean; |
29 | 25 | }; |
30 | 26 |
|
31 | | -interface ConfigModel { |
32 | | - model: string; |
33 | | - token: string; |
34 | | -} |
35 | | - |
36 | 27 | export default class AiChat extends Service { |
37 | 28 | /** |
38 | 29 | * 获取ai的答复 |
@@ -80,68 +71,6 @@ export default class AiChat extends Service { |
80 | 71 | } |
81 | 72 | } |
82 | 73 |
|
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 | | - |
145 | 74 | /** |
146 | 75 | * 知识库检索 |
147 | 76 | * @remarks |
|
0 commit comments