> Azure 容器应用动态会话 提供对安全沙箱环境的快速访问,非常适合运行需要与其他工作负载强隔离的代码或应用程序。
您可以在 本页了解更多关于 Azure 容器应用动态会话及其代码解释功能。如果没有 Azure 账户,您可以 创建免费账户 开始使用。
设置
您首先需要安装 @langchain/azure-dynamic-sessions package:
npm install @langchain/azure-dynamic-sessions @langchain/core
您还需要运行代码解释器会话池实例。您可以使用 Azure CLI 参考 本指南.
实例运行后,您需要确保已正确 为其设置了 Azure Entra 身份验证.
添加身份角色后,您需要获取 **会话池管理端点**。您可以在 Azure 门户中,在实例的"概述"部分找到它。然后您需要设置以下环境变量:
AZURE_CONTAINER_APP_SESSION_POOL_MANAGEMENT_ENDPOINT=<your_endpoint>
使用示例
以下是创建一个新的 Python 代码解释器会话、调用工具并打印结果的简单示例。
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 代码解释器会话工具来执行代码并获取结果:
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("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);