Find answers from the community

Updated 3 weeks ago

Passing Parameters to a Tool Function

Hello... I have a question:

How can I pass some parameters inside the tool's function?

I want to have something like this:
Plain Text
def multiply(x: int, y: int, alpha: float) -> float:
    """Multiply two numbers."""
    return x * y * alpha

......

# In another file

alpha = 0.1

multiply_tool = FunctionTool.from_defaults(fn=multiply, alpha=alpha)
agent = ReActAgent.from_tools([multiply_tool], llm=llm, verbose=True)


I found 2 solutions:
  1. Bad solution:
    In a new file create an additional function:
Plain Text
alpha = 0.1

def new_multiply_fn(x: int, y: int) -> float:
    """Multiply two numbers."""
    return multiply_fn(x, y, alpha)

  1. Better solution. Wrap tool function in class:
Plain Text
class ToolFunctionWrapper:
  def __init__(self, alpha):
   self.alpha = alpha
  def multiply(x: int, y: int) -> float:
    """Multiply two numbers."""
    return x * y * self.alpha
tool = ToolFunctionWrapper(alpha=0.1)
multiply_tool = FunctionTool.from_defaults(fn=tool.multiply)
agent = ReActAgent.from_tools([multiply_tool], llm=llm, verbose=True)


Maybe there is a better way to do this?
f
L
2 comments
@Logan M via class Wrapper?
Both seem sensible, class wrapper is probably better yea
Add a reply
Sign up and join the conversation on Discord