|
| 1 | +# pylint: disable=too-many-lines,line-too-long,useless-suppression |
| 2 | +# ------------------------------------ |
| 3 | +# Copyright (c) Microsoft Corporation. |
| 4 | +# Licensed under the MIT License. |
| 5 | +# ------------------------------------ |
| 6 | +# cSpell:disable |
| 7 | + |
| 8 | +from test_base import TestBase, servicePreparer |
| 9 | +from devtools_testutils.aio import recorded_by_proxy_async |
| 10 | +from devtools_testutils import RecordedTransport |
| 11 | +from azure.ai.projects.models import ( |
| 12 | + PromptAgentDefinition, |
| 13 | + CodeInterpreterTool, |
| 14 | + CodeInterpreterToolAuto, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +class TestAgentCodeInterpreterAsync(TestBase): |
| 19 | + |
| 20 | + @servicePreparer() |
| 21 | + @recorded_by_proxy_async(RecordedTransport.AZURE_CORE, RecordedTransport.HTTPX) |
| 22 | + async def test_agent_code_interpreter_simple_math_async(self, **kwargs): |
| 23 | + """ |
| 24 | + Test agent with Code Interpreter for simple Python code execution (async version). |
| 25 | +
|
| 26 | + This test verifies that an agent can execute simple Python code |
| 27 | + without any file uploads or downloads - just pure code execution. |
| 28 | + """ |
| 29 | + |
| 30 | + model = self.test_agents_params["model_deployment_name"] |
| 31 | + agent_name = "code-interpreter-simple-agent-async" |
| 32 | + |
| 33 | + async with ( |
| 34 | + self.create_async_client(operation_group="agents", **kwargs) as project_client, |
| 35 | + project_client.get_openai_client() as openai_client, |
| 36 | + ): |
| 37 | + # Create agent with code interpreter tool (no files) |
| 38 | + agent = await project_client.agents.create_version( |
| 39 | + agent_name=agent_name, |
| 40 | + definition=PromptAgentDefinition( |
| 41 | + model=model, |
| 42 | + instructions="You are a helpful assistant that can execute Python code.", |
| 43 | + tools=[CodeInterpreterTool(container=CodeInterpreterToolAuto(file_ids=[]))], |
| 44 | + ), |
| 45 | + description="Simple code interpreter agent for basic Python execution.", |
| 46 | + ) |
| 47 | + self._validate_agent_version(agent, expected_name=agent_name) |
| 48 | + |
| 49 | + # Ask the agent to execute a complex Python calculation |
| 50 | + # Problem: Calculate the sum of cubes from 1 to 50, then add 12!/(8!) |
| 51 | + # Expected answer: 1637505 |
| 52 | + print("\nAsking agent to calculate: sum of cubes from 1 to 50, plus 12!/(8!)") |
| 53 | + |
| 54 | + response = await openai_client.responses.create( |
| 55 | + input="Calculate this using Python: First, find the sum of cubes from 1 to 50 (1³ + 2³ + ... + 50³). Then add 12 factorial divided by 8 factorial (12!/8!). What is the final result?", |
| 56 | + extra_body={"agent": {"name": agent.name, "type": "agent_reference"}}, |
| 57 | + ) |
| 58 | + |
| 59 | + print(f"Response completed (id: {response.id})") |
| 60 | + assert response.id is not None |
| 61 | + assert response.output is not None |
| 62 | + assert len(response.output) > 0 |
| 63 | + |
| 64 | + # Get the response text |
| 65 | + last_message = response.output[-1] |
| 66 | + response_text = "" |
| 67 | + |
| 68 | + if last_message.type == "message": |
| 69 | + for content_item in last_message.content: |
| 70 | + if content_item.type == "output_text": |
| 71 | + response_text += content_item.text |
| 72 | + |
| 73 | + print(f"Agent's response: {response_text}") |
| 74 | + |
| 75 | + # Verify the response contains the correct answer (1637505) |
| 76 | + # Note: sum of cubes 1-50 = 1,625,625; 12!/8! = 11,880; total = 1,637,505 |
| 77 | + assert ( |
| 78 | + "1637505" in response_text or "1,637,505" in response_text |
| 79 | + ), f"Expected answer 1637505 to be in response, but got: {response_text}" |
| 80 | + |
| 81 | + print("✓ Code interpreter successfully executed Python code and returned correct answer") |
| 82 | + |
| 83 | + # Teardown |
| 84 | + await project_client.agents.delete_version(agent_name=agent.name, agent_version=agent.version) |
| 85 | + print("Agent deleted") |
0 commit comments