|
|
@@ -6,6 +6,7 @@ import { Logger } from '@nestjs/common'
|
|
|
import { HumanMessage, SystemMessage } from 'langchain/schema'
|
|
|
import * as NumberToChinese from '@siakhooi/number-to-chinese-words'
|
|
|
import { createAbstract } from './abstract'
|
|
|
+import * as fastq from 'fastq'
|
|
|
|
|
|
function chapters2md(chapters, sub = []) {
|
|
|
let md = ''
|
|
|
@@ -86,60 +87,34 @@ ${chapters2md(chapters)}
|
|
|
// const { content } = await llm.call([new SystemMessage(sysPrompt), new HumanMessage(prompt)])
|
|
|
// paper.write('\n\n' + content)
|
|
|
// }
|
|
|
- const PQueue = (await eval("import('p-queue')")).default
|
|
|
- const queue = new PQueue({concurrency: 4});
|
|
|
- let content: any = []
|
|
|
- chapter.children.forEach(async (section, j) => {
|
|
|
- queue.add(async () => {
|
|
|
- let prompt
|
|
|
- if (section.children?.length) {
|
|
|
- prompt = `你可以适当加入一些口语,让文章更接近真人的书写的感觉
|
|
|
+ let content: any = Array(chapter.children.length)
|
|
|
+
|
|
|
+ const q = fastq.promise(async ({ section, j }) => {
|
|
|
+ let prompt
|
|
|
+ if (section.children?.length) {
|
|
|
+ prompt = `你可以适当加入一些口语,让文章更接近真人的书写的感觉
|
|
|
请务必不能让人觉得你是一个AI,否则我可能会丢掉我的学位
|
|
|
|
|
|
${chapters2md(section.children, [i + 1, j + 1])}
|
|
|
请按照以上结构编写第${i + 1}章${chapter.title}的${i + 1}.${j + 1}小节(请注意只需要编写这一小节的内容)
|
|
|
## ${i + 1}.${j + 1} ${section.title} :`
|
|
|
- } else {
|
|
|
- prompt = `请务必不能让人觉得你是一个AI,否则我可能会丢掉我的学位\n\n请编写第${i + 1}章${
|
|
|
- chapter.title
|
|
|
- }的${i + 1}.${j + 1}小节(请注意只需要编写这一小节的内容)
|
|
|
+ } else {
|
|
|
+ prompt = `请务必不能让人觉得你是一个AI,否则我可能会丢掉我的学位\n\n请编写第${i + 1}章${
|
|
|
+ chapter.title
|
|
|
+ }的${i + 1}.${j + 1}小节(请注意只需要编写这一小节的内容)
|
|
|
## ${i + 1}.${j + 1} ${section.title}:`
|
|
|
- }
|
|
|
- let { content: res } = await llm.call([new SystemMessage(sysPrompt), new HumanMessage(prompt)])
|
|
|
- res = res
|
|
|
- .replace(new RegExp(`#.*${chapter.title}.*\n`), '')
|
|
|
- .replace(new RegExp(`#.*${section.title}.*\n`), '')
|
|
|
- content[j] = `## ${i + 1}.${j + 1} ${section.title}\n\n` + res
|
|
|
- })
|
|
|
+ }
|
|
|
+ let { content: res } = await llm.call([new SystemMessage(sysPrompt), new HumanMessage(prompt)])
|
|
|
+ res = res
|
|
|
+ .replace(new RegExp(`#.*${chapter.title}.*\n`), '')
|
|
|
+ .replace(new RegExp(`#.*${section.title}.*\n`), '')
|
|
|
+ content[j] = `## ${i + 1}.${j + 1} ${section.title}\n\n` + res
|
|
|
+ }, 4)
|
|
|
+ chapter.children.forEach((section, j) => {
|
|
|
+ q.push({ section, j })
|
|
|
})
|
|
|
- await queue.onIdle()
|
|
|
+ await q.drained()
|
|
|
content = content.join('\n\n')
|
|
|
-
|
|
|
- // const content = (
|
|
|
- // await Promise.all(
|
|
|
- // chapter.children.map(async (section, j) => {
|
|
|
- // let prompt
|
|
|
- // if (section.children?.length) {
|
|
|
- // prompt = `你可以适当加入一些口语,让文章更接近真人的书写的感觉
|
|
|
- // 请务必不能让人觉得你是一个AI,否则我可能会丢掉我的学位
|
|
|
-
|
|
|
- // ${chapters2md(section.children, [i + 1, j + 1])}
|
|
|
- // 请按照以上结构编写第${i + 1}章${chapter.title}的${i + 1}.${j + 1}小节(请注意只需要编写这一小节的内容)
|
|
|
- // ## ${i + 1}.${j + 1} ${section.title} :`
|
|
|
- // } else {
|
|
|
- // prompt = `请务必不能让人觉得你是一个AI,否则我可能会丢掉我的学位\n\n请编写第${i + 1}章${
|
|
|
- // chapter.title
|
|
|
- // }的${i + 1}.${j + 1}小节(请注意只需要编写这一小节的内容)
|
|
|
- // ## ${i + 1}.${j + 1} ${section.title}:`
|
|
|
- // }
|
|
|
- // let { content } = await llm.call([new SystemMessage(sysPrompt), new HumanMessage(prompt)])
|
|
|
- // content = content
|
|
|
- // .replace(new RegExp(`#.*${chapter.title}.*\n`), '')
|
|
|
- // .replace(new RegExp(`#.*${section.title}.*\n`), '')
|
|
|
- // return `## ${i + 1}.${j + 1} ${section.title}\n\n` + content
|
|
|
- // })
|
|
|
- // )
|
|
|
- // ).join('\n\n')
|
|
|
paper.write('\n\n' + content)
|
|
|
} else {
|
|
|
let prompt = `第${i + 1}章: # ${chapter.title} (请注意只需要编写这一章的内容)`
|