资讯动态

GPT Researcher 如何接入已有 LangChain 向量库(FAISS、PGVector)执行知识库研究

发布时间:2026/9/12 17:42:41 来源:尧图企业网站定制
GPT Researcher 如何接入已有 LangChain 向量库FAISS、PGVector执行知识库研究【免费下载链接】gpt-researcherAn autonomous agent that conducts deep research on any data using any LLM providers项目地址: https://gitcode.com/GitHub_Trending/gp/gpt-researcher你手里已经有一个填充好知识的向量库FAISS 或 PostgreSQL 上的 PGVector想让 GPT Researcher 只基于这些已有知识生成研究报告而不是再去爬网页。GPT Researcher 支持把任意已填充的 LangChain 向量库直接传入GPTResearcher构造函数配合report_sourcelangchain_vectorstore完成这条路径研究上下文完全来自你的知识库报告由 LLM 基于检索到的向量生成。本文基于 Vector Stores 官方文档 与 Data Ingestion 文档分别给出 FAISS 与 PGVector 两条可执行路径。前置条件通过pip install gpt-researcher安装 GPT Researcher见 README如果是从源码运行先按 Getting Started 安装 Python 3.11 或更高版本再执行pip install -r requirements.txt。准备一个已存在的向量库其中已存入相关文档且 embeddings 已生成。文档对向量库的唯一硬性要求是实现了asimilarity_search方法。示例代码使用OpenAIEmbeddings()生成嵌入、默认 LLM 为 OpenAI需要设置OPENAI_API_KEYexport OPENAI_API_KEY{Your OpenAI API Key here}GPTResearcher构造函数中与本场景直接相关的参数是query、report_type、report_source、vector_store以及可选的vector_store_filter用于向量库查询的过滤条件签名见 gpt_researcher/agent.py。其中report_sourcelangchain_vectorstore对应的枚举值为ReportSource.LangChainVectorStore定义在 gpt_researcher/utils/enum.py。关键约束官方文档明确提示如果你想使用向量库中已有的知识必须设置report_sourcelangchain_vectorstore。其他设置会引入抓取数据作为额外信息可能污染你的向量库。知识库文档的元数据要求无论用 FAISS 还是 PGVector存入向量库的都是 LangChainDocument对象。Data Ingestion 文档指出创建 LangChain Documents 时应在 metadata 中包含source和title字段GPT Researcher 依赖这两个字段无缝利用你的文档。如果你的知识库是在 GPT Researcher 之外构建的入库前建议补齐这两个字段。路径一FAISS完整示例来自 Vector Stores 文档。下面保留文档的示例结构essay是文档中用于演示的一篇文章正文你应将其替换为自己的知识库文本分块参数chunk_size200, chunk_overlap30为文档示例值。from gpt_researcher import GPTResearcher from langchain_text_splitters import CharacterTextSplitter from langchain_openai import OpenAIEmbeddings from langchain_community.vectorstores import FAISS from langchain_core.documents import Document # 文档示例使用一篇 Paul Graham 文章做演示替换为你自己的知识库文本 document [Document(page_contentessay)] text_splitter CharacterTextSplitter(chunk_size200, chunk_overlap30, separator\n) docs text_splitter.split_documents(documentsdocument) vector_store FAISS.from_documents(docs, OpenAIEmbeddings()) query Summarize the essay into 3 or 4 succinct sections. Make sure to include key points regarding wealth creation. Include some recommendations for entrepreneurs in the conclusion. # Create an instance of GPTResearcher researcher GPTResearcher( queryquery, report_typeresearch_report, report_sourcelangchain_vectorstore, vector_storevector_store, ) # Conduct research and write the report await researcher.conduct_research() report await researcher.write_report()如果你已经有一个构建好的 FAISS 向量库实例只需保证它实现了asimilarity_search把它传给vector_store参数即可研究流程不变。路径二PGVector已有索引PGVector 示例对应向量库已存在、相关文档已入库、embeddings 已生成的场景使用PGVector.from_existing_index挂载已有索引from gpt_researcher import GPTResearcher from langchain_postgres.vectorstores import PGVector from langchain_openai import OpenAIEmbeddings # 替换为你自己的 PostgreSQL 连接串文档中的占位示例值 CONNECTION_STRING postgresql://someuser:somepasslocalhost:5432/somedatabase # assuming the vector store exists and contains the relevant documents # also assuming embeddings have been or will be generated vector_store PGVector.from_existing_index( use_jsonbTrue, embeddingOpenAIEmbeddings(), collection_namesome collection name, # 替换为你已有的 collection 名称 connectionCONNECTION_STRING, async_modeTrue, ) query Create a short report about apples. Include a section about which apples are considered best during each season. # Create an instance of GPTResearcher researcher GPTResearcher( queryquery, report_typeresearch_report, report_sourcelangchain_vectorstore, vector_storevector_store, ) # Conduct research and write the report await researcher.conduct_research() report await researcher.write_report()注意两处需要你替换的占位值CONNECTION_STRING换成你自己的 PostgreSQL 连接串collection_name换成已存在的 collection 名称。async_modeTrue是因为后续研究调用是异步的await。如果你的库是同步方式构建的Data Ingestion 文档给出了异步挂载的另一种写法把连接串postgresql://前缀替换为postgresqlpsycopg://用create_async_engine创建异步引擎后传入PGVector其余参数collection_name、use_jsonbTrue一致见>from gpt_researcher import GPTResearcher from langchain_community.vectorstores import InMemoryVectorStore from langchain_openai import OpenAIEmbeddings vector_store InMemoryVectorStore(embeddingOpenAIEmbeddings()) query The best LLM researcher GPTResearcher( queryquery, report_typeresearch_report, report_sourceweb, vector_storevector_store, ) # Conduct research, the context will be chunked and stored in the vector_store await researcher.conduct_research() # Query the 5 most relevant context in our vector store related_contexts await vector_store.asimilarity_search(GPT-4, k 5) print(related_contexts) print(len(related_contexts)) # Should be 5文档给出的验证方式就是最后两行对向量库做asimilarity_search示例预期返回 5 条k5时的示例结果。这条分支与上面的只读知识库场景互斥前者只消费已有知识后者会向向量库写入抓取数据。不要混用report_source设置否则按文档警告抓取数据会混入你的知识库。限制与注意事项向量库必须实现asimilarity_search方法这是文档声明的兼容性标准完整支持的 LangChain 向量库列表以 LangChain 官方文档为准。report_source必须设为langchain_vectorstore才能纯使用已有知识其他设置会混入抓取数据并可能污染向量库。入库文档缺少source、title元数据时GPT Researcher 对文档的引用信息会不完整。大规模入库时若嵌入模型或向量库底层数据库遇到速率限制官方建议改为独立的自定义入库流程把内容转成 LangChain Documents、按批插入向量库再走本文的接入方式参见 contenteditable="false">【免费下载链接】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),仅供参考

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价