Skip to main content

SearchApi

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

设置

包装器

工具

有一个SearchApiAPIWrapper工具可以包装这个API。要导入这个工具:

from langchain_community.utilities import SearchApiAPIWrapper

您可以将其作为自我询问链的一部分使用:

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="Intermediate Answer",
func=search.run,
description="useful for when you need to ask with search"
)
]

self_ask_with_search = initialize_agent(tools, llm, agent=AgentType.SELF_ASK_WITH_SEARCH, verbose=True)
self_ask_with_search.run("Who lived longer: Plato, Socrates, or Aristotle?")

输出

> 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"])

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


Was this page helpful?


You can also leave detailed feedback on GitHub.

扫我,入群扫我,找书