File: test_container_app_agents_async.py

package info (click to toggle)
python-azure 20251118%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 783,356 kB
  • sloc: python: 6,474,533; ansic: 804; javascript: 287; sh: 205; makefile: 198; xml: 109
file content (65 lines) | stat: -rw-r--r-- 3,041 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
# pylint: disable=too-many-lines,line-too-long,useless-suppression
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
# cSpell:disable

import pytest
from test_base import TestBase, servicePreparer

from devtools_testutils import is_live_and_not_recording

# from azure.ai.projects.models import ResponsesUserMessageItemParam
from azure.ai.projects.models import AgentReference, ContainerAppAgentDefinition, ProtocolVersionRecord, AgentProtocol


class TestContainerAppAgentsAsync(TestBase):

    @servicePreparer()
    @pytest.mark.skipif(
        condition=(not is_live_and_not_recording()),
        reason="Skipped because we cannot record network calls with OpenAI client",
    )
    async def test_container_app_agent_async(self, **kwargs):

        container_app_resource_id = kwargs.pop("azure_ai_projects_tests_container_app_resource_id")
        ingress_subdomain_suffix = kwargs.pop("azure_ai_projects_tests_container_ingress_subdomain_suffix")

        projects_client = self.create_async_client(operation_group="container", **kwargs)
        openai_client = await projects_client.get_openai_client()

        agent_version = await projects_client.agents.create_version(
            agent_name="MyContainerAppAgent",
            definition=ContainerAppAgentDefinition(
                container_app_resource_id=container_app_resource_id,
                container_protocol_versions=[ProtocolVersionRecord(protocol=AgentProtocol.RESPONSES, version="1")],
                ingress_subdomain_suffix=ingress_subdomain_suffix,
            ),
        )
        print(f"Created agent id: {agent_version.id}, name: {agent_version.name}, version: {agent_version.name}")

        try:
            conversation = await openai_client.conversations.create(
                # items=[ResponsesUserMessageItemParam(content="How many feet are in a mile?")]
                items=[{"type": "message", "role": "user", "content": "How many feet are in a mile?"}]
            )
            print(f"Created conversation with initial user message (id: {conversation.id})")

            try:
                response = await openai_client.responses.create(
                    conversation=conversation.id,
                    extra_body={"agent": AgentReference(name=agent_version.name).as_dict()},
                )
                print(f"Response id: {response.id}, output text: {response.output_text}")
                assert "5280" in response.output_text or "5,280" in response.output_text

            finally:
                await openai_client.conversations.delete(conversation.id)
                print(f"Deleted conversation id: {conversation.id}")

        finally:
            await projects_client.agents.delete_version(
                agent_name=agent_version.name, agent_version=agent_version.version
            )
            print(f"Deleted agent id: {agent_version.id}, name: {agent_version.name}, version: {agent_version.version}")