> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-docs-zh.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Azure容器应用动态会话集成

> 使用 LangChain 集成 Azure 容器应用动态会话工具。

> [Azure Container Apps 动态会话](https://learn.microsoft.com/azure/container-apps/sessions) 提供快速访问安全的隔离沙箱环境，非常适合运行需要与其他工作负载强隔离的代码或应用程序。

您可以在 [此页面](https://learn.microsoft.com/azure/container-apps/sessions) 上了解更多关于 Azure Container Apps 动态会话及其代码解释能力的信息。如果您还没有 Azure 账户，可以 [创建免费账户](https://azure.microsoft.com/free/) 开始使用。

## 设置

您首先需要安装 [`@langchain/azure-dynamic-sessions`](https://www.npmjs.com/package/@langchain/azure-dynamic-sessions) 包：

> [!TIP]
>
> 请参阅 [有关安装 LangChain 包的通用说明部分](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/install.md)。

```bash npm theme={null}
npm install @langchain/azure-dynamic-sessions @langchain/core
```

您还需要运行一个代码解释器会话池实例。您可以使用 [Azure CLI](https://learn.microsoft.com/cli/azure/install-azure-cli) 按照 [此指南](https://learn.microsoft.com/azure/container-apps/sessions-code-interpreter) 部署一个版本。

一旦您有了运行中的实例，您需要确保您已正确地为其 [设置 Azure Entra 身份验证](https://learn.microsoft.com/azure/container-apps/sessions?tabs=azure-cli#authentication)。

在您为您的身份添加角色后，您需要检索 **会话池管理端点**。您可以在 Azure 门户中找到它，位于您的实例的“概述”部分。然后您需要设置以下环境变量：

```bash .env example theme={null}
AZURE_CONTAINER_APP_SESSION_POOL_MANAGEMENT_ENDPOINT=<your_endpoint>
```

## 使用示例

以下是一个简单的示例，它创建一个新的 Python 代码解释器会话，调用工具并打印结果。

```typescript theme={null}
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,
// }
```

这是一个完整的示例，其中我们使用 Azure OpenAI 对话模型调用 Python 代码解释器会话工具来执行代码并获取结果：

```typescript theme={null}
import type { ChatPromptTemplate } from "@langchain/core/prompts";
import { pull } from "@langchain/classic/hub";
import { AgentExecutor, createToolCallingAgent } from "@langchain/classic/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/jacob/tool-calling-agent
const prompt = await pull<ChatPromptTemplate>("jacob/tool-calling-agent");

const agent = await createToolCallingAgent({
  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);
```

## 相关

* 工具 [概念指南](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/tools.md)
* 工具 [操作指南](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/tools.md)

***

> 官方原文：[src/oss/javascript/integrations/tools/azure\_dynamic\_sessions.mdx](https://github.com/langchain-ai/docs/blob/main/src/oss/javascript/integrations/tools/azure_dynamic_sessions.mdx) · 非官方翻译，译文由 AI 生成，以英文原文为准。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.