| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- import { HumanMessage } from "langchain/schema"
- import { StructuredOutputParser } from "langchain/output_parsers"
- import { conversation, llm, logger } from "./llm.mjs"
- import { z } from "zod"
- import { uploadUml } from "./upload.mjs"
- import pRetry from "p-retry"
- async function genPlotNames(title, desc) {
- const scheme = StructuredOutputParser.fromZodSchema(
- z.array(
- z.object({
- title: z.string().describe("流程图名称")
- })
- )
- )
- const { content } = await llm.call([
- new HumanMessage(`${scheme.getFormatInstructions()}
- ------
- 你是一个计算机专业擅长写毕业论文的专家,你的任务是帮我的毕业设计撰写一篇论文
- 我的毕业设计项目是${title}
- 这个项目包含以下功能:
- ${desc}
- 现在我们分析一下论文中需要用到的流程图,我们需要绘制以下五个主要流程图:`)
- ])
- return await scheme.parse(content)
- }
- async function plotFlow(title, desc, name) {
- const { chain } = conversation(`你是一个计算机专业擅长写毕业论文的专家,你的任务是帮我的毕业设计撰写一篇论文
- 我的毕业设计项目是${title}
- 这个项目包含以下功能:
- ${desc}`)
- const { response: descr } = await chain.call({
- input: `现在我们需要在论文中附上${name},请你先用文字描述一下这个流程`
- })
- let { response: code } = await chain.call({
- input: `现在根据文字描述将它转换为plantuml语言
- 这是一个plantuml流程图的示例:
- \`\`\`
- @startuml 名称
- start
- if (Graphviz installed?) then (yes)
- :process all\ndiagrams;
- else (no)
- :process only
- __sequence__ and __activity__ diagrams;
- endif
- stop
- @enduml
- \`\`\``
- })
- code = /\`\`\`(?:plantuml)?([\s\S]+)\`\`\`/.exec(code)[1]
- const url = await uploadUml(code)
- return {
- name,
- desc: descr,
- url
- }
- }
- async function createFlow(title, desc) {
- const list = await pRetry(() => genPlotNames(title, desc), { retries: 5 })
- return await Promise.all(list.map((item) => pRetry(() => plotFlow(title, desc, item.title), { retries: 5 })))
- }
- export { createFlow }
|