Find answers from the community

Home
Members
fiksii4290
f
fiksii4290
Offline, last seen last week
Joined December 5, 2024
Hi everybody... I have 2 questions, I solved them... But I don't like the way, maybe you have other solutions

Imagine I have some Enum:
Plain Text
class MyEnum(Enum): ...


Does anybody know how to:
  1. Parse output to Enum?
    I did the following: I created the Pydantic class with this field and used PydanticOutputParser:
Plain Text
  class MyEnumType(BaseModel):
    my_enum: MyEnum
  
  program = LLMTextCompletionProgram.from_defaults(
    llm=llm,
    output_parser=PydanticOutputParser(output_cls=MyEnumType),
    prompt_template_str='......',
  )
  result = program(prompt=....)
  return query_type.my_enum
  


  1. How can I pass Enum as a parameter to the agent function?
Plain Text
def my_agent(param: MyEnum):
  ....

As I understand it passes only basic Python types. So I just manually convert it back:

Plain Text
def my_agent(value: str):
  param = MyEnum(value)
1 comment
L
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?
2 comments
L
f