办公效率
Cosmos编排dbt Core
提供仓库类型、Airflow版本、运行环境及manifest情况,选任务编排方式并配置
# Cosmos + dbt Core:实施清单
按顺序执行步骤。优先采用满足用户约束的最简单配置。
> **版本说明**:此 skill 适用于 Cosmos 1.11+ 和 Airflow 3.x。如果用户使用 Airflow 2.x,请相应调整导入(见附录 A)。
>
> **参考资料**:最新稳定版:https://pypi.org/project/astronomer-cosmos/
> **开始前**,确认:(1)dbt 引擎 = Core(不是 Fusion → 使用 **cosmos-dbt-fusion**),(2)数据仓库类型,(3)Airflow 版本,(4)执行环境(Airflow 环境 / venv / 容器),(5)DbtDag、DbtTaskGroup 还是单独的 operators,(6)manifest 是否可用。
---
## 1. 配置项目(ProjectConfig)
| 方式 | 使用场景 | 必需参数 |
|----------|-------------|----------------|
| 项目路径 | 文件在本地可用 | `dbt_project_path` |
| 仅使用 Manifest | `dbt_manifest` 加载 | `manifest_path` + `project_name` |
```python
from cosmos import ProjectConfig
_project_config = ProjectConfig(
dbt_project_path="/path/to/dbt/project",
# manifest_path="/path/to/manifest.json", # for dbt_manifest load mode
# project_name="my_project", # if using manifest_path without dbt_project_path
# install_dbt_deps=False, # if deps precomputed in CI
)
```
## 2. 选择解析策略(RenderConfig)
根据约束选择一种加载模式:
| 加载模式 | 使用场景 | 必需输入 | 约束 |
|-----------|-------------|-----------------|-------------|
| `dbt_manifest` | 大型项目;容器化执行;速度最快 | `ProjectConfig.manifest_path` | 远程 manifest 需要 `manifest_conn_id` |
| `dbt_ls` | 复杂选择器;需要 dbt 原生选择功能 | 已安装 dbt 或 `dbt_executable_path` | 也可用于容器化执行 |
| `dbt_ls_file` | 使用 dbt_ls 选择功能,无需每次解析都运行 dbt_ls | `RenderConfig.dbt_ls_path` | `select`/`exclude` 无法使用 |
| `automatic`(默认)| 简单配置;让 Cosmos 选择 |(无)| 依次回退:manifest → dbt_ls → 自定义 |
> **关键**:容器化执行(`DOCKER`/`KUBERNETES`/等)
```python
from cosmos import RenderConfig, LoadMode
_render_config = RenderConfig(
load_method=LoadMode.DBT_MANIFEST, # or DBT_LS, DBT_LS_FILE, AUTOMATIC
)
```
---
## 3. 选择执行模式(ExecutionConfig)
> **参考资料**:各模式的详细配置示例见 **[reference/cosmos-config.md](reference/cosmos-config.md#execution-modes-executionconfig)**。
选择一种执行模式:
| 执行模式 | 使用场景 | 速度 | 必需配置 |
|----------------|-------------|-------|----------------|
| `WATCHER` | 速度最快;单个 `dbt build` 可见 | 最快 | 环境中有 dbt 适配器或 `dbt_executable_path` 或 dbt Fusion |
| `WATCHER_KUBERNETES` | 速度最快的隔离方式;单个 `dbt build` 可见 | 快 | 容器中已安装 dbt |
| `LOCAL` + `DBT_RUNNER` | dbt + 适配器与 Airflow 安装在同一个 Python 环境中 | 快 | dbt 1.5+ 位于 `requirements.txt` |
| `LOCAL` + `SUBPROCESS` | Airflow 部署环境中可用 dbt + 适配器,且安装在隔离的 Python 环境中 | 中等 | `dbt_executable_path` |
| `AIRFLOW_ASYNC` | BigQuery + 长时间运行的转换任务 | 快 | Airflow ≥2.8;provider 依赖 |
| `KUBERNETES` | Airflow 与 dbt 之间隔离 | 中等 | Airflow ≥2.8;provider 依赖 |
| `VIRTUALENV` | 无法修改镜像;运行时 venv | 较慢 | operator_args 中的 `py_requirements` |
| 其他容器化方式 | 支持 Airflow 与 dbt 隔离 | 中等 | 容器配置 |
```python
from cosmos import ExecutionConfig, ExecutionMode
_execution_config = ExecutionConfig(
execution_mode=ExecutionMode.WATCHER, # or LOCAL, VIRTUALENV, AIRFLOW_ASYNC, KUBERNETES, etc.
)
```
---
## 4. 配置数据仓库连接(ProfileConfig)
> **参考资料**:详细的 ProfileConfig 选项和所有 ProfileMapping 类见 **[reference/cosmos-config.md](reference/cosmos-config.md#profileconfig-warehouse-connection)**。
### 方案 A:Airflow Connection + ProfileMapping(推荐)
```python
from cosmos import ProfileConfig
from cosmos.profiles import SnowflakeUserPasswordProfileMapping
_profile_config = ProfileConfig(
profile_name="default",
target_name="dev",
profile_mapping=SnowflakeUserPasswordProfileMapping(
conn_id="snowflake_default",
profile_args={"schema": "my_schema"},
),
)
```
### 方案 B:现有 profiles.yml
> **关键**:不要硬编码密钥;使用环境变量。
```python
from cosmos import ProfileConfig
_profile_config = ProfileConfig(
profile_name="my_profile",
target_name="dev",
profiles_yml_filepath="/path/to/profiles.yml",
)
```
---
## 5. 配置测试行为(RenderConfig)
> **参考资料**:详细测试选项见 **[reference/cosmos-config.md](reference/cosmos-config.md#testing-behavior-renderconfig)**。
| TestBehavior | 行为 |
|--------------|----------|
| `AFTER_EACH`(默认)| 每个模型运行后立即执行测试(默认)|
| `BUILD` | 将运行 + 测试合并为单个 `dbt build` |
| `AFTER_ALL` | 所有模型完成后执行全部测试 |
| `NONE` | 跳过测试 |
```python
from cosmos import RenderConfig, TestBehavior
_render_config = RenderConfig(
test_behavior=TestBehavior.AFTER_EACH,
)
```
---
## 6. 配置 operator_args
> **参考资料**:详细的 operator_args 选项见 **[reference/cosmos-config.md](reference/cosmos-config.md#operator_args-configuration)**。
```python
_operator_args = {
# BaseOperator params
"retries": 3,
# Cosmos-specific params
"install_deps": False,
"full_refresh": False,
"quiet": True,
# Runtime dbt vars (XCom / params)
"vars": '{"my_var": "{{ ti.xcom_pull(task_ids=\'pre_dbt\') }}"}',
}
```
---
## 7. 组装 DAG / TaskGroup
### 方案 A:DbtDag(独立使用)
```python
from cosmos import DbtDag, ProjectConfig, ProfileConfig, ExecutionConfig, RenderConfig
from cosmos.profiles import SnowflakeUserPasswordProfileMapping
from pendulum import datetime
_project_config = ProjectConfig(
dbt_project_path="/usr/local/airflow/dbt/my_project",
)
_profile_config = ProfileConfig(
profile_name="default",
target_name="dev",
profile_mapping=SnowflakeUserPasswordProfileMapping(
conn_id="snowflake_default",
),
)
_execution_config = ExecutionConfig()
_render_config = RenderConfig()
my_cosmos_dag = DbtDag(
dag_id="my_cosmos_dag",
project_config=_project_config,
profile_config=_profile_config,
execution_config=_execution_config,
render_config=_render_config,
operator_args={},
start_date=datetime(2025, 1, 1),
schedule="@daily",
)
```
### 方案 B:DbtTaskGroup(在现有 DAG 内使用)
```python
from airflow.sdk import dag, task # Airflow 3.x
# from airflow.decorators import dag, task # Airflow 2.x
from airflow.models.baseoperator import chain
from cosmos import DbtTaskGroup, ProjectConfig, ProfileConfig, ExecutionConfig, RenderConfig
from pendulum import datetime
_project_config = ProjectConfig(dbt_project_path="/usr/local/airflow/dbt/my_project")
_profile_config = ProfileConfig(profile_name="default", target_name="dev")
_execution_config = ExecutionConfig()
_render_config = RenderConfig()
@dag(start_date=datetime(2025, 1, 1), schedule="@daily")
def my_dag():
@task
def pre_dbt():
return "some_value"
dbt = DbtTaskGroup(
group_id="dbt_project",
project_config=_project_config,
profile_config=_profile_config,
execution_config=_execution_config,
render_config=_render_config,
)
@task
def post_dbt():
pass
chain(pre_dbt(), dbt, post_dbt())
my_dag()
```
### 方案 C:直接使用 Cosmos operators
```python
import os
from datetime import datetime
from pathlib import Path
from typing import Any
from airflow import DAG
try:
from airflow.providers.standard.operators.python import PythonOperator
except ImportError:
from airflow.operators.python import PythonOperator
from cosmos import DbtCloneLocalOperator, DbtRunLocalOperator, DbtSeedLocalOperator, ProfileConfig
from cosmos.io import upload_to_aws_s3
DEFAULT_DBT_ROOT_PATH = Path(__file__).parent / "dbt"
DBT_ROOT_PATH = Path(os.getenv("DBT_ROOT_PATH", DEFAULT_DBT_ROOT_PATH))
DBT_PROJ_DIR = DBT_ROOT_PATH / "jaffle_shop"
DBT_PROFILE_PATH = DBT_PROJ_DIR / "profiles.yml"
DBT_ARTIFACT = DBT_PROJ_DIR / "target"
profile_config = ProfileConfig(
profile_name="default",
target_name="dev",
profiles_yml_filepath=DBT_PROFILE_PATH,
)
def check_s3_file(bucket_name: str, file_key: str, aws_conn_id: str = "aws_default", **context: Any) -> bool:
"""Check if a file exists in the given S3 bucket."""
from airflow.providers.amazon.aws.hooks.s3 import S3Hook
s3_key = f"{context['dag'].dag_id}/{context['run_id']}/seed/0/{file_key}"
print(f"Checking if file {s3_key} exists in S3 bucket...")
hook = S3Hook(aws_conn_id=aws_conn_id)
return hook.check_for_key(key=s3_key, bucket_name=bucket_name)
with DAG("example_operators", start_date=datetime(2024, 1, 1), catchup=False) as dag:
seed_operator = DbtSeedLocalOperator(
profile_config=profile_config,
project_dir=DBT_PROJ_DIR,
task_id="seed",
dbt_cmd_flags=["--select", "raw_customers"],
install_deps=True,
append_env=True,
)
check_file_uploaded_task = PythonOperator(
task_id="check_file_uploaded_task",
python_callable=check_s3_file,
op_kwargs={
"aws_conn_id": "aws_s3_conn",
"bucket_name": "cosmos-artifacts-upload",
"file_key": "target/run_results.json",
},
)
run_operator = DbtRunLocalOperator(
profile_config=profile_config,
project_dir=DBT_PROJ_DIR,
task_id="run",
dbt_cmd_flags=["--models", "stg_customers"],
install_deps=True,
append_env=True,
)
clone_operator = DbtCloneLocalOperator(
profile_config=profile_config,
project_dir=DBT_PROJ_DIR,
task_id="clone",
dbt_cmd_flags=["--models", "stg_customers", "--state", DBT_ARTIFACT],
install_deps=True,
append_env=True,
)
seed_operator >> run_operator >> clone_operator
seed_operator >> check_file_uploaded_task
```
### 为单个 Cosmos 任务设置依赖关系
```python
from cosmos import DbtDag, DbtResourceType
from airflow.sdk import task, chain
with DbtDag(...) as dag:
@task
def upstream_task():
pass
_upstream = upstream_task()
for unique_id, dbt_node in dag.dbt_graph.filtered_nodes.items():
if dbt_node.resource_type == DbtResourceType.SEED:
my_dbt_task = dag.tasks_map[unique_id]
chain(_upstream, my_dbt_task)
```
---
## 8. 安全检查
完成前,请确认:
- [ ] 执行模式符合约束(AIRFLOW_ASYNC → 仅限 BigQuery)
- [ ] 已为所选执行模式安装数据仓库适配器
- [ ] 密钥通过 Airflow 连接或环境变量提供,而非明文
- [ ] 加载模式与执行方式匹配(复杂选择器 → dbt_ls)
- [ ] 如果下游 DAG 基于 Cosmos 资产调度,已使用 Airflow 3 资产 URI(见附录 A)
---
## 附录 A:Airflow 3 兼容性
### 导入差异
| Airflow 3.x | Airflow 2.x |
|-------------|-------------|
| `from airflow.sdk import dag, task` | `from airflow.decorators import dag, task` |
| `from airflow.sdk import chain` | `from airflow.models.baseoperator import chain` |
### Asset/Dataset URI 格式变更
Cosmos ≤1.9(Airflow 2 Datasets):
```
postgres://0.0.0.0:5434/postgres.public.orders
```
Cosmos ≥1.10(Airflow 3 Assets):
```
postgres://0.0.0.0:5434/postgres/public/orders
```
> **关键**:升级到 Airflow 3 时更新资产 URI。
---
## 附录 B:其他运维功能
### 缓存
Cosmos 缓存产物以加快解析速度。默认启用。
参考资料:https://astronomer.github.io/astronomer-cosmos/configuration/caching.html
### 内存优化导入
```bash
AIRFLOW__COSMOS__ENABLE_MEMORY_OPTIMISED_IMPORTS=True
```
启用后:
```python
from cosmos.airflow.dag import DbtDag # instead of: from cosmos import DbtDag
```
### 将产物上传到对象存储
```bash
AIRFLOW__COSMOS__REMOTE_TARGET_PATH=s3://bucket/target_dir/
AIRFLOW__COSMOS__REMOTE_TARGET_PATH_CONN_ID=aws_default
```
```python
from cosmos.io import upload_to_cloud_storage
my_dag = DbtDag(
# ...
operator_args={"callback": upload_to_cloud_storage},
)
```
### 托管 dbt 文档
Cosmos 在 Airflow UI 中提供 dbt 文档。配置取决于你的 Airflow 主版本
(每个版本使用不同的 UI 插件系统)——不能自由选择单项目或多项目方式:
| Airflow | 配置 | 范围 | 自此版本起 |
|-----------------|------------------------------------------------------------------|----------------------|----------------|
| 2(FAB 插件) | `DBT_DOCS_DIR` (+ `DBT_DOCS_CONN_ID`, `DBT_DOCS_INDEX_FILE_NAME`) | 单个项目 | Cosmos 1.4.0+ |
| 3.1+ (FastAPI) | `DBT_DOCS_PROJECTS` (JSON) | 一个或多个项目 | Cosmos 1.11.0+ |
Airflow 2:
```bash
AIRFLOW__COSMOS__DBT_DOCS_DIR="path/to/docs" # local path or S3/GCS/Azure/HTTP URI; defaults to the dbt target/ folder
AIRFLOW__COSMOS__DBT_DOCS_CONN_ID="my_conn_id" # optional; for cloud storage
AIRFLOW__COSMOS__DBT_DOCS_INDEX_FILE_NAME="static_index.html" # optional; only if docs built with --static
```
Airflow 3.1+:
```bash
AIRFLOW__COSMOS__DBT_DOCS_PROJECTS='{
"my_project": {
"dir": "s3://bucket/docs/",
"index": "index.html",
"conn_id": "aws_default",
"name": "My Project"
}
}'
```
按 Airflow 版本选择,而不是按项目数量选择。单项目设置适用于 Airflow 2;
Cosmos 没有发布关于这些设置的弃用通知——不要将其描述为“旧版”
或“已弃用”。
参考资料:https://astronomer.github.io/astronomer-cosmos/configuration/hosting-docs.html
---
## 相关 Skills
- **cosmos-dbt-fusion**:适用于 dbt Fusion 项目(不是 dbt Core)
- **authoring-dags**:通用 DAG 编写模式
- **testing-dags**:创建后测试 DAG同类提示词
办公效率· n8n 工作流的对话模型
商品照片转棚拍广告生图指令
上传商品照片并提供说明文字,分析材质、配色和原图问题,写出保留商品外观的棚拍广告生图指令。
=你是一位专业产品摄影师和广告创意总监。分析上传的产品图片及其配文:{{ $('Telegram Trigger Received').item.json.message.caption }} 你的任务是创建一段详细的图像生成提示词,将这张产品照片转化为高端、商业级的广告视觉作品。 分析要求: 1. **产品识别**:识别产品类型、尺寸、类别、材质、配色和关键特征 2. **当前状态评估**:指出当前的光线、背景、构图和质量问题 3. **目标美学**:根据产品类别确定理想的广告风格(奢侈品、科技、食品、时尚等) 输出(严格使用 JSON 格式):办公效率· n8n 工作流的对话模型
Veo 3营销视频分镜指令
填写视频需求、营销目标、图像描述和画幅,生成包含场景、运镜、动作、灯光与结尾的结构化制片指令。
=# Veo3 视频生成系统提示词 你是一名专业的视频提示词工程师,擅长为 Veo3 视频生成撰写细致、富有电影感的提示词。你的角色是分析用户输入,将其转化为全面的视频生成指令,以产出高质量、引人入胜的内容。 ## 输入分析 你将收到以下用户输入: - **AI Videos instructions**:视频生成的核心用户请求 - **Campaign objectives**:营销活动目标和说明 - **Images Description**:用户提供的视觉背景信息 - **Aspect Ratio**:要求的视频尺寸比例 ## 输出格式 你
可填 21 项
办公效率· n8n 工作流的对话模型
保留原品的社媒广告视觉方案
提供商品图和配文,生成保留原品外观的广告生图说明,广告文字仅从配文提取,缺项留空。
={ "system_prompt": "你是一位顶尖创意总监,专攻爆款产品广告,拥有15年以上奢侈品牌广告活动经验。将产品图片转化为商业级广告,让浏览者停下滑动并促进转化。", "instructions": { "core_rules": [ "保持原始产品100%不变——不得修改、替换或扭曲", "只提取和使用用户配文中提供的文字", "如果配文中没有相关文字,文字字段留空——绝不生成AI广告语", "专注于高端视觉强化和商业摄影技巧", "制作适合在Instagram展办公效率· Claude Fable 5.2
用代码制作长纪录片流程
从题材调研、脚本和样帧审核开始,按章节生成三维分镜、旁白、环境声与可复现渲染。
<inputs> 向我询问: 1. 我的主题 2. 目标视频时长 3. 我是否已有脚本 如果我只给你一个主题,先研究该主题并撰写脚本。 开始制作任何内容之前,先向我展示: - 完整脚本 - 章节拆分 - 前 60 seconds 的分镜计划 - 6 张展示预期视觉方向的静帧 在我认可视觉效果之前,不要渲染完整视频。 </inputs> <story> 按照高品质 YouTube 纪录片的方式来写,不要写成 AI 科普解说。 故事必须始终向前推进。 每个部分都需要包含以下内容之一: - 一个新问题 - 一个揭示 - 一个矛盾 - 一个后果 - 一条证据
办公效率
同场景表情变化九宫格自拍
上传主角照片,可选搭档参考图或设定,并填写服装、背景或姿势偏好,生成同一布光和构图的九张变化。
# photo-booth ## 何时使用此 skill - 当用户希望根据参考图制作大头贴宫格、表情宫格、情侣自拍宫格、同步姿势组图或多种人像变体时使用。 - 当请求制作 3x3 宫格,且主体、房间、服装、构图和布光保持一致,仅表情或细微姿势发生变化时触发。 ## 必需输入 - 主要人物的主参考图。 - 可选的伴侣或角色参考图。 - 如果未提供伴侣参考图,需提供期望的伴侣设定。 - 用户指定的服装、背景、摄影时代、姿势清单或氛围。 如果缺少主参考图,请用户附上。如果伴侣设定或预期风格不明确,提出一个有针对性的问题。 ## 连续性规则 -
需参考图
办公效率· Claude Code / 动效编程
单镜头立体落地页飞越动画
提供落地页链接、品牌名、标志和两段收尾文案,可选配乐,制作卡片随镜头掠过而浮起的连续飞越短片。
<inputs> 向我索取:我的落地页链接、品牌名称和标志、由两部分组成的结尾文案(一句陈述,后接一小段斜体文字),以及一条 15 second 音轨(可选)。 </inputs> <direction> 以 60fps 对我的落地页进行 14.5 second 3D 飞越拍摄,1440x1152。页面平放在明亮的摄影棚地面上(中央为 #f9fbfb,角落为 #f2f3f6),周围有柔和宽阔的阴影,圆角为 24px。全程使用一个连续镜头,绝不切镜:镜头从首屏上方低处开始,以恒定速度沿页面向后滑行,同时朝页面上方看去;然后垂直升至正上方,将整个页面呈现为