| title | Tutorial: Use code interpreter sessions in AutoGen with Azure Container Apps | |
|---|---|---|
| description | Learn to use code interpreter sessions in AutoGen on Azure Container Apps. | |
| services | container-apps | |
| author | thegovind | |
| ms.service | azure-container-apps | |
| ms.custom |
|
|
| ms.topic | tutorial | |
| ms.date | 10/11/2024 | |
| ms.author | gok |
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 to use to perform useful computations and take actions.
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.
[!INCLUDE sessions-tutorial-prerequisites]
Before you deploy the app to Azure Container Apps, you can run it locally to test it.
-
Clone the Azure Container Apps sessions samples repository.
git clone https://github.com/Azure-Samples/container-apps-dynamic-sessions-samples.git
-
Change to the directory that contains the sample app:
cd container-apps-dynamic-sessions-samples/autogen-python-webapi
[!INCLUDE container-apps/sessions-tutorial-configure-local]
Before running the sample app, open 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.
The following lines of code instantiate a ACASessionsExecutor and provides it to the autogen agent:
aca_sessions_executor = ACASessionsExecutor(aca_pool_management_endpoint)
code_executor_agent = ConversableAgent(
name="CodeExecutor",
llm_config=False,
code_execution_config={"executor": aca_sessions_executor},
human_input_mode="NEVER",
is_termination_msg=lambda msg: "TERMINATE" in msg.get("content", "").strip().upper()
)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.
ACASessionsExecutor is implemented in aca_sessions_executor.py.
[!INCLUDE container-apps/sessions-tutorial-run-local]
[!INCLUDE container-apps/sessions-tutorial-deploy-autogen]
[!INCLUDE container-apps/sessions-tutorial-clean-up]
[!div class="nextstepaction"] Code interpreter sessions