|
|
@@ -1,51 +1,91 @@
|
|
|
-import { HumanMessage } from 'langchain/schema'
|
|
|
+import { HumanMessage, SystemMessage } from 'langchain/schema'
|
|
|
import { StructuredOutputParser } from 'langchain/output_parsers'
|
|
|
import { z } from 'zod'
|
|
|
import { uploadUml } from './upload'
|
|
|
|
|
|
-async function _genChapters(tools, title, desc) {
|
|
|
+async function _genChapters(tools, major, title, desc) {
|
|
|
const { llm, usage, conversation } = tools
|
|
|
const scheme = StructuredOutputParser.fromZodSchema(
|
|
|
z.array(
|
|
|
z.object({
|
|
|
chapterName: z.string().describe('章节名称'),
|
|
|
- chapterDesc: z.string().describe('章节描述')
|
|
|
+ chapterDesc: z.string().describe('章节描述'),
|
|
|
+ sections: z.array(
|
|
|
+ z.object({
|
|
|
+ sectionName: z.string().describe('小节名称'),
|
|
|
+ sectionDesc: z.string().describe('小节描述')
|
|
|
+ })
|
|
|
+ )
|
|
|
})
|
|
|
)
|
|
|
)
|
|
|
|
|
|
const { chain } = conversation(
|
|
|
- `你是一个擅长写毕业论文的专家,你的任务是帮我完成我的论文。
|
|
|
+ `你是一个擅长写${major}专业毕业论文的专家,你的任务是帮我完成我的论文。
|
|
|
你要根据我的要求,帮我完成我的论文`,
|
|
|
100
|
|
|
)
|
|
|
|
|
|
- await chain.call({
|
|
|
- input: `我们一步一步来完成。
|
|
|
-首先我们要拟定一个论文的大纲。
|
|
|
-我们先不着急确定,先想一想一般毕业论文的大纲是怎样的呢,比如:一般第一章是引言,最后是参考文献`
|
|
|
- })
|
|
|
+ // await chain.call({
|
|
|
+ // input: `我们一步一步来完成。
|
|
|
+ // 首先我们要拟定一个论文的大纲。
|
|
|
+ // 我们先不着急确定,先想一想一般${major}专业的毕业论文的大纲是怎样的呢`
|
|
|
+ // })
|
|
|
|
|
|
await chain.call({
|
|
|
- input: `现在我告诉你我们的论文标题是${title}
|
|
|
-那么现在根据我们的论文标题,应该做哪些修改呢?`
|
|
|
+ input: `我的论文标题是${title},
|
|
|
+请你帮我拟定一个论文的大纲。
|
|
|
+请注意:拟订的大纲不要过于笼统,结构要清晰,要与论文主题贴切`
|
|
|
})
|
|
|
|
|
|
const { response } = await chain.call({
|
|
|
- input: `好的,现在根据你的修改,我们来确定一个更加详细的论文的大纲:
|
|
|
-------
|
|
|
+ input: `------
|
|
|
${scheme.getFormatInstructions()}
|
|
|
------
|
|
|
-现在我们来确定一个更加详细的论文的大纲(你的回答关系到我的工作和职业生涯,请认真一点)`
|
|
|
+好的根据以上内容我们来确定一个更加详细的论文的大纲(你的回答关系到我的工作和职业生涯,请认真一点,注意JSON格式):`
|
|
|
})
|
|
|
|
|
|
return await scheme.parse(response)
|
|
|
}
|
|
|
|
|
|
-export async function genChapters(tools, title, desc) {
|
|
|
+async function genSections(tools, major, title, desc, chapters, i) {
|
|
|
+ const { llm, usage, conversation } = tools
|
|
|
+
|
|
|
+ const scheme = StructuredOutputParser.fromZodSchema(
|
|
|
+ z.array(
|
|
|
+ z.object({
|
|
|
+ sectionName: z.string().describe('小节名称'),
|
|
|
+ sectionDesc: z.string().describe('小节描述')
|
|
|
+ })
|
|
|
+ )
|
|
|
+ )
|
|
|
+
|
|
|
+ const { content } = await llm.call([
|
|
|
+ new SystemMessage(`你是一个擅长写${major}专业毕业论文的专家,你的任务是帮我完成我的论文。
|
|
|
+ 你要根据我的要求,帮我完成我的论文`),
|
|
|
+ new HumanMessage(`${scheme.getFormatInstructions()}
|
|
|
+------
|
|
|
+我的论文标题是${title}
|
|
|
+目前我们已经确定了论文的粗略大纲:
|
|
|
+${chapters.map((i) => `- ${i.chapterName}\n ${i.chapterDesc}`).join('\n')}
|
|
|
+
|
|
|
+现在我们来完善每章的小节
|
|
|
+首先是第${i + 1}章${chapters[i].chapterName}:`)
|
|
|
+ ])
|
|
|
+ chapters[i].sections = await scheme.parse(content)
|
|
|
+}
|
|
|
+
|
|
|
+export async function genChapters(tools, major, title, desc) {
|
|
|
const pRetry = (await eval("import('p-retry')")).default
|
|
|
- return await pRetry(() => _genChapters(tools, title, desc), {
|
|
|
+ const chapters = await pRetry(() => _genChapters(major, tools, title, desc), {
|
|
|
retries: 5,
|
|
|
onFailedAttempt: (e) => console.log(e.stack)
|
|
|
})
|
|
|
+
|
|
|
+ // await Promise.all(
|
|
|
+ // chapters.map((i, index) => {
|
|
|
+ // return pRetry(() => genSections(major, tools, title, desc, chapters, index), { retries: 5 })
|
|
|
+ // })
|
|
|
+ // )
|
|
|
+ return chapters
|
|
|
}
|