輕量級 LLM 模型追蹤與自動替換 Decorator
Project description
LLM Smart Route 🚀
A lightweight, non-intrusive self-healing model router for LLM applications.
llm-smart-route 是一個輕量的 Python Decorator 套件,專為解決 LLM 模型生命週期管理而生。當您指定的模型因供應商更新或棄用而失效時,此工具會自動偵測並切換至同家族的最佳候選版本,確保您的生產環境服務不中斷。
📌 目錄
✨ 核心特點
- 🧠 智慧路由 (Self-healing): 自動捕獲模型失效異常,動態查詢並切換至同家族最佳候選版本。
- 🔌 非侵入性設計: 無須改動現有 SDK 呼叫邏輯,僅需掛載一個
@smart_model裝飾器。 - 💾 持久化快取 (Persistence): 記憶成功的模型替換映射,顯著降低 API 偵測開銷。
- 🔔 主動警報: 支援 Slack、Line 與自訂通知器,讓開發者即時掌握模型更新。
- 🚀 極致輕量: 核心僅依賴標準庫與必要的供應商 SDK (可選安裝)。
- 🌍 多供應商支援: 深度整合 Anthropic, Google, OpenAI, Alibaba, Mistral 等主流 AI 模型。
🚀 快速開始
安裝
您可以根據需求選擇安裝核心功能或特定供應商支援:
# 基礎安裝
pip install llm-smart-route
# 針對特定供應商與通知功能安裝 (推薦)
pip install "llm-smart-route[gemini,slack]"
pip install "llm-smart-route[all]"
基礎用法
只需在您的模型呼叫函數上掛載裝飾器:
from llm_smart_route import smart_model
import anthropic
@smart_model(provider="claude", model="claude-3-5-sonnet-20241022")
def ask_ai(model: str, prompt: str):
# 如果 claude-3-5-sonnet 過期, Decorator 會自動將 model 替換為最新版 (例如 claude-4-5-sonnet)
client = anthropic.Anthropic()
return client.messages.create(
model=model,
messages=[{"role": "user", "content": prompt}]
)
# 像往常一樣正常呼叫
response = ask_ai(prompt="Hello, who are you?")
🤖 供應商支援矩陣
| 供應商 (Provider) | 別名 (Aliases) | 建議標籤 | 支援 SDK |
|---|---|---|---|
| Anthropic | claude, anthropic |
haiku, sonnet, opus |
anthropic |
gemini, google |
flash, pro |
google-genai |
|
| OpenAI | openai, gpt |
mini, o1, gpt-4o |
openai |
| Alibaba | qwen, alibaba |
turbo, plus, max |
dashscope |
| Mistral | mistral |
small, medium, large |
mistralai |
⚙️ 進階配置
通知功能
當發生模型自動替換時,您可以設定主動通知:
from llm_smart_route import smart_model, create_slack_notifier
slack_notify = create_slack_notifier() # 預設讀取環境變數 SLACK_BOT_TOKEN
@smart_model(provider="openai", model="gpt-4o-mini", on_replace=slack_notify)
def call_gpt(model, prompt):
...
持久化快取
您可以自訂快取檔案路徑,以便在不同的執行環境中共享模型映射:
from llm_smart_route import configure_cache
configure_cache(cache_path="./data/router_cache.json")
🛠️ 工作原理
llm-smart-route 採用「攔截-偵測-修復」的工作流:
- 攔截: Decorator 接入並讀取配置。
- 偵測: 執行函數,若觸發
ModelNotFoundError類型的異常,則進入自動修復。 - 修復:
- 查詢 Provider API 獲取 Active 模型清單。
- 基於語意與關鍵字匹配同系列模型。
- 依據版本號進行排序與選擇。
- 驗證與快取: 使用新模型重試,成功後將映射存入快取文件,效期內不再重複查詢。
📖 文檔與範例
🧪 開發與測試
本專案使用 Poetry 進行依賴管理與 pytest 測試。
# 安裝開發依賴
poetry install
# 執行單元測試
poetry run pytest tests/unit/
📄 授權
基於 MIT License 開源。詳見 LICENSE 文件。
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file llm_smart_route-0.1.1.tar.gz.
File metadata
- Download URL: llm_smart_route-0.1.1.tar.gz
- Upload date:
- Size: 21.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bdf98bdb60dad9de255966c89c112c474311e84f378675b7763d2243f1f0286c
|
|
| MD5 |
1b847467d9f842dbd26829dde625c59c
|
|
| BLAKE2b-256 |
65f66ab3a33b225466e4a11927bb34a3d7eefe07b6bf3b22323d6ee900548fb5
|
Provenance
The following attestation bundles were made for llm_smart_route-0.1.1.tar.gz:
Publisher:
publish.yml on YuChieh-Chiu/LLM-Smart-Route
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
llm_smart_route-0.1.1.tar.gz -
Subject digest:
bdf98bdb60dad9de255966c89c112c474311e84f378675b7763d2243f1f0286c - Sigstore transparency entry: 829092776
- Sigstore integration time:
-
Permalink:
YuChieh-Chiu/LLM-Smart-Route@6adc47e8073f8ae32fccb555a2cb97f89aa1e147 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/YuChieh-Chiu
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@6adc47e8073f8ae32fccb555a2cb97f89aa1e147 -
Trigger Event:
push
-
Statement type:
File details
Details for the file llm_smart_route-0.1.1-py3-none-any.whl.
File metadata
- Download URL: llm_smart_route-0.1.1-py3-none-any.whl
- Upload date:
- Size: 28.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
06d5cf44de97a1ec01c468ab2cea2e6babdb2ff1da2108ee81be4129ee2a79f2
|
|
| MD5 |
e79bcd1e4b2b21edc6b24030e67c8737
|
|
| BLAKE2b-256 |
e30a206962698ca2c83240aad3a5ec40a33ef1ca11d5254030feed77b69d1fb4
|
Provenance
The following attestation bundles were made for llm_smart_route-0.1.1-py3-none-any.whl:
Publisher:
publish.yml on YuChieh-Chiu/LLM-Smart-Route
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
llm_smart_route-0.1.1-py3-none-any.whl -
Subject digest:
06d5cf44de97a1ec01c468ab2cea2e6babdb2ff1da2108ee81be4129ee2a79f2 - Sigstore transparency entry: 829092780
- Sigstore integration time:
-
Permalink:
YuChieh-Chiu/LLM-Smart-Route@6adc47e8073f8ae32fccb555a2cb97f89aa1e147 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/YuChieh-Chiu
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@6adc47e8073f8ae32fccb555a2cb97f89aa1e147 -
Trigger Event:
push
-
Statement type: