lydgs commited on
Commit
2e775ec
·
verified ·
1 Parent(s): 78bc769

Update sync_cliproxy_cleanup.py

Browse files
Files changed (1) hide show
  1. sync_cliproxy_cleanup.py +41 -27
sync_cliproxy_cleanup.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import os
2
  import time
3
  import requests
@@ -15,7 +16,7 @@ SYNC_INTERVAL = int(os.environ.get("SYNC_INTERVAL_SECONDS", 3600))
15
 
16
  SYNC_TAG = "cliproxy-synced"
17
  MASTER_HEADERS = {"Authorization": f"Bearer {LITELLM_MASTER_KEY}"}
18
- UPDATE_MODEL_URL = f"{LITELLM_BASE_URL}/model/update" # 用于更新已存在的模型
19
 
20
  # ---------- 等待 LiteLLM 就绪 ----------
21
  def wait_for_litellm_ready(timeout: int = 180):
@@ -34,7 +35,6 @@ def wait_for_litellm_ready(timeout: int = 180):
34
 
35
  # ---------- 检查凭证是否可用 ----------
36
  def credential_exists(credential_name: str) -> bool:
37
- """检查凭证是否存在,并输出详细的调试信息"""
38
  try:
39
  resp = requests.get(f"{LITELLM_BASE_URL}/credentials", headers=MASTER_HEADERS)
40
  if resp.status_code != 200:
@@ -76,22 +76,40 @@ def get_existing_model_ids() -> Set[str]:
76
  resp.raise_for_status()
77
  return {m["id"] for m in resp.json().get("data", [])}
78
 
79
- # ---------- 判断是否为同步模型 ----------
80
- def is_synced_model(model_id: str) -> bool:
81
- if "/" not in model_id:
82
- return False
83
- prefix, name = model_id.split("/", 1)
84
- return prefix != "openai" and len(name) > 0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
- # ---------- 模型删除 ----------
87
- def delete_model(model_name: str):
88
- # 使用 POST 方法,避免 405 错误
89
  resp = requests.post(
90
  f"{LITELLM_BASE_URL}/model/delete",
91
  json={"model_name": model_name},
92
  headers=MASTER_HEADERS,
93
  )
94
- return resp.status_code
 
 
 
 
 
95
 
96
  # ---------- 模型添加 ----------
97
  def add_model_to_litellm(original_id: str, owner: str, use_credential: bool) -> bool:
@@ -131,16 +149,15 @@ def add_model_to_litellm(original_id: str, owner: str, use_credential: bool) ->
131
  # ---------- 更新已存在模型为凭证模式 ----------
132
  def update_model_credential(model_name: str, use_credential: bool) -> bool:
133
  if not use_credential:
134
- return True # 未启用凭证,无需更新
135
 
136
  update_payload = {
137
  "model_name": model_name,
138
  "litellm_params": {
139
- "litellm_credential_name": CREDENTIAL_NAME,
140
- "api_base": None, # 清除旧的 api_base,防止凭证冲突
141
- "api_key": None, # 清除旧的 api_key
142
  }
143
  }
 
144
  try:
145
  resp = requests.post(UPDATE_MODEL_URL, json=update_payload, headers=MASTER_HEADERS)
146
  if resp.status_code in (200, 201):
@@ -192,31 +209,27 @@ def sync():
192
  cliproxy_map[new_name] = owner
193
 
194
  existing_ids = get_existing_model_ids()
 
195
 
196
  added = 0
197
  migrated = 0
198
  for new_name, owner in cliproxy_map.items():
199
  if new_name not in existing_ids:
200
- # 新模型:直接添加
201
  if add_model_to_litellm(new_name.split("/", 1)[1], owner, use_credential):
202
  print(f" ➕ 新增: {new_name}")
203
  added += 1
204
  else:
205
- # 已存在模型:如果启用了凭证,则尝试将其迁移到凭证模式
206
  if use_credential:
207
  if update_model_credential(new_name, use_credential):
208
  migrated += 1
209
 
210
- # 删除失效模型
211
  deleted = 0
212
- for mid in list(existing_ids):
213
- if is_synced_model(mid) and mid not in cliproxy_map:
214
- status = delete_model(mid)
215
- if status == 200:
216
- print(f" 🗑️ 删除失效模型: {mid}")
217
  deleted += 1
218
- else:
219
- print(f" ❌ 删除 {mid} 失败 (status {status})")
220
 
221
  current_valid_models = list(cliproxy_map.keys())
222
  update_fallback_chain(current_valid_models)
@@ -229,4 +242,5 @@ if __name__ == "__main__":
229
  print("🚀 守护同步任务启动,间隔 " + str(SYNC_INTERVAL) + " 秒")
230
  while True:
231
  sync()
