Skip to main content

SearchApi

本页面介绍如何在 LangChain 中使用 SearchApi Google Search API。SearchApi 是一个实时的 SERP API,便于进行 SERP 抓取。

设置

包装器

工具

有一个名为 SearchApiAPIWrapper 的工具,它封装了这个 API。要导入这个工具:

from langchain_community.utilities import SearchApiAPIWrapper

您可以将其作为 Self Ask 链的一部分使用:

from langchain_community.utilities import SearchApiAPIWrapper
from langchain_openai import OpenAI
from langchain.agents import initialize_agent, Tool
from langchain.agents import AgentType

import os

os.environ["SEARCHAPI_API_KEY"] = ""
os.environ['OPENAI_API_KEY'] = ""

llm = OpenAI(temperature=0)
search = SearchApiAPIWrapper()
tools = [
Tool(
name="中间答案",
func=search.run,
description="在需要通过搜索提问时非常有用"
)
]

self_ask_with_search = initialize_agent(tools, llm, agent=AgentType.SELF_ASK_WITH_SEARCH, verbose=True)
self_ask_with_search.run("谁活得更久:柏拉图、苏格拉底还是亚里士多德?")

输出

> Entering new AgentExecutor chain...
Yes.
Follow up: How old was Plato when he died?
Intermediate answer: eighty
Follow up: How old was Socrates when he died?
Intermediate answer: | Socrates |
| -------- |
| Born | c. 470 BC Deme Alopece, Athens |
| Died | 399 BC (aged approximately 71) Athens |
| Cause of death | Execution by forced suicide by poisoning |
| Spouse(s) | Xanthippe, Myrto |

Follow up: How old was Aristotle when he died?
Intermediate answer: 62 years
So the final answer is: Plato

> Finished chain.
'Plato'

工具

您也可以轻松地将这个包装器加载为工具(以便与代理一起使用)。 您可以这样做:

from langchain.agents import load_tools
tools = load_tools(["searchapi"])

有关工具的更多信息,请参见此页面


此页面是否有帮助?


您还可以留下详细的反馈 在 GitHub 上