Azure Container Apps 动态会话
Azure Container Apps 动态会话 提供了快速访问安全沙箱环境的能力,非常适合运行需要与其他工作负载强隔离的代码或应用程序。
您可以在此页面了解更多关于 Azure Container Apps 动态会话及其代码解释功能的信息。如果您还没有 Azure 帐户,可以创建一个免费帐户开始使用。
配置
首先,您需要安装 @langchain/azure-dynamic-sessions 包:
:::提示 请参阅安装集成包的一般说明部分。 :::
- 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 部署一个实例。
实例运行后,您需要确保已正确为其设置 Azure Entra 身份验证。您可以在此处找到相关操作说明。
在为您的身份添加了角色后,您需要获取会话池管理端点。您可以在 Azure 门户中实例的“概述”部分找到它。然后,您需要设置以下环境变量:
AZURE_CONTAINER_APP_SESSION_POOL_MANAGEMENT_ENDPOINT=<your_endpoint>
API Reference:
使用示例
以下是一个简单的示例,它创建一个新的 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,
// }
API Reference:
- SessionsPythonREPLTool from
@langchain/azure-dynamic-sessions
下面是一个完整示例,我们使用 Azure OpenAI 聊天模型调用 Python 代码解释器会话工具来执行代码并获取结果:
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