mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-06 09:55:21 +03:00
12 KiB
12 KiB
In [8]:
from langchain import LLMMathChain, OpenAI, SerpAPIChain, SQLDatabase, SQLDatabaseChain
from langchain.agents import initialize_agent, ToolIn [9]:
llm = OpenAI(temperature=0)
search = SerpAPIChain()
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 [10]:
mrkl = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)In [11]:
mrkl.run("What was our total number of sales and total revenue in 2013?")Out [11]:
[1m> Entering new chain...[0m
What was our total number of sales and total revenue in 2013?
Thought:[32;1m[1;3m I need to find out how many sales and how much revenue we had in 2013
Action: FooBar DB
Action Input: sales 2013[0m
[1m> Entering new chain...[0m
sales 2013
SQLQuery:[32;1m[1;3m SELECT SUM(Total) FROM Invoice WHERE InvoiceDate BETWEEN '2013-01-01' AND '2013-12-31'[0m
SQLResult: [33;1m[1;3m[(450.58000000000027,)][0m
Answer:[32;1m[1;3m 450.58[0m
[1m> Finished chain.[0m
Observation: [36;1m[1;3m 450.58[0m
Thought:[32;1m[1;3m I need to find out how many sales and how much revenue we had in 2013
Action: FooBar DB
Action Input: revenue 2013[0m
[1m> Entering new chain...[0m
revenue 2013
SQLQuery:[32;1m[1;3m SELECT SUM(Total) FROM Invoice WHERE strftime('%Y', InvoiceDate) = '2013'[0m
SQLResult: [33;1m[1;3m[(450.58000000000027,)][0m
Answer:[32;1m[1;3m 450.58[0m
[1m> Finished chain.[0m
Observation: [36;1m[1;3m 450.58[0m
Thought:[32;1m[1;3m I now know the final answer
Final Answer: We had 450.58 sales and 450.58 revenue in 2013[0m
[1m> Finished chain.[0m
'We had 450.58 sales and 450.58 revenue in 2013'
In [18]:
from langchain.agents import ZeroShotAgent
from langchain import PromptTemplate
from langchain import LLMChain
TEMPLATE = """You are a data engineer answering questions using a SQL database.
Given an input question, first create a syntactically correct {dialect} query to run, then look at the results of the query and return the answer. If a previous query produced an error, do NOT try it again.
Only use the following tables:
{table_info}
Use the following format:
Question: the input question you must answer
Thought: you should always think about what to do
Action: SQLDB
Action Input: the query to run against the SQL database
Observation: the result of the action
... (this Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question
Begin!
Question: {{input}}""".format(**{
"dialect": db.dialect,
"table_info": db.table_info})
prompt = PromptTemplate(template=TEMPLATE, input_variables=["input"])In [19]:
llm_chain = LLMChain(llm=llm, prompt=prompt)
tools = [
Tool("SQLDB", db.run, "foo")
]
agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools, verbose=True)In [20]:
agent.run("What was our total number of sales and total revenue in 2013?")Out [20]:
[1m> Entering new chain...[0m What was our total number of sales and total revenue in 2013? Thought:[32;1m[1;3m I need to find the total number of sales and total revenue Action: SQLDB Action Input: SELECT SUM(Total) AS "Total Sales", SUM(Total) AS "Total Revenue" FROM Invoice WHERE InvoiceDate LIKE '2013%'[0m Observation: [36;1m[1;3m[(450.58000000000027, 450.58000000000027)][0m Thought:[32;1m[1;3m I now know the final answer Final Answer: 450.58[0m [1m> Finished chain.[0m
'450.58'
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 [ ]: