ai-query对接新版freesun-agent接口的分支
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

89 lines
2.8 KiB

'use strict';
const superagent = require('superagent');
const moment = require('moment');
const { convertHTMLToDOCX } = require('../utils/tools');
module.exports.addIndustrySolution = async (ctx, next) => {
try {
const { models } = ctx.app.fs.dc;
const { apiUrl, industrySolutionAppKey } = ctx.app.fs.config.fastGpt;
const { monitoringItem, factors, userId } = ctx.request.body;
if (!monitoringItem || !factors || !userId) { throw '缺少请求参数' };
const userText = `生成${monitoringItem}监测方案, 监测因素: ${factors.join('、')}`;
const res = await superagent
.post(`${apiUrl}/api/v1/chat/completions`)
.send({
"stream": false,
"detail": true,
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": userText
}
]
}
]
})
.set({
"Authorization": `Bearer ${industrySolutionAppKey}`,
"Content-Type": "application/json",
})
.timeout({
response: 1000 * 60 * 24,
deadline: 1000 * 60 * 24,
});
const content = res.body.choices[0].message.content.replace('\nFS·监测方案GENERATE:', '');
const tokens = res.body.responseData.reduce((sum, item) => {
if (item.inputTokens) sum += item.inputTokens;
if (item.outputTokens) sum += item.outputTokens;
if (item.embeddingTokens) sum += item.embeddingTokens;
return sum;
}, 0);
// 保存到数据库
await models.IndustrySolutionRecord.create({
content,
tokens,
userId,
createdAt: moment().format('YYYY-MM-DD HH:mm:ss'),
updatedAt: moment().format('YYYY-MM-DD HH:mm:ss'),
});
ctx.body = content;
ctx.status = 200;
} catch (error) {
ctx.logger.log(error);
ctx.status = 400;
ctx.body = {
message: typeof error == 'string' ? error : '生成行业方案失败'
};
}
}
module.exports.downloadIndustrySolution = async (ctx, next) => {
try {
const { content } = ctx.request.body;
if (!content) { throw '缺少请求参数' };
const docxBuffer = await convertHTMLToDOCX(content);
ctx.set('Content-Type', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
ctx.set('Content-Disposition', `attachment; filename=${encodeURIComponent(`监测方案`)}.docx`);
ctx.body = docxBuffer;
ctx.status = 200;
} catch (error) {
ctx.logger.log(error);
ctx.status = 400;
ctx.body = {
message: typeof error == 'string' ? error : '下载行业方案失败'
};
}
}