mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-08 02:45:22 +03:00
Co-authored-by: Jason Liu <jxnl@users.noreply.github.com> Co-authored-by: Jason Liu <jason@jxnl.coA>
15 KiB
15 KiB
In [1]:
# Import things that are needed generically
from langchain.agents import initialize_agent, Tool
from langchain.llms import OpenAI
from langchain import LLMMathChain, SerpAPIWrapperIn [2]:
llm = OpenAI(temperature=0)In [3]:
# Load the tool configs that are needed.
search = SerpAPIWrapper()
llm_math_chain = LLMMathChain(llm=llm, 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"
)
]In [6]:
# Construct the agent. We will use the default agent type here.
# See documentation for a full list of options.
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)In [7]:
agent.run("Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?")Out [7]:
[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's boyfriend[0m Observation: [36;1m[1;3mHarry Styles[0m Thought:[32;1m[1;3m I need to calculate Harry Styles' age raised to the 0.23 power. Action: Calculator Action Input: 23^0.23[0m [1m> Entering new LLMMathChain chain...[0m 23^0.23[32;1m[1;3m ```python import math print(math.pow(23, 0.23)) ``` [0m Answer: [33;1m[1;3m2.0568252837687546 [0m [1m> Finished LLMMathChain chain.[0m Observation: [33;1m[1;3mAnswer: 2.0568252837687546 [0m Thought:[32;1m[1;3m I now know the final answer. Final Answer: Harry Styles' age raised to the 0.23 power is 2.0568252837687546.[0m [1m> Finished AgentExecutor chain.[0m
"Harry Styles' age raised to the 0.23 power is 2.0568252837687546."
In [1]:
from langchain.agents import tool
@tool
def search_api(query: str) -> str:
"""Searches the API for the query."""
return "Results"In [2]:
search_apiOut [2]:
Tool(name='search_api', func=<function search_api at 0x10dad7d90>, description='search_api(query: str) -> str - Searches the API for the query.', return_direct=False)
In [3]:
@tool("search", return_direct=True)
def search_api(query: str) -> str:
"""Searches the API for the query."""
return "Results"In [4]:
search_apiOut [4]:
Tool(name='search', func=<function search_api at 0x112301bd0>, description='search(query: str) -> str - Searches the API for the query.', return_direct=True)
In [8]:
from langchain.agents import load_toolsIn [9]:
tools = load_tools(["serpapi", "llm-math"], llm=llm)In [10]:
tools[0].name = "Google Search"In [11]:
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)In [12]:
agent.run("Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?")Out [12]:
[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: Google Search Action Input: "Olivia Wilde boyfriend"[0m Observation: [36;1m[1;3mHarry Styles[0m Thought:[32;1m[1;3m I need to find out Harry Styles' age Action: Google Search Action Input: "Harry Styles age"[0m Observation: [36;1m[1;3m28 years[0m Thought:[32;1m[1;3m I need to calculate 28 raised to the 0.23 power Action: Calculator Action Input: 28^0.23[0m Observation: [33;1m[1;3mAnswer: 2.1520202182226886 [0m Thought:[32;1m[1;3m I now know the final answer Final Answer: Harry Styles is Olivia Wilde's boyfriend and his current age raised to the 0.23 power is 2.1520202182226886.[0m [1m> Finished AgentExecutor chain.[0m
"Harry Styles is Olivia Wilde's boyfriend and his current age raised to the 0.23 power is 2.1520202182226886."
In [7]:
# Import things that are needed generically
from langchain.agents import initialize_agent, Tool
from langchain.llms import OpenAI
from langchain import LLMMathChain, SerpAPIWrapper
search = SerpAPIWrapper()
tools = [
Tool(
name = "Search",
func=search.run,
description="useful for when you need to answer questions about current events"
),
Tool(
name="Music Search",
func=lambda x: "'All I Want For Christmas Is You' by Mariah Carey.", #Mock Function
description="A Music search engine. Use this more than the normal search if the question is about Music, like 'who is the singer of yesterday?' or 'what is the most popular song in 2022?'",
)
]
agent = initialize_agent(tools, OpenAI(temperature=0), agent="zero-shot-react-description", verbose=True)In [8]:
agent.run("what is the most famous song of christmas")Out [8]:
[1m> Entering new AgentExecutor chain...[0m [32;1m[1;3m I should use a music search engine to find the answer Action: Music Search Action Input: most famous song of christmas[0m Observation: [33;1m[1;3m'All I Want For Christmas Is You' by Mariah Carey.[0m Thought:[32;1m[1;3m I now know the final answer Final Answer: 'All I Want For Christmas Is You' by Mariah Carey.[0m [1m> Finished chain.[0m
"'All I Want For Christmas Is You' by Mariah Carey."
In [3]:
llm_math_chain = LLMMathChain(llm=llm)
tools = [
Tool(
name="Calculator",
func=llm_math_chain.run,
description="useful for when you need to answer questions about math",
return_direct=True
)
]In [4]:
llm = OpenAI(temperature=0)
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)In [5]:
agent.run("whats 2**.12")Out [5]:
[1m> Entering new AgentExecutor chain...[0m [32;1m[1;3m I need to calculate this Action: Calculator Action Input: 2**.12[0m Observation: [36;1m[1;3mAnswer: 1.2599210498948732[0m [32;1m[1;3m[0m [1m> Finished chain.[0m
'Answer: 1.2599210498948732'
In [ ]: