title | description | services | author | ms.service | ms.custom | ms.topic | ms.date | ms.author |
---|---|---|---|---|---|---|---|---|
Tutorial: Use code interpreter sessions in Semantic Kernel with Azure Container Apps |
Learn to use code interpreter sessions in Semantic Kernel on Azure Container Apps. |
container-apps |
anthonychu |
container-apps |
devx-track-azurecli |
tutorial |
05/08/2024 |
antchu |
Semantic Kernel is an open-source AI framework created by Microsoft for .NET, Python, and Java developers working with Large Language Models (LLMs). When you build an AI agent with Semantic Kernel, an LLM interprets user input and generates a response. The AI agent often struggles when it needs to perform mathematical and symbolic reasoning to produce a response. By integrating Azure Container Apps dynamic sessions with Semantic Kernel, you give the agent a code interpreter to use to perform specialized tasks.
In this tutorial, you learn how to run a Semantic Kernel AI agent 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.
Note
Azure Container Apps dynamic sessions is currently in preview. See preview limitations for more information.
[!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/semantic-kernel-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 SessionsPythonTool and provide it to the Semantic Kernel agent:
sessions_tool = SessionsPythonTool(
pool_management_endpoint,
auth_callback=auth_callback_factory("https://dynamicsessions.io/.default"),
)
kernel.add_plugin(sessions_tool, "SessionsTool")
When it needs to perform calculations, the kernel uses the code interpreter in SessionsPythonTool 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 kernel uses the tool to run multiple Python code snippets, it uses the same session. To ensure each end user has a unique session, use a separate kernel and tool for each user.
SessionsPythonTool is available in version 0.9.8b1
or later of the semantic-kernel
package.
[!INCLUDE container-apps/sessions-tutorial-run-local]
[!INCLUDE container-apps/sessions-tutorial-deploy]
[!INCLUDE container-apps/sessions-tutorial-clean-up]
[!div class="nextstepaction"] Code interpreter sessions