文档索引 在以下地址获取完整的文档索引:https://docs.langchain.org.cn/llms.txt
在进一步探索之前,请使用此文件发现所有可用页面。
Modal 提供具备 GPU 支持的无服务器容器基础设施。最适用于机器学习/人工智能工作负载和 Python 开发。
npm install @langchain/modal
身份验证
从 modal.com/settings/tokens 获取您的令牌。
export MODAL_TOKEN_ID = your_token_id
export MODAL_TOKEN_SECRET = your_token_secret
或者直接传递凭据
const sandbox = await ModalSandbox . create ( {
auth : {
tokenId : "your-token-id" ,
tokenSecret : "your-token-secret" ,
},
} ) ;
与 deepagents 的用法
import { createDeepAgent } from "deepagents" ;
import { ChatAnthropic } from "@langchain/anthropic" ;
import { ModalSandbox } from "@langchain/modal" ;
const sandbox = await ModalSandbox . create ( {
imageName : "python:3.12-slim" ,
timeoutMs : 600_000 , // 10 minutes
} ) ;
try {
const agent = createDeepAgent ( {
model : new ChatAnthropic ( { model : "claude-sonnet-4-20250514" } ) ,
systemPrompt : "You are a coding assistant with sandbox access." ,
backend : sandbox ,
} ) ;
const result = await agent . invoke ( {
messages : [ { role : "user" , content : "Install numpy and calculate pi" } ] ,
} ) ;
} finally {
await sandbox . close () ;
}
独立使用
import { ModalSandbox } from "@langchain/modal" ;
const sandbox = await ModalSandbox . create ( {
imageName : "python:3.12-slim" ,
timeoutMs : 600_000 ,
} ) ;
const result = await sandbox . execute ( "python --version" ) ;
console . log (result . output) ;
await sandbox . close () ;
选项 类型 默认 描述 imageName字符串"alpine:3.21"使用的 Docker 镜像 timeoutMs数字300000最大生存时间(毫秒) workdir字符串- 工作目录 gpu字符串- GPU 类型 ("T4", "A100", "H100" 等) cpu数字- CPU 核心数(允许小数) memoryMiB数字- 以 MiB 为单位的内存分配 volumesRecord<string, string>- 卷名称映射(挂载路径到卷名称) secrets字符串[]- 要注入的 Modal 密钥名称 initialFilesRecord<string, string | Uint8Array>- 启动时创建的文件 envRecord<string, string>- 环境变量 blockNetwork布尔值- 阻止网络访问 名称字符串- 沙箱名称(在应用内唯一)
GPU 支持
Modal 为机器学习工作负载提供 NVIDIA GPU 支持
const sandbox = await ModalSandbox . create ( {
imageName : "python:3.12-slim" ,
gpu : "T4" , // or "L4", "A10G", "A100", "H100"
} ) ;
卷(Volumes)与密钥(Secrets)
挂载 Modal 卷以实现持久存储,并将密钥作为环境变量注入
// Volumes and secrets must be created in Modal first
const sandbox = await ModalSandbox . create ( {
imageName : "python:3.12-slim" ,
volumes : {
"/data" : "my-data-volume" ,
"/models" : "my-models-volume" ,
},
secrets : [ "my-api-keys" , "database-credentials" ] ,
} ) ;
// Files in /data and /models persist across sandbox restarts
await sandbox . execute ( "echo 'Hello' > /data/test.txt" ) ;
// Secrets are available as environment variables
await sandbox . execute ( "echo $API_KEY" ) ;
初始文件
在创建过程中预置沙箱文件
const sandbox = await ModalSandbox . create ( {
imageName : "python:3.12-slim" ,
initialFiles : {
"/app/main.py" : 'print("Hello from Python!")' ,
"/app/config.json" : JSON . stringify ( { name : "my-app" }, null , 2 ) ,
},
} ) ;
const result = await sandbox . execute ( "python /app/main.py" ) ;
访问 Modal SDK
对于 BaseSandbox 未提供的进阶功能,可访问底层的 Modal SDK
const modalSandbox = await ModalSandbox . create () ;
const client = modalSandbox . client ; // ModalClient
const instance = modalSandbox . instance ; // Sandbox
// Direct SDK operations
const process = await instance . exec ([ "python" , "-c" , "print('Hello')" ] , {
stdout : "pipe" ,
stderr : "pipe" ,
} ) ;
重新连接到现有的沙箱
// Reconnect by ID
const reconnected = await ModalSandbox . fromId (sandboxId) ;
// Reconnect by name
const reconnected2 = await ModalSandbox . fromName ( "my-app" , "my-sandbox" ) ;
工厂函数
import { createModalSandboxFactory , createModalSandboxFactoryFromSandbox } from "@langchain/modal" ;
// Create new sandbox per invocation
const factory = createModalSandboxFactory ( { imageName : "python:3.12-slim" } ) ;
// Or reuse an existing sandbox across invocations
const sandbox = await ModalSandbox . create () ;
const reuseFactory = createModalSandboxFactoryFromSandbox (sandbox) ;
错误处理
import { ModalSandboxError } from "@langchain/modal" ;
try {
await sandbox . execute ( "some command" ) ;
} catch (error) {
if (error instanceof ModalSandboxError ) {
switch (error . code) {
case "NOT_INITIALIZED" :
await sandbox . initialize () ;
break ;
case "COMMAND_TIMEOUT" :
console . error ( "Command took too long" ) ;
break ;
case "AUTHENTICATION_FAILED" :
console . error ( "Check your Modal token credentials" ) ;
break ;
}
}
}
错误代码
代码 描述 NOT_INITIALIZED沙箱未初始化 - 调用 initialize() ALREADY_INITIALIZED无法重复初始化 AUTHENTICATION_FAILEDModal 令牌无效或缺失 SANDBOX_CREATION_FAILED沙箱创建失败 SANDBOX_NOT_FOUND未找到沙箱 ID/名称或已过期 COMMAND_TIMEOUT命令执行超时 COMMAND_FAILED命令执行失败 FILE_OPERATION_FAILED文件读/写失败 RESOURCE_LIMIT_EXCEEDEDCPU、内存或存储限制超出 VOLUME_ERROR卷操作失败
环境变量
变量 描述 MODAL_TOKEN_IDModal API 令牌 ID MODAL_TOKEN_SECRETModal API 令牌密钥
将这些文档 连接到 Claude、VSCode 等,以获得实时答案。