Azure 容器应用动态会话
¥Azure Container Apps Dynamic Sessions
Azure 容器应用动态会话 提供对安全沙盒环境的快速访问,非常适合运行需要与其他工作负载严格隔离的代码或应用。
¥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.
你可以在 此页面 上了解有关 Azure 容器应用动态会话及其代码解释功能的更多信息。如果你没有 Azure 账户,可以使用 创建免费账户 开始使用。
¥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
你首先需要安装 @langchain/azure-dynamic-sessions
软件包:
¥You'll first need to install the @langchain/azure-dynamic-sessions
package:
- npm
- Yarn
- pnpm
npm install @langchain/azure-dynamic-sessions @langchain/core
yarn add @langchain/azure-dynamic-sessions @langchain/core
pnpm add @langchain/azure-dynamic-sessions @langchain/core
你还需要运行代码解释器会话池实例。你可以按照 此指南 使用 Azure CLI 部署一个版本。
¥You'll also need to have a code interpreter session pool instance running. You can deploy a version using Azure CLI following this guide.
实例运行后,你需要确保已正确设置 Azure Entra 身份验证。你可以找到有关如何执行 此处 的说明。
¥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.
为你的身份添加角色后,你需要检索会话池管理端点。你可以在 Azure 门户中实例的 "概述" 部分下找到它。然后,你需要设置以下环境变量:
¥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
下面是一个简单的示例,它创建一个新的 Python 代码解释器会话,调用该工具并打印结果。
¥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:
- SessionsPythonREPLTool from
@langchain/azure-dynamic-sessions
以下是一个完整的示例,我们使用 Azure OpenAI 聊天模型调用 Python 代码解释器会话工具来执行代码并获取结果:
¥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, createToolCallingAgent } 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/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);
API Reference:
- ChatPromptTemplate from
@langchain/core/prompts
- pull from
langchain/hub
- AgentExecutor from
langchain/agents
- createToolCallingAgent from
langchain/agents
- SessionsPythonREPLTool from
@langchain/azure-dynamic-sessions
- AzureChatOpenAI from
@langchain/openai
相关
¥Related
工具 概念指南
¥Tool conceptual guide
工具 操作指南
¥Tool how-to guides