doc-pipeline

doc-pipeline

熱門

將文件操作串聯成可重複使用的管線

320星標
70分支
更新於 2026/1/31
SKILL.md
唯讀
名稱
doc-pipeline
描述

將文件操作串聯成可重複使用的管線

版本
1.0

Doc Pipeline 技能

概述

此技能可建立文件處理管線——將多個操作(擷取、轉換、轉檔)串聯成可重複使用的工作流程,並在階段之間傳遞資料。

使用方式

  1. 描述你想完成的工作
  2. 提供所需的輸入資料或檔案
  3. 我將執行適當的操作

範例提示:

  • "PDF → 擷取文字 → 翻譯 → 產生 DOCX"
  • "圖片 → OCR → 摘要 → 建立報告"
  • "Excel → 分析 → 產生圖表 → 建立 PPT"
  • "多個輸入 → 合併 → 格式化 → 輸出"

領域知識

管線架構

Stage 1      Stage 2      Stage 3      Stage 4
┌──────┐    ┌──────┐    ┌──────┐    ┌──────┐
│Extract│ → │Transform│ → │ AI   │ → │Output│
│ PDF  │    │  Data  │    │Analyze│   │ DOCX │
└──────┘    └──────┘    └──────┘    └──────┘
     │           │           │           │
     └───────────┴───────────┴───────────┘
                 Data Flow

管線 DSL(領域特定語言)

# pipeline.yaml
name: contract-review-pipeline
description: 擷取、分析並產出合約報告

stages:
  - name: extract
    operation: pdf-extraction
    input: $input_file
    output: $extracted_text
    
  - name: analyze
    operation: ai-analyze
    input: $extracted_text
    prompt: "Review this contract for risks..."
    output: $analysis
    
  - name: report
    operation: docx-generation
    input: $analysis
    template: templates/review_report.docx
    output: $output_file

Python 實作

from typing import Callable, Any
from dataclasses import dataclass

@dataclass
class Stage:
    name: str
    operation: Callable
    
class Pipeline:
    def __init__(self, name: str):
        self.name = name
        self.stages: list[Stage] = []
    
    def add_stage(self, name: str, operation: Callable):
        self.stages.append(Stage(name, operation))
        return self  # Fluent API
    
    def run(self, input_data: Any) -> Any:
        data = input_data
        for stage in self.stages:
            print(f"Running stage: {stage.name}")
            data = stage.operation(data)
        return data

# Example usage
pipeline = Pipeline("contract-review")
pipeline.add_stage("extract", extract_pdf_text)
pipeline.add_stage("analyze", analyze_with_ai)
pipeline.add_stage("generate", create_docx_report)

result = pipeline.run("/path/to/contract.pdf")

進階:條件式管線

class ConditionalPipeline(Pipeline):
    def add_conditional_stage(self, name: str, condition: Callable, 
                               if_true: Callable, if_false: Callable):
        def conditional_op(data):
            if condition(data):
                return if_true(data)
            return if_false(data)
        return self.add_stage(name, conditional_op)

# Usage
pipeline.add_conditional_stage(
    "ocr_if_needed",
    condition=lambda d: d.get("has_images"),
    if_true=run_ocr,
    if_false=lambda d: d
)

最佳實務

  1. 保持階段專注(單一職責)
  2. 使用中間輸出進行除錯
  3. 實作階段層級的錯誤處理
  4. 透過 YAML/JSON 讓管線可設定

安裝

# Install required dependencies
pip install python-docx openpyxl python-pptx reportlab jinja2

資源