SerpAPI 加载器
¥SerpAPI Loader
本指南介绍如何使用 SerpAPI 和 LangChain 加载网页搜索结果。
¥This guide shows how to use SerpAPI with LangChain to load web search results.
概述
¥Overview
SerpAPI 是一个实时 API,允许开发者访问各种搜索引擎的搜索结果。它通常用于竞争对手分析和排名跟踪等任务。它使企业能够从所有搜索引擎的结果页面中抓取、提取和理解数据。
¥SerpAPI is a real-time API that provides access to search results from various search engines. It is commonly used for tasks like competitor analysis and rank tracking. It empowers businesses to scrape, extract, and make sense of data from all search engines' result pages.
本指南介绍如何使用 LangChain 中的 SerpAPILoader 加载网页搜索结果。SerpAPILoader 简化了从 SerpAPI 加载和处理网页搜索结果的过程。
¥This guide shows how to load web search results using the SerpAPILoader in LangChain. The SerpAPILoader simplifies the process of loading and processing web search results from SerpAPI.
设置
¥Setup
你需要注册并获取你的 SerpAPI API 密钥。
¥You'll need to sign up and retrieve your SerpAPI API key.
用法
¥Usage
以下是如何使用 SerpAPILoader 的示例:
¥Here's an example of how to use the SerpAPILoader:
- npm
- Yarn
- pnpm
npm install @langchain/community @langchain/core @langchain/openai
yarn add @langchain/community @langchain/core @langchain/openai
pnpm add @langchain/community @langchain/core @langchain/openai
import { ChatOpenAI, OpenAIEmbeddings } from "@langchain/openai";
import { MemoryVectorStore } from "langchain/vectorstores/memory";
import { SerpAPILoader } from "@langchain/community/document_loaders/web/serpapi";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { createStuffDocumentsChain } from "langchain/chains/combine_documents";
import { createRetrievalChain } from "langchain/chains/retrieval";
// Initialize the necessary components
const llm = new ChatOpenAI();
const embeddings = new OpenAIEmbeddings();
const apiKey = "Your SerpAPI API key";
// Define your question and query
const question = "Your question here";
const query = "Your query here";
// Use SerpAPILoader to load web search results
const loader = new SerpAPILoader({ q: query, apiKey });
const docs = await loader.load();
// Use MemoryVectorStore to store the loaded documents in memory
const vectorStore = await MemoryVectorStore.fromDocuments(docs, embeddings);
const questionAnsweringPrompt = ChatPromptTemplate.fromMessages([
[
"system",
"Answer the user's questions based on the below context:\n\n{context}",
],
["human", "{input}"],
]);
const combineDocsChain = await createStuffDocumentsChain({
llm,
prompt: questionAnsweringPrompt,
});
const chain = await createRetrievalChain({
retriever: vectorStore.asRetriever(),
combineDocsChain,
});
const res = await chain.invoke({
input: question,
});
console.log(res.answer);
API Reference:
- ChatOpenAI from
@langchain/openai - OpenAIEmbeddings from
@langchain/openai - MemoryVectorStore from
langchain/vectorstores/memory - SerpAPILoader from
@langchain/community/document_loaders/web/serpapi - ChatPromptTemplate from
@langchain/core/prompts - createStuffDocumentsChain from
langchain/chains/combine_documents - createRetrievalChain from
langchain/chains/retrieval
在本例中,SerpAPILoader 用于加载网页搜索结果,然后使用 MemoryVectorStore 将搜索结果存储在内存中。然后,使用检索链从内存中检索最相关的文档,并根据这些文档回答问题。这演示了 SerpAPILoader 如何简化网页搜索结果的加载和处理流程。
¥In this example, the SerpAPILoader is used to load web search results, which are then stored in memory using MemoryVectorStore. A retrieval chain is then used to retrieve the most relevant documents from the memory and answer the question based on these documents. This demonstrates how the SerpAPILoader can streamline the process of loading and processing web search results.