Skip to content

Commit 31600b6

Browse files
authored
Merge pull request #275830 from thegovind/main
add autogen tutorial for sessions
2 parents 1de048f + 594d884 commit 31600b6

8 files changed

+265
-4
lines changed

articles/container-apps/TOC.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@
134134
items:
135135
- name: Code interpreter sessions
136136
items:
137+
- name: AutoGen
138+
href: sessions-tutorial-autogen.md
137139
- name: LangChain
138140
href: sessions-tutorial-langchain.md
139141
- name: LlamaIndex

articles/container-apps/sessions-code-interpreter.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ To create a code interpreter session pool using the Azure CLI, ensure you have t
4545
az upgrade
4646

4747
# Install or upgrade the Azure Container Apps extension
48-
az extension add --name containerapp --upgrade --allow-preview -y
48+
az extension add --name containerapp --upgrade --allow-preview true -y
4949
```
5050

5151
Use the `az containerapps sessionpool create` command to create the pool. The following example creates a Python code interpreter session pool named `my-session-pool`. Make sure to replace `<RESOURCE_GROUP>` with your resource group name before you run the command.

articles/container-apps/sessions-custom-container.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ To create a custom container session pool using the Azure CLI, ensure you have t
4242

4343
```bash
4444
az upgrade
45-
az extension add --name containerapp --upgrade --allow-preview -y
45+
az extension add --name containerapp --upgrade --allow-preview true -y
4646
```
4747

4848
Custom container session pools require a workload profile enabled Azure Container Apps environment. If you don't have an environment, use the `az containerapp env create -n <ENVIRONMENT_NAME> -g <RESOURCE_GROUP> --location <LOCATION> --enable-workload-profiles` command to create one.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
title: "Tutorial: Use code interpreter sessions in AutoGen with Azure Container Apps"
3+
description: Learn to use code interpreter sessions in AutoGen on Azure Container Apps.
4+
services: container-apps
5+
author: thegovind
6+
ms.service: container-apps
7+
ms.topic: tutorial
8+
ms.date: 05/15/2024
9+
ms.author: gok
10+
---
11+
12+
# Tutorial: Use code interpreter sessions in AutoGen with Azure Container Apps
13+
14+
[AutoGen](https://microsoft.github.io/autogen/) is a framework for developing large language model (LLM) applications using multiple agents that converse with each other to solve tasks. Agents built with AutoGen can operate in various modes that employ combinations of LLMs, human inputs, and tools. One important type of tool for AutoGen agents is code executors. They enable agents to perform complex tasks by writing and executing code. By integrating Azure Container Apps dynamic sessions with AutoGen, you give the agent a [code interpreter](sessions-code-interpreter.md) to use to perform useful computations and take actions.
15+
16+
In this tutorial, you learn how to run an AI agent authored in AutoGen in a web API. The API accepts user input and returns a response generated by the AI agent. The agent uses a code interpreter in dynamic sessions to perform calculations.
17+
18+
> [!NOTE]
19+
> Azure Container Apps dynamic sessions is currently in preview. See [preview limitations](./sessions.md#preview-limitations) for more information.
20+
21+
[!INCLUDE [sessions-tutorial-prerequisites](../../includes/container-apps/sessions-tutorial-prerequisites.md)]
22+
23+
## Run the sample app locally
24+
25+
Before you deploy the app to Azure Container Apps, you can run it locally to test it.
26+
27+
### Clone the app
28+
29+
1. Clone the [Azure Container Apps sessions samples repository](https://github.com/Azure-Samples/container-apps-dynamic-sessions-samples).
30+
31+
```bash
32+
git clone https://github.com/Azure-Samples/container-apps-dynamic-sessions-samples.git
33+
```
34+
35+
1. Change to the directory that contains the sample app:
36+
37+
```bash
38+
cd container-apps-dynamic-sessions-samples/autogen-python-webapi
39+
```
40+
41+
[!INCLUDE [container-apps/sessions-tutorial-configure-local](../../includes/container-apps/sessions-tutorial-configure-local-autogen.md)]
42+
43+
### Run the app
44+
45+
Before running the sample app, open [*main.py*](https://github.com/Azure-Samples/container-apps-dynamic-sessions-samples/blob/main/autogen-python-webapi/main.py) in an editor and review the code. The app uses FastAPI to create a web API that accepts a user message in the query string.
46+
47+
The following lines of code instantiate a *ACASessionsExecutor* and provides it to the autogen agent:
48+
49+
```python
50+
aca_sessions_executor = ACASessionsExecutor(aca_pool_management_endpoint)
51+
code_executor_agent = ConversableAgent(
52+
name="CodeExecutor",
53+
llm_config=False,
54+
code_execution_config={"executor": aca_sessions_executor},
55+
human_input_mode="NEVER",
56+
is_termination_msg=lambda msg: "TERMINATE" in msg.get("content", "").strip().upper()
57+
)
58+
```
59+
60+
When it needs to perform calculations and tasks, the agent uses the code interpreter in *ACASessionsExecutor* to run the code. The code is executed in a session in the session pool. By default, a random session identifier is generated when you instantiate the tool. If the agent uses the same tool to run multiple Python code snippets, it uses the same session. To ensure each end user has a unique session, use a separate agent and tool for each user.
61+
62+
*ACASessionsExecutor* is implemented in [*aca_sessions_executor.py*](https://github.com/Azure-Samples/container-apps-dynamic-sessions-samples/blob/main/autogen-python-webapi/aca_sessions_executor.py).
63+
64+
[!INCLUDE [container-apps/sessions-tutorial-run-local](../../includes/container-apps/sessions-tutorial-run-local.md)]
65+
66+
[!INCLUDE [container-apps/sessions-tutorial-deploy-autogen](../../includes/container-apps/sessions-tutorial-deploy-autogen.md)]
67+
68+
[!INCLUDE [container-apps/sessions-tutorial-clean-up](../../includes/container-apps/sessions-tutorial-clean-up.md)]
69+
70+
## Next steps
71+
72+
> [!div class="nextstepaction"]
73+
> [Code interpreter sessions](./sessions-code-interpreter.md)
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
---
2+
author: anthonychu
3+
ms.service: container-apps
4+
ms.topic: include
5+
ms.date: 05/08/2024
6+
ms.author: antchu
7+
---
8+
9+
### Configure the app
10+
11+
1. Create a Python virtual environment and activate it:
12+
13+
```bash
14+
python3.11 -m venv .venv
15+
source .venv/bin/activate
16+
```
17+
18+
Change the Python version in the command if you're using a different version. It's recommended to use Python 3.10 or later.
19+
20+
> [!NOTE]
21+
> If you're using Windows, replace `.venv/bin/activate` with `.venv\Scripts\activate`.
22+
23+
1. Install the required Python packages:
24+
25+
```bash
26+
python -m pip install -r requirements.txt
27+
```
28+
29+
1. To run the app, you need to configure environment variables.
30+
31+
1. Retrieve the Azure OpenAI account endpoint:
32+
33+
```bash
34+
az cognitiveservices account show \
35+
--name $AZURE_OPENAI_NAME \
36+
--resource-group $RESOURCE_GROUP_NAME \
37+
--query properties.endpoint \
38+
--output tsv
39+
```
40+
41+
1. Retrieve the Azure OpenAI API key:
42+
43+
```bash
44+
az cognitiveservices account keys list \
45+
--name $AZURE_OPENAI_NAME \
46+
--resource-group $RESOURCE_GROUP_NAME \
47+
--query key1 \
48+
--output tsv
49+
```
50+
51+
1. Retrieve the Azure Container Apps session pool management endpoint:
52+
53+
```bash
54+
az containerapp sessionpool show \
55+
--name $SESSION_POOL_NAME \
56+
--resource-group $RESOURCE_GROUP_NAME \
57+
--query properties.poolManagementEndpoint \
58+
--output tsv
59+
```
60+
61+
1. Create a `.env` file in the root of the sample app directory (same location as `main.py`). Add the following content to the file:
62+
63+
```text
64+
OAI_CONFIG_LIST=[{"model": "gpt-4", "api_key": "<AZURE_OPENAI_KEY>", "api_type": "azure", "base_url": "<AZURE_OPENAI_ENDPOINT>", "api_version": "2023-12-01-preview"}]
65+
POOL_MANAGEMENT_ENDPOINT=<SESSION_POOL_MANAGEMENT_ENDPOINT>
66+
```
67+
68+
Replace `<AZURE_OPENAI_ENDPOINT>` with the Azure OpenAI account endpoint, `<AZURE_OPENAI_KEY>` with the Azure OpenAI API key, and `<SESSION_POOL_MANAGEMENT_ENDPOINT>` with the session pool management endpoint.
69+
70+
1. The app uses `DefaultAzureCredential` to authenticate with Azure services. On your local machine, it uses your current Azure CLI login credentials. You must give yourself the *Azure ContainerApps Session Executor* role on the session pool for the app to access the session pool.
71+
72+
1. Retrieve your Azure CLI user name:
73+
74+
```bash
75+
az account show --query user.name --output tsv
76+
```
77+
78+
1. Run the following commands to retrieve the session pool resource ID:
79+
80+
```bash
81+
az containerapp sessionpool show --name $SESSION_POOL_NAME --resource-group $RESOURCE_GROUP_NAME --query id --output tsv
82+
```
83+
84+
1. Assign the *Azure ContainerApps Session Executor* role to your Azure CLI user on the session pool:
85+
86+
```bash
87+
az role assignment create \
88+
--role "Azure ContainerApps Session Executor" \
89+
--assignee <CLI_USERNAME> \
90+
--scope <SESSION_POOL_RESOURCE_ID>
91+
```
92+
93+
Replace `<CLI_USERNAME>` with your Azure CLI user name and `<SESSION_POOL_RESOURCE_ID>` with the session pool resource ID.

includes/container-apps/sessions-tutorial-configure-local.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ ms.author: antchu
4848
--output tsv
4949
```
5050
51-
1. Create a `.env` file in the root of the sample app directory (`langchain-python-webapi`). Add the following content to the file:
51+
1. Create a `.env` file in the root of the sample app directory (same location as `main.py`). Add the following content to the file:
5252
5353
```text
5454
AZURE_OPENAI_ENDPOINT=<AZURE_OPENAI_ENDPOINT>
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
---
2+
author: anthonychu
3+
ms.service: container-apps
4+
ms.topic: include
5+
ms.date: 05/08/2024
6+
ms.author: antchu
7+
---
8+
9+
## Optional: Deploy the sample app to Azure Container Apps
10+
11+
To deploy the FastAPI app to Azure Container Apps, you need to create a container image and push it to a container registry. Then you can deploy the image to Azure Container Apps. The `az containerapp up` command combines these steps into a single command.
12+
13+
You then need to configure managed identity for the app and assign it the proper roles to access Azure OpenAI and the session pool.
14+
15+
1. Set the variables for the Container Apps environment and app name:
16+
17+
```bash
18+
ENVIRONMENT_NAME=aca-sessions-tutorial-env
19+
CONTAINER_APP_NAME=chat-api
20+
```
21+
22+
1. Build and deploy the app to Azure Container Apps:
23+
24+
```bash
25+
az containerapp up \
26+
--name $CONTAINER_APP_NAME \
27+
--resource-group $RESOURCE_GROUP_NAME \
28+
--location $SESSION_POOL_LOCATION \
29+
--environment $ENVIRONMENT_NAME \
30+
--env-vars 'OAI_CONFIG_LIST=[{"model": "gpt-4", "api_key": "<AZURE_OPENAI_KEY>", "api_type": "azure", "base_url": "<AZURE_OPENAI_ENDPOINT>", "api_version": "2023-12-01-preview"}]' 'POOL_MANAGEMENT_ENDPOINT=<SESSION_POOL_MANAGMENT_ENDPOINT>' \
31+
--source .
32+
```
33+
34+
Replace `<AZURE_OPENAI_ENDPOINT>` with the Azure OpenAI account endpoint, `<AZURE_OPENAI_KEY>` with the Azure OpenAI key, and `<SESSION_POOL_MANAGMENT_ENDPOINT>` with the session pool management endpoint.
35+
36+
1. Enable the system-assigned managed identity for the app:
37+
38+
```bash
39+
az containerapp identity assign \
40+
--name $CONTAINER_APP_NAME \
41+
--resource-group $RESOURCE_GROUP_NAME \
42+
--system-assigned
43+
```
44+
45+
1. For the app to access the session pool, you need to assign the managed identity the proper roles.
46+
47+
1. Retrieve the managed identity's principal ID:
48+
49+
```bash
50+
az containerapp show \
51+
--name $CONTAINER_APP_NAME \
52+
--resource-group $RESOURCE_GROUP_NAME \
53+
--query identity.principalId \
54+
--output tsv
55+
```
56+
57+
1. Retrieve the session pool resource ID:
58+
59+
```bash
60+
az containerapp sessionpool show \
61+
--name $SESSION_POOL_NAME \
62+
--resource-group $RESOURCE_GROUP_NAME \
63+
--query id \
64+
--output tsv
65+
```
66+
67+
1. Assign the managed identity the `Azure ContainerApps Session Executor` and `Contributor` roles on the session pool:
68+
69+
Before you run the following command, replace `<PRINCIPAL_ID>` and `<SESSION_POOL_RESOURCE_ID>` with the values you retrieved in the previous steps.
70+
71+
```bash
72+
az role assignment create \
73+
--role "Azure ContainerApps Session Executor" \
74+
--assignee <PRINCIPAL_ID> \
75+
--scope <SESSION_POOL_RESOURCE_ID>
76+
77+
az role assignment create \
78+
--role "Contributor" \
79+
--assignee <PRINCIPAL_ID> \
80+
--scope <SESSION_POOL_RESOURCE_ID>
81+
```
82+
83+
1. Retrieve the app's fully qualified domain name (FQDN):
84+
85+
```bash
86+
az containerapp show \
87+
--name $CONTAINER_APP_NAME \
88+
--resource-group $RESOURCE_GROUP_NAME \
89+
--query properties.configuration.ingress.fqdn \
90+
--output tsv
91+
```
92+
93+
1. Open the browser to `https://<FQDN>/docs` to test the deployed app.

includes/container-apps/sessions-tutorial-prerequisites.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ The sample app in this quickstart uses an LLM from Azure OpenAI. It also uses Az
2929
```bash
3030
az extension remove --name containerapp
3131
az extension add \
32-
--source https://acacli.blob.core.windows.net/sessionspreview/containerapp-0.3.50-py2.py3-none-any.whl \
32+
--name containerapp \
3333
--allow-preview true -y
3434
```
3535

0 commit comments

Comments
 (0)