> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-zh.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# SerpAPI 加载器集成

> 使用 LangChain JavaScript 集成 SerpAPI 加载器文档加载器。

本指南展示了如何将 SerpApi 与 LangChain 结合使用，以加载网络搜索结果。

## 概述

[SerpApi](https://serpapi.com/) 是一个实时 API，提供对各种搜索引擎搜索结果的访问。它通常用于竞争对手分析和排名跟踪等任务。它使企业能够从所有搜索引擎的结果页面中抓取、提取并理解数据。

本指南展示了如何使用 LangChain 中的 `SerpAPILoader` 加载网络搜索结果。`SerpAPILoader` 简化了从 SerpApi 加载和处理网络搜索结果的过程。

## 设置

您需要注册并获取您的 [SerpApi API 密钥](https://serpapi.com/dashboard)。

## 使用方法

以下是使用 `SerpAPILoader` 的示例：

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { ChatOpenAI, OpenAIEmbeddings } from "@langchain/openai";
import { MemoryVectorStore } from "@langchain/classic/vectorstores/memory";
import { SerpAPILoader } from "@langchain/community/document_loaders/web/serpapi";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { createStuffDocumentsChain } from "@langchain/classic/chains/combine_documents";
import { createRetrievalChain } from "@langchain/classic/chains/retrieval";

// 初始化必要的组件
const llm = new ChatOpenAI({
  model: "gpt-4.1-mini",
});
const embeddings = new OpenAIEmbeddings();
const apiKey = "您的 SerpApi API 密钥";

// 定义您的问题和查询
const question = "您的问题在此";
const query = "您的查询在此";

// 使用 SerpAPILoader 加载网络搜索结果
const loader = new SerpAPILoader({ q: query, apiKey });
const docs = await loader.load();

// 使用 MemoryVectorStore 将加载的文档存储在内存中
const vectorStore = await MemoryVectorStore.fromDocuments(docs, embeddings);

const questionAnsweringPrompt = ChatPromptTemplate.fromMessages([
  [
    "system",
    "根据以下上下文回答用户的问题：\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);
```

在此示例中，`SerpAPILoader` 用于加载网络搜索结果，然后使用 `MemoryVectorStore` 将其存储在内存中。随后，使用检索链从内存中检索最相关的文档，并基于这些文档回答问题。这展示了 `SerpAPILoader` 如何简化加载和处理网络搜索结果的过程。

***

<div className="source-links">
  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/i18n\zh-CN\oss\javascript\integrations\document_loaders\web_loaders\serpapi.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>

  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>
</div>
