当前位置: 首页 > news >正文

OpenAI-Translator 实战总结

最近在极客时间学习《AI 大模型应用开发实战营》,自己一边跟着学一边开发了一个进阶版本的 OpenAI-Translator,在这里简单记录下开发过程和心得体会,供有兴趣的同学参考

  • 功能概览

    • 通过openai的chat API,实现一个pdf翻译器
    • 实现一个web GUI,可以上传pdf文件,然后翻译成目标语言的pdf文件
    • 实现了一个openai的plugin,pdf翻译器
  • 系统概述

    PDF Translator
    Load
    Prompt
    Response
    Save
    Parsed Content
    Model
    PDF Parser
    Writer
    PDF File
    ChatGPT
    Translated PDF File
    • PDF Parser
      • 通过pdfminer.six库,解析pdf文件,得到pdf的内容
      • 通过解析pdf的内容,得到pdf的图片,表格,文本等内容
    • Model
      • 通过prompt的方式,把pdf的内容转化为目标语言
    • Writer
      • 把翻译后的内容,写入到pdf 或者markdown文件中
  • Prompt 优化过程

    • 第一次尝试

      • 为了支持其他语言的翻译,在prompt里把目标语言设置为变量 {target_language}
      [{"role": "system","content": "For the input text, translate it into {target_language} as native speaker"
      },
      {"role": "user","content": "“Santiago,” the boy said.\n"
      }]
      
      • 问题
        • 表格格式没有保留
        • 翻译不准确,表现是没有把user 的content内容翻译成目标语言
    • 优化Prompt里system的描述

      • 增加格式化输出的描述 output each of these data in json format,方便代码后续解析
      • 增加prompt,让模型知道需要翻译的内容所属语言是什么 Identify the language of input text
      [{"role": "system","content": "From the input text, output each of these data in json format:\n\n1. (language) Identify the language of input text \n2. (translation) Translate to Chinese as a native speaker and keep the original character format of the text unchanged\n3. (translation_language) The language translated from the input text"
      },
      {"role": "user","content": "“Santiago,” the boy said.\n"
      } ]
      
      • 问题
        • 偶尔翻译不准确,表现是没有把user 的content内容翻译成目标语言
    • 再次优化Prompt,增加黑魔法,think step by step

      • 翻译text的prompt
         [{"role": "system","content": f"You act as a language expert, do a language translation job, here are the steps:\n\n"f"1. (language) Identify the language of input text \n"f"2. (translation) Translate the input text to {target_language} as a native speaker \n"f"3. (output) output the language and translation in json format\n"},{"role": "user","content": f"“Santiago,” the boy said.\n"}]
      
      • 翻译table的prompt
        • 加注:保持表格的原有格式与标点符号 format and maintain spacing (spaces, separators), and return in tabular form
           [{"role": "system","content": f"From the input text, do a language translation job, here are the steps:\n\n"f"1. (language) Identify the language of input text \n"f"2. (translation) Translate the input text to {target_language} as a native speaker, format and maintain spacing (spaces, separators), and return in tabular form\n"f"3. (output) output the language and translation in json format"},{"role": "user","content": f"{table content}"}]
      
  • 代码结构

    • 重构content
      Content
      set_translation
      ImageContent
      set_translation
      TextContent
      set_translation
    • 重构parser
      PageParser
      parse
      PageImageParser
      parse
      PageTableParser
      parse
      PageTextParser
      parse
    • 重构writer
      Writer
      save
      PDFWriter
      save
      MarkdownWriter
      save
  • PDF格式

    • 为了保持PDF文档内容结构,需要保留原文档的空格,换行,分隔符等
    • 分段策略
      • 以空行为分段标志
    • 翻译后的文档,保持原文档的空格,换行,分隔符等
    • 示例:老人与海的pdf文档
      在这里插入图片描述
  • 图形用户界面GUI 设计

    • 设计以及代码由 gpt4 生成
    • 如下是使用的prompt
      • 对一个翻译pdf功能的服务器设计一个GUI
      • 请用python实现一下
      • 请用 HTML、CSS 和 JavaScript 实现一个你上文提到的UI
      • 请增加这些功能显示文件上传和翻译进度、选择源语言和目标语言、处理文件上传大小的限制
      • 对于代码的错误,交给gpt4,让它来修复代码中的问题
    • 详见 chatGPT share
    • Web GUI效果图
      在这里插入图片描述
  • plugin开发

    • openapi文档的生成,通过gpt4生成
      • 通过把路由代码,交给gpt,然后让它生成对应的openapi文档,然后对应着plugin 官方文档修正下,就可以了
openapi: 3.0.1info:title: PDF Translatordescription: A Plugin that allows the user to translate the PDF to any language they want.version: 'v1'servers:- url: http://localhost:8080paths:/translate:post:operationId: translatePDFsummary: Translate the content of PDF file to target languageparameters:- content:multipart/form-data:schema:type: objectproperties:file:type: stringformat: binarydescription: The PDF file that should be translated todst_lang:type: stringdescription: The language and should be translated to, e.g. Chinese.responses:"200":description: OKcontent:application/json:schema:$ref: '#/components/schemas/translatePDF''400':description: Bad Requestcontent:text/plain:schema:type: string'500':description: Internal Server Errorcomponents:schemas:translatePDF:type: stringproperties:weather:type: stringformat: binarydescription: The content of PDF file that has been translated
  • 收获和总结

    • openai API的使用 chat API调用,以及各个参数的含义
    • openai playground的使用,调试prompt很好用的工具
    • prompt的使用以及优化,这一点需要在实际的案例中加强训练
    • 熟悉了openai plugin开发步骤以及流程
  • Github Code

http://www.lryc.cn/news/117127.html

相关文章:

  • 【工业机器人】用于轨迹规划和执行器分析的机械手和移动机器人模型(MatlabSimulink)
  • 开源在线文档服务OnlyOffice
  • 汽车基本常识
  • 百度资深PMO阚洁受邀为第十二届中国PMO大会演讲嘉宾
  • 为什么C++有多种整型?
  • 玩一玩通义千问Qwen开源版,Win11 RTX3060本地安装记录!
  • oracle积累增量和差异增量
  • 利用C++nlohmann库解析json文件
  • OpenCV 中的光流 (C++/Python)
  • 第9集丨Vue 江湖 —— 监测数据原理
  • 【YOLO】替换骨干网络为轻量级网络MobileNet3
  • 如何识别手机是否有灵动岛(dynamic island)
  • Linux设备树简介
  • Ubuntu类IOS主题设置
  • RabbitMQ学习——发布订阅/fanout模式 topic模式 rabbitmq回调确认 延迟队列(死信)设计
  • Leetcode算法递归类—合并两个有序链表
  • YOLOv5可视化界面
  • C语言使用库函数实现大小写字母转换
  • Redis简单学习
  • 《Python入门到精通》函数详解
  • PHP流浪动物招领网站mysql数据库web结构apache计算机软件工程网页wamp
  • android—ktor-client封装使用,请求网络
  • GD32F103VE侵入事件
  • 将tp5项目、fastadmin项目部署到服务器宝塔面板
  • Jenkins+Docker+SpringCloud微服务持续集成
  • 系统架构设计师-系统可靠性分析与设计
  • Linux(CentOS7)搭建达梦数据库
  • [杂谈]-国产MCU竞争力浅析
  • 4.1、Flink任务怎样读取集合中的数据
  • JD商品详情页面+关键词搜索商品列表API接口数据,详情页面数据返回值说明