mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-16 14:49:21 +03:00
Adds Google Search integration with [Serper](https://serper.dev) a low-cost alternative to SerpAPI (10x cheaper + generous free tier). Includes documentation, tests and examples. Hopefully I am not missing anything. Developers can sign up for a free account at [serper.dev](https://serper.dev) and obtain an api key. ## Usage ```python from langchain.utilities import GoogleSerperAPIWrapper from langchain.llms.openai import OpenAI from langchain.agents import initialize_agent, Tool import os os.environ["SERPER_API_KEY"] = "" os.environ['OPENAI_API_KEY'] = "" llm = OpenAI(temperature=0) search = GoogleSerperAPIWrapper() tools = [ Tool( name="Intermediate Answer", func=search.run ) ] self_ask_with_search = initialize_agent(tools, llm, agent="self-ask-with-search", verbose=True) self_ask_with_search.run("What is the hometown of the reigning men's U.S. Open champion?") ``` ### Output ``` Entering new AgentExecutor chain... Yes. Follow up: Who is the reigning men's U.S. Open champion? Intermediate answer: Current champions Carlos Alcaraz, 2022 men's singles champion. Follow up: Where is Carlos Alcaraz from? Intermediate answer: El Palmar, Spain So the final answer is: El Palmar, Spain > Finished chain. 'El Palmar, Spain' ```
8.5 KiB
8.5 KiB
In [ ]:
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.llms import OpenAIIn [3]:
llm = OpenAI(temperature=0)In [4]:
tools = load_tools(["google-serper"], llm=llm)In [5]:
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)In [6]:
agent.run("What is the weather in Pomfret?")Out [6]:
[1m> Entering new AgentExecutor chain...[0m [32;1m[1;3m I need to find out what the current weather is in Pomfret. Action: Search Action Input: "weather in Pomfret"[0m Observation: [36;1m[1;3mShowers early becoming a steady light rain later in the day. Near record high temperatures. High around 60F. Winds SW at 10 to 15 mph. Chance of rain 60%.[0m Thought:[32;1m[1;3m I now know the current weather in Pomfret. Final Answer: Showers early becoming a steady light rain later in the day. Near record high temperatures. High around 60F. Winds SW at 10 to 15 mph. Chance of rain 60%.[0m [1m> Finished AgentExecutor chain.[0m
'Showers early becoming a steady light rain later in the day. Near record high temperatures. High around 60F. Winds SW at 10 to 15 mph. Chance of rain 60%.'
In [4]:
tools = load_tools(["serpapi"], llm=llm)In [5]:
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)In [6]:
agent.run("What is the weather in Pomfret?")Out [6]:
[1m> Entering new AgentExecutor chain...[0m [32;1m[1;3m I need to find out what the current weather is in Pomfret. Action: Search Action Input: "weather in Pomfret"[0m Observation: [36;1m[1;3mShowers early becoming a steady light rain later in the day. Near record high temperatures. High around 60F. Winds SW at 10 to 15 mph. Chance of rain 60%.[0m Thought:[32;1m[1;3m I now know the current weather in Pomfret. Final Answer: Showers early becoming a steady light rain later in the day. Near record high temperatures. High around 60F. Winds SW at 10 to 15 mph. Chance of rain 60%.[0m [1m> Finished AgentExecutor chain.[0m
'Showers early becoming a steady light rain later in the day. Near record high temperatures. High around 60F. Winds SW at 10 to 15 mph. Chance of rain 60%.'
In [13]:
tools = load_tools(["google-search"], llm=llm)In [14]:
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)In [17]:
agent.run("What is the weather in Pomfret?")Out [17]:
[1m> Entering new AgentExecutor chain...[0m [32;1m[1;3m I should look up the current weather conditions. Action: Google Search Action Input: "weather in Pomfret"[0m Observation: [36;1m[1;3mShowers early becoming a steady light rain later in the day. Near record high temperatures. High around 60F. Winds SW at 10 to 15 mph. Chance of rain 60%. Pomfret, CT Weather Forecast, with current conditions, wind, air quality, and what to expect for the next 3 days. Hourly Weather-Pomfret, CT. As of 12:52 am EST. Special Weather Statement +2 ... Hazardous Weather Conditions. Special Weather Statement ... Pomfret CT. Tonight ... National Digital Forecast Database Maximum Temperature Forecast. Pomfret Center Weather Forecasts. Weather Underground provides local & long-range weather forecasts, weatherreports, maps & tropical weather conditions for ... Pomfret, CT 12 hour by hour weather forecast includes precipitation, temperatures, sky conditions, rain chance, dew-point, relative humidity, wind direction ... North Pomfret Weather Forecasts. Weather Underground provides local & long-range weather forecasts, weatherreports, maps & tropical weather conditions for ... Today's Weather - Pomfret, CT. Dec 31, 2022 4:00 PM. Putnam MS. --. Weather forecast icon. Feels like --. Hi --. Lo --. Pomfret, CT temperature trend for the next 14 Days. Find daytime highs and nighttime lows from TheWeatherNetwork.com. Pomfret, MD Weather Forecast Date: 332 PM EST Wed Dec 28 2022. The area/counties/county of: Charles, including the cites of: St. Charles and Waldorf.[0m Thought:[32;1m[1;3m I now know the current weather conditions in Pomfret. Final Answer: Showers early becoming a steady light rain later in the day. Near record high temperatures. High around 60F. Winds SW at 10 to 15 mph. Chance of rain 60%.[0m [1m> Finished AgentExecutor chain.[0m
'Showers early becoming a steady light rain later in the day. Near record high temperatures. High around 60F. Winds SW at 10 to 15 mph. Chance of rain 60%.'