跳到主要内容
Azure 容器应用动态会话提供对安全沙盒环境的快速访问,非常适合运行需要与其他工作负载强隔离的代码或应用程序。
您可以在此页面上了解有关 Azure 容器应用动态会话及其代码解释功能的更多信息。如果您没有 Azure 帐户,可以创建一个免费帐户以开始使用。

设置

您首先需要安装@langchain/azure-dynamic-sessions
有关安装 LangChain 软件包的一般说明,请参阅此部分
npm
npm install @langchain/azure-dynamic-sessions @langchain/core
您还需要运行一个代码解释器会话池实例。您可以按照此指南使用Azure CLI部署一个版本。 一旦您的实例正在运行,您需要确保已为其正确设置 Azure Entra 身份验证。您可以在此处找到如何执行此操作的说明。 添加身份角色后,您需要检索会话池管理端点。您可以在 Azure 门户中,在实例的“概述”部分找到它。然后您需要设置以下环境变量:
.env 示例
AZURE_CONTAINER_APP_SESSION_POOL_MANAGEMENT_ENDPOINT=<your_endpoint>

使用示例

下面是一个简单的示例,它创建一个新的 Python 代码解释器会话,调用工具并打印结果。
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 代码解释器会话工具来执行代码并获取结果
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);

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.