mirror of
https://github.com/tiennm99/litellm.git
synced 2026-08-01 10:20:48 +00:00
6.9 KiB
6.9 KiB
In [1]:
import os
os.environ["LANGCHAIN_TRACING"] = "true"In [2]:
from langchain import OpenAI
from langchain.agents import initialize_agent, AgentType
llm = OpenAI(temperature=0)In [3]:
from langchain.tools import StructuredTool
def multiplier(a: float, b: float) -> float:
"""Multiply the provided floats."""
return a * b
tool = StructuredTool.from_function(multiplier)In [4]:
# Structured tools are compatible with the STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION agent type.
agent_executor = initialize_agent(
[tool],
llm,
agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,
verbose=True,
)In [5]:
agent_executor.run("What is 3 times 4")Out [5]:
[1m> Entering new AgentExecutor chain...[0m [32;1m[1;3m Thought: I need to multiply 3 and 4 Action: ``` { "action": "multiplier", "action_input": {"a": 3, "b": 4} } ``` [0m Observation: [36;1m[1;3m12[0m Thought:[32;1m[1;3m I know what to respond Action: ``` { "action": "Final Answer", "action_input": "3 times 4 is 12" } ```[0m [1m> Finished chain.[0m
'3 times 4 is 12'
In [6]:
from langchain.llms import OpenAI
from langchain.agents import initialize_agent, Tool
from langchain.agents import AgentTypeIn [7]:
def multiplier(a, b):
return a * b
def parsing_multiplier(string):
a, b = string.split(",")
return multiplier(int(a), int(b))In [8]:
llm = OpenAI(temperature=0)
tools = [
Tool(
name="Multiplier",
func=parsing_multiplier,
description="useful for when you need to multiply two numbers together. The input to this tool should be a comma separated list of numbers of length two, representing the two numbers you want to multiply together. For example, `1,2` would be the input if you wanted to multiply 1 by 2.",
)
]
mrkl = initialize_agent(
tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True
)In [9]:
mrkl.run("What is 3 times 4")Out [9]:
[1m> Entering new AgentExecutor chain...[0m [32;1m[1;3m I need to multiply two numbers Action: Multiplier Action Input: 3,4[0m Observation: [36;1m[1;3m12[0m Thought:[32;1m[1;3m I now know the final answer Final Answer: 3 times 4 is 12[0m [1m> Finished chain.[0m
'3 times 4 is 12'
In [ ]: