import { HumanMessage, SystemMessage } from 'langchain/schema'
import { StructuredOutputParser } from 'langchain/output_parsers'
import { z } from 'zod'
import { uploadUml } from './upload'
import { LLMTools } from './types'

export async function _createAbstract(tools: LLMTools, title, desc) {
    const { llm, usage, conversation } = tools
    let { content } = await llm.call([
        new HumanMessage(`我正在给我的毕业设计撰写一篇论文
我的毕业设计项目是${title}
这个项目包含以下功能：
${desc}

请你帮我的论文写一篇摘，然后抽取几个关键词
开始：#摘要`)
    ])

    const abstract = content.replace(/^摘要[：:][\s\n]+/, '')
    const { content: translated } = await llm.call([
        new SystemMessage('You are a translator, translate directly without explanation'),
        new HumanMessage(`Translate the following text from Chinese to English without the style of machine translation.
------
${abstract}`)
    ])

    return `# 摘要\n\n${abstract}\n\n# Abstract\n\n${translated}`
}

export async function createAbstract(tools: LLMTools, title, desc) {
    const pRetry = (await eval("import('p-retry')")).default
    return await pRetry(() => _createAbstract(tools, title, desc), { retries: 5 })
}
