mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-19 08:09:21 +03:00
7.5 KiB
7.5 KiB
In [1]:
from langchain import LLMMathChain, OpenAI, SerpAPIWrapper, SQLDatabase, SQLDatabaseChain
from langchain.agents import initialize_agent, ToolIn [2]:
llm = OpenAI(temperature=0)
search = SerpAPIWrapper()
llm_math_chain = LLMMathChain(llm=llm, verbose=True)
db = SQLDatabase.from_uri("sqlite:///../../../notebooks/Chinook.db")
db_chain = SQLDatabaseChain(llm=llm, database=db, verbose=True)
tools = [
Tool(
name = "Search",
func=search.run,
description="useful for when you need to answer questions about current events"
),
Tool(
name="Calculator",
func=llm_math_chain.run,
description="useful for when you need to answer questions about math"
),
Tool(
name="FooBar DB",
func=db_chain.run,
description="useful for when you need to answer questions about FooBar. Input should be in the form of a question"
)
]In [3]:
mrkl = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)In [4]:
mrkl.run("What is the age of Olivia Wilde's boyfriend raised to the 0.23 power?")Out [4]:
What is the age of Olivia Wilde's boyfriend raised to the 0.23 power? Thought:[32;1m[1;3m I need to find the age of Olivia Wilde's boyfriend Action: Search Action Input: "Olivia Wilde's boyfriend"[0m Observation: [36;1m[1;3mOlivia Wilde started dating Harry Styles after ending her years-long engagement to Jason Sudeikis — see their relationship timeline.[0m Thought:[32;1m[1;3m I need to find the age of Harry Styles Action: Search Action Input: "Harry Styles age"[0m Observation: [36;1m[1;3m28 years[0m Thought:[32;1m[1;3m I need to calculate 28 to the 0.23 power Action: Calculator Action Input: 28^0.23[0m [1m> Entering new chain...[0m 28^0.23[32;1m[1;3m ```python print(28**0.23) ``` [0m Answer: [33;1m[1;3m2.1520202182226886 [0m [1m> Finished chain.[0m Observation: [33;1m[1;3mAnswer: 2.1520202182226886 [0m Thought:[32;1m[1;3m I now know the final answer Final Answer: 2.1520202182226886[0m
'2.1520202182226886'
In [5]:
mrkl.run("Who recently released an album called 'The Storm Before the Calm' and are they in the FooBar database? If so, what albums of theirs are in the FooBar database?")Out [5]:
Who recently released an album called 'The Storm Before the Calm' and are they in the FooBar database? If so, what albums of theirs are in the FooBar database?
Thought:[32;1m[1;3m I need to find an album called 'The Storm Before the Calm'
Action: Search
Action Input: "The Storm Before the Calm album"[0m
Observation: [36;1m[1;3mThe Storm Before the Calm (stylized in all lowercase) is the tenth (and eighth international) studio album by Canadian-American singer-songwriter Alanis ...[0m
Thought:[32;1m[1;3m I need to check if Alanis is in the FooBar database
Action: FooBar DB
Action Input: "Does Alanis Morissette exist in the FooBar database?"[0m
[1m> Entering new chain...[0m
Does Alanis Morissette exist in the FooBar database?
SQLQuery:[32;1m[1;3m SELECT * FROM Artist WHERE Name = 'Alanis Morissette'[0m
SQLResult: [33;1m[1;3m[(4, 'Alanis Morissette')][0m
Answer:[32;1m[1;3m Yes[0m
[1m> Finished chain.[0m
Observation: [38;5;200m[1;3m Yes[0m
Thought:[32;1m[1;3m I need to find out what albums of Alanis's are in the FooBar database
Action: FooBar DB
Action Input: "What albums by Alanis Morissette are in the FooBar database?"[0m
[1m> Entering new chain...[0m
What albums by Alanis Morissette are in the FooBar database?
SQLQuery:[32;1m[1;3m SELECT Album.Title FROM Album JOIN Artist ON Album.ArtistId = Artist.ArtistId WHERE Artist.Name = 'Alanis Morissette'[0m
SQLResult: [33;1m[1;3m[('Jagged Little Pill',)][0m
Answer:[32;1m[1;3m Jagged Little Pill[0m
[1m> Finished chain.[0m
Observation: [38;5;200m[1;3m Jagged Little Pill[0m
Thought:[32;1m[1;3m I now know the final answer
Final Answer: The album is by Alanis Morissette and the albums in the FooBar database by her are Jagged Little Pill[0m'The album is by Alanis Morissette and the albums in the FooBar database by her are Jagged Little Pill'
In [ ]: