Glossary
Harness
LLM client input 與 LLM output 之間的程式碼 / script 層,透過多道處理機制確保輸出的正確性與可靠性。
| 功能層 | 說明 | 實作 / 工具 |
|---|---|---|
| 輸入過濾 Input Filtering | 送入 LLM 前清除不合法字元、敏感詞或超長內容 | regex、tokenizer 截斷 |
| 參數校驗 Parameter Validation | 驗證呼叫參數格式是否正確(型別、範圍、必填欄位) | Pydantic model |
| 機械攔截 Guard / Interception | 二元決策:阻擋整個請求或回應(flow halt);過濾是改資料後繼續,攔截是直接拒絕 | NeMo Guardrails、Guardrails AI、LlamaGuard、OpenAI Moderation API |
| 格式清洗 Format Cleaning | 清理 / 正規化 LLM 輸出(去除多餘空白、修正 JSON 格式等) | 自定義 parser、json.loads + 修復 |
| 輸出過濾 Output Filtering | 過濾不符條件的輸出(格式錯誤、包含禁止詞等) | regex、Guardrails AI output validator |
| 錯誤重試 Error Retry | 偵測錯誤或不合格輸出後自動重試,直到取得正確結果 | Instructor(內建 schema + auto-retry loop) |
Instructor 和 Guardrails AI 實務上將 Guard、Validation、Retry 整合為單一 library,不需逐層自建。
flowchart TD
A[LLM Client Input] --> B[輸入過濾 Input Filtering]
B --> C[參數校驗 Parameter Validation]
C --> D[機械攔截 Guard / Interception]
D --> E[[LLM]]
E --> F[格式清洗 Format Cleaning]
F --> G[輸出過濾 Output Filtering]
G --> H{合格?}
H -- Yes --> I[LLM Output 正確結果]
H -- No --> J[錯誤重試 Error Retry]
J --> E