Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions invariant/analyzer/language/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"sum",
"print",
"tuple",
"tool_call",
"text",
"image",
]
Expand Down
16 changes: 16 additions & 0 deletions invariant/analyzer/stdlib/invariant/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,19 @@ def tuple(*args, **kwargs):
Creates a tuple from the given arguments.
"""
return py_builtins.tuple(*args, **kwargs)


def tool_call(tool_output: ToolOutput, *args, **kwargs) -> ToolCall:
"""
Gets the tool call object from a tool output.

Args:
tool_output: A ToolOutput object.

Returns:
The ToolCall object that corresponds to the tool call in the tool output.
"""
if not isinstance(tool_output, ToolOutput):
raise ValueError("tool_output argument must be a ToolOutput.")

return tool_output._tool_call
36 changes: 36 additions & 0 deletions invariant/tests/analyzer/test_builtins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import unittest

from invariant.analyzer import Policy
from invariant.analyzer.traces import assistant, tool, tool_call, user


class TestBuiltins(unittest.TestCase):
def test_tool_call_name(self):
policy = Policy.from_string("""
raise "error" if:
(tool_output: ToolOutput)
tool_call(tool_output).function.name == "some_tool"
""")

trace = [
user("What is the result of something?"),
assistant(None, tool_call("1", "some_tool", {})),
tool("1", "some_output"),
]
result = policy.analyze(trace)
assert len(result.errors) == 1

def test_tool_call_name_no_match(self):
policy = Policy.from_string("""
raise "error" if:
(tool_output: ToolOutput)
tool_call(tool_output).function.name == "some_tool"
""")

trace = [
user("What is the result of something?"),
assistant(None, tool_call("1", "some_other_tool", {})),
tool("1", "some_output"),
]
result = policy.analyze(trace)
assert len(result.errors) == 0