mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Python: Add tau2 benchmark integration with comprehensive testing and documentation (#817)
* first commit to tau2-bench * tau2-bench agent * tau2 agent * add condition * checkpoint * bug fix * add tests * fix tests * add comments * add comments * minor fix * fix * batch test script * . * init.bak -> init.py * fix mypy * update readme * fix env * remove temp files * setup tests * fix gaia tasks * fix tau2 tests * fix coverage * fix default version * update cookiecutter template --------- Co-authored-by: Eric Zhu <ekzhu@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
@@ -0,0 +1,20 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def purge_tau2_data():
|
||||
"""Purge tau2 data directory if it exists."""
|
||||
|
||||
data_dir = Path.cwd() / "data"
|
||||
|
||||
if data_dir.exists():
|
||||
shutil.rmtree(data_dir)
|
||||
print(f"Data directory at {data_dir} has been purged.")
|
||||
else:
|
||||
print("Data directory not found. Skipping purge.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
purge_tau2_data()
|
||||
@@ -0,0 +1,62 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
import shutil
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def setup_tau2_data():
|
||||
"""Set up tau2 data directory by cloning repository if needed."""
|
||||
|
||||
# Get project directory (parent of tests directory)
|
||||
data_dir = Path.cwd() / "data"
|
||||
print(data_dir)
|
||||
|
||||
print("Setting up tau2 data directory...")
|
||||
|
||||
# Check if data directory already exists
|
||||
if data_dir.exists():
|
||||
print(f"Data directory already exists at {data_dir}")
|
||||
else:
|
||||
print("Data directory not found. Cloning tau2-bench repository...")
|
||||
|
||||
try:
|
||||
# Clone the repository
|
||||
print("Cloning https://github.com/sierra-research/tau2-bench.git...")
|
||||
subprocess.run(
|
||||
["git", "clone", "https://github.com/sierra-research/tau2-bench.git"],
|
||||
check=True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
|
||||
# Move data directory
|
||||
print("Moving data directory...")
|
||||
tau2_bench_dir = Path.cwd() / "tau2-bench"
|
||||
tau2_data_dir = tau2_bench_dir / "data"
|
||||
|
||||
if tau2_data_dir.exists():
|
||||
shutil.move(str(tau2_data_dir), str(data_dir))
|
||||
else:
|
||||
raise FileNotFoundError(f"Data directory not found in cloned repository: {tau2_data_dir}")
|
||||
|
||||
# Clean up cloned repository
|
||||
print("Cleaning up cloned repository...")
|
||||
shutil.rmtree(tau2_bench_dir)
|
||||
|
||||
print("Data directory setup completed successfully!")
|
||||
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"ERROR: Failed to clone repository: {e}")
|
||||
raise
|
||||
except Exception as e:
|
||||
print(f"ERROR: Failed to set up data directory: {e}")
|
||||
raise
|
||||
|
||||
print(f"TAU2_DATA_DIR should be set to: {data_dir}")
|
||||
|
||||
return str(data_dir)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
setup_tau2_data()
|
||||
@@ -0,0 +1,265 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
from agent_framework._types import ChatMessage, Role, TextContent, FunctionCallContent, FunctionResultContent
|
||||
from agent_framework_lab_tau2._message_utils import flip_messages, log_messages
|
||||
|
||||
|
||||
def test_flip_messages_user_to_assistant():
|
||||
"""Test flipping user message to assistant."""
|
||||
messages = [
|
||||
ChatMessage(
|
||||
role=Role.USER, contents=[TextContent(text="Hello assistant")], author_name="User1", message_id="msg_001"
|
||||
)
|
||||
]
|
||||
|
||||
flipped = flip_messages(messages)
|
||||
|
||||
assert len(flipped) == 1
|
||||
assert flipped[0].role == Role.ASSISTANT
|
||||
assert flipped[0].text == "Hello assistant"
|
||||
assert flipped[0].author_name == "User1"
|
||||
assert flipped[0].message_id == "msg_001"
|
||||
|
||||
|
||||
def test_flip_messages_assistant_to_user():
|
||||
"""Test flipping assistant message to user."""
|
||||
messages = [
|
||||
ChatMessage(
|
||||
role=Role.ASSISTANT,
|
||||
contents=[TextContent(text="Hello user")],
|
||||
author_name="Assistant1",
|
||||
message_id="msg_002",
|
||||
)
|
||||
]
|
||||
|
||||
flipped = flip_messages(messages)
|
||||
|
||||
assert len(flipped) == 1
|
||||
assert flipped[0].role == Role.USER
|
||||
assert flipped[0].text == "Hello user"
|
||||
assert flipped[0].author_name == "Assistant1"
|
||||
assert flipped[0].message_id == "msg_002"
|
||||
|
||||
|
||||
def test_flip_messages_assistant_with_function_calls_filtered():
|
||||
"""Test that function calls are filtered out when flipping assistant to user."""
|
||||
function_call = FunctionCallContent(call_id="call_123", name="test_function", arguments={"param": "value"})
|
||||
|
||||
messages = [
|
||||
ChatMessage(
|
||||
role=Role.ASSISTANT,
|
||||
contents=[TextContent(text="I'll call a function"), function_call, TextContent(text="After the call")],
|
||||
message_id="msg_003",
|
||||
)
|
||||
]
|
||||
|
||||
flipped = flip_messages(messages)
|
||||
|
||||
assert len(flipped) == 1
|
||||
assert flipped[0].role == Role.USER
|
||||
# Function call should be filtered out
|
||||
assert len(flipped[0].contents) == 2
|
||||
assert all(content.type == "text" for content in flipped[0].contents)
|
||||
assert "I'll call a function" in flipped[0].text
|
||||
assert "After the call" in flipped[0].text
|
||||
|
||||
|
||||
def test_flip_messages_assistant_with_only_function_calls_skipped():
|
||||
"""Test that assistant messages with only function calls are skipped."""
|
||||
function_call = FunctionCallContent(call_id="call_456", name="another_function", arguments={"key": "value"})
|
||||
|
||||
messages = [
|
||||
ChatMessage(role=Role.ASSISTANT, contents=[function_call], message_id="msg_004") # Only function call, no text
|
||||
]
|
||||
|
||||
flipped = flip_messages(messages)
|
||||
|
||||
# Should be empty since the message had no text content after filtering
|
||||
assert len(flipped) == 0
|
||||
|
||||
|
||||
def test_flip_messages_tool_messages_skipped():
|
||||
"""Test that tool messages are skipped."""
|
||||
function_result = FunctionResultContent(call_id="call_789", result={"success": True})
|
||||
|
||||
messages = [ChatMessage(role=Role.TOOL, contents=[function_result])]
|
||||
|
||||
flipped = flip_messages(messages)
|
||||
|
||||
# Tool messages should be skipped
|
||||
assert len(flipped) == 0
|
||||
|
||||
|
||||
def test_flip_messages_system_messages_preserved():
|
||||
"""Test that system messages are preserved as-is."""
|
||||
messages = [ChatMessage(role=Role.SYSTEM, contents=[TextContent(text="System instruction")], message_id="sys_001")]
|
||||
|
||||
flipped = flip_messages(messages)
|
||||
|
||||
assert len(flipped) == 1
|
||||
assert flipped[0].role == Role.SYSTEM
|
||||
assert flipped[0].text == "System instruction"
|
||||
assert flipped[0].message_id == "sys_001"
|
||||
|
||||
|
||||
def test_flip_messages_mixed_conversation():
|
||||
"""Test flipping a mixed conversation."""
|
||||
function_call = FunctionCallContent(call_id="call_mixed", name="mixed_function", arguments={})
|
||||
|
||||
function_result = FunctionResultContent(call_id="call_mixed", result="function result")
|
||||
|
||||
messages = [
|
||||
ChatMessage(role=Role.SYSTEM, contents=[TextContent(text="System prompt")]),
|
||||
ChatMessage(role=Role.USER, contents=[TextContent(text="User question")]),
|
||||
ChatMessage(role=Role.ASSISTANT, contents=[TextContent(text="Assistant response"), function_call]),
|
||||
ChatMessage(role=Role.TOOL, contents=[function_result]),
|
||||
ChatMessage(role=Role.ASSISTANT, contents=[TextContent(text="Final response")]),
|
||||
]
|
||||
|
||||
flipped = flip_messages(messages)
|
||||
|
||||
# Should have: system (unchanged), assistant (from user), user (from assistant, filtered), assistant (from final assistant)
|
||||
assert len(flipped) == 4
|
||||
|
||||
# Check each flipped message
|
||||
assert flipped[0].role == Role.SYSTEM
|
||||
assert flipped[0].text == "System prompt"
|
||||
|
||||
assert flipped[1].role == Role.ASSISTANT
|
||||
assert flipped[1].text == "User question"
|
||||
|
||||
assert flipped[2].role == Role.USER
|
||||
assert flipped[2].text == "Assistant response" # Function call filtered out
|
||||
|
||||
# Tool message skipped
|
||||
|
||||
assert flipped[3].role == Role.USER
|
||||
assert flipped[3].text == "Final response"
|
||||
|
||||
|
||||
def test_flip_messages_empty_list():
|
||||
"""Test flipping empty message list."""
|
||||
messages = []
|
||||
flipped = flip_messages(messages)
|
||||
assert len(flipped) == 0
|
||||
|
||||
|
||||
def test_flip_messages_preserves_metadata():
|
||||
"""Test that message metadata is preserved during flipping."""
|
||||
messages = [
|
||||
ChatMessage(
|
||||
role=Role.USER, contents=[TextContent(text="Test message")], author_name="TestUser", message_id="test_123"
|
||||
)
|
||||
]
|
||||
|
||||
flipped = flip_messages(messages)
|
||||
|
||||
assert len(flipped) == 1
|
||||
assert flipped[0].author_name == "TestUser"
|
||||
assert flipped[0].message_id == "test_123"
|
||||
|
||||
|
||||
@patch("agent_framework_lab_tau2._message_utils.logger")
|
||||
def test_log_messages_text_content(mock_logger):
|
||||
"""Test logging messages with text content."""
|
||||
messages = [
|
||||
ChatMessage(role=Role.USER, contents=[TextContent(text="Hello")]),
|
||||
ChatMessage(role=Role.ASSISTANT, contents=[TextContent(text="Hi there!")]),
|
||||
]
|
||||
|
||||
log_messages(messages)
|
||||
|
||||
# Should have called logger.info for each message
|
||||
assert mock_logger.opt.return_value.info.call_count == 2
|
||||
|
||||
|
||||
@patch("agent_framework_lab_tau2._message_utils.logger")
|
||||
def test_log_messages_function_call(mock_logger):
|
||||
"""Test logging messages with function calls."""
|
||||
function_call = FunctionCallContent(call_id="call_log", name="log_function", arguments={"param": "value"})
|
||||
|
||||
messages = [ChatMessage(role=Role.ASSISTANT, contents=[function_call])]
|
||||
|
||||
log_messages(messages)
|
||||
|
||||
# Should log the function call
|
||||
mock_logger.opt.return_value.info.assert_called()
|
||||
call_args = mock_logger.opt.return_value.info.call_args[0][0]
|
||||
assert "TOOL_CALL" in call_args
|
||||
assert "log_function" in call_args
|
||||
|
||||
|
||||
@patch("agent_framework_lab_tau2._message_utils.logger")
|
||||
def test_log_messages_function_result(mock_logger):
|
||||
"""Test logging messages with function results."""
|
||||
function_result = FunctionResultContent(call_id="call_result", result="success")
|
||||
|
||||
messages = [ChatMessage(role=Role.TOOL, contents=[function_result])]
|
||||
|
||||
log_messages(messages)
|
||||
|
||||
# Should log the function result
|
||||
mock_logger.opt.return_value.info.assert_called()
|
||||
call_args = mock_logger.opt.return_value.info.call_args[0][0]
|
||||
assert "TOOL_RESULT" in call_args
|
||||
|
||||
|
||||
@patch("agent_framework_lab_tau2._message_utils.logger")
|
||||
def test_log_messages_different_roles(mock_logger):
|
||||
"""Test logging messages with different roles get different colors."""
|
||||
messages = [
|
||||
ChatMessage(role=Role.SYSTEM, contents=[TextContent(text="System")]),
|
||||
ChatMessage(role=Role.USER, contents=[TextContent(text="User")]),
|
||||
ChatMessage(role=Role.ASSISTANT, contents=[TextContent(text="Assistant")]),
|
||||
ChatMessage(role=Role.TOOL, contents=[TextContent(text="Tool")]),
|
||||
]
|
||||
|
||||
log_messages(messages)
|
||||
|
||||
# Should have called logger for each message
|
||||
assert mock_logger.opt.return_value.info.call_count == 4
|
||||
|
||||
# Check that different color tags are used
|
||||
calls = mock_logger.opt.return_value.info.call_args_list
|
||||
system_call = calls[0][0][0]
|
||||
user_call = calls[1][0][0]
|
||||
assistant_call = calls[2][0][0]
|
||||
tool_call = calls[3][0][0]
|
||||
|
||||
assert "cyan" in system_call or "SYSTEM" in system_call
|
||||
assert "green" in user_call or "USER" in user_call
|
||||
assert "blue" in assistant_call or "ASSISTANT" in assistant_call
|
||||
assert "yellow" in tool_call or "TOOL" in tool_call
|
||||
|
||||
|
||||
@patch("agent_framework_lab_tau2._message_utils.logger")
|
||||
def test_log_messages_escapes_html(mock_logger):
|
||||
"""Test that HTML-like characters are properly escaped in log output."""
|
||||
messages = [ChatMessage(role=Role.USER, contents=[TextContent(text="Message with <tag> content")])]
|
||||
|
||||
log_messages(messages)
|
||||
|
||||
mock_logger.opt.return_value.info.assert_called()
|
||||
call_args = mock_logger.opt.return_value.info.call_args[0][0]
|
||||
# Should escape < characters
|
||||
assert "\\<tag>" in call_args or "<tag>" in call_args
|
||||
|
||||
|
||||
@patch("agent_framework_lab_tau2._message_utils.logger")
|
||||
def test_log_messages_mixed_content_types(mock_logger):
|
||||
"""Test logging messages with mixed content types."""
|
||||
function_call = FunctionCallContent(call_id="mixed_call", name="mixed_function", arguments={"key": "value"})
|
||||
|
||||
messages = [
|
||||
ChatMessage(
|
||||
role=Role.ASSISTANT,
|
||||
contents=[TextContent(text="I'll call a function"), function_call, TextContent(text="Done!")],
|
||||
)
|
||||
]
|
||||
|
||||
log_messages(messages)
|
||||
|
||||
# Should log multiple times for different content types
|
||||
assert mock_logger.opt.return_value.info.call_count == 3
|
||||
@@ -0,0 +1,267 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
"""Tests for sliding window message list."""
|
||||
|
||||
import pytest
|
||||
from unittest.mock import patch
|
||||
|
||||
from agent_framework._types import ChatMessage, Role, TextContent, FunctionCallContent, FunctionResultContent
|
||||
from agent_framework_lab_tau2._sliding_window import SlidingWindowChatMessageList
|
||||
|
||||
|
||||
def test_initialization_empty():
|
||||
"""Test initializing with no messages."""
|
||||
sliding_window = SlidingWindowChatMessageList(max_tokens=1000)
|
||||
|
||||
assert sliding_window.max_tokens == 1000
|
||||
assert sliding_window.system_message is None
|
||||
assert sliding_window.tool_definitions is None
|
||||
assert len(sliding_window._messages) == 0
|
||||
assert len(sliding_window._truncated_messages) == 0
|
||||
|
||||
|
||||
def test_initialization_with_parameters():
|
||||
"""Test initializing with system message and tool definitions."""
|
||||
system_msg = "You are a helpful assistant"
|
||||
tool_defs = [{"name": "test_tool", "description": "A test tool"}]
|
||||
|
||||
sliding_window = SlidingWindowChatMessageList(
|
||||
max_tokens=2000, system_message=system_msg, tool_definitions=tool_defs
|
||||
)
|
||||
|
||||
assert sliding_window.max_tokens == 2000
|
||||
assert sliding_window.system_message == system_msg
|
||||
assert sliding_window.tool_definitions == tool_defs
|
||||
|
||||
|
||||
def test_initialization_with_messages():
|
||||
"""Test initializing with existing messages."""
|
||||
messages = [
|
||||
ChatMessage(role=Role.USER, contents=[TextContent(text="Hello")]),
|
||||
ChatMessage(role=Role.ASSISTANT, contents=[TextContent(text="Hi there!")]),
|
||||
]
|
||||
|
||||
sliding_window = SlidingWindowChatMessageList(messages=messages, max_tokens=1000)
|
||||
|
||||
assert len(sliding_window._messages) == 2
|
||||
assert len(sliding_window._truncated_messages) == 2
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_messages_simple():
|
||||
"""Test adding messages without truncation."""
|
||||
sliding_window = SlidingWindowChatMessageList(max_tokens=10000) # Large limit
|
||||
|
||||
new_messages = [
|
||||
ChatMessage(role=Role.USER, contents=[TextContent(text="What's the weather?")]),
|
||||
ChatMessage(role=Role.ASSISTANT, contents=[TextContent(text="I can help with that.")]),
|
||||
]
|
||||
|
||||
await sliding_window.add_messages(new_messages)
|
||||
|
||||
messages = await sliding_window.list_messages()
|
||||
assert len(messages) == 2
|
||||
assert messages[0].text == "What's the weather?"
|
||||
assert messages[1].text == "I can help with that."
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_all_messages_vs_list_messages():
|
||||
"""Test difference between list_all_messages and list_messages."""
|
||||
sliding_window = SlidingWindowChatMessageList(max_tokens=50) # Small limit to force truncation
|
||||
|
||||
# Add many messages to trigger truncation
|
||||
messages = [
|
||||
ChatMessage(role=Role.USER, contents=[TextContent(text=f"Message {i} with some content")]) for i in range(10)
|
||||
]
|
||||
|
||||
await sliding_window.add_messages(messages)
|
||||
|
||||
truncated_messages = await sliding_window.list_messages()
|
||||
all_messages = await sliding_window.list_all_messages()
|
||||
|
||||
# All messages should contain everything
|
||||
assert len(all_messages) == 10
|
||||
|
||||
# Truncated messages should be fewer due to token limit
|
||||
assert len(truncated_messages) < len(all_messages)
|
||||
|
||||
|
||||
def test_get_token_count_basic():
|
||||
"""Test basic token counting."""
|
||||
sliding_window = SlidingWindowChatMessageList(max_tokens=1000)
|
||||
sliding_window._truncated_messages = [ChatMessage(role=Role.USER, contents=[TextContent(text="Hello")])]
|
||||
|
||||
token_count = sliding_window.get_token_count()
|
||||
|
||||
# Should be more than 0 (exact count depends on encoding)
|
||||
assert token_count > 0
|
||||
|
||||
|
||||
def test_get_token_count_with_system_message():
|
||||
"""Test token counting includes system message."""
|
||||
system_msg = "You are a helpful assistant"
|
||||
sliding_window = SlidingWindowChatMessageList(max_tokens=1000, system_message=system_msg)
|
||||
|
||||
# Without messages
|
||||
token_count_empty = sliding_window.get_token_count()
|
||||
|
||||
# Add a message
|
||||
sliding_window._truncated_messages = [ChatMessage(role=Role.USER, contents=[TextContent(text="Hello")])]
|
||||
token_count_with_message = sliding_window.get_token_count()
|
||||
|
||||
# With message should be more tokens
|
||||
assert token_count_with_message > token_count_empty
|
||||
assert token_count_empty > 0 # System message contributes tokens
|
||||
|
||||
|
||||
def test_get_token_count_function_call():
|
||||
"""Test token counting with function calls."""
|
||||
function_call = FunctionCallContent(call_id="call_123", name="test_function", arguments={"param": "value"})
|
||||
|
||||
sliding_window = SlidingWindowChatMessageList(max_tokens=1000)
|
||||
sliding_window._truncated_messages = [ChatMessage(role=Role.ASSISTANT, contents=[function_call])]
|
||||
|
||||
token_count = sliding_window.get_token_count()
|
||||
assert token_count > 0
|
||||
|
||||
|
||||
def test_get_token_count_function_result():
|
||||
"""Test token counting with function results."""
|
||||
function_result = FunctionResultContent(call_id="call_123", result={"success": True, "data": "result"})
|
||||
|
||||
sliding_window = SlidingWindowChatMessageList(max_tokens=1000)
|
||||
sliding_window._truncated_messages = [ChatMessage(role=Role.TOOL, contents=[function_result])]
|
||||
|
||||
token_count = sliding_window.get_token_count()
|
||||
assert token_count > 0
|
||||
|
||||
|
||||
@patch("agent_framework_lab_tau2._sliding_window.logger")
|
||||
def test_truncate_messages_removes_old_messages(mock_logger):
|
||||
"""Test that truncation removes old messages when token limit exceeded."""
|
||||
sliding_window = SlidingWindowChatMessageList(max_tokens=20) # Very small limit
|
||||
|
||||
# Create messages that will exceed the limit
|
||||
messages = [
|
||||
ChatMessage(
|
||||
role=Role.USER,
|
||||
contents=[TextContent(text="This is a very long message that should exceed the token limit")],
|
||||
),
|
||||
ChatMessage(
|
||||
role=Role.ASSISTANT,
|
||||
contents=[TextContent(text="This is another very long message that should also exceed the token limit")],
|
||||
),
|
||||
ChatMessage(role=Role.USER, contents=[TextContent(text="Short msg")]),
|
||||
]
|
||||
|
||||
sliding_window._truncated_messages = messages.copy()
|
||||
sliding_window.truncate_messages()
|
||||
|
||||
# Should have fewer messages after truncation
|
||||
assert len(sliding_window._truncated_messages) < len(messages)
|
||||
|
||||
# Should have logged warnings
|
||||
assert mock_logger.warning.called
|
||||
|
||||
|
||||
@patch("agent_framework_lab_tau2._sliding_window.logger")
|
||||
def test_truncate_messages_removes_leading_tool_messages(mock_logger):
|
||||
"""Test that truncation removes leading tool messages."""
|
||||
sliding_window = SlidingWindowChatMessageList(max_tokens=10000) # Large limit
|
||||
|
||||
# Create messages starting with tool message
|
||||
tool_message = ChatMessage(role=Role.TOOL, contents=[FunctionResultContent(call_id="call_123", result="result")])
|
||||
user_message = ChatMessage(role=Role.USER, contents=[TextContent(text="Hello")])
|
||||
|
||||
sliding_window._truncated_messages = [tool_message, user_message]
|
||||
sliding_window.truncate_messages()
|
||||
|
||||
# Tool message should be removed from the beginning
|
||||
assert len(sliding_window._truncated_messages) == 1
|
||||
assert sliding_window._truncated_messages[0].role == Role.USER
|
||||
|
||||
# Should have logged warning about removing tool message
|
||||
mock_logger.warning.assert_called()
|
||||
|
||||
|
||||
def test_estimate_any_object_token_count_dict():
|
||||
"""Test token counting for dictionary objects."""
|
||||
sliding_window = SlidingWindowChatMessageList(max_tokens=1000)
|
||||
|
||||
test_dict = {"key": "value", "number": 42}
|
||||
token_count = sliding_window.estimate_any_object_token_count(test_dict)
|
||||
|
||||
assert token_count > 0
|
||||
|
||||
|
||||
def test_estimate_any_object_token_count_string():
|
||||
"""Test token counting for string objects."""
|
||||
sliding_window = SlidingWindowChatMessageList(max_tokens=1000)
|
||||
|
||||
test_string = "This is a test string"
|
||||
token_count = sliding_window.estimate_any_object_token_count(test_string)
|
||||
|
||||
assert token_count > 0
|
||||
|
||||
|
||||
def test_estimate_any_object_token_count_non_serializable():
|
||||
"""Test token counting for non-JSON-serializable objects."""
|
||||
sliding_window = SlidingWindowChatMessageList(max_tokens=1000)
|
||||
|
||||
# Create an object that can't be JSON serialized
|
||||
class CustomObject:
|
||||
def __str__(self):
|
||||
return "CustomObject instance"
|
||||
|
||||
custom_obj = CustomObject()
|
||||
token_count = sliding_window.estimate_any_object_token_count(custom_obj)
|
||||
|
||||
# Should fall back to string representation
|
||||
assert token_count > 0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_real_world_scenario():
|
||||
"""Test a realistic conversation scenario."""
|
||||
sliding_window = SlidingWindowChatMessageList(
|
||||
max_tokens=30, system_message="You are a helpful assistant" # Moderate limit
|
||||
)
|
||||
|
||||
# Simulate a conversation
|
||||
conversation = [
|
||||
ChatMessage(role=Role.USER, contents=[TextContent(text="Hello, how are you?")]),
|
||||
ChatMessage(
|
||||
role=Role.ASSISTANT, contents=[TextContent(text="I'm doing well, thank you! How can I help you today?")]
|
||||
),
|
||||
ChatMessage(role=Role.USER, contents=[TextContent(text="Can you tell me about the weather?")]),
|
||||
ChatMessage(
|
||||
role=Role.ASSISTANT,
|
||||
contents=[
|
||||
TextContent(
|
||||
text="I'd be happy to help with weather information, but I don't have access to current weather data."
|
||||
)
|
||||
],
|
||||
),
|
||||
ChatMessage(role=Role.USER, contents=[TextContent(text="What about telling me a joke instead?")]),
|
||||
ChatMessage(
|
||||
role=Role.ASSISTANT,
|
||||
contents=[TextContent(text="Sure! Why don't scientists trust atoms? Because they make up everything!")],
|
||||
),
|
||||
]
|
||||
|
||||
await sliding_window.add_messages(conversation)
|
||||
|
||||
current_messages = await sliding_window.list_messages()
|
||||
all_messages = await sliding_window.list_all_messages()
|
||||
|
||||
# All messages should be preserved
|
||||
assert len(all_messages) == 6
|
||||
|
||||
# Current messages might be truncated
|
||||
assert len(current_messages) <= 6
|
||||
|
||||
# Token count should be within or close to limit
|
||||
token_count = sliding_window.get_token_count()
|
||||
# Allow some margin since truncation happens when exceeded
|
||||
assert token_count <= sliding_window.max_tokens * 1.1
|
||||
@@ -0,0 +1,189 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
"""Tests for tau2 utils module."""
|
||||
|
||||
from typing import Any, cast
|
||||
from pydantic import BaseModel
|
||||
|
||||
from agent_framework._tools import AIFunction
|
||||
from agent_framework._types import ChatMessage, Role, TextContent, FunctionCallContent, FunctionResultContent
|
||||
from agent_framework_lab_tau2._tau2_utils import (
|
||||
convert_tau2_tool_to_ai_function,
|
||||
convert_agent_framework_messages_to_tau2_messages,
|
||||
)
|
||||
from tau2.data_model.message import SystemMessage, UserMessage, AssistantMessage, ToolMessage, ToolCall
|
||||
from tau2.domains.airline.environment import get_environment
|
||||
|
||||
|
||||
def test_convert_tau2_tool_to_ai_function_basic():
|
||||
"""Test basic conversion from tau2 tool to AIFunction."""
|
||||
# Get real tools from tau2 environment
|
||||
env = get_environment()
|
||||
tools = env.get_tools()
|
||||
|
||||
# Use the first available tool for testing
|
||||
assert len(tools) > 0, "No tools available in environment"
|
||||
tau2_tool = tools[0]
|
||||
|
||||
# Convert the tool
|
||||
ai_function = convert_tau2_tool_to_ai_function(tau2_tool)
|
||||
|
||||
# Verify the conversion
|
||||
assert isinstance(ai_function, AIFunction)
|
||||
assert ai_function.name == tau2_tool.name
|
||||
assert ai_function.description == tau2_tool._get_description()
|
||||
assert ai_function.input_model == tau2_tool.params
|
||||
|
||||
# Test that the function is callable (we won't call it with real params to avoid side effects)
|
||||
assert callable(ai_function.func)
|
||||
|
||||
|
||||
def test_convert_tau2_tool_to_ai_function_multiple_tools():
|
||||
"""Test conversion with multiple tau2 tools."""
|
||||
# Get real tools from tau2 environment
|
||||
env = get_environment()
|
||||
tools = env.get_tools()
|
||||
|
||||
# Convert multiple tools
|
||||
ai_functions = [convert_tau2_tool_to_ai_function(tool) for tool in tools[:3]] # Test first 3 tools
|
||||
|
||||
# Verify all conversions
|
||||
for ai_function, tau2_tool in zip(ai_functions, tools[:3]):
|
||||
assert isinstance(ai_function, AIFunction)
|
||||
assert ai_function.name == tau2_tool.name
|
||||
assert ai_function.description == tau2_tool._get_description()
|
||||
assert ai_function.input_model == tau2_tool.params
|
||||
assert callable(ai_function.func)
|
||||
|
||||
|
||||
def test_convert_agent_framework_messages_to_tau2_messages_system():
|
||||
"""Test converting system message."""
|
||||
messages = [ChatMessage(role=Role.SYSTEM, contents=[TextContent(text="System instruction")])]
|
||||
|
||||
tau2_messages = convert_agent_framework_messages_to_tau2_messages(messages)
|
||||
|
||||
assert len(tau2_messages) == 1
|
||||
assert isinstance(tau2_messages[0], SystemMessage)
|
||||
assert tau2_messages[0].role == "system"
|
||||
assert tau2_messages[0].content == "System instruction"
|
||||
|
||||
|
||||
def test_convert_agent_framework_messages_to_tau2_messages_user():
|
||||
"""Test converting user message."""
|
||||
messages = [ChatMessage(role=Role.USER, contents=[TextContent(text="Hello assistant")])]
|
||||
|
||||
tau2_messages = convert_agent_framework_messages_to_tau2_messages(messages)
|
||||
|
||||
assert len(tau2_messages) == 1
|
||||
assert isinstance(tau2_messages[0], UserMessage)
|
||||
assert tau2_messages[0].role == "user"
|
||||
assert tau2_messages[0].content == "Hello assistant"
|
||||
assert tau2_messages[0].tool_calls is None
|
||||
|
||||
|
||||
def test_convert_agent_framework_messages_to_tau2_messages_assistant():
|
||||
"""Test converting assistant message."""
|
||||
messages = [ChatMessage(role=Role.ASSISTANT, contents=[TextContent(text="Hello user")])]
|
||||
|
||||
tau2_messages = convert_agent_framework_messages_to_tau2_messages(messages)
|
||||
|
||||
assert len(tau2_messages) == 1
|
||||
assert isinstance(tau2_messages[0], AssistantMessage)
|
||||
assert tau2_messages[0].role == "assistant"
|
||||
assert tau2_messages[0].content == "Hello user"
|
||||
assert tau2_messages[0].tool_calls is None
|
||||
|
||||
|
||||
def test_convert_agent_framework_messages_to_tau2_messages_with_function_call():
|
||||
"""Test converting message with function call."""
|
||||
function_call = FunctionCallContent(call_id="call_123", name="test_function", arguments={"param": "value"})
|
||||
|
||||
messages = [ChatMessage(role=Role.ASSISTANT, contents=[TextContent(text="I'll call a function"), function_call])]
|
||||
|
||||
tau2_messages = convert_agent_framework_messages_to_tau2_messages(messages)
|
||||
|
||||
assert len(tau2_messages) == 1
|
||||
assert isinstance(tau2_messages[0], AssistantMessage)
|
||||
assert tau2_messages[0].content == "I'll call a function"
|
||||
assert tau2_messages[0].tool_calls is not None
|
||||
assert len(tau2_messages[0].tool_calls) == 1
|
||||
|
||||
tool_call = tau2_messages[0].tool_calls[0]
|
||||
assert isinstance(tool_call, ToolCall)
|
||||
assert tool_call.id == "call_123"
|
||||
assert tool_call.name == "test_function"
|
||||
assert tool_call.arguments == {"param": "value"}
|
||||
assert tool_call.requestor == "assistant"
|
||||
|
||||
|
||||
def test_convert_agent_framework_messages_to_tau2_messages_with_function_result():
|
||||
"""Test converting message with function result."""
|
||||
function_result = FunctionResultContent(call_id="call_123", result={"success": True, "data": "result data"})
|
||||
|
||||
messages = [ChatMessage(role=Role.TOOL, contents=[function_result])]
|
||||
|
||||
tau2_messages = convert_agent_framework_messages_to_tau2_messages(messages)
|
||||
|
||||
assert len(tau2_messages) == 1
|
||||
assert isinstance(tau2_messages[0], ToolMessage)
|
||||
assert tau2_messages[0].id == "call_123"
|
||||
assert tau2_messages[0].role == "tool"
|
||||
assert tau2_messages[0].content is not None
|
||||
assert '{"success": true, "data": "result data"}' in tau2_messages[0].content
|
||||
assert tau2_messages[0].requestor == "assistant"
|
||||
assert tau2_messages[0].error is False
|
||||
|
||||
|
||||
def test_convert_agent_framework_messages_to_tau2_messages_with_error():
|
||||
"""Test converting function result with error."""
|
||||
function_result = FunctionResultContent(
|
||||
call_id="call_456", result="Error occurred", exception=Exception("Test error")
|
||||
)
|
||||
|
||||
messages = [ChatMessage(role=Role.TOOL, contents=[function_result])]
|
||||
|
||||
tau2_messages = convert_agent_framework_messages_to_tau2_messages(messages)
|
||||
|
||||
assert len(tau2_messages) == 1
|
||||
assert isinstance(tau2_messages[0], ToolMessage)
|
||||
assert tau2_messages[0].error is True
|
||||
|
||||
|
||||
def test_convert_agent_framework_messages_to_tau2_messages_multiple_text_contents():
|
||||
"""Test converting message with multiple text contents."""
|
||||
messages = [ChatMessage(role=Role.USER, contents=[TextContent(text="First part"), TextContent(text="Second part")])]
|
||||
|
||||
tau2_messages = convert_agent_framework_messages_to_tau2_messages(messages)
|
||||
|
||||
assert len(tau2_messages) == 1
|
||||
assert isinstance(tau2_messages[0], UserMessage)
|
||||
assert tau2_messages[0].content == "First part Second part"
|
||||
|
||||
|
||||
def test_convert_agent_framework_messages_to_tau2_messages_complex_scenario():
|
||||
"""Test converting complex scenario with multiple message types."""
|
||||
function_call = FunctionCallContent(call_id="call_789", name="complex_tool", arguments='{"key": "value"}')
|
||||
|
||||
function_result = FunctionResultContent(call_id="call_789", result={"output": "tool result"})
|
||||
|
||||
messages = [
|
||||
ChatMessage(role=Role.SYSTEM, contents=[TextContent(text="System prompt")]),
|
||||
ChatMessage(role=Role.USER, contents=[TextContent(text="User request")]),
|
||||
ChatMessage(role=Role.ASSISTANT, contents=[TextContent(text="I'll help you"), function_call]),
|
||||
ChatMessage(role=Role.TOOL, contents=[function_result]),
|
||||
ChatMessage(role=Role.ASSISTANT, contents=[TextContent(text="Based on the result...")]),
|
||||
]
|
||||
|
||||
tau2_messages = convert_agent_framework_messages_to_tau2_messages(messages)
|
||||
|
||||
assert len(tau2_messages) == 5
|
||||
assert isinstance(tau2_messages[0], SystemMessage)
|
||||
assert isinstance(tau2_messages[1], UserMessage)
|
||||
assert isinstance(tau2_messages[2], AssistantMessage)
|
||||
assert isinstance(tau2_messages[3], ToolMessage)
|
||||
assert isinstance(tau2_messages[4], AssistantMessage)
|
||||
|
||||
# Check the assistant message with tool call
|
||||
assert tau2_messages[2].tool_calls is not None
|
||||
assert len(tau2_messages[2].tool_calls) == 1
|
||||
assert tau2_messages[2].tool_calls[0].name == "complex_tool"
|
||||
Reference in New Issue
Block a user