Skip to content

Commit 58f4ee1

Browse files
committed
add python agent web app tutorial
1 parent 3b9e8e8 commit 58f4ee1

7 files changed

Lines changed: 275 additions & 5 deletions

File tree

.openpublishing.publish.config.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,12 @@
632632
"branch": "main",
633633
"branch_mapping": {}
634634
},
635+
{
636+
"path_to_root": "app-service-agentic-langgraph-foundry-python",
637+
"url": "https://github.com/Azure-Samples/app-service-agentic-langgraph-foundry-python",
638+
"branch": "main",
639+
"branch_mapping": {}
640+
},
635641
{
636642
"path_to_root": "app-service-agentic-semantic-kernel-java",
637643
"url": "https://github.com/Azure-Samples/app-service-agentic-semantic-kernel-java",
130 KB
Loading
126 KB
Loading

articles/app-service/overview-ai-integration.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Add AI features to your Node.js web applications:
6161
- [Run a chatbot with a local SLM (Express.js)](tutorial-ai-slm-expressjs.md) - Deploy an Express.js application with a local SLM sidecar.
6262
- [Integrate an App Service app as an MCP Server for GitHub Copilot Chat (Node.js)](tutorial-ai-model-context-protocol-server-node.md) - Host an MCP server with Node.js to extend GitHub Copilot Chat capabilities.
6363
- [Add an App Service app as a tool in Azure AI Foundry Agent Service (Node.js)](tutorial-ai-integrate-azure-ai-agent-node.md) - Make your Node.js application available as a tool for AI agents.
64-
- [Build an agentic web app with LangGraph and Azure AI Foundry (Node.js)](tutorial-ai-agent-web-app-langgraph-foundry-node.md) - Create an intelligent agent-based application using Node.js and LangGraph.
64+
- [Build an agentic web app with LangGraph and Azure AI Foundry (Node.js)](tutorial-ai-agent-web-app-langgraph-foundry-node.md) - Create an intelligent agent-based application using Node.js and a choicer of LangGraph or Azure AI Foundry Agent Service.
6565

6666
## Python applications
6767

@@ -70,7 +70,8 @@ Implement AI capabilities in your Python web applications:
7070
- [Build a chatbot with Azure OpenAI (Flask)](tutorial-ai-openai-chatbot-python.md) - Create a Flask application that connects to Azure OpenAI using managed identity.
7171
- [Build a RAG application with Azure OpenAI and Azure AI Search (Python)](tutorial-ai-openai-search-python.md) - Implement RAG with Python.
7272
- [Run a chatbot with a local SLM (FastAPI)](tutorial-ai-slm-fastapi.md) - Deploy a FastAPI application with a local SLM sidecar.
73-
- [Azure AI Foundry tutorial: Deploy an enterprise chat web app](/azure/ai-foundry/tutorials/deploy-chat-web-app?toc=/azure/app-service/toc.json&bc=/azure/bread/toc.json) - Deploy fully integrated AI web app straight from your deployment in Azure AI Foundry.
73+
- [Azure AI Foundry tutorial: Deploy an enterprise chat web app](/azure/ai-foundry/tutorials/deploy-chat-web-app?toc=/azure/app-service/toc.json&bc=/azure/bread/toc.json) - Deploy fully integrated AI web app straight from your deployment in Azure AI Foundry.
74+
- [Build an agentic web app with LangGraph or Azure AI Foundry Agent Service (Python)](tutorial-ai-agent-web-app-langgraph-foundry-python.md) - Create an intelligent agent-based application using Python and a choice of LangGraph or Azure AI Foundry Agent Service.
7475

7576
## Model Context Protocol (MCP) servers
7677

articles/app-service/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ items:
180180
href: tutorial-ai-slm-fastapi.md
181181
- name: Web app from Azure AI Foundry
182182
href: /azure/ai-foundry/tutorials/deploy-chat-web-app?toc=/azure/app-service/toc.json&bc=/azure/bread/toc.json
183+
- name: Agentic web app
184+
href: tutorial-ai-agent-web-app-langgraph-foundry-python.md
183185
- name: PHP
184186
items:
185187
- name: Quickstart

articles/app-service/tutorial-ai-agent-web-app-langgraph-foundry-node.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: Learn how to quickly deploy a production-ready, agentic web applica
44
ms.service: azure-app-service
55
author: cephalin
66
ms.author: cephalin
7-
ms.devlang: csharp
7+
ms.devlang: javascript
88
ms.topic: tutorial
99
ms.date: 06/25/2025
1010
ms.custom:
@@ -72,6 +72,8 @@ The easiest way to get started is by using GitHub Codespaces, which provides a c
7272

7373
5. When you see **Your application running on port 3000 is available**, select **Open in Browser** and add a few tasks.
7474

75+
The agents aren't fully configured so they don't work yet. You'll configure them later.
76+
7577
## Review the agent code
7678
7779
Both approaches use the same implementation pattern, where the agent is initialized on application start, and responds to user messages by POST requests.
@@ -81,7 +83,7 @@ Both approaches use the same implementation pattern, where the agent is initiali
8183
The `LangGraphTaskAgent` is initialized in the constructor in *src/agents/LangGraphTaskAgent.ts*. The initialization code does the following:
8284
8385
- Configures the [AzureChatOpenAI](https://js.langchain.com/docs/integrations/llms/azure/) client using environment variables.
84-
- Creates the prebuilt ReAct agent a set of CRUD tools for task management (see [LangGraph: How to use the prebuilt ReAct agent](https://langchain-ai.github.io/langgraphjs/how-tos/create-react-agent)).
86+
- Creates the prebuilt ReAct agent with a set of CRUD tools for task management (see [LangGraph: How to use the prebuilt ReAct agent](https://langchain-ai.github.io/langgraphjs/how-tos/create-react-agent)).
8587
- Sets up memory management (see [LangGraph: How to add memory to the prebuilt ReAct agent](https://langchain-ai.github.io/langgraphjs/how-tos/react-memory/)).
8688
8789
:::code language="typescript" source="~/app-service-agentic-langgraph-foundry-node/src/agents/LangGraphTaskAgent.ts" range="23-143" highlight="13-21,24-37,106-117" :::
@@ -91,7 +93,7 @@ The `LangGraphTaskAgent` is initialized in the constructor in *src/agents/LangGr
9193
The `FoundryTaskAgent` is initialized in the constructor of *src/agents/FoundryTaskAgent.ts*. The initialization code does the following:
9294
9395
- Creates an `AgentsClient` using Azure credentials.
94-
- Fetches the agent from Azure AI Foundry by name.
96+
- Fetches the agent from Azure AI Foundry by the agent ID.
9597
- Creates a new thread for the session.
9698
9799
:::code language="typescript" source="~/app-service-agentic-langgraph-foundry-node/src/agents/FoundryTaskAgent.ts" range="34-66" highlight="15,18,22" :::
Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
---
2+
title: Agentic app with LangGraph or Azure AI Foundry (Python)
3+
description: Learn how to quickly deploy a production-ready, agentic web application using Python with Azure App Service, LangGraph, and Azure AI Foundry Agent Service.
4+
ms.service: azure-app-service
5+
author: cephalin
6+
ms.author: cephalin
7+
ms.devlang: python
8+
ms.topic: tutorial
9+
ms.date: 08/20/2025
10+
ms.custom:
11+
- devx-track-python
12+
ms.collection: ce-skilling-ai-copilot
13+
---
14+
15+
# Tutorial: Build an agentic web app in Azure App Service with LangGraph or Azure AI Foundry Agent Service (Python)
16+
17+
This tutorial demonstrates how to add agentic capability to an existing data-driven FastAPI CRUD application. It does this using two different approaches: LangGraph and Azure AI Foundry Agent Service.
18+
19+
If your web application already has useful features, like shopping, hotel booking, or data management, it's relatively straightforward to add agent functionality to your web application by wrapping those functionalities in a plugin (for LangGraph) or as an OpenAPI endpoint (for AI Foundry Agent Service). In this tutorial, you start with a simple to-do list app. By the end, you'll be able to create, update, and manage tasks with an agent in an App Service app.
20+
21+
### [LangGraph](#tab/langgraph)
22+
23+
24+
:::image type="content" source="media/tutorial-ai-agent-web-app-langgraph-foundry-python/langgraph-agent.png" alt-text="Screenshot of a chat completion session with a LangGraph agent.":::
25+
26+
### [Azure AI Foundry Agent Service](#tab/aifoundry)
27+
28+
:::image type="content" source="media/tutorial-ai-agent-web-app-langgraph-foundry-python/foundry-agent.png" alt-text="Screenshot of a chat completion session with an Azure AI Foundry agent.":::
29+
30+
-----
31+
32+
Both LangGraph and Azure AI Foundry Agent Service enable you to build agentic web applications with AI-driven capabilities. LangGraph is similar to Microsoft Semantic Kernel and is an SDK. The following table shows some of the considerations and trade-offs:
33+
34+
| Consideration | LangGraph or Semantic Kernel | Azure AI Foundry Agent Service |
35+
|--------------------|-------------------------------|----------------------------------------|
36+
| Performance | Fast (runs locally) | Slower (managed, remote service) |
37+
| Development | Full code, maximum control | Low code, rapid integration |
38+
| Testing | Manual/unit tests in code | Built-in playground for quick testing |
39+
| Scalability | App-managed | Azure-managed, autoscaled |
40+
41+
In this tutorial, you learn how to:
42+
43+
> [!div class="checklist"]
44+
> * Convert existing app functionality into a plugin for LangGraph.
45+
> * Add the plugin to a LangGraph agent and use it in a web app.
46+
> * Convert existing app functionaltiy into an OpenAPI endpoint for Azure AI Foundry Agent Service.
47+
> * Call an Azure AI Foundry agent in a web app.
48+
- Assign the required permissions for managed identity connectivity.
49+
50+
## Prerequisites
51+
52+
- An Azure account with an active subscription - [Create an account for free](https://azure.microsoft.com/free/python).
53+
- GitHub account to use GitHub Codespaces - [Learn more about GitHub Codespaces](https://docs.github.com/codespaces/overview).
54+
55+
## Open the sample with Codespaces
56+
57+
The easiest way to get started is by using GitHub Codespaces, which provides a complete development environment with all required tools preinstalled.
58+
59+
1. Navigate to the GitHub repository at [https://github.com/Azure-Samples/app-service-agentic-langgraph-foundry-python](https://github.com/Azure-Samples/app-service-agentic-langgraph-foundry-python).
60+
61+
2. Select the **Code** button, select the **Codespaces** tab, and select **Create codespace on main**.
62+
63+
3. Wait a few moments for your Codespace to initialize. When ready, you'll see a fully configured development environment in your browser.
64+
65+
4. Run the application locally:
66+
67+
```bash
68+
python3 -m venv venv
69+
source venv/bin/activate
70+
pip install -r requirements.txt
71+
uvicorn src.app:app --host 0.0.0.0 --port 3000
72+
```
73+
74+
5. When you see **Your application running on port 3000 is available**, select **Open in Browser** and add a few tasks.
75+
76+
The agents aren't fully configured so they don't work yet. You'll configure them later.
77+
78+
## Review the agent code
79+
80+
Both approaches use the same implementation pattern, where the agent is initialized on application start, and responds to user messages by POST requests.
81+
82+
### [LangGraph](#tab/langgraph)
83+
84+
The `LangGraphTaskAgent` is initialized in the constructor in *src/agents/LangGraphTaskAgent.ts*. The initialization code does the following:
85+
86+
- Configures the [AzureChatOpenAI](https://python.langchain.com/docs/integrations/chat/azure_chat_openai/) client using environment variables.
87+
- Creates the prebuilt ReAct agent with memory and a set of CRUD tools for task management (see [LangGraph quickstart¶](https://langchain-ai.github.io/langgraph/agents/agents)).
88+
89+
:::code language="typescript" source="~/app-service-agentic-langgraph-foundry-python/src/agents/langgraph_task_agent.py" range="58-82" :::
90+
91+
### [Azure AI Foundry Agent Service](#tab/aifoundry)
92+
93+
The `FoundryTaskAgent` is initialized in the constructor of *src/agents/foundry_task_agent.py*. The initialization code does the following:
94+
95+
- Creates an `AIProjectClient` using Azure credentials.
96+
- Fetches the agent from Azure AI Foundry by the agent ID.
97+
- Creates a new thread for the session.
98+
99+
:::code language="typescript" source="~/app-service-agentic-langgraph-foundry-python/src/agents/foundry_task_agent.py" range="39-48" :::
100+
101+
This initialization code doesn't define any functionality for the agent, because you would typically build the agent in the Azure AI Foundry portal. As part of the example scenario, it also follows the OpenAPI pattern shown in [Add an App Service app as a tool in Azure AI Foundry Agent Service (Python)](tutorial-ai-integrate-azure-ai-agent-python.md), and makes its CRUD functionality available as an OpenAPI endpoint. This lets you add it to the agent later as a callable tool.
102+
103+
The OpenAPI code is defined in *src/routes/api.py*. For example, the "GET /tasks" route defines a custom `operation_id` in the ourter parmeter, as required by the [OpenAPI spec tool in Azure AI Foundry](/azure/ai-foundry/agents/how-to/tools/openapi-spec#prerequisites), and `description` helps the agent determine how to call the API:
104+
105+
:::code language="csharp" source="~/app-service-agentic-langgraph-foundry-python/src/routes/api.py" range="27-42" highlight="4-5" :::
106+
107+
-----
108+
109+
## Deploy the sample application
110+
111+
The sample repository contains an Azure Developer CLI (AZD) template, which creates an App Service app with managed identity and deploys your sample application.
112+
113+
1. In the terminal, log into Azure using Azure Developer CLI:
114+
115+
```bash
116+
azd auth login
117+
```
118+
119+
Follow the instructions to complete the authentication process.
120+
121+
4. Deploy the Azure App Service app with the AZD template:
122+
123+
```bash
124+
azd up
125+
```
126+
127+
1. When prompted, give the following answers:
128+
129+
|Question |Answer |
130+
|---------|---------|
131+
|Enter a new environment name: | Type a unique name. |
132+
|Select an Azure Subscription to use: | Select the subscription. |
133+
|Pick a resource group to use: | Select **Create a new resource group**. |
134+
|Select a location to create the resource group in:| Select any region. The resources will actually be created in **East US 2**.|
135+
|Enter a name for the new resource group:| Type **Enter**.|
136+
137+
1. In the AZD output, find the URL of your app and navigate to it in the browser. The URL looks like this in the AZD output:
138+
139+
<pre>
140+
Deploying services (azd deploy)
141+
142+
(✓) Done: Deploying service web
143+
- Endpoint: &lt;URL>
144+
</pre>
145+
146+
1. Select the **OpenAPI schema** item to open the autogenerated OpenAPI schema at the default `/openapi.json` path. You need this schema later.
147+
148+
1. After successful deployment, you'll see a URL for your deployed application.
149+
150+
You now have an App Service app with a system-assigned managed identity.
151+
152+
## Create and configure the Azure AI Foundry resource
153+
154+
1. In the [Azure AI Foundry portal](https://ai.azure.com), deploy a model of your choice (see [Quickstart: Get started with Azure AI Foundry](/azure/ai-foundry/quickstarts/get-started-code?tabs=azure-ai-foundry&pivots=fdp-project)). A project and a default agent are created for you in the process.
155+
156+
1. From the left menu, select **Overview**.
157+
158+
1. Select **Azure AI Foundry** and copy the URL in **Azure AI Foundry project endpoint**.
159+
160+
1. Select **Azure OpenAI** and copy the URL in **Azure OpenAI endpoint** for later.
161+
162+
:::image type="content" source="media/tutorial-ai-agent-web-app-semantic-kernel-foundry-dotnet/foundry-project-endpoints.png" alt-text="Screenshot showing how to copy the OpenAI endpoint and the foundry project endpoint in the foundry portal.":::
163+
164+
1. From the left menu, select **Agents**, then select the default agent.
165+
166+
1. From the **Setup** pane, copy **Agent ID**, as well as the model name in **Deployment**.
167+
168+
:::image type="content" source="media/tutorial-ai-agent-web-app-semantic-kernel-foundry-dotnet/foundry-agent-id-model.png" alt-text="Screenshot showing how to copy the agent ID and the model deployment name in the foundry portal.":::
169+
170+
1. In the **Setup** pane, add an action with the OpenAPI spec tool. Use the OpenAPI schema that you get from the deployed web app and **anonymous** authentication. For detailed steps, see [How to use the OpenAPI spec tool](/azure/ai-foundry/agents/how-to/tools/openapi-spec-samples?pivots=portal).
171+
172+
Your application code is already configured to include the server's `url` and `operationId`, which are needed by the agent. For more information, see [How to use Azure AI Foundry Agent Service with OpenAPI Specified Tools: Prerequisites](/azure/ai-foundry/agents/how-to/tools/openapi-spec#prerequisites).
173+
174+
1. Select **Try in playground** and test your AI Foundry agent with prompts like "*Show me all the tasks*."
175+
176+
If you get a valid response, the agent is making tool calls to the OpenAPI endpoint on your deployed web app.
177+
178+
## Assign required permissions
179+
180+
1. At the upper right corner of the foundry portal, select the name of the resource, then select **Resource Group** to open it in the Azure portal.
181+
182+
:::image type="content" source="media/tutorial-ai-agent-web-app-semantic-kernel-foundry-dotnet/go-to-azure-portal.png" alt-text="Screenshot showing how to quickly go to the resource group view for the foundry resource in the Azure portal.":::
183+
184+
1. Add a role for each of the two resources for the App Service app's manage identity using the following table:
185+
186+
| Target resource | Required role | Needed for |
187+
|--------------------------------|-------------------------------------|-------------------------|
188+
| Azure AI Foundry | Cognitive Services OpenAI User | The chat completion service in the LangGraph. |
189+
| Azure AI Foundry Project | Azure AI User | Reading and calling the AI Foundry agent. |
190+
191+
For instructions, see [Assign Azure roles using the Azure portal](/azure/role-based-access-control/role-assignments-portal).
192+
193+
## Configure connection variables in your sample application
194+
195+
1. Open *.env*. Using the values you copied earlier from the AI Foundry portal, configure the following variables:
196+
197+
| Variable | Description |
198+
|-------------------------------|----------------------------------------------------------|
199+
| `AZURE_OPENAI_ENDPOINT` | Azure OpenAI endpoint (copied from the Overview page). This is needed by the LangGraph agent. |
200+
| `AZURE_OPENAI_DEPLOYMENT_NAME` | Model name in the deployment (copied from the Agents setup pane). This is needed by the LangGraph agent. |
201+
| `AZURE_AI_FOUNDRY_PROJECT_ENDPOINT` | Azure AI Foundry project endpoint (copied from Overview page). This is needed for the Azure AI Foundry Agent Service. |
202+
| `AZURE_AI_FOUNDRY_AGENT_ID` | Agent ID (copied from the Agents setup pane). This is needed to invoke an existing Azure AI Foundry agent. |
203+
204+
> [!NOTE]
205+
> To keep the tutorial simple, you'll use these variables in *.env* instead of overwriting them with app settings in App Service.
206+
207+
1. Sign in to Azure with the Azure CLI:
208+
209+
```bash
210+
az login
211+
```
212+
213+
This allows the Azure Identity client library in the sample code to receive an authentication token for the logged in user. Remember that you added the required role for this user earlier.
214+
215+
1. Run the application locally:
216+
217+
```bash
218+
npm run build
219+
npm start
220+
```
221+
222+
1. When you see **Your application running on port 3000 is available**, select **Open in Browser**.
223+
224+
1. Select the **LangGraph Agent** link and the **Foundry Agent** link to try out the chat interface. If you get a response, your application is connecting successfully to the Azure AI Foundry resource.
225+
226+
1. Back in the GitHub codespace, deploy your app changes.
227+
228+
```bash
229+
azd up
230+
```
231+
232+
1. Navigate to the deployed application again and test the chat agents.
233+
234+
### [LangGraph](#tab/langgraph)
235+
236+
237+
:::image type="content" source="media/tutorial-ai-agent-web-app-langgraph-foundry-python/langgraph-agent.png" alt-text="Screenshot of a chat completion session with a LangGraph agent.":::
238+
239+
### [Azure AI Foundry Agent Service](#tab/aifoundry)
240+
241+
:::image type="content" source="media/tutorial-ai-agent-web-app-langgraph-foundry-python/foundry-agent.png" alt-text="Screenshot of a chat completion session with an Azure AI Foundry agent.":::
242+
243+
-----
244+
245+
## Clean up resources
246+
247+
When you're done with the application, you can delete the App Service resources to avoid incurring further costs:
248+
249+
```bash
250+
azd down --purge
251+
```
252+
253+
Since the AZD template doesn't include the Azure AI Foundry resources, you need to delete them manually if you want.
254+
255+
## More resources
256+
257+
- [Integrate AI into your Azure App Service applications](overview-ai-integration.md)
258+
- [What is Azure AI Foundry Agent Service?](/azure/ai-foundry/agents/overview)
259+
- [LangGraph - Quickstart](https://langchain-ai.github.io/langgraph/agents/agents/)

0 commit comments

Comments
 (0)