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_source="langchain_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_KEY:
export OPENAI_API_KEY={Your OpenAI API Key here}GPTResearcher构造函数中与本场景直接相关的参数是query、report_type、report_source、vector_store(以及可选的vector_store_filter,用于向量库查询的过滤条件),签名见 gpt_researcher/agent.py。其中report_source="langchain_vectorstore"对应的枚举值为ReportSource.LangChainVectorStore,定义在 gpt_researcher/utils/enum.py。
关键约束:官方文档明确提示,如果你想使用向量库中已有的知识,必须设置report_source="langchain_vectorstore"。其他设置会引入抓取数据作为额外信息,可能污染你的向量库。
知识库文档的元数据要求
无论用 FAISS 还是 PGVector,存入向量库的都是 LangChainDocument对象。Data Ingestion 文档指出:创建 LangChain Documents 时,应在 metadata 中包含source和title字段,GPT Researcher 依赖这两个字段无缝利用你的文档。如果你的知识库是在 GPT Researcher 之外构建的,入库前建议补齐这两个字段。
路径一:FAISS
完整示例来自 Vector Stores 文档。下面保留文档的示例结构:essay是文档中用于演示的一篇文章正文,你应将其替换为自己的知识库文本;分块参数chunk_size=200, chunk_overlap=30为文档示例值。
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_content=essay)] text_splitter = CharacterTextSplitter(chunk_size=200, chunk_overlap=30, separator="\n") docs = text_splitter.split_documents(documents=document) 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( query=query, report_type="research_report", report_source="langchain_vectorstore", vector_store=vector_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:somepass@localhost: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_jsonb=True, embedding=OpenAIEmbeddings(), collection_name='some collection name', # 替换为你已有的 collection 名称 connection=CONNECTION_STRING, async_mode=True, ) 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( query=query, report_type="research_report", report_source="langchain_vectorstore", vector_store=vector_store, ) # Conduct research and write the report await researcher.conduct_research() report = await researcher.write_report()注意两处需要你替换的占位值:CONNECTION_STRING换成你自己的 PostgreSQL 连接串,collection_name换成已存在的 collection 名称。async_mode=True是因为后续研究调用是异步的(await)。如果你的库是同步方式构建的,Data Ingestion 文档给出了异步挂载的另一种写法:把连接串postgresql://前缀替换为postgresql+psycopg://,用create_async_engine创建异步引擎后传入PGVector,其余参数(collection_name、use_jsonb=True)一致,见>from gpt_researcher import GPTResearcher from langchain_community.vectorstores import InMemoryVectorStore from langchain_openai import OpenAIEmbeddings vector_store = InMemoryVectorStore(embedding=OpenAIEmbeddings()) query = "The best LLM" researcher = GPTResearcher( query=query, report_type="research_report", report_source="web", vector_store=vector_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 条(k=5时的示例结果)。这条分支与上面的"只读知识库"场景互斥:前者只消费已有知识,后者会向向量库写入抓取数据。不要混用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),仅供参考