Skip to main content

Azure Container Apps Dynamic Sessions

Azure Container Apps dynamic sessions provide fast access to secure sandboxed environments that are ideal for running code or applications that require strong isolation from other workloads.

You can learn more about Azure Container Apps dynamic sessions and its code interpretation capabilities on this page. If you don't have an Azure account, you can create a free account to get started.

Setup

You'll first need to install the @langchain/azure-dynamic-sessions package:

npm install @langchain/azure-dynamic-sessions

You'll also need to have a code interpreter session pool instance running. You can deploy a version using Azure CLI following this guide.

Once you have your instance running, you need to make sure you have properly set up the Azure Entra authentication for it. You can find the instructions on how to do that here.

After you've added the role for your identity, you need to retrieve the session pool management endpoint. You can find it in the Azure Portal, under the "Overview" section of your instance. Then you need to set the following environment variable:

AZURE_CONTAINER_APP_SESSION_POOL_MANAGEMENT_ENDPOINT=<your_endpoint>

API Reference:

    Usage example

    Below is a simple example that creates a new Python code interpreter session, invoke the tool and prints the result.

    import { SessionsPythonREPLTool } from "@langchain/azure-dynamic-sessions";

    const tool = new SessionsPythonREPLTool({
    poolManagementEndpoint:
    process.env.AZURE_CONTAINER_APP_SESSION_POOL_MANAGEMENT_ENDPOINT || "",
    });

    const result = await tool.invoke("print('Hello, World!')\n1+2");

    console.log(result);

    // {
    // stdout: "Hello, World!\n",
    // stderr: "",
    // result: 3,
    // }

    API Reference:

    Here is a complete example where we use an Azure OpenAI chat model to call the Python code interpreter session tool to execute the code and get the result:

    import type { ChatPromptTemplate } from "@langchain/core/prompts";
    import { pull } from "langchain/hub";
    import { AgentExecutor, createOpenAIFunctionsAgent } from "langchain/agents";
    import { SessionsPythonREPLTool } from "@langchain/azure-dynamic-sessions";
    import { AzureChatOpenAI } from "@langchain/openai";

    const tools = [
    new SessionsPythonREPLTool({
    poolManagementEndpoint:
    process.env.AZURE_CONTAINER_APP_SESSION_POOL_MANAGEMENT_ENDPOINT || "",
    }),
    ];

    // Note: you need a model deployment that supports function calling,
    // like `gpt-35-turbo` version `1106`.
    const llm = new AzureChatOpenAI({
    temperature: 0,
    });

    // Get the prompt to use - you can modify this!
    // If you want to see the prompt in full, you can at:
    // https://smith.langchain.com/hub/hwchase17/openai-functions-agent
    const prompt = await pull<ChatPromptTemplate>(
    "hwchase17/openai-functions-agent"
    );

    const agent = await createOpenAIFunctionsAgent({
    llm,
    tools,
    prompt,
    });

    const agentExecutor = new AgentExecutor({
    agent,
    tools,
    });

    const result = await agentExecutor.invoke({
    input:
    "Create a Python program that prints the Python version and return the result.",
    });

    console.log(result);

    API Reference:


    Was this page helpful?


    You can leave detailed feedback on GitHub.