FastGPT icon indicating copy to clipboard operation
FastGPT copied to clipboard

【插件】请教大佬,请添加大模型生成pdf和word之类的插件。

Open arraycto opened this issue 1 year ago • 4 comments

版本:所有版本 【插件】请教大佬,请添加大模型生成pdf和word之类的插件。 非导入,是导出插件 导出插件

arraycto avatar Aug 26 '24 02:08 arraycto

目前我是通过laf实现生成word,上传以及返回url的插件 `import cloud from '@lafjs/cloud'; import officegen from 'officegen'; import { PassThrough } from 'stream';

export default async function (ctx) { const bucket = cloud.storage.bucket('存储桶的名称'); const { content } = ctx.body;

if (!content) { console.error('Content is required'); return { code: 400, message: 'Content is required' }; }

const now = new Date(); const formattedDate = now.toISOString().replace(/T/, '-').replace(/:/g, '-').slice(0, 19); const fileName = Memo${formattedDate}.docx;

// 创建一个新的 Word 文档 const doc = officegen('docx');

// 解析 Markdown 内容并添加到文档 const lines = content.split('\n'); lines.forEach((line) => { const paragraph = doc.createP();

// 处理标题
if (line.startsWith('# ')) {
  paragraph.addText(line.replace('# ', ''), { bold: true, font_size: 24 });
  paragraph.options.align = 'center'; // 设置对齐方式
} else if (line.startsWith('## ')) {
  paragraph.addText(line.replace('## ', ''), { bold: true, font_size: 20 });
  paragraph.options.align = 'left'; // 设置对齐方式
} else if (line.startsWith('### ')) {
  paragraph.addText(line.replace('### ', ''), { bold: true, font_size: 16 });
  paragraph.options.align = 'left'; // 设置对齐方式
} else {
  // 处理加粗文本
  const parts = line.split(/\*\*(.*?)\*\*/g); // 使用正则表达式分割加粗文本
  parts.forEach((part, index) => {
    if (index % 2 === 0) {
      // 普通文本
      paragraph.addText(part);
    } else {
      // 加粗文本
      paragraph.addText(part, { bold: true });
    }
  });
}

});

// 创建一个 PassThrough 流 const pass = new PassThrough();

// 将文档生成到 PassThrough 流 doc.generate(pass);

// 将 PassThrough 流转换为 Buffer const buffer = await new Promise((resolve, reject) => { const chunks = []; pass.on('data', chunk => chunks.push(chunk)); pass.on('end', () => resolve(Buffer.concat(chunks))); pass.on('error', reject); });

// 写文件到存储桶 await bucket.writeFile(xxx/${fileName}, buffer);

// 构造文件的公共 URL,换成laf存储桶的url const fileUrl = https://xxxx/xxxx/${fileName};

return { code: 200, message: 'File uploaded successfully', url: fileUrl }; } `

lijiajun1997 avatar Aug 26 '24 10:08 lijiajun1997

大佬,牛啊:请问:1、私有化部署能使用上面的代码达到预期效果么?2、是不是只能源码部署,更改源码才能达到效果,docker-compose部署,不可以。跪求回复@lijiajun1997 少年,果然真豪杰。恭迎各路大佬。

arraycto avatar Aug 27 '24 07:08 arraycto

大佬,牛啊:请问:1、私有化部署能使用上面的代码达到预期效果么?2、是不是只能源码部署,更改源码才能达到效果,docker-compose部署,不可以。跪求回复@lijiajun1997 少年,果然真豪杰。恭迎各路大佬。 不用,本地部署也可以自己写api,你用python更简单,丢给gpt写一个api出来把收到的内容转成word,然后输出文档。然后把这个api作为fastgpt的插件使用就好

lijiajun1997 avatar Aug 27 '24 07:08 lijiajun1997

有参考的开源例子么,大佬,我学习一下。夯实一下您的思路。@lijiajun1997

arraycto avatar Aug 27 '24 08:08 arraycto