Skip to content

FunctionToolCall

FunctionToolCall dataclass

Represents a function tool call with its name, arguments, and optional ID.

Attributes:

  • name (str) –

    The name of the function to call.

  • arguments (str) –

    Arguments to pass to the function. This is must be a JSON formatted string

  • id (int | None) –

    An optional identifier for the tool call.

Source code in flexeval/core/tool_parser/base.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
@dataclass
class FunctionToolCall(ToolCall):
    """
    Represents a function tool call with its name, arguments, and optional ID.

    Attributes:
        name: The name of the function to call.
        arguments: Arguments to pass to the function.
            This is must be a JSON formatted string
        id: An optional identifier for the tool call.
    """

    name: str
    arguments: str
    id: int | None = None

    def __post_init__(self) -> None:
        if self.id is None:
            self.id = generate_tool_call_id()
        try:
            json.loads(self.arguments)
        except json.JSONDecodeError as exc:
            msg = "'arguments' must be a JSON formatted string"
            raise ValueError(msg) from exc

    def to_dict(self) -> dict[str, Any]:
        """
        Return a dictionary representation of the function tool call.

        Returns:
            A dictionary with the structure required for function tool calls.
        """
        return {"id": self.id, "type": "function", "function": {"name": self.name, "arguments": self.arguments}}

name instance-attribute

name: str

arguments instance-attribute

arguments: str

id class-attribute instance-attribute

id: int | None = None

__init__

__init__(
    name: str, arguments: str, id: int | None = None
) -> None

__post_init__

__post_init__() -> None
Source code in flexeval/core/tool_parser/base.py
51
52
53
54
55
56
57
58
def __post_init__(self) -> None:
    if self.id is None:
        self.id = generate_tool_call_id()
    try:
        json.loads(self.arguments)
    except json.JSONDecodeError as exc:
        msg = "'arguments' must be a JSON formatted string"
        raise ValueError(msg) from exc

to_dict

to_dict() -> dict[str, Any]

Return a dictionary representation of the function tool call.

Returns:

  • dict[str, Any]

    A dictionary with the structure required for function tool calls.

Source code in flexeval/core/tool_parser/base.py
60
61
62
63
64
65
66
67
def to_dict(self) -> dict[str, Any]:
    """
    Return a dictionary representation of the function tool call.

    Returns:
        A dictionary with the structure required for function tool calls.
    """
    return {"id": self.id, "type": "function", "function": {"name": self.name, "arguments": self.arguments}}

ToolCall dataclass

An abstract base class representing a generic tool call. Subclasses should implement the to_dict method to return a dictionary representation of the tool call.

Source code in flexeval/core/tool_parser/base.py
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@dataclass
class ToolCall(ABC):
    """
    An abstract base class representing a generic tool call.
    Subclasses should implement the `to_dict` method to return a dictionary representation of the tool call.
    """

    @abstractmethod
    def to_dict(self) -> dict[str, Any]:
        """
        Return a dictionary representation of the tool call.

        Returns:
            A dictionary describing the tool call.
        """
        raise NotImplementedError

__init__

__init__() -> None

to_dict abstractmethod

to_dict() -> dict[str, Any]

Return a dictionary representation of the tool call.

Returns:

  • dict[str, Any]

    A dictionary describing the tool call.

Source code in flexeval/core/tool_parser/base.py
24
25
26
27
28
29
30
31
32
@abstractmethod
def to_dict(self) -> dict[str, Any]:
    """
    Return a dictionary representation of the tool call.

    Returns:
        A dictionary describing the tool call.
    """
    raise NotImplementedError

ToolCallingMessage dataclass

Represents the parsed result of a model output that may contain tool calls.

Attributes:

  • validation_result (Literal['CompleteToolCall', 'InCompleteToolCall', 'TextOnly']) –

    The validation result of the parsing (e.g., 'CompleteToolCall', 'InCompleteToolCall', or 'TextOnly').

  • text (str) –

    The text remaining after extracting the tool-calling part.

  • raw_text (str) –

    The raw, unprocessed text.

  • tool_calls (list[ToolCall]) –

    A list of ToolCall objects extracted from the text.

Source code in flexeval/core/tool_parser/base.py
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
@dataclass
class ToolCallingMessage:
    """
    Represents the parsed result of a model output that may contain tool calls.

    Attributes:
        validation_result: The validation result of the parsing
            (e.g., 'CompleteToolCall', 'InCompleteToolCall', or 'TextOnly').
        text: The text remaining after extracting the tool-calling part.
        raw_text: The raw, unprocessed text.
        tool_calls: A list of ToolCall objects extracted from the text.
    """

    validation_result: Literal[
        "CompleteToolCall",
        "InCompleteToolCall",
        "TextOnly",
    ]
    text: str = None
    raw_text: str = None
    tool_calls: list[ToolCall] = field(default_factory=list)

    def __post_init__(self) -> None:
        self.tool_call_dicts = [tool_call.to_dict() for tool_call in self.tool_calls]

validation_result instance-attribute

validation_result: Literal[
    "CompleteToolCall", "InCompleteToolCall", "TextOnly"
]

text class-attribute instance-attribute

text: str = None

raw_text class-attribute instance-attribute

raw_text: str = None

tool_calls class-attribute instance-attribute

tool_calls: list[ToolCall] = field(default_factory=list)

__init__

__init__(
    validation_result: Literal[
        "CompleteToolCall", "InCompleteToolCall", "TextOnly"
    ],
    text: str = None,
    raw_text: str = None,
    tool_calls: list[ToolCall] = list(),
) -> None

__post_init__

__post_init__() -> None
Source code in flexeval/core/tool_parser/base.py
92
93
def __post_init__(self) -> None:
    self.tool_call_dicts = [tool_call.to_dict() for tool_call in self.tool_calls]

ToolParser

An interface class used to extract tool calls from the model's output.

Source code in flexeval/core/tool_parser/base.py
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
class ToolParser(ABC):
    """
    An interface class used to extract tool calls from the model's output.
    """

    @abstractmethod
    def __call__(self, text: str) -> ToolCallingMessage:
        """
        Extract tool_calls from the input text.

        Args:
            text: The text to process.
        """
        raise NotImplementedError

__call__ abstractmethod

__call__(text: str) -> ToolCallingMessage

Extract tool_calls from the input text.

Parameters:

  • text (str) –

    The text to process.

Source code in flexeval/core/tool_parser/base.py
101
102
103
104
105
106
107
108
109
@abstractmethod
def __call__(self, text: str) -> ToolCallingMessage:
    """
    Extract tool_calls from the input text.

    Args:
        text: The text to process.
    """
    raise NotImplementedError