<EXTREMELY-IMPORTANT> 如果你认为此 skill 哪怕只有 1% 的可能性适用于你的任务,你也必须调用它。
如果 SKILL 适用于你的任务,你就别无选择。你必须使用它。 </EXTREMELY-IMPORTANT>
指令优先级
- 用户的明确指令(CLAUDE.md、GEMINI.md、AGENTS.md)— 最高优先级
- 用于 MaxFrame 编码的 skills——发生冲突时覆盖默认系统行为
- 默认系统提示词——最低优先级
平台适配
此 skill 使用 Claude Code 的工具名称。非 CC 平台:改用等效工具。
MaxFrame 编码 - 创建、测试、调试、迭代并构建自定义运行时
此 Skill 的功能
创建、测试、调试和迭代开发 MaxFrame 程序,并构建自定义 DPE 运行时镜像。
- 查阅 MaxFrame 文档,了解 APIs、概念、示例和受支持的 pandas APIs
- 从头创建 MaxFrame 作业或修改现有作业
- 使用与 pandas 兼容的 APIs 设计数据处理管道
- 通过正确的会话管理执行 MaxFrame 代码
- 使用远程 logview URL 或本地 IDE 断点进行调试
- 生成包含特定 Python 库的自定义 Docker 镜像
强制检查清单
- 检测场景类型——确定是文档导航场景,还是 4 个实现场景中的哪一个适用
- 了解需求——针对数据、操作和约束提出澄清问题
- 选择适当的工作流——为场景匹配工作流模式
- 执行工作流步骤——遵循下方针对具体场景的步骤
- 验证执行——确保已调用 execute(),且会话已清理
- 提供后续指导——提供调试技巧和优化建议
流程
仅涉及文档的问题可以跳过实现流程,并使用下面的场景 0。
digraph maxframe_workflow {
"User Request Arrives" [shape=box];
"Detect Scenario Type" [shape=diamond];
"Scenario 1: Writing Code" [shape=box];
"Scenario 2: Remote Debug" [shape=box];
"Scenario 3: Local Debug" [shape=box];
"Scenario 4: Custom Runtime" [shape=box];
"Understand Requirements" [shape=box];
"Operator Selection Needed?" [shape=diamond];
"Use lookup_operator.py" [shape=box];
"Confirm with User" [shape=box];
"Implement Code/Config" [shape=box];
"Add Error Handling" [shape=box];
"Validate execute() Called" [shape=box];
"Validate Session Cleanup" [shape=box];
"Provide Guidance" [shape=doublecircle];
"User Request Arrives" -> "Detect Scenario Type";
"Detect Scenario Type" -> "Scenario 1: Writing Code" [label="new pipeline"];
"Detect Scenario Type" -> "Scenario 2: Remote Debug" [label="cluster testing"];
"Detect Scenario Type" -> "Scenario 3: Local Debug" [label="IDE breakpoints"];
"Detect Scenario Type" -> "Scenario 4: Custom Runtime" [label="custom image"];
"Scenario 1: Writing Code" -> "Understand Requirements";
"Scenario 2: Remote Debug" -> "Understand Requirements";
"Scenario 3: Local Debug" -> "Understand Requirements";
"Scenario 4: Custom Runtime" -> "Understand Requirements";
"Understand Requirements" -> "Operator Selection Needed?";
"Operator Selection Needed?" -> "Use lookup_operator.py" [label="yes"];
"Operator Selection Needed?" -> "Implement Code/Config" [label="no"];
"Use lookup_operator.py" -> "Confirm with User";
"Confirm with User" -> "Implement Code/Config";
"Implement Code/Config" -> "Add Error Handling";
"Add Error Handling" -> "Validate execute() Called";
"Validate execute() Called" -> "Validate Session Cleanup";
"Validate Session Cleanup" -> "Provide Guidance";
}
场景检测逻辑
场景 0:文档导航
- 用户询问一般性的 MaxFrame API、概念、示例或受支持的 pandas API 问题
- 用户希望搜索或浏览 MaxFrame 文档
- 用户询问某个算子是否存在,或文档中所述的 API 应如何使用
- 关键词:"MaxFrame 文档", "文档", "API 参考", "官方示例", "教程", "受支持的 pandas API", "<api> 如何工作"
场景 1:编写 MaxFrame 代码
- 用户希望创建新的数据处理管道
- 用户提到从 MaxCompute 表读取数据或向其写入数据
- 用户请求完整的 MaxFrame 程序
- 关键词:"创建 MaxFrame", "编写 MaxFrame 代码", "构建管道", "使用 MaxCompute 处理数据"
场景 2:远程调试模式
- 用户希望使用实际集群资源进行测试
- 用户提到作业执行错误
- 用户请求 logview URL
- 用户希望诊断执行失败
- 关键词:"调试 MaxFrame 作业", "logview", "远程测试", "执行错误", "集群测试"
场景 3:本地调试模式
- 用户希望以迭代方式调试 UDF 函数
- 用户提到 IDE 断点(VSCode、PyCharm)
- 用户希望在本地使用样本数据进行测试
- 用户希望无需联网即可快速迭代
- 关键词:"本地调试", "IDE 断点", "在本地调试 UDF", "VSCode/PyCharm 调试"
场景 4:创建自定义运行时镜像
- 用户需要标准运行时中未包含的 Python 库
- 用户希望使用支持 GPU 的运行时
- 用户提到构建自定义 DPE 镜像
- 关键词:"自定义运行时"、"DPE 运行时镜像"、"GPU 运行时"、"安装自定义软件包"、"构建 Docker 镜像"
核心规则
1. 仅使用公开 APIs
使用以下模块中的 APIs:maxframe.dataframe、maxframe.tensor、maxframe.learn、maxframe.session、maxframe.udf、maxframe.config。使用规范导入:import maxframe.dataframe as md 和 from maxframe.session import new_session;不得使用 from maxframe import new_session。
2. 不得读取私有 .env 文件
以编程方式使用 dotenv.load_dotenv()。不得通过 Read 工具直接读取 .env 文件。
3. 延迟执行
MaxFrame 采用延迟执行机制:各项操作会构建计算图,只有调用 .execute() 时才会运行。仅执行最终结果或写入操作;除非用户明确要求输出预览或调试结果,否则不得对中间 DataFrame 或 Series 变量调用 .execute()。
4. 会话管理
必须始终在操作前创建会话,并在 finally 块中销毁会话以完成清理。
5. 算子选择与用户确认
在实现处理逻辑前,使用 scripts/lookup_operator.py 与用户确认算子选择。如果用户已明确指定确切的算子或仅要求提供代码,请在实现前写出 Operator confirmed via user prompt: <operator>。
6. 文档来源顺序
对于 MaxFrame 文档问题,应先检查官方在线文档,因为 APIs 可能会发生变化。将随附的本地文档用作离线回退方案和快速交叉核对材料,或在网站内容不够详细时使用。 除非最后一个非空行以 Sources: 开头,并包含完整的 https://maxframe.readthedocs.io/ URL,否则不得结束场景 0;严禁输出 ...。如果无法检查官方文档,必须在该行之前加入 Official docs unavailable: <reason>; using local fallback.。
危险信号
| 想法 | 实际情况 | |---------|---------| | "这只是一个简单的 MaxFrame 问题" | 问题即任务。调用该 skill。 | | "我已经了解 MaxFrame API" | Skills 包含最新模式。使用它们。 | | "让我直接编写代码" | 算子选择是强制要求。 | | "我可以跳过算子确认" | 用户确认是强制要求。 |
场景 0:文档导航
适用于无需新建 MaxFrame 程序的 API/概念/示例问题。
工作流步骤
- 问题分类——API/算子、概念、支持的 pandas API、故障排查、运行时镜像或示例
- 先检查官方文档——在查看本地文档前,先尝试访问官方 URL:https://maxframe.readthedocs.io/en/latest/ 用于查询 APIs/概念,https://maxframe.readthedocs.io/en/latest/examples/index.html 用于查询示例
- 将本地文档用作回退方案或交叉核对材料
- API/算子:
python scripts/lookup_operator.py search "<term>" - API 详情:
python scripts/lookup_operator.py info "<operator>" --section signature|params|examples - 概念/示例:
rg -n "<keyword>" references/maxframe-client-docs references/practical-guides references/operators-and-modules
- 必要时使用本地主题位置
- API 参考文档:
references/maxframe-client-docs/reference/ - 入门指南:
references/maxframe-client-docs/getting_started/ - 用户指南:
references/maxframe-client-docs/user_guide/ - 支持的 pandas APIs:
references/maxframe-client-docs/user_guide/dataframe/supported_pd_apis.md - 实践指南:
references/practical-guides/ - 运行时镜像:
references/runtime-image-guides/
- 基于来源作答——最后一个非空行必须为
Sources: <full official URL> (Primary);仅当使用了本地文档时才追加| <local path> (Fallback/Cross-check)
场景 1:编写 MaxFrame 代码
工作流步骤
- 了解需求——源表/目标表、表结构、分区过滤条件、写入模式、处理逻辑
- 算子选择(强制要求)——使用
python scripts/lookup_operator.py search "<operation>",列出选项并获得确认 - 实现代码——设置会话、读取数据、使用已确认的算子处理数据、写入结果、添加 execute()、在 finally 中清理
- 添加错误处理——用 try/except 包裹 execute(),出现错误时打印 logview URL
- 验证——使用规范导入、仅对最终操作调用
.execute()、在 finally 中调用session.destroy()、不硬编码凭据
示例代码结构
import maxframe.dataframe as md
from maxframe.session import new_session
import dotenv
dotenv.load_dotenv()
session = new_session()
try:
df = md.read_odps_table("source_table")
result = df.groupby('column').agg({'value': 'sum'})
md.to_odps_table(result, "target_table", overwrite=True).execute()
finally:
session.destroy()
另请参阅:references/common-workflow.md,了解完整模式。
场景 2:远程调试模式
工作流步骤
- 了解需求——当前代码状态、错误消息、表名
- 添加 Logview 支持——在操作前创建会话,仅用 try/except 包裹最终的 execute,并在 except 中包含 logview URL
- 提供调试指导——说明 logview 的用法和常见错误模式
示例代码结构
import maxframe.dataframe as md
from maxframe.session import new_session
session = new_session()
try:
df = md.read_odps_table("table_name")
result = df.groupby('region').agg({'sales': 'sum'})
result.execute()
except Exception as e:
print(f"Error: {e}")
print(f"Logview URL: {session.get_logview_address()}")
finally:
session.destroy()
常见错误模式
- 身份验证错误——验证环境变量
- 未找到表——检查表名和权限
- 超时错误——检查 logview,优化查询
- 类型不匹配——检查 DataFrame 的 dtypes
- SQL 错误——在 logview 中检查生成的 SQL
另请参阅:references/remote-debug-guide.md,了解详细解决方案。
场景 3:本地调试模式
工作流步骤
- 了解需求——UDF 逻辑、样本数据结构、IDE 偏好
- 创建本地调试环境——使用
debug=True创建会话,并使用md.DataFrame(pd.DataFrame(...))创建示例数据 - 提供 IDE 设置指导——断点设置、执行流程,并且仅执行最终结果
示例代码结构
import maxframe.dataframe as md
from maxframe.session import new_session
import pandas as pd
session = new_session(debug=True)
sample_data = pd.DataFrame({
'user_id': ['u1', 'u2', 'u3'],
'level': ['gold', 'silver', 'bronze'],
'amount': [1000, 500, 100]
})
df = md.DataFrame(sample_data)
def calculate_discount(row):
# Set breakpoint here in IDE
if row['level'] == 'gold':
return row['amount'] * 0.1
return row['amount'] * 0.02
try:
result = df.apply(calculate_discount, axis=1)
result.execute()
finally:
session.destroy()
参见: references/local-debug-guide.md 获取完整指南。
场景 4:创建自定义运行时镜像
参考指南中的最佳实践,通过对话式指导构建自定义 Docker 镜像。
何时创建自定义运行时
以下情况应创建: 需要标准 DPE 运行时中未包含的 Python 库、支持 GPU 的处理、特定 Python 版本或自定义系统依赖项; 以下情况无需创建: 标准软件包已足够,且没有 GPU 要求
对话式工作流
如果用户已指定基础镜像、Python 版本、GPU 要求、软件包或输出目录,请复述这些选择并继续。仅针对缺失、含糊或不兼容的选项提问。
- 阅读最佳实践指南——
references/runtime-image-guides/README.md - 基础镜像选择——Ubuntu 22.04(GPU/ML 工作负载)或 Ubuntu 24.04(现代开发)
- Python 版本选择——Python 3.11(生产环境)、3.10-3.12(开发环境)或所有版本
- GPU 配置——CUDA 12.4 + PyTorch 2.6.0+cu124(如用于 ML 工作负载)
- 迭代式收集软件包——收集所需软件包,记录版本约束
- 输出目录——确认文件的创建位置
- 逐部分构建 Dockerfile——标头、基础设置、conda 设置、GPU 设置、软件包、环境配置和验证
- 创建配套文件——README.md、.dockerignore、requirements.txt
- 提供构建和测试说明
- MaxFrame 使用示例
分步指导
步骤 1:基础镜像选择(如未指定则询问)
介绍 Ubuntu 选项及其权衡:
Which Ubuntu version for your custom runtime?
A. Ubuntu 22.04 (Recommended for most cases)
- Stable, production-ready
- Excellent CUDA support (12.4, 12.1, 11.8)
- Widely tested ML libraries (PyTorch, TensorFlow)
- LTS until 2027
B. Ubuntu 24.04 (Modern/latest)
- Newer system packages
- Latest LTS (until 2029)
- Better for non-GPU workloads
- Python 3.12 integration
Recommendation:
- GPU/ML workloads → Ubuntu 22.04
- Modern development → Ubuntu 24.04
步骤 2:Python 版本选择(如未指定则询问)
Which Python versions?
A. Python 3.11 only (Recommended for production)
- Best performance
- Smallest image (~1 GB)
- Excellent package support
B. Python 3.10, 3.11, 3.12 (Development)
- Good compatibility
- Medium size (~2 GB)
- Recent versions
C. All versions 3.7-3.12 (Maximum flexibility)
- Largest image (~3-5 GB)
- Maximum compatibility
- Testing across versions
Recommendation:
- Production → Single version (3.11)
- Development → Recent versions (3.10-3.12)
步骤 3:GPU 配置(如未指定则询问)
如果用户提到 GPU 或 ML 软件包:
Need GPU support?
A. Yes - GPU-enabled with CUDA 12.4 (Recommended)
- Install PyTorch 2.6.0+cu124
- CUDA toolkit 12.4
- Note: Requires Ubuntu 22.04 for best compatibility
B. No - CPU only
- Standard package installation
- Smaller image size
Recommendation: For ML/AI workloads, GPU support significantly improves performance.
兼容性处理: 如果用户之前选择了 Ubuntu 24.04,现在又要求支持 GPU:
- 说明:"Ubuntu 24.04 对 CUDA 的支持有限。对于 GPU 工作负载,建议使用 Ubuntu 22.04。"
- AskUserQuestion:"是否应改用 Ubuntu 22.04,以获得更好的 GPU 兼容性?"(建议选择“是”)
步骤 4:逐部分构建 Dockerfile
对于每个部分:
- 读取最佳实践指南中的模式
- 说明用途和权衡
- 使用行内注释编写该部分
- 将各部分汇总成完整的 Dockerfile
各部分:
- 标头——镜像元数据、配置摘要
- 基础设置——FROM、apt 软件包、区域设置、时区
- Conda 设置——安装 Miniforge、创建环境
- GPU 设置——安装 CUDA、安装支持 CUDA 的 PyTorch(如适用)
- 软件包安装——在多环境循环中安装用户软件包
- 环境配置——MF_PYTHON_EXECUTABLE、CONDA_DEFAULT_ENV、PATH
- 验证——健康检查、Python 版本验证
步骤 5:提供构建和测试说明
# Build
docker build -t <image-tag> <output-dir>
# Test Python
docker run --rm <image-tag> conda run -n py311 python --version
# Test GPU (if applicable)
docker run --rm --gpus all <image-tag> python -c "import torch; print(torch.cuda.is_available())"
# Test packages
docker run --rm <image-tag> conda run -n py311 python -c "import transformers; print(transformers.__version__)"
# Push to registry
docker push <image-tag>
步骤 6:MaxFrame 使用示例——必须包含 new_session(odps=odps_connection, image="...");禁止单独展示 new_session(image=...)。
from maxframe.session import new_session
session = new_session(odps=odps_connection, image="your-registry/your-image:v1")
# Your MaxFrame operations here
默认建议
| 组件 | 建议 | |-----------|---------------| | 基础镜像 | Ubuntu 22.04(生产环境、GPU、ML) | | Python | 3.11(生产环境)、3.10-3.12(开发环境) | | GPU | Ubuntu 22.04 + CUDA 12.4 + PyTorch 2.6.0+cu124 |
关键说明
运行时镜像中不包含 MaxFrame SDK: SDK 和 pyodps 仅用于客户端。自定义运行时需要用户特定的软件包(transformers、pandas 等)。
MF_PYTHON_EXECUTABLE(关键): 必须始终设置:ENV MF_PYTHON_EXECUTABLE=/py-runtime/envs/<env_name>/bin/python
最佳实践参考
参见: references/runtime-image-guides/,了解有关基础镜像选择、Python 环境策略、软件包管理、GPU/CUDA 配置、Dockerfile 模板以及测试/验证的详细指南。
算子选择工作流
实现处理逻辑前必须执行此工作流:当用户提到具体操作、询问效率/性能,或你需要查找合适的 MaxFrame 算子时。 对于仅涉及文档的回答,无需用户确认;但仍须使用查询脚本,为 API 相关结论提供依据。 如果用户明确指定了算子或要求跳过交互,则输出 Operator confirmed via user prompt: <operator> 并直接实现。
工作流
- 识别操作——列出所需的转换
- 查找算子——
python scripts/lookup_operator.py search "<operation>" - 展示选项——展示算子名称、说明和权衡因素
- 获取用户确认——确认算子和参数,或输出上述用户 Prompt 确认行
- 实现——使用已确认的算子
请参阅:references/operators-and-modules/operator-selector.md,了解详细指南。
关键验证点
完成前,请验证:
- [ ] 已在结果 DataFrame 上调用
.execute() - [ ] 使用了规范的导入方式;未使用
from maxframe import new_session;未在中间结果上调用.execute() - [ ] 在执行操作前创建会话
- [ ] 在
finally块中销毁会话 - [ ] 未硬编码凭证
- [ ] 已与用户确认算子选择
- [ ] 文档类回答首先引用官方文档 URL;如果将本地文档用作回退或交叉检查,则引用本地文档路径
- [ ] 错误处理包含 logview URL(远程调试)
- [ ] 使用了
debug=True(本地调试) - [ ] 已设置
MF_PYTHON_EXECUTABLE(自定义运行时)
资源
参考资料
- 算子选择器:
references/operators-and-modules/operator-selector.md - 本地调试:
references/local-debug-guide.md - 远程调试:
references/remote-debug-guide.md - 完整工作流:
references/common-workflow.md - MaxFrame 客户端文档:
references/maxframe-client-docs/ - 实用指南:
references/practical-guides/ - 运行时指南:
references/runtime-image-guides/ - 在线文档:https://maxframe.readthedocs.io/en/latest/
- 在线示例:https://maxframe.readthedocs.io/en/latest/examples/index.html
- 源代码:https://github.com/aliyun/alibabacloud-odps-maxframe-client.git
示例
- 可运行示例:
assets/examples/*.py
脚本
- 算子查询:
scripts/lookup_operator.py