|
| 1 | +# pylint: disable=line-too-long,useless-suppression |
| 2 | +# ------------------------------------ |
| 3 | +# Copyright (c) Microsoft Corporation. |
| 4 | +# Licensed under the MIT License. |
| 5 | +# ------------------------------------ |
| 6 | + |
| 7 | +""" |
| 8 | +DESCRIPTION: |
| 9 | + This sample demonstrates how to run a Prompt Agent that uses the |
| 10 | + WorkIQ preview tool with an asynchronous client. |
| 11 | +
|
| 12 | +USAGE: |
| 13 | + python sample_agent_work_iq_async.py |
| 14 | +
|
| 15 | + Before running the sample: |
| 16 | +
|
| 17 | + pip install "azure-ai-projects>=2.2.0" python-dotenv aiohttp |
| 18 | +
|
| 19 | + Set these environment variables with your own values: |
| 20 | + 1) FOUNDRY_PROJECT_ENDPOINT - The Azure AI Project endpoint, as found in the Overview |
| 21 | + page of your Microsoft Foundry portal. |
| 22 | + 2) FOUNDRY_MODEL_NAME - The deployment name of the AI model, as found under the "Name" column in |
| 23 | + the "Models + endpoints" tab in your Microsoft Foundry project. |
| 24 | + 3) WORK_IQ_PROJECT_CONNECTION_ID - The fully-qualified resource id of the WorkIQ project connection. |
| 25 | + 4) WORK_IQ_USER_INPUT - The natural-language question to send to the agent. |
| 26 | +""" |
| 27 | + |
| 28 | +import os |
| 29 | +import asyncio |
| 30 | +from dotenv import load_dotenv |
| 31 | +from azure.identity.aio import DefaultAzureCredential |
| 32 | +from azure.ai.projects.aio import AIProjectClient |
| 33 | +from azure.ai.projects.models import PromptAgentDefinition, WorkIQPreviewTool |
| 34 | + |
| 35 | +load_dotenv() |
| 36 | + |
| 37 | +endpoint = os.environ["FOUNDRY_PROJECT_ENDPOINT"] |
| 38 | + |
| 39 | + |
| 40 | +async def main(): |
| 41 | + async with ( |
| 42 | + DefaultAzureCredential() as credential, |
| 43 | + AIProjectClient(endpoint=endpoint, credential=credential) as project_client, |
| 44 | + project_client.get_openai_client() as openai_client, |
| 45 | + ): |
| 46 | + tool_payload = WorkIQPreviewTool( |
| 47 | + project_connection_id=os.environ["WORK_IQ_PROJECT_CONNECTION_ID"], |
| 48 | + ) |
| 49 | + |
| 50 | + agent = await project_client.agents.create_version( |
| 51 | + agent_name="MyAgent", |
| 52 | + definition=PromptAgentDefinition( |
| 53 | + model=os.environ["FOUNDRY_MODEL_NAME"], |
| 54 | + instructions="Use the available WorkIQ tools to answer questions and perform tasks.", |
| 55 | + tools=[tool_payload], |
| 56 | + ), |
| 57 | + ) |
| 58 | + print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})") |
| 59 | + |
| 60 | + user_input = os.environ.get("WORK_IQ_USER_INPUT") or input("Enter your question:\n") |
| 61 | + |
| 62 | + response = await openai_client.responses.create( |
| 63 | + input=user_input, |
| 64 | + extra_body={"agent_reference": {"name": agent.name, "type": "agent_reference"}}, |
| 65 | + ) |
| 66 | + |
| 67 | + print(f"Agent response: {response.output_text}") |
| 68 | + |
| 69 | + # Clean up the agent version so unused versions don't accumulate in the project. |
| 70 | + await project_client.agents.delete_version(agent_name=agent.name, agent_version=agent.version) |
| 71 | + print("Agent deleted") |
| 72 | + |
| 73 | + |
| 74 | +if __name__ == "__main__": |
| 75 | + asyncio.run(main()) |
0 commit comments