ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

GPT Researcher 如何用 source_urls 与 complement_source_urls 只研究指定的一组网页

GPT Researcher 如何用 source_urls 与 complement_source_urls 只研究指定的一组网页 GPT Researcher 如何用 source_urls 与 complement_source_urls 只研究指定的一组网页【免费下载链接】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 会针对你的查询自行进行网络搜索再抓取搜索结果指向的网页。如果你的需求相反——报告只能基于你指定的那几篇网页生成不允许 Agent 自行去网上找其他来源——就需要用到GPTResearcher构造参数中的source_urls和complement_source_urls。本文说明如何在 Python 环境中配置这两个参数、运行一次限定来源的研究并验证报告确实只用了你提供的 URL。适用前提来自 pip-package 文档本机已安装 Python 3.10已通过pip install gpt-researcher安装官方 PyPI 包已设置 LLM 的 API Key 环境变量例如export OPENAI_API_KEY{Your OpenAI API Key here}。两个参数如何控制研究来源tailored-research 文档 对这两个参数的说明是source_urls传入一个 URL 列表GPT Researcher 会对这些来源进行抓取和研究而不是先搜索再抓取complement_source_urls控制是否在提供的 URL 之外补充网络搜索。设为True时Agent 会额外在网络上搜索它认为适合该查询的其他网站默认值为False此时只研究你通过source_urls提供的网页。这一点也可以从源码参数定义得到印证gpt_researcher/agent.py 中__init__签名为source_urls: list[str] | None None, document_urls: list[str] | None None, complement_source_urls: bool False,其中complement_source_urls的 docstring 解释为 Whether to complement source URLs with web search是否用网络搜索来补充 source URLs。执行逻辑在 gpt_researcher/skills/researcher.py 中只要传入了source_urls就先走_get_context_by_urls抓取这批 URL随后仅当complement_source_urls为真时才调用_get_context_by_web_search追加搜索结果。准备安装与环境变量pip install gpt-researcherexport OPENAI_API_KEY{Your OpenAI API Key here}把{Your OpenAI API Key here}替换为你自己的 Key。如果后续要启用complement_source_urlsTrue的补充搜索分支还需要配置可用的搜索检索器retriever例如按 pip-package 文档 设置export TAVILY_API_KEY{Your Tavily API Key here}。执行只研究指定 URL 的完整脚本以下脚本基于项目自带的 sample_sources_only.py 示例 整理可直接作为主路径运行。query与sources需要替换成你自己的问题和网页列表from gpt_researcher import GPTResearcher import asyncio async def get_report(query: str, report_source: str, sources: list) - str: researcher GPTResearcher(queryquery, report_sourcereport_source, source_urlssources) research_context await researcher.conduct_research() return await researcher.write_report() if __name__ __main__: query What are the biggest trends in AI lately? report_source static sources [ https://en.wikipedia.org/wiki/Artificial_intelligence, https://www.ibm.com/think/insights/artificial-intelligence-trends, https://www.forbes.com/advisor/business/ai-statistics ] report asyncio.run(get_report(queryquery, report_sourcereport_source, sourcessources)) print(report)要点说明构造函数里显式传入source_urlssources研究入口就从先搜索变为直接抓取这批 URL这里没有传complement_source_urls因此取默认值False补充搜索分支不会触发文档示例中同时传了report_sourcestatic与仓库示例保持一致若想显式写出限制语义推荐意图更清晰按 tailored-research 文档 的写法加上complement_source_urlsFalse即可researcher GPTResearcher( queryquery, report_typereport_type, source_urlssources, complement_source_urlsFalse )保存为sources_only.py后运行python sources_only.py验证确认报告只用了你提供的来源运行完成后用 pip-package 文档 中列出的 getter 方法核对实际使用的来源async def get_report(query: str, report_source: str, sources: list): researcher GPTResearcher(queryquery, report_sourcereport_source, source_urlssources) await researcher.conduct_research() report await researcher.write_report() # 返回研究实际使用的来源 URL source_urls researcher.get_source_urls() # 返回研究来源列表含标题、正文和图片 research_sources researcher.get_research_sources() # 返回研究过程中抓取到的全部内容 research_context researcher.get_research_context() return report, source_urls, research_sources, research_contextget_source_urls()按文档定义返回 the URLs that were used to gather information for the research即实际用于收集信息的 URL 列表——核对它是否都落在你传入的sources范围内get_research_sources()返回带标题、正文和图片的来源明细get_research_context()返回完整抓取上下文可用于人工抽查内容出处。另外GPTResearcher默认verboseTrue见 agent.py 构造函数默认值运行日志中会出现 researcher.py 中的分支日志可据此判断走了哪条路径Using provided source URLs出现这条日志说明程序确实以你传入的 URL 为研究入口。可选分支允许补充搜索时如何切换把complement_source_urls设为True后流程变为先抓取你提供的 URL再用网络搜索补充 Agent 认为相关的其他网站日志会多出Complementing with web search两个边界需要注意该分支依赖已配置的 web retriever如 Tavily 等需相应 API Key没有可用的检索器时补充搜索拿不到内容开启后报告来源将不再限定于source_urls与你只用指定网页的初衷相反。如果你的目标是严格限定来源保持默认False或显式传False即可。限制与注意严格限定来源的前提是complement_source_urls保持False一旦置为True来源范围就扩大到搜索到的网页source_urls为空或未传时不会进入抓取指定 URL分支而是按report_source走普通网络搜索或其他来源模式见 researcher.py 中的分支判断所以必须显式传入非空列表若研究在提供的来源里找不到与查询相关的内容日志会提示 I was unable to find relevant context in the provided sourcesverbose 模式下经 websocket 输出此时报告质量取决于你能提供的页面覆盖面——扩大sources列表是文档示例之外的自行调整手段文档未给出固定做法按你的场景取舍。相关文档入口tailored-research、sample_sources_only.py、pip-package。【免费下载链接】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),仅供参考
返回列表