flow.mjs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { HumanMessage } from "langchain/schema"
  2. import { StructuredOutputParser } from "langchain/output_parsers"
  3. import { conversation, llm, logger } from "./llm.mjs"
  4. import { z } from "zod"
  5. import { uploadUml } from "./upload.mjs"
  6. import pRetry from "p-retry"
  7. async function genPlotNames(title, desc) {
  8. const scheme = StructuredOutputParser.fromZodSchema(
  9. z.array(
  10. z.object({
  11. title: z.string().describe("流程图名称")
  12. })
  13. )
  14. )
  15. const { content } = await llm.call([
  16. new HumanMessage(`${scheme.getFormatInstructions()}
  17. ------
  18. 你是一个计算机专业擅长写毕业论文的专家,你的任务是帮我的毕业设计撰写一篇论文
  19. 我的毕业设计项目是${title}
  20. 这个项目包含以下功能:
  21. ${desc}
  22. 现在我们分析一下论文中需要用到的流程图,我们需要绘制以下五个主要流程图:`)
  23. ])
  24. return await scheme.parse(content)
  25. }
  26. async function plotFlow(title, desc, name) {
  27. const { chain } = conversation(`你是一个计算机专业擅长写毕业论文的专家,你的任务是帮我的毕业设计撰写一篇论文
  28. 我的毕业设计项目是${title}
  29. 这个项目包含以下功能:
  30. ${desc}`)
  31. const { response: descr } = await chain.call({
  32. input: `现在我们需要在论文中附上${name},请你先用文字描述一下这个流程`
  33. })
  34. let { response: code } = await chain.call({
  35. input: `现在根据文字描述将它转换为plantuml语言
  36. 这是一个plantuml流程图的示例:
  37. \`\`\`
  38. @startuml 名称
  39. start
  40. if (Graphviz installed?) then (yes)
  41. :process all\ndiagrams;
  42. else (no)
  43. :process only
  44. __sequence__ and __activity__ diagrams;
  45. endif
  46. stop
  47. @enduml
  48. \`\`\``
  49. })
  50. code = /\`\`\`(?:plantuml)?([\s\S]+)\`\`\`/.exec(code)[1]
  51. const url = await uploadUml(code)
  52. return {
  53. name,
  54. desc: descr,
  55. url
  56. }
  57. }
  58. async function createFlow(title, desc) {
  59. const list = await pRetry(() => genPlotNames(title, desc), { retries: 5 })
  60. return await Promise.all(list.map((item) => pRetry(() => plotFlow(title, desc, item.title), { retries: 5 })))
  61. }
  62. export { createFlow }