| title | Tutorial: Use code interpreter sessions in LangChain with Azure Container Apps |
|---|---|
| description | Learn to use code interpreter sessions in LangChain 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 |
LangChain is a framework designed to simplify the creation of applications using large language models (LLMs). When you build an AI agent with LangChain, 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 LangChain, you give the agent a code interpreter to use to perform specialized tasks.
In this tutorial, you learn how to run a LangChain 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]
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/langchain-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 SessionPythonREPLTool and provide it to the LangChain agent:
repl = SessionsPythonREPLTool(pool_management_endpoint=pool_management_endpoint)
tools = [repl]
prompt = hub.pull("hwchase17/openai-functions-agent")
agent = agents.create_tool_calling_agent(llm, tools, prompt)When it needs to perform calculations, the agent uses the SessionPythonREPLTool 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 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.
SessionPythonREPLTool is available in the langchain-azure-dynamic-sessions 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