> ## 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.

# SearchAPILoader 集成

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

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

## 概述

[SearchApi](https://www.searchapi.io/) 是一个实时 API，为开发者提供来自多种搜索引擎的搜索结果访问权限，包括 [Google 搜索](https://www.searchapi.io/docs/google)、[Google 新闻](https://www.searchapi.io/docs/google-news)、[Google 学术](https://www.searchapi.io/docs/google-scholar)、[YouTube 字幕](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="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\searchapi.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>
