Skip to content

Commit e010ace

Browse files
author
ecfan
committed
Add zone pivots and Python content
1 parent 16a2188 commit e010ace

1 file changed

Lines changed: 143 additions & 2 deletions

File tree

articles/logic-apps/add-agent-action-create-run-workflow.md

Lines changed: 143 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ This guide shows how to add an action as a tool for an agent in Foundry. This ac
3838

3939
> [!NOTE]
4040
>
41-
> This guide refers to the Foundry (classic) portal. For more information, see the [What is Microsoft Foundry](/azure/foundry/what-is-foundry)?
41+
> This guide refers to the [Microsoft Foundry (classic)](/azure/foundry-classic/what-is-foundry#microsoft-foundry-portals) portal. For more information about the new portal, see the new [Microsoft Foundry portal](/azure/foundry/what-is-foundry)?
4242
4343
For more information, see:
4444

@@ -52,7 +52,7 @@ For more information, see:
5252
- A [Foundry project](/azure/foundry-classic/how-to/create-projects?tabs=foundry).
5353

5454
This project organizes your work and saves the state while you build your AI apps.
55-
55+
5656
If you want to [create a hub project](/azure/foundry-classic/how-to/hub-create-projects?tabs=portal) so you can host your project and set up a team collaboration environment, you need one of the following roles for Microsoft Entra role-based access control (RBAC), based on the [principle of least privilege](/entra/identity-platform/secure-least-privileged-access):
5757

5858
- **Contributor** (least privilege)
@@ -110,6 +110,8 @@ This release has the following limitations or known problems:
110110

111111
For more information, see [Hosting options for logic app deployments](/azure/logic-apps/logic-apps-overview#create-and-deploy-to-different-environments).
112112

113+
:::zone pivot="portal"
114+
113115
## 1: Add an action to your agent
114116

115117
To set up an action for your agent to run a logic app workflow, follow these steps:
@@ -243,6 +245,129 @@ To try the new agent action by using the **Agents playground**, follow these ste
243245

244246
:::image type="content" source="media/add-agent-action-create-run-workflow/test-action.png" alt-text="Screenshot shows Foundry window with Agents playground page, test prompt about London weather with format instructions, and response." lightbox="media/add-agent-action-create-run-workflow/test-action.png":::
245247

248+
:::zone-end
249+
250+
:::zone pivot="python"
251+
252+
## 1: Set the environment variables
253+
254+
Set the following environment variables:
255+
PROJECT_ENDPOINT: The Azure AI Agents endpoint.
256+
MODEL_DEPLOYMENT_NAME: The deployment name of the AI model.
257+
SUBSCRIPTION_ID: Your Azure subscription ID.
258+
resource_group_name: The name of your resource group.
259+
260+
For the full sample that shows integrating an agent in Microsoft Foundry with a Consumption logic app in the Azure portal, see []
261+
262+
https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-agents/samples/agents_tools/sample_agents_logic_apps.py
263+
264+
For the sample code, see the [AzureLogicAppTool utility on GitHub](https://github.com/azure-ai-foundry/foundry-samples/blob/main/samples-classic/python/getting-started-agents/logic_apps/user_logic_apps.py).
265+
266+
267+
## 2: Create a project client
268+
269+
To connect to your Foundry project and other resources, follow these steps to create a client object:
270+
271+
```python
272+
import os
273+
from azure.ai.projects import AIProjectClient
274+
from azure.identity import DefaultAzureCredential
275+
276+
# Initialize the AIProjectClient
277+
project_client = AIProjectClient(
278+
endpoint=os.environ["PROJECT_ENDPOINT"],
279+
credential=DefaultAzureCredential()
280+
)
281+
```
282+
283+
## 3: Register your logic app
284+
285+
Register your Consumption logic app workflow by providing its name and trigger information. For the sample code, see the [AzureLogicAppTool utility on GitHub](https://github.com/azure-ai-foundry/foundry-samples/blob/main/samples-classic/python/getting-started-agents/logic_apps/user_logic_apps.py).
286+
287+
```python
288+
from user_logic_apps import AzureLogicAppTool
289+
290+
# Extract subscription and resource group from environment variables
291+
subscription_id = os.environ["SUBSCRIPTION_ID"]
292+
resource_group = os.environ["resource_group_name"]
293+
294+
# Logic app details
295+
logic_app_name = "<LOGIC_APP_NAME>"
296+
trigger_name = "<TRIGGER_NAME>"
297+
298+
# Create and initialize AzureLogicAppTool utility
299+
logic_app_tool = AzureLogicAppTool(subscription_id, resource_group)
300+
logic_app_tool.register_logic_app(logic_app_name, trigger_name)
301+
print(f"Registered logic app '{logic_app_name}' with trigger '{trigger_name}'.")
302+
```
303+
304+
## 4: Create an agent and connect a logic app workflow as a tool through an action
305+
306+
The following code creates an agent and adds an action that runs a logic app workflow as a tool. For this example, the logic app workflow sends an email.
307+
308+
```python
309+
from azure.ai.agents.models import ToolSet, FunctionTool
310+
from user_functions import fetch_current_datetime
311+
from user_logic_apps import create_send_email_function
312+
313+
# Create the specialized "send_email_via_logic_app" function
314+
send_email_func = create_send_email_function(logic_app_tool, logic_app_name)
315+
316+
# Prepare the function tools for the agent
317+
functions_to_use = {fetch_current_datetime, send_email_func}
318+
319+
# Create an agent
320+
functions = FunctionTool(functions=functions_to_use)
321+
toolset = ToolSet()
322+
toolset.add(functions)
323+
324+
agent = project_client.agents.create_agent(
325+
model=os.environ["MODEL_DEPLOYMENT_NAME"],
326+
name="SendEmailAgent",
327+
instructions="You're a specialized agent for sending emails.",
328+
toolset=toolset,
329+
)
330+
print(f"Created agent, ID: {agent.id}")
331+
```
332+
333+
## 5: Create a thread for communication
334+
335+
The following code creates a thread to commuicate between the project client and your agent.
336+
337+
```python
338+
# Create a thread for communication
339+
thread = project_client.agents.threads.create()
340+
print(f"Created thread, ID: {thread.id}")
341+
342+
# Create a message in the thread
343+
message = project_client.agents.messages.create(
344+
thread_id=thread.id,
345+
role="user",
346+
content="Hello, send an email to <RECIPIENT_EMAIL> with the date and time in '%Y-%m-%d %H:%M:%S' format.",
347+
)
348+
print(f"Created message, ID: {message['id']}")
349+
```
350+
351+
## 6: Test your agent
352+
353+
To test how well the agent performs the task, run the agent, observe how the model uses the logic app tool, and check the output.
354+
355+
```python
356+
# Create and process an agent run in the thread
357+
run = project_client.agents.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
358+
print(f"Run finished with status: {run.status}")
359+
360+
if run.status == "failed":
361+
print(f"Run failed: {run.last_error}")
362+
363+
# Fetch and log all messages
364+
messages = project_client.agents.messages.list(thread_id=thread.id)
365+
for message in messages:
366+
print(f"Role: {message['role']}, Content: {message['content']}")
367+
```
368+
369+
:::zone-end
370+
246371
## Optional: Review underlying logic app and workflow
247372

248373
After the action runs, you can view the underlying logic app resource and workflow in the Azure portal. You can review the workflow's run history to debug or troubleshoot problems that the workflow might encounter.
@@ -343,6 +468,8 @@ For Foundry, see the following resources:
343468

344469
If you don't need the resources that you created for this guide, delete the resources so you don't continue getting charged. You can either follow these steps to delete the resource group that contains these resources, or you can delete each resource individually.
345470

471+
:::zone pivot="portal"
472+
346473
1. In the Foundry portal, to remove the action from the agent, next to the action name, select the ellipses (**...**) button, and then select **Remove**.
347474

348475
1. In the [Azure portal](https://portal.azure.com) title bar search box, enter **resource groups**, and select **Resource groups**.
@@ -353,6 +480,20 @@ If you don't need the resources that you created for this guide, delete the reso
353480

354481
1. When the confirmation pane appears, enter the resource group name, and select **Delete**.
355482

483+
:::zone-end
484+
485+
:::zone-pivot="python"
486+
487+
The following code
488+
489+
```python
490+
# Delete the agent
491+
project_client.agents.delete_agent(agent.id)
492+
print("Deleted agent.")
493+
```
494+
495+
:::zone-end
496+
356497
## Related content
357498

358499
- [What are connectors in Azure Logic Apps?](/azure/connectors/introduction)

0 commit comments

Comments
 (0)