mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-06 18:05:25 +03:00
19 KiB
19 KiB
In [2]:
# Import things that are needed generically
from langchain.agents import initialize_agent, Tool
from langchain.agents import AgentType
from langchain.tools import BaseTool
from langchain.llms import OpenAI
from langchain import LLMMathChain, SerpAPIWrapperIn [3]:
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 [4]:
# 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=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)In [5]:
agent.run("Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?")Out [5]:
[1m> Entering new AgentExecutor chain...[0m [32;1m[1;3m I need to find out who Leo DiCaprio's girlfriend is and then calculate her age raised to the 0.43 power. Action: Search Action Input: "Leo DiCaprio girlfriend"[0m Observation: [36;1m[1;3mCamila Morrone[0m Thought:[32;1m[1;3m I now need to calculate her age raised to the 0.43 power Action: Calculator Action Input: 22^0.43[0m [1m> Entering new LLMMathChain chain...[0m 22^0.43[32;1m[1;3m ```python import math print(math.pow(22, 0.43)) ``` [0m Answer: [33;1m[1;3m3.777824273683966 [0m [1m> Finished chain.[0m Observation: [33;1m[1;3mAnswer: 3.777824273683966 [0m Thought:[32;1m[1;3m I now know the final answer Final Answer: Camila Morrone's age raised to the 0.43 power is 3.777824273683966.[0m [1m> Finished chain.[0m
"Camila Morrone's age raised to the 0.43 power is 3.777824273683966."
In [8]:
class CustomSearchTool(BaseTool):
name = "Search"
description = "useful for when you need to answer questions about current events"
def _run(self, query: str) -> str:
"""Use the tool."""
return search.run(query)
async def _arun(self, query: str) -> str:
"""Use the tool asynchronously."""
raise NotImplementedError("BingSearchRun does not support async")
class CustomCalculatorTool(BaseTool):
name = "Calculator"
description = "useful for when you need to answer questions about math"
def _run(self, query: str) -> str:
"""Use the tool."""
return llm_math_chain.run(query)
async def _arun(self, query: str) -> str:
"""Use the tool asynchronously."""
raise NotImplementedError("BingSearchRun does not support async")In [9]:
tools = [CustomSearchTool(), CustomCalculatorTool()]In [10]:
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)In [11]:
agent.run("Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?")Out [11]:
[1m> Entering new AgentExecutor chain...[0m [32;1m[1;3m I need to find out who Leo DiCaprio's girlfriend is and then calculate her age raised to the 0.43 power. Action: Search Action Input: "Leo DiCaprio girlfriend"[0m Observation: [36;1m[1;3mCamila Morrone[0m Thought:[32;1m[1;3m I now need to calculate her age raised to the 0.43 power Action: Calculator Action Input: 22^0.43[0m [1m> Entering new LLMMathChain chain...[0m 22^0.43[32;1m[1;3m ```python import math print(math.pow(22, 0.43)) ``` [0m Answer: [33;1m[1;3m3.777824273683966 [0m [1m> Finished chain.[0m Observation: [33;1m[1;3mAnswer: 3.777824273683966 [0m Thought:[32;1m[1;3m I now know the final answer Final Answer: Camila Morrone's age raised to the 0.43 power is 3.777824273683966.[0m [1m> Finished chain.[0m
"Camila Morrone's age raised to the 0.43 power is 3.777824273683966."
In [4]:
from langchain.agents import tool
@tool
def search_api(query: str) -> str:
"""Searches the API for the query."""
return "Results"In [5]:
search_apiOut [5]:
Tool(name='search_api', description='search_api(query: str) -> str - Searches the API for the query.', return_direct=False, verbose=False, callback_manager=<langchain.callbacks.shared.SharedCallbackManager object at 0x1184e0cd0>, func=<function search_api at 0x1635f8700>, coroutine=None)
In [6]:
@tool("search", return_direct=True)
def search_api(query: str) -> str:
"""Searches the API for the query."""
return "Results"In [7]:
search_apiOut [7]:
Tool(name='search', description='search(query: str) -> str - Searches the API for the query.', return_direct=True, verbose=False, callback_manager=<langchain.callbacks.shared.SharedCallbackManager object at 0x1184e0cd0>, func=<function search_api at 0x1635f8670>, coroutine=None)
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=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)In [12]:
agent.run("Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?")Out [12]:
[1m> Entering new AgentExecutor chain...[0m [32;1m[1;3m I need to find out who Leo DiCaprio's girlfriend is and then calculate her age raised to the 0.43 power. Action: Google Search Action Input: "Leo DiCaprio girlfriend"[0m Observation: [36;1m[1;3mCamila Morrone[0m Thought:[32;1m[1;3m I need to find out Camila Morrone's age Action: Google Search Action Input: "Camila Morrone age"[0m Observation: [36;1m[1;3m25 years[0m Thought:[32;1m[1;3m I need to calculate 25 raised to the 0.43 power Action: Calculator Action Input: 25^0.43[0m Observation: [33;1m[1;3mAnswer: 3.991298452658078 [0m Thought:[32;1m[1;3m I now know the final answer Final Answer: Camila Morrone is Leo DiCaprio's girlfriend and her current age raised to the 0.43 power is 3.991298452658078.[0m [1m> Finished chain.[0m
"Camila Morrone is Leo DiCaprio's girlfriend and her current age raised to the 0.43 power is 3.991298452658078."
In [13]:
# Import things that are needed generically
from langchain.agents import initialize_agent, Tool
from langchain.agents import AgentType
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=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)In [14]:
agent.run("what is the most famous song of christmas")Out [14]:
[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 [15]:
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 [16]:
llm = OpenAI(temperature=0)
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)In [17]:
agent.run("whats 2**.12")Out [17]:
[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 [ ]: