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

# SearchAPILoader 集成

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

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

## 概述

[SearchApi](https://www.searchapi.io/) 是一个实时 API，允许开发者访问来自多种搜索引擎的结果，包括 [Google Search](https://www.searchapi.io/docs/google)、[Google News](https://www.searchapi.io/docs/google-news)、[Google Scholar](https://www.searchapi.io/docs/google-scholar)、[YouTube Transcripts](https://www.searchapi.io/docs/youtube-transcripts) 或文档中可找到的任何其他引擎。
该 API 使开发者和企业能够直接从所有这些搜索引擎的结果页面中抓取和提取有意义的数据，为不同的用例提供有价值的见解。

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

## 设置

您需要注册并获取您的 [SearchApi API 密钥](https://www.searchapi.io/)。

## 用法

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

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { ChatOpenAI, OpenAIEmbeddings } from "@langchain/openai";
import { MemoryVectorStore } from "@langchain/classic/vectorstores/memory";
import { TokenTextSplitter } from "@langchain/textsplitters";
import { SearchApiLoader } from "@langchain/community/document_loaders/web/searchapi";
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-3.5-turbo-1106",
});
const embeddings = new OpenAIEmbeddings();
const apiKey = "您的 SearchApi API 密钥";

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

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

const textSplitter = new TokenTextSplitter({
  chunkSize: 800,
  chunkOverlap: 100,
});

const splitDocs = await textSplitter.splitDocuments(docs);

// 使用 MemoryVectorStore 将加载的文档存储在内存中
const vectorStore = await MemoryVectorStore.fromDocuments(
  splitDocs,
  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);
```

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

***

<div className="source-links">
  <Callout icon="terminal-2">
    [连接这些文档](/use-these-docs) 到 Claude、VSCode 等，通过 MCP 获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/oss/javascript/integrations/document_loaders/web_loaders/searchapi.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
