SearchApi 加载器
¥SearchApi Loader
本指南介绍如何使用 SearchApi 和 LangChain 加载网页搜索结果。
¥This guide shows how to use SearchApi with LangChain to load web search results.
概述
¥Overview
SearchApi 是一个实时 API,允许开发者访问各种搜索引擎的结果,包括 Google 搜索、Google 新闻、Google 学术、YouTube 成绩单 或文档中可以找到的任何其他引擎。此 API 使开发者和企业能够直接从所有这些搜索引擎的结果页面中抓取和提取有意义的数据,为不同的用例提供有价值的见解。
¥SearchApi is a real-time API that grants developers access to results from a variety of search engines, including engines like Google Search, Google News, Google Scholar, YouTube Transcripts or any other engine that could be found in documentation. This API enables developers and businesses to scrape and extract meaningful data directly from the result pages of all these search engines, providing valuable insights for different use-cases.
本指南介绍如何使用 LangChain 中的 SearchApiLoader 加载网页搜索结果。SearchApiLoader 简化了从 SearchApi 加载和处理网页搜索结果的过程。
¥This guide shows how to load web search results using the SearchApiLoader in LangChain. The SearchApiLoader simplifies the process of loading and processing web search results from SearchApi.
设置
¥Setup
你需要注册并获取你的 SearchApi API 密钥。
¥You'll need to sign up and retrieve your SearchApi API key.
用法
¥Usage
以下是如何使用 SearchApiLoader 的示例:
¥Here's an example of how to use the SearchApiLoader:
- 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 { TokenTextSplitter } from "@langchain/textsplitters";
import { SearchApiLoader } from "@langchain/community/document_loaders/web/searchapi";
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({
model: "gpt-3.5-turbo-1106",
});
const embeddings = new OpenAIEmbeddings();
const apiKey = "Your SearchApi API key";
// Define your question and query
const question = "Your question here";
const query = "Your query here";
// Use SearchApiLoader to load web search results
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);
// Use MemoryVectorStore to store the loaded documents in memory
const vectorStore = await MemoryVectorStore.fromDocuments(
splitDocs,
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 - TokenTextSplitter from
@langchain/textsplitters - SearchApiLoader from
@langchain/community/document_loaders/web/searchapi - ChatPromptTemplate from
@langchain/core/prompts - createStuffDocumentsChain from
langchain/chains/combine_documents - createRetrievalChain from
langchain/chains/retrieval
在本例中,SearchApiLoader 用于加载网页搜索结果,然后使用 MemoryVectorStore 将搜索结果存储在内存中。然后,使用检索链从内存中检索最相关的文档,并根据这些文档回答问题。这演示了 SearchApiLoader 如何简化网页搜索结果的加载和处理流程。
¥In this example, the SearchApiLoader 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 SearchApiLoader can streamline the process of loading and processing web search results.