From 6c15573217001edcb39ce15b61b5f34661077eda Mon Sep 17 00:00:00 2001 From: Mateusz Chmurski Date: Fri, 29 Aug 2025 15:24:30 +0200 Subject: [PATCH] output function initial impl --- .../src/ragbits/agents/output.py | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 packages/ragbits-agents/src/ragbits/agents/output.py diff --git a/packages/ragbits-agents/src/ragbits/agents/output.py b/packages/ragbits-agents/src/ragbits/agents/output.py new file mode 100644 index 0000000000..244af9ca77 --- /dev/null +++ b/packages/ragbits-agents/src/ragbits/agents/output.py @@ -0,0 +1,76 @@ +from collections.abc import Callable +from dataclasses import dataclass +from typing import Any + +from typing_extensions import Self + +from ragbits.core.utils.function_schema import convert_function_to_function_schema, get_context_variable_name + + +@dataclass +class OutputCallResult: + """ + Filtered results + """ + + id: str + """Unique identifier of the given output""" + output_type: str + """Type of the given output""" + result: Any + """Filtered result""" + + +@dataclass +class OutputFunction: + """ + Output function that can be handled by agent + """ + + name: str + """The name of the output function.""" + description: str | None + """Optional description of what the output function does.""" + parameters: dict[str, Any] + """Dictionary containing the parameters JSON schema.""" + on_tool_call: Callable + """The actual callable function to execute when the tool is called.""" + context_var_name: str | None = None + """The name of the context variable that this tool accepts.""" + + @classmethod + def from_callable(cls, callable: Callable) -> Self: + """ + Create a OutputFunction instance from a callable function. + + Args: + callable: The function to convert into a OutputFunction + + Returns: + A new OutputFunction instance representing the callable function. + """ + schema = convert_function_to_function_schema(callable) + + return cls( + name=schema["function"]["name"], + description=schema["function"]["description"], + parameters=schema["function"]["parameters"], + on_tool_call=callable, + context_var_name=get_context_variable_name(callable), + ) + + def to_function_schema(self) -> dict[str, Any]: + """ + Convert the Tool to a standardized function schema format. + + Returns: + Function schema dictionary with 'type' and 'function' keys. + """ + return { + "type": "function", + "function": { + "name": self.name, + "description": self.description, + "parameters": self.parameters, + }, + }