
GPT Researcher 如何用 write_report 的 custom_prompt 参数控制报告格式与结构【免费下载链接】gpt-researcherAn autonomous agent that conducts deep research on any data using any LLM providers项目地址: https://gitcode.com/GitHub_Trending/gp/gpt-researcher如果你用 GPT Researcher 跑完一次研究后发现默认生成的报告格式不符合需求——想要一份 2 段以内的摘要、按要点列表、QA 问答或者面向技术读者的版本——write_report方法的custom_prompt参数就是官方文档给出的做法在同一次研究的基础上不改研究过程只换生成报告的提示词就能得到不同格式与结构的多份报告。本文基于仓库中的 PIP Package 文档docs/docs/gpt-researcher/gptr/pip-package.md和示例脚本docs/docs/examples/custom_prompt.py、docs/docs/examples/examples.md演示这条完整路径。适用前提通过 pip 包在 Python 脚本中使用GPTResearcher需要 Python 3.10pip-package.md 的 Pre-requisite 写的是 3.10而docs/docs/gpt-researcher/getting-started/getting-started.md写的是 Python 3.11 or later两处文档要求不一致建议按较高的 3.11 准备环境以及一个可用的 LLM API Key 和搜索 API Key。准备环境PIP Package 文档给出的安装步骤pip install gpt-researcher设置环境变量export方式或写入项目目录下的.env文件export OPENAI_API_KEY{Your OpenAI API Key here} export TAVILY_API_KEY{Your Tavily API Key here}文档推荐 OpenAI 作为 LLM、Tavily 作为搜索 API也可以换成其他 provider这里不影响后文流程。基本调用路径先研究再用 custom_prompt 生成报告custom_prompt是write_report方法的可选参数默认值为空字符串见 agent.py 中的方法签名与文档注释 Custom prompt to guide report generation。核心顺序是固定的三步from gpt_researcher import GPTResearcher import asyncio async def main(): # Query query What are the latest advancements in renewable energy? # Report Type report_type research_report # Initialize the researcher researcher GPTResearcher(queryquery, report_typereport_type) # Conduct research on the given query await researcher.conduct_research() # Generate a standard report不传 custom_prompt走默认提示词模板 standard_report await researcher.write_report() print(Standard Report Generated) # Generate a short, concise report using custom_prompt custom_prompt Provide a concise summary in 2 paragraphs without citations. short_report await researcher.write_report(custom_promptcustom_prompt) print(Short Report Generated) return standard_report, short_report if __name__ __main__: asyncio.run(main())这段代码取自docs/docs/examples/examples.md的 Custom Report Formatting 部分只保留标准报告与摘要对比这两步。custom_prompt的作用范围只在报告生成阶段。conduct_research()负责检索并汇总上下文write_report()负责把这份上下文写成报告传不传custom_prompt研究阶段都不变。pip-package.md 对此的说明是The custom prompt will be combined with the research context to generate your customized report.从源码看具体合并方式在 report_generation.pygenerate_report中如果custom_prompt非空用户内容就变成f{custom_prompt}\n\nContext: {context}直接替代按report_type选择的默认提示词模板为空时才使用默认模板。也就是说custom_prompt一旦传入报告的格式、长度、结构完全由你写的这段提示词决定默认的 report_type 模板不再生效。custom_prompt 常见的三类用法pip-package.md 把自定义提示词的用途归为三类下面按文档原文给出对应提示词保留英文原文直接可用1. 格式控制Format Control——指定报告的结构、长度或风格report await researcher.write_report( custom_promptWrite a blog post in a conversational tone using the research. Include headings and a conclusion. )2. 面向特定读者Audience Targeting——为不同读者定制内容report await researcher.write_report( custom_promptCreate a report for technical stakeholders, focusing on methodologies and implementation details. )3. 特定输出形态Specialized Outputs——生成特定类型的内容report await researcher.write_report( custom_promptCreate a FAQ section based on the research with at least 5 questions and detailed answers. )文档Handling Research Results一节还给了几个结构性的例子可以控制报告内部章节# 短摘要 custom_report await researcher.write_report(custom_promptAnswer in short, 2 paragraphs max without citations.) # 聚焦业务影响的执行摘要限制字数 executive_summary await researcher.write_report(custom_promptCreate an executive summary focused on business impact and ROI. Keep it under 500 words.) # 指定章节结构 technical_report await researcher.write_report(custom_promptCreate a technical report with problem statement, methodology, findings, and recommendations sections.)一次研究产出多种格式并核对成本docs/docs/examples/custom_prompt.py是仓库自带的完整示例先跑一次conduct_research()然后依次生成标准报告、短摘要、要点列表、QA、技术读者版共 5 份报告最后用get_costs()打印总 token 消耗。完整脚本见 custom_prompt.py。其结构可以精简为from gpt_researcher import GPTResearcher import asyncio async def custom_report_example(): query What are the latest advancements in renewable energy? report_type research_report researcher GPTResearcher( queryquery, report_typereport_type, verboseTrue # Set to True to see detailed logs ) # Conduct the research (this step is the same regardless of custom prompts) await researcher.conduct_research() # Standard report (no custom prompt) standard_report await researcher.write_report() print(fStandard Report Length: {len(standard_report.split())} words\n) print(standard_report[:500] ...\n) # Print first 500 chars # Short summary with custom prompt short_prompt Provide a brief summary of the research findings in 2-3 paragraphs without citations. short_report await researcher.write_report(custom_promptshort_prompt) print(fShort Report Length: {len(short_report.split())} words\n) print(short_report \n) # Bullet point format bullet_prompt List the top 5 advancements in renewable energy as bullet points with a brief explanation for each. bullet_report await researcher.write_report(custom_promptbullet_prompt) print(bullet_report \n) # Show research costs print(fTotal tokens used: {researcher.get_costs()}) if __name__ __main__: asyncio.run(custom_report_example())脚本开头还用了nest_asyncio.apply()来支持 notebook / 交互式环境中的嵌套事件循环在普通.py脚本里直接asyncio.run即可无需引入。注意示例中custom_prompt.py的完整版本会对同一个researcher连续调用多次write_report每次调用都是一次独立的 LLM 生成请求token 消耗会累加最后统一由researcher.get_costs()读出。结果验证文档给出的验证方式是直接检查返回值write_report返回报告字符串agent.py 中 docstring 明确 The generated report as a string示例脚本用print(report[:500])查看开头 500 字符、用len(report.split())统计词数以此确认格式是否生效例如2 paragraphs的摘要是否确实更短。researcher.get_costs()返回研究过程中的 token 消耗用于核对多次生成的总成本。其余辅助 getter 与本报告无关按需使用researcher.get_source_urls()可核对报告基于的来源 URL。边界与限制custom_prompt只影响报告生成不改变检索结果。要改变研究范围子主题、报告类型应在初始化时指定report_type文档中给出的取值如research_report、resource_report、outline_report或调整研究配置如max_subtopics、tone、report_format。传入custom_prompt后按report_type选择的默认提示词模板被完全替换报告中不再保留默认模板规定的结构需要哪些章节、语言、长度都要写进提示词本身。每次write_report调用都会产生新的 LLM 请求和费用同一研究要产出 N 种格式就调用 N 次。更完整的提示词示例见 custom_prompt.pypip 包集成与更多 getter 说明见 pip-package.md入门安装与环境变量配置见 getting-started.md。【免费下载链接】gpt-researcherAn autonomous agent that conducts deep research on any data using any LLM providers项目地址: https://gitcode.com/GitHub_Trending/gp/gpt-researcher创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考