232
- time.sleep(SYNC_INTERVAL)
 
 
1
+ ```python
2
  import os
3
  import time
4
  import requests
 
16
 
17
  SYNC_TAG = "cliproxy-synced"
18
  MASTER_HEADERS = {"Authorization": f"Bearer {LITELLM_MASTER_KEY}"}
19
+ UPDATE_MODEL_URL = f"{LITELLM_BASE_URL}/model/update"
20
 
21
  # ---------- 等待 LiteLLM 就绪 ----------
22
  def wait_for_litellm_ready(timeout: int = 180):
 
35
 
36
  # ---------- 检查凭证是否可用 ----------
37
  def credential_exists(credential_name: str) -> bool:
 
38
  try:
39
  resp = requests.get(f"{LITELLM_BASE_URL}/credentials", headers=MASTER_HEADERS)
40
  if resp.status_code != 200:
 
76
  resp.raise_for_status()
77
  return {m["id"] for m in resp.json().get("data", [])}
78
 
79
+ # ---------- 获取所有模型的完整信息(包含 tags)----------
80
+ def get_existing_models_full() -> List[Dict]:
81
+ """返回完整的模型信息列表,用于判断标签"""
82
+ resp = requests.get(f"{LITELLM_BASE_URL}/v1/models", headers=MASTER_HEADERS)
83
+ resp.raise_for_status()
84
+ return resp.json().get("data", [])
85
+
86
+ # ---------- 判断是否为同步模型(通过标签)----------
87
+ def is_synced_model(model_id: str, models_full: List[Dict]) -> bool:
88
+ """通过模型的 owned_by 和 tags 判断是否由本脚本同步生成"""
89
+ for model in models_full:
90
+ if model["id"] == model_id:
91
+ # 如果有 tags 且包含 SYNC_TAG
92
+ if "tags" in model and SYNC_TAG in model["tags"]:
93
+ return True
94
+ # 退一步:owned_by 不是 openai 且包含斜杠
95
+ owner = model.get("owned_by", "unknown")
96
+ if "/" in model_id and owner != "openai":
97
+ return True
98
+ return False
99
 
100
+ # ---------- 模型删除 (接受200/204) ----------
101
+ def delete_model(model_name: str) -> bool:
 
102
  resp = requests.post(
103
  f"{LITELLM_BASE_URL}/model/delete",
104
  json={"model_name": model_name},
105
  headers=MASTER_HEADERS,
106
  )
107
+ if resp.status_code in [200, 204]:
108
+ print(f" 🗑️ 成功删除模型: {model_name}")
109
+ return True
110
+ else:
111
+ print(f" ❌ 删除模型 {model_name} 失败,状态码: {resp.status_code}, 响应: {resp.text}")
112
+ return False
113
 
114
  # ---------- 模型添加 ----------
115
  def add_model_to_litellm(original_id: str, owner: str, use_credential: bool) -> bool:
 
149
  # ---------- 更新已存在模型为凭证模式 ----------
150
  def update_model_credential(model_name: str, use_credential: bool) -> bool:
151
  if not use_credential:
152
+ return True
153
 
154
  update_payload = {
155
  "model_name": model_name,
156
  "litellm_params": {
157
+ "litellm_credential_name": CREDENTIAL_NAME
 
 
158
  }
159
  }
160
+
161
  try:
162
  resp = requests.post(UPDATE_MODEL_URL, json=update_payload, headers=MASTER_HEADERS)
163
  if resp.status_code in (200, 201):
 
209
  cliproxy_map[new_name] = owner
210
 
211
  existing_ids = get_existing_model_ids()
212
+ existing_full = get_existing_models_full()
213
 
214
  added = 0
215
  migrated = 0
216
  for new_name, owner in cliproxy_map.items():
217
  if new_name not in existing_ids:
 
218
  if add_model_to_litellm(new_name.split("/", 1)[1], owner, use_credential):
219
  print(f" ➕ 新增: {new_name}")
220
  added += 1
221
  else:
 
222
  if use_credential:
223
  if update_model_credential(new_name, use_credential):
224
  migrated += 1
225
 
226
+ # 删除失效模型(使用标签识别)
227
  deleted = 0
228
+ for model_full in existing_full:
229
+ model_id = model_full["id"]
230
+ if is_synced_model(model_id, existing_full) and model_id not in cliproxy_map:
231
+ if delete_model(model_id):
 
232
  deleted += 1
 
 
233
 
234
  current_valid_models = list(cliproxy_map.keys())
235
  update_fallback_chain(current_valid_models)
 
242
  print("🚀 守护同步任务启动,间隔 " + str(SYNC_INTERVAL) + " 秒")
243
  while True:
244
  sync()
245
+ time.sleep(SYNC_INTERVAL)
246
+ ```