table.mjs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { HumanMessage } from "langchain/schema"
  2. import { StructuredOutputParser } from "langchain/output_parsers"
  3. import { llm } from "./llm.mjs"
  4. import { z } from "zod"
  5. import pRetry from "p-retry"
  6. async function listTables(title, desc) {
  7. const scheme = StructuredOutputParser.fromZodSchema(z.array(z.string().describe("表名")))
  8. const res = await llm.call([
  9. new HumanMessage(`${scheme.getFormatInstructions()}
  10. ------
  11. 你是一个计算机专业擅长写毕业论文的专家,你的任务是帮我的毕业设计撰写一篇论文
  12. 我的毕业设计项目是${title}
  13. 这个项目包含以下功能:
  14. ${desc}
  15. 现在我们需要开始为这个系统设计数据库,首先请帮我列出我们需要哪些表:`)
  16. ])
  17. return await scheme.parse(res.content)
  18. }
  19. async function describeTable(title, desc, list, name) {
  20. const res = await llm.call([
  21. new HumanMessage(`
  22. 你是一个计算机专业擅长写毕业论文的专家,你的任务是帮我的毕业设计撰写一篇论文
  23. 我的毕业设计项目是${title}
  24. 这个项目包含以下功能:
  25. ${desc}
  26. 现在我们需要开始为这个系统设计数据库,我们需要以下这些数据库表:
  27. ${list.map((i) => `- ${i}`).join("\n")}
  28. 现在我们一步一步设计这些表的具体字段,首先我们需要设计${name}表的字段,请你帮我设计一下这个表的字段,以表格的形式返回,包含字段名称、类型、长度、字段说明、主键、默认值`)
  29. ])
  30. return {
  31. name,
  32. desc: "\n" + new RegExp("\\|[\\s\\S]+\\|").exec(res.content)[0] + "\n"
  33. }
  34. }
  35. async function createTable(title, desc) {
  36. const list = await pRetry(() => listTables(title, desc), { retries: 5 })
  37. const tables = []
  38. for (const item of list) {
  39. tables.push(await pRetry(() => describeTable(title, desc, list, item), { retries: 5 }))
  40. }
  41. return tables
  42. }
  43. export { createTable }