streaming tool call test update

This commit is contained in:
Brandon Hancock
2025-03-04 16:45:43 -05:00
parent ae8d4afbc7
commit 445c27bf6b
2 changed files with 148 additions and 161 deletions

View File

@@ -1805,46 +1805,36 @@ def test_agent_streaming_with_tool_calling():
received_chunks = []
tool_called = False
# Set up event listener to capture streaming chunks
with crewai_event_bus.scoped_handlers():
# Define a tool that will be called
@tool
def calculator(first_number: int, second_number: int) -> float:
"""Calculate the product of two numbers."""
nonlocal tool_called
tool_called = True
return first_number * second_number
@crewai_event_bus.on(LLMStreamChunkEvent)
def handle_stream_chunk(source, event):
received_chunks.append(event.chunk)
# Create an agent with streaming enabled
agent = Agent(
role="Math Assistant",
goal="Help users with mathematical calculations",
backstory="I am an AI assistant specialized in mathematics.",
verbose=True,
llm=LLM(model="gpt-4o", stream=True), # Enable streaming
max_iter=3,
)
# Define a tool that will be called
@tool
def calculator(first_number: int, second_number: int) -> float:
"""Calculate the product of two numbers."""
nonlocal tool_called
tool_called = True
return first_number * second_number
# Create a task that will require tool calling
task = Task(
description="Calculate 23 * 45 using the calculator tool.",
agent=agent,
expected_output="The result of the calculation.",
)
# Create an agent with streaming enabled
agent = Agent(
role="Math Assistant",
goal="Help users with mathematical calculations",
backstory="I am an AI assistant specialized in mathematics.",
verbose=True,
llm=LLM(model="gpt-4o", stream=True), # Enable streaming
max_iter=3,
)
# Execute the task with the calculator tool
output = agent.execute_task(task=task, tools=[calculator])
# Create a task that will require tool calling
task = Task(
description="Calculate 23 * 45 using the calculator tool.",
agent=agent,
expected_output="The result of the calculation.",
)
# Verify that the tool was called
assert tool_called, "Calculator tool was not called"
# Execute the task with the calculator tool
output = agent.execute_task(task=task, tools=[calculator])
# Verify that streaming chunks were received
assert len(received_chunks) > 0, "No streaming chunks were received"
# Verify that the tool was called
assert tool_called, "Calculator tool was not called"
# Verify the output contains the correct result (23 * 45 = 1035)
assert "1035" in output, f"Expected result '1035' not found in output: {output}"
# Verify the output contains the correct result (23 * 45 = 1035)
assert "1035" in output, f"Expected result '1035' not found in output: {output}"