如何在catalog.yml中指定任意二进制文件及URL/脚本获取方案?
处理LLM二进制模型文件的Kedro配置方案
在catalog.yml中指定二进制文件的正确方式
直接使用kedro.io.BinaryDataSet,这是Kedro专门用于存储原生二进制文件的数据集类型,完全不依赖Python的pickle序列化,天然支持跨语言访问,能完美保留.bin/.gguf这类模型文件的原始格式。
示例配置:
llm_model_weights: type: kedro.io.BinaryDataSet filepath: data/06_models/llm_model.gguf versioned: true # 按需开启版本控制,便于模型迭代管理
从URL或网络脚本获取文件的处理方式
不需要创建单独流水线,直接将下载/获取逻辑封装为节点,加入现有流水线的起始阶段即可,这样能统一管理数据依赖和流程版本:
1. 从静态URL下载
首先在catalog.yml中定义数据源和目标存储:
# 远程数据源 llm_model_remote: type: kedro.io.HTTPDataSet url: "https://example.com/models/your_model.bin" auth: null # 若需认证,可填写{"username": "xxx", "password": "xxx"}或token信息 # 本地二进制存储 llm_model_weights: type: kedro.io.BinaryDataSet filepath: data/06_models/your_model.bin
然后在nodes.py中编写下载逻辑:
def download_llm_model(remote_url: str, local_path: str): import requests response = requests.get(remote_url) response.raise_for_status() # 捕获下载错误 with open(local_path, "wb") as f: f.write(response.content)
最后在pipeline.py中将该节点加入现有流水线:
from kedro.pipeline import Pipeline, node from .nodes import download_llm_model, your_existing_processing_nodes def create_pipeline(**kwargs) -> Pipeline: return Pipeline([ node( func=download_llm_model, inputs="llm_model_remote", outputs="llm_model_weights", name="download_llm_model" ), # 后续衔接你的现有数据处理/模型加载节点 *your_existing_processing_nodes ])
2. 通过自定义脚本从网络获取
如果需要动态处理(比如调用API生成下载链接、处理分页数据等),只需将脚本逻辑封装为一个节点,同样加入现有流水线即可。比如需要调用模型仓库API获取最新模型:
def fetch_latest_model(api_key: str, local_path: str): import requests # 自定义API调用逻辑 api_url = "https://api.model-repo.com/latest-model" headers = {"Authorization": f"Bearer {api_key}"} response = requests.get(api_url, headers=headers) download_url = response.json()["download_url"] # 下载并保存 model_response = requests.get(download_url) with open(local_path, "wb") as f: f.write(model_response.content)
在catalog.yml中添加API密钥的配置(建议用环境变量):
model_repo_api_key: type: kedro.io.TextDataSet filepath: conf/local/api_key.txt # 或直接用环境变量注入
然后将该节点加入流水线,确保依赖正确即可。
内容的提问来源于stack exchange,提问作者Quakumei
相关产品推荐
相关产品推荐

