SearchApi
This notebook shows examples of how to use SearchApi to search the web. Go to https://www.searchapi.io/ to sign up for a free account and get API key.
import os
os.environ["SEARCHAPI_API_KEY"] = ""
from langchain_community.utilities import SearchApiAPIWrapper
API Reference:SearchApiAPIWrapper
search = SearchApiAPIWrapper()
search.run("Obama's first name?")
'Barack Hussein Obama II'
Using as part of a Self Ask With Search Chain​
os.environ["OPENAI_API_KEY"] = ""
from langchain.agents import AgentType, initialize_agent
from langchain_community.utilities import SearchApiAPIWrapper
from langchain_core.tools import Tool
from langchain_openai import OpenAI
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?")
API Reference:SearchApiAPIWrapper
[1m> Entering new AgentExecutor chain...[0m
[32;1m[1;3m Yes.
Follow up: How old was Plato when he died?[0m
Intermediate answer: [36;1m[1;3meighty[0m
[32;1m[1;3mFollow up: How old was Socrates when he died?[0m
Intermediate answer: [36;1m[1;3m| 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 |
[0m
[32;1m[1;3mFollow up: How old was Aristotle when he died?[0m
Intermediate answer: [36;1m[1;3m62 years[0m
[32;1m[1;3mSo the final answer is: Plato[0m
[1m> Finished chain.[0m
'Plato'
Custom parameters​
SearchApi wrapper can be customized to use different engines like Google News, Google Jobs, Google Scholar, or others which can be found in SearchApi documentation. All parameters supported by SearchApi can be passed when executing the query.
search = SearchApiAPIWrapper(engine="google_jobs")
search.run("AI Engineer", location="Portugal", gl="pt")[0:500]
'Azure AI Engineer Be an XpanderCandidatar-meCandidatar-meCandidatar-me\n\nShare:\n\nAzure AI Engineer\n\nA área Digital Xperience da Xpand IT é uma equipa tecnológica de rápido crescimento que se concentra em tecnologias Microsoft e Mobile. A sua principal missão é fornecer soluções de software de alta qualidade que atendam às necessidades do utilizador final, num mundo tecnológico continuamente exigente e em ritmo acelerado, proporcionando a melhor experiência em termos de personalização, performance'
Getting results with metadata​
import pprint
search = SearchApiAPIWrapper(engine="google_scholar")
results = search.results("Large Language Models")
pprint.pp(results)
{'search_metadata': {'id': 'search_qVdXG2jzvrlqTzayeYoaOb8A',
'status': 'Success',
'created_at': '2023-09-25T15:22:30Z',
'request_time_taken': 3.21,
'parsing_time_taken': 0.03,
'total_time_taken': 3.24,
'request_url': 'https://scholar.google.com/scholar?q=Large+Language+Models&hl=en',
'html_url': 'https://www.searchapi.io/api/v1/searches/search_qVdXG2jzvrlqTzayeYoaOb8A.html',
'json_url': 'https://www.searchapi.io/api/v1/searches/search_qVdXG2jzvrlqTzayeYoaOb8A'},
'search_parameters': {'engine': 'google_scholar',
'q': 'Large Language Models',
'hl': 'en'},
'search_information': {'query_displayed': 'Large Language Models',
'total_results': 6420000,
'page': 1,
'time_taken_displayed': 0.06},
'organic_results': [{'position': 1,
'title': 'ChatGPT for good? On opportunities and '
'challenges of large language models for '
'education',
'data_cid': 'uthwmf2nU3EJ',
'link': 'https://www.sciencedirect.com/science/article/pii/S1041608023000195',
'publication': 'E Kasneci, K SeĂźler, S KĂĽchemann, M '
'Bannert… - Learning and individual …, '
'2023 - Elsevier',
'snippet': '