mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-08 19:05:20 +03:00
This PR adds * `ZeroShotAgent.as_sql_agent`, which returns an agent for interacting with a sql database. This builds off of `SQLDatabaseChain`. The main advantages are 1) answering general questions about the db, 2) access to a tool for double checking queries, and 3) recovering from errors * `ZeroShotAgent.as_json_agent` which returns an agent for interacting with json blobs. * Several examples in notebooks --------- Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
20 KiB
20 KiB
In [1]:
import asyncio
import time
from langchain.agents import initialize_agent, load_tools
from langchain.llms import OpenAI
from langchain.callbacks.stdout import StdOutCallbackHandler
from langchain.callbacks.base import CallbackManager
from langchain.callbacks.tracers import LangChainTracer
from aiohttp import ClientSession
questions = [
"Who won the US Open men's final in 2019? What is his age raised to the 0.334 power?",
"Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?",
"Who won the most recent formula 1 grand prix? What is their age raised to the 0.23 power?",
"Who won the US Open women's final in 2019? What is her age raised to the 0.34 power?",
"Who is Beyonce's husband? What is his age raised to the 0.19 power?"
]In [2]:
def generate_serially():
for q in questions:
llm = OpenAI(temperature=0)
tools = load_tools(["llm-math", "serpapi"], llm=llm)
agent = initialize_agent(
tools, llm, agent="zero-shot-react-description", verbose=True
)
agent.run(q)
s = time.perf_counter()
generate_serially()
elapsed = time.perf_counter() - s
print(f"Serial executed in {elapsed:0.2f} seconds.")[1m> Entering new AgentExecutor chain...[0m [32;1m[1;3m I need to find out who won the US Open men's final in 2019 and then calculate his age raised to the 0.334 power. Action: Search Action Input: "US Open men's final 2019 winner"[0m Observation: [33;1m[1;3mRafael Nadal[0m Thought:[32;1m[1;3m I need to find out Rafael Nadal's age Action: Search Action Input: "Rafael Nadal age"[0m Observation: [33;1m[1;3m36 years[0m Thought:[32;1m[1;3m I need to calculate 36 raised to the 0.334 power Action: Calculator Action Input: 36^0.334[0m Observation: [36;1m[1;3mAnswer: 3.3098250249682484 [0m Thought:[32;1m[1;3m I now know the final answer Final Answer: Rafael Nadal, aged 36, won the US Open men's final in 2019 and his age raised to the 0.334 power is 3.3098250249682484.[0m [1m> Finished chain.[0m [1m> Entering new AgentExecutor chain...[0m [32;1m[1;3m I need to find out who Olivia Wilde's boyfriend is and then calculate his age raised to the 0.23 power. Action: Search Action Input: "Olivia Wilde boyfriend"[0m Observation: [33;1m[1;3mJason Sudeikis[0m Thought:[32;1m[1;3m I need to find out Jason Sudeikis' age Action: Search Action Input: "Jason Sudeikis age"[0m Observation: [33;1m[1;3m47 years[0m Thought:[32;1m[1;3m I need to calculate 47 raised to the 0.23 power Action: Calculator Action Input: 47^0.23[0m Observation: [36;1m[1;3mAnswer: 2.4242784855673896 [0m Thought:[32;1m[1;3m I now know the final answer Final Answer: Jason Sudeikis, Olivia Wilde's boyfriend, is 47 years old and his age raised to the 0.23 power is 2.4242784855673896.[0m [1m> Finished chain.[0m [1m> Entering new AgentExecutor chain...[0m [32;1m[1;3m I need to find out who won the grand prix and then calculate their age raised to the 0.23 power. Action: Search Action Input: "Formula 1 Grand Prix Winner"[0m Observation: [33;1m[1;3mMax Verstappen[0m Thought:[32;1m[1;3m I need to find out Max Verstappen's age Action: Search Action Input: "Max Verstappen Age"[0m Observation: [33;1m[1;3m25 years[0m Thought:[32;1m[1;3m I need to calculate 25 raised to the 0.23 power Action: Calculator Action Input: 25^0.23[0m Observation: [36;1m[1;3mAnswer: 1.84599359907945[0m Thought:[32;1m[1;3m I now know the final answer Final Answer: Max Verstappen, 25 years old, raised to the 0.23 power is 1.84599359907945.[0m [1m> Finished chain.[0m [1m> Entering new AgentExecutor chain...[0m [32;1m[1;3m I need to find out who won the US Open women's final in 2019 and then calculate her age raised to the 0.34 power. Action: Search Action Input: "US Open women's final 2019 winner"[0m Observation: [33;1m[1;3mBianca Andreescu defeated Serena Williams in the final, 6–3, 7–5 to win the women's singles tennis title at the 2019 US Open. It was her first major title, and she became the first Canadian, as well as the first player born in the 2000s, to win a major singles title.[0m Thought:[32;1m[1;3m I need to find out Bianca Andreescu's age. Action: Search Action Input: "Bianca Andreescu age"[0m Observation: [33;1m[1;3m22 years[0m Thought:[32;1m[1;3m I now know the age of Bianca Andreescu and can calculate her age raised to the 0.34 power. Action: Calculator Action Input: 22^0.34[0m Observation: [36;1m[1;3mAnswer: 2.8603798598506933 [0m Thought:[32;1m[1;3m I now know the final answer. Final Answer: Bianca Andreescu won the US Open women's final in 2019 and her age raised to the 0.34 power is 2.8603798598506933.[0m [1m> Finished chain.[0m [1m> Entering new AgentExecutor chain...[0m [32;1m[1;3m I need to find out who Beyonce's husband is and then calculate his age raised to the 0.19 power. Action: Search Action Input: "Who is Beyonce's husband?"[0m Observation: [33;1m[1;3mJay-Z[0m Thought:[32;1m[1;3m I need to find out Jay-Z's age Action: Search Action Input: "How old is Jay-Z?"[0m Observation: [33;1m[1;3m53 years[0m Thought:[32;1m[1;3m I need to calculate 53 raised to the 0.19 power Action: Calculator Action Input: 53^0.19[0m Observation: [36;1m[1;3mAnswer: 2.12624064206896 [0m Thought:[32;1m[1;3m I now know the final answer Final Answer: Jay-Z is Beyonce's husband and his age raised to the 0.19 power is 2.12624064206896.[0m [1m> Finished chain.[0m Serial executed in 65.11 seconds.
In [4]:
async def generate_concurrently():
agents = []
# To make async requests in Tools more efficient, you can pass in your own aiohttp.ClientSession,
# but you must manually close the client session at the end of your program/event loop
aiosession = ClientSession()
for _ in questions:
manager = CallbackManager([StdOutCallbackHandler()])
llm = OpenAI(temperature=0, callback_manager=manager)
async_tools = load_tools(["llm-math", "serpapi"], llm=llm, aiosession=aiosession, callback_manager=manager)
agents.append(
initialize_agent(async_tools, llm, agent="zero-shot-react-description", verbose=True, callback_manager=manager)
)
tasks = [async_agent.arun(q) for async_agent, q in zip(agents, questions)]
await asyncio.gather(*tasks)
await aiosession.close()
s = time.perf_counter()
# If running this outside of Jupyter, use asyncio.run(generate_concurrently())
await generate_concurrently()
elapsed = time.perf_counter() - s
print(f"Concurrent executed in {elapsed:0.2f} seconds.")[1m> Entering new AgentExecutor chain...[0m [1m> Entering new AgentExecutor chain...[0m [1m> Entering new AgentExecutor chain...[0m [1m> Entering new AgentExecutor chain...[0m [1m> Entering new AgentExecutor chain...[0m [32;1m[1;3m I need to find out who Olivia Wilde's boyfriend is and then calculate his age raised to the 0.23 power. Action: Search Action Input: "Olivia Wilde boyfriend"[0m[32;1m[1;3m I need to find out who Beyonce's husband is and then calculate his age raised to the 0.19 power. Action: Search Action Input: "Who is Beyonce's husband?"[0m Observation: [33;1m[1;3mJay-Z[0m Thought:[32;1m[1;3m I need to find out who won the grand prix and then calculate their age raised to the 0.23 power. Action: Search Action Input: "Formula 1 Grand Prix Winner"[0m[32;1m[1;3m I need to find out who won the US Open women's final in 2019 and then calculate her age raised to the 0.34 power. Action: Search Action Input: "US Open women's final 2019 winner"[0m Observation: [33;1m[1;3mJason Sudeikis[0m Thought: Observation: [33;1m[1;3mMax Verstappen[0m Thought: Observation: [33;1m[1;3mBianca Andreescu defeated Serena Williams in the final, 6–3, 7–5 to win the women's singles tennis title at the 2019 US Open. It was her first major title, and she became the first Canadian, as well as the first player born in the 2000s, to win a major singles title.[0m Thought:[32;1m[1;3m I need to find out Jason Sudeikis' age Action: Search Action Input: "Jason Sudeikis age"[0m[32;1m[1;3m I need to find out Jay-Z's age Action: Search Action Input: "How old is Jay-Z?"[0m Observation: [33;1m[1;3m53 years[0m Thought:[32;1m[1;3m I need to find out who won the US Open men's final in 2019 and then calculate his age raised to the 0.334 power. Action: Search Action Input: "US Open men's final 2019 winner"[0m Observation: [33;1m[1;3mRafael Nadal defeated Daniil Medvedev in the final, 7–5, 6–3, 5–7, 4–6, 6–4 to win the men's singles tennis title at the 2019 US Open. It was his fourth US ...[0m Thought: Observation: [33;1m[1;3m47 years[0m Thought:[32;1m[1;3m I need to find out Max Verstappen's age Action: Search Action Input: "Max Verstappen Age"[0m Observation: [33;1m[1;3m25 years[0m Thought:[32;1m[1;3m I need to find out Bianca Andreescu's age. Action: Search Action Input: "Bianca Andreescu age"[0m Observation: [33;1m[1;3m22 years[0m Thought:[32;1m[1;3m I need to calculate 53 raised to the 0.19 power Action: Calculator Action Input: 53^0.19[0m[32;1m[1;3m I need to find out the age of the winner Action: Search Action Input: "Rafael Nadal age"[0m[32;1m[1;3m I need to calculate 47 raised to the 0.23 power Action: Calculator Action Input: 47^0.23[0m Observation: [33;1m[1;3m36 years[0m Thought:[32;1m[1;3m I need to calculate 25 raised to the 0.23 power Action: Calculator Action Input: 25^0.23[0m Observation: [36;1m[1;3mAnswer: 2.12624064206896 [0m Thought:[32;1m[1;3m I now know the age of Bianca Andreescu and can calculate her age raised to the 0.34 power. Action: Calculator Action Input: 22^0.34[0m Observation: [36;1m[1;3mAnswer: 1.84599359907945[0m Thought: Observation: [36;1m[1;3mAnswer: 2.4242784855673896 [0m Thought:[32;1m[1;3m I now need to calculate his age raised to the 0.334 power Action: Calculator Action Input: 36^0.334[0m Observation: [36;1m[1;3mAnswer: 2.8603798598506933 [0m Thought:[32;1m[1;3m I now know the final answer Final Answer: Jay-Z is Beyonce's husband and his age raised to the 0.19 power is 2.12624064206896.[0m [1m> Finished chain.[0m [32;1m[1;3m I now know the final answer Final Answer: Max Verstappen, 25 years old, raised to the 0.23 power is 1.84599359907945.[0m [1m> Finished chain.[0m Observation: [36;1m[1;3mAnswer: 3.3098250249682484 [0m Thought:[32;1m[1;3m I now know the final answer Final Answer: Jason Sudeikis, Olivia Wilde's boyfriend, is 47 years old and his age raised to the 0.23 power is 2.4242784855673896.[0m [1m> Finished chain.[0m [32;1m[1;3m I now know the final answer. Final Answer: Bianca Andreescu won the US Open women's final in 2019 and her age raised to the 0.34 power is 2.8603798598506933.[0m [1m> Finished chain.[0m [32;1m[1;3m I now know the final answer Final Answer: Rafael Nadal, aged 36, won the US Open men's final in 2019 and his age raised to the 0.334 power is 3.3098250249682484.[0m [1m> Finished chain.[0m Concurrent executed in 12.38 seconds.
In [7]:
# To make async requests in Tools more efficient, you can pass in your own aiohttp.ClientSession,
# but you must manually close the client session at the end of your program/event loop
aiosession = ClientSession()
tracer = LangChainTracer()
tracer.load_default_session()
manager = CallbackManager([StdOutCallbackHandler(), tracer])
# Pass the manager into the llm if you want llm calls traced.
llm = OpenAI(temperature=0, callback_manager=manager)
async_tools = load_tools(["llm-math", "serpapi"], llm=llm, aiosession=aiosession)
async_agent = initialize_agent(async_tools, llm, agent="zero-shot-react-description", verbose=True, callback_manager=manager)
await async_agent.arun(questions[0])
await aiosession.close()[1m> Entering new AgentExecutor chain...[0m [32;1m[1;3m I need to find out who won the US Open men's final in 2019 and then calculate his age raised to the 0.334 power. Action: Search Action Input: "US Open men's final 2019 winner"[0m Observation: [33;1m[1;3mRafael Nadal[0m Thought:[32;1m[1;3m I need to find out Rafael Nadal's age Action: Search Action Input: "Rafael Nadal age"[0m Observation: [33;1m[1;3m36 years[0m Thought:[32;1m[1;3m I need to calculate 36 raised to the 0.334 power Action: Calculator Action Input: 36^0.334[0m Observation: [36;1m[1;3mAnswer: 3.3098250249682484 [0m Thought:[32;1m[1;3m I now know the final answer Final Answer: Rafael Nadal, aged 36, won the US Open men's final in 2019 and his age raised to the 0.334 power is 3.3098250249682484.[0m [1m> Finished chain.[0m