Skip to content

Latest commit

 

History

History
68 lines (45 loc) · 3.66 KB

File metadata and controls

68 lines (45 loc) · 3.66 KB
title Tutorial: Use code interpreter sessions in Semantic Kernel with Azure Container Apps
description Learn to use code interpreter sessions in Semantic Kernel on Azure Container Apps.
services container-apps
author anthonychu
ms.service azure-container-apps
ms.custom devx-track-azurecli, ignite-2024
ms.topic tutorial
ms.date 10/11/2024
ms.author antchu

Tutorial: Use code interpreter sessions in Semantic Kernel with Azure Container Apps

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.

[!INCLUDE sessions-tutorial-prerequisites]

Run the sample app locally

Before you deploy the app to Azure Container Apps, you can run it locally to test it.

Clone the app

  1. Clone the Azure Container Apps sessions samples repository.

    git clone https://github.com/Azure-Samples/container-apps-dynamic-sessions-samples.git
  2. 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]

Run the app

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]

Next steps

[!div class="nextstepaction"] Code interpreter sessions