Skip to content

Commit f3cb3da

Browse files
authored
Merge pull request wyeeeee#212 from wyeeeee/dev
Dev
2 parents 5ec39fe + 44bc484 commit f3cb3da

3 files changed

Lines changed: 120 additions & 19 deletions

File tree

app/config/persistence.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,56 @@ def load_settings():
9393
else:
9494
setattr(settings, name, value)
9595

96+
# 在加载完设置后,检查是否需要刷新模型配置
97+
try:
98+
# 如果加载了Google Credentials JSON或Vertex Express API Key,需要刷新模型配置
99+
if (hasattr(settings, 'GOOGLE_CREDENTIALS_JSON') and settings.GOOGLE_CREDENTIALS_JSON) or \
100+
(hasattr(settings, 'VERTEX_EXPRESS_API_KEY') and settings.VERTEX_EXPRESS_API_KEY):
101+
log('info', "检测到Google Credentials JSON或Vertex Express API Key,准备刷新模型配置")
102+
103+
# 导入必要的模块
104+
from app.vertex.model_loader import refresh_models_config_cache
105+
from app.vertex.vertex_ai_init import init_vertex_ai
106+
from app.vertex.credentials_manager import CredentialManager
107+
108+
# 创建新的CredentialManager实例
109+
credential_manager = CredentialManager()
110+
111+
# 如果有Google Credentials JSON,加载到CredentialManager
112+
if hasattr(settings, 'GOOGLE_CREDENTIALS_JSON') and settings.GOOGLE_CREDENTIALS_JSON:
113+
from app.vertex.credentials_manager import parse_multiple_json_credentials
114+
parsed_json_objects = parse_multiple_json_credentials(settings.GOOGLE_CREDENTIALS_JSON)
115+
if parsed_json_objects:
116+
loaded_count = credential_manager.load_credentials_from_json_list(parsed_json_objects)
117+
log('info', f"从持久化的Google Credentials JSON中加载了{loaded_count}个凭据")
118+
119+
# 初始化Vertex AI
120+
import asyncio
121+
loop = asyncio.new_event_loop()
122+
asyncio.set_event_loop(loop)
123+
try:
124+
success = loop.run_until_complete(init_vertex_ai(credential_manager=credential_manager))
125+
if success:
126+
log('info', "成功初始化Vertex AI服务")
127+
else:
128+
log('warning', "初始化Vertex AI服务失败")
129+
finally:
130+
loop.close()
131+
132+
# 刷新模型配置缓存
133+
loop = asyncio.new_event_loop()
134+
asyncio.set_event_loop(loop)
135+
try:
136+
refresh_success = loop.run_until_complete(refresh_models_config_cache())
137+
if refresh_success:
138+
log('info', "成功刷新模型配置缓存")
139+
else:
140+
log('warning', "刷新模型配置缓存失败")
141+
finally:
142+
loop.close()
143+
except Exception as e:
144+
log('error', f"刷新模型配置时出错: {str(e)}")
145+
96146
log('info', f"加载设置成功")
97147
return True
98148
except Exception as e:

app/vertex/main.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,35 @@
4646

4747
@app.on_event("startup")
4848
async def startup_event():
49-
if await init_vertex_ai(credential_manager): # Added await
50-
vertex_log('info', "Vertex AI credential and model config initialization check completed successfully.")
51-
else:
52-
vertex_log('error', "Failed to initialize a fallback Vertex AI client. API will likely fail.")
49+
try:
50+
# 检查是否有Google Credentials JSON
51+
if hasattr(settings, 'GOOGLE_CREDENTIALS_JSON') and settings.GOOGLE_CREDENTIALS_JSON:
52+
vertex_log('info', "检测到持久化的Google Credentials JSON,准备加载")
53+
from app.vertex.credentials_manager import parse_multiple_json_credentials
54+
parsed_json_objects = parse_multiple_json_credentials(settings.GOOGLE_CREDENTIALS_JSON)
55+
if parsed_json_objects:
56+
loaded_count = credential_manager.load_credentials_from_json_list(parsed_json_objects)
57+
vertex_log('info', f"从持久化的Google Credentials JSON中加载了{loaded_count}个凭据")
58+
59+
# 检查是否有Vertex Express API Key
60+
if hasattr(settings, 'VERTEX_EXPRESS_API_KEY') and settings.VERTEX_EXPRESS_API_KEY:
61+
vertex_log('info', "检测到持久化的Vertex Express API Key")
62+
63+
# 初始化Vertex AI
64+
if await init_vertex_ai(credential_manager):
65+
vertex_log('info', "Vertex AI credential and model config initialization check completed successfully.")
66+
67+
# 刷新模型配置缓存
68+
from app.vertex.model_loader import refresh_models_config_cache
69+
refresh_success = await refresh_models_config_cache()
70+
if refresh_success:
71+
vertex_log('info', "成功刷新模型配置缓存")
72+
else:
73+
vertex_log('warning', "刷新模型配置缓存失败")
74+
else:
75+
vertex_log('error', "Failed to initialize a fallback Vertex AI client. API will likely fail.")
76+
except Exception as e:
77+
vertex_log('error', f"启动时初始化Vertex AI服务出错: {str(e)}")
5378

5479
@app.get("/")
5580
async def root():

app/vertex/routes/models_api.py

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -195,45 +195,70 @@ async def list_models(fastapi_request: Request, api_key: str = Depends(get_api_k
195195
# Add base models and their variations
196196
for original_model_id in sorted(list(all_model_ids)):
197197
current_display_prefix = ""
198-
# Only add PAY_PREFIX if the model is not already an EXPRESS model (which has its own prefix)
199-
if not original_model_id.startswith("[EXPRESS]") and \
200-
has_sa_creds and not has_express_key and EXPERIMENTAL_MARKER not in original_model_id:
198+
# 检查是否为[EXPRESS]模型,保留原始前缀
199+
is_express_model = original_model_id.startswith("[EXPRESS]")
200+
base_model_without_prefix = original_model_id
201+
202+
# 如果是EXPRESS模型,移除前缀用于后续处理,但在显示时会保留
203+
if is_express_model:
204+
# 从原始ID中提取不带[EXPRESS]前缀的模型名
205+
base_model_without_prefix = original_model_id[len("[EXPRESS] "):]
206+
vertex_log('info', f"处理EXPRESS模型: {original_model_id}, 基础名称: {base_model_without_prefix}")
207+
208+
# 只有非EXPRESS模型才考虑添加PAY_PREFIX
209+
if not is_express_model and has_sa_creds and EXPERIMENTAL_MARKER not in original_model_id:
210+
# 只要有SA凭证就应该显示PAY前缀,不管是否有Express Key
201211
current_display_prefix = PAY_PREFIX
212+
vertex_log('info', f"为非EXPRESS模型添加PAY前缀: {current_display_prefix}{original_model_id}")
202213

203-
base_display_id = f"{current_display_prefix}{original_model_id}"
214+
# 构建显示ID,如果是EXPRESS模型,确保保留[EXPRESS]前缀
215+
base_display_id = original_model_id if is_express_model else f"{current_display_prefix}{original_model_id}"
204216

217+
vertex_log('info', f"添加模型到列表: {base_display_id}")
205218
dynamic_models_data.append({
206219
"id": base_display_id, "object": "model", "created": current_time, "owned_by": "google",
207-
"permission": [], "root": original_model_id, "parent": None
220+
"permission": [], "root": base_model_without_prefix, "parent": None
208221
})
209222

210223
# Conditionally add common variations (standard suffixes)
211-
if not original_model_id.startswith("gemini-2.0"): # Suffix rules based on original_model_id
224+
if not base_model_without_prefix.startswith("gemini-2.0"): # Suffix rules based on original_model_id
212225
standard_suffixes = ["-search", "-encrypt", "-encrypt-full", "-auto"]
213226
for suffix in standard_suffixes:
214-
# Suffix is applied to the original model ID part
215-
suffixed_model_part = f"{original_model_id}{suffix}"
216-
# Then the whole thing is prefixed
217-
final_suffixed_display_id = f"{current_display_prefix}{suffixed_model_part}"
227+
# Suffix is applied to the original model ID part (without EXPRESS prefix)
228+
suffixed_model_part = f"{base_model_without_prefix}{suffix}"
229+
# Then the whole thing is prefixed - retain EXPRESS prefix if it was there
230+
final_suffixed_display_id = ""
231+
if is_express_model:
232+
final_suffixed_display_id = f"[EXPRESS] {suffixed_model_part}"
233+
else:
234+
# 非EXPRESS模型的后缀版本也需要正确显示PAY前缀
235+
final_suffixed_display_id = f"{current_display_prefix}{suffixed_model_part}"
218236

219237
# Check if this suffixed ID is already in all_model_ids (unlikely with prefix) or already added
220238
if final_suffixed_display_id not in all_model_ids and not any(m['id'] == final_suffixed_display_id for m in dynamic_models_data):
239+
vertex_log('info', f"添加后缀模型到列表: {final_suffixed_display_id}")
221240
dynamic_models_data.append({
222241
"id": final_suffixed_display_id, "object": "model", "created": current_time, "owned_by": "google",
223-
"permission": [], "root": original_model_id, "parent": None
242+
"permission": [], "root": base_model_without_prefix, "parent": None
224243
})
225244

226245
# Apply special suffixes for models starting with "gemini-2.5-flash"
227-
if original_model_id.startswith("gemini-2.5-flash"): # Suffix rules based on original_model_id
246+
if base_model_without_prefix.startswith("gemini-2.5-flash"): # Suffix rules based on original_model_id
228247
special_flash_suffixes = ["-nothinking", "-max"]
229248
for special_suffix in special_flash_suffixes:
230-
suffixed_model_part = f"{original_model_id}{special_suffix}"
231-
final_special_suffixed_display_id = f"{current_display_prefix}{suffixed_model_part}"
249+
suffixed_model_part = f"{base_model_without_prefix}{special_suffix}"
250+
# Retain EXPRESS prefix if original model had it
251+
if is_express_model:
252+
final_special_suffixed_display_id = f"[EXPRESS] {suffixed_model_part}"
253+
else:
254+
# 非EXPRESS模型的特殊后缀版本也需要正确显示PAY前缀
255+
final_special_suffixed_display_id = f"{current_display_prefix}{suffixed_model_part}"
232256

233257
if final_special_suffixed_display_id not in all_model_ids and not any(m['id'] == final_special_suffixed_display_id for m in dynamic_models_data):
258+
vertex_log('info', f"添加特殊后缀模型到列表: {final_special_suffixed_display_id}")
234259
dynamic_models_data.append({
235260
"id": final_special_suffixed_display_id, "object": "model", "created": current_time, "owned_by": "google",
236-
"permission": [], "root": original_model_id, "parent": None
261+
"permission": [], "root": base_model_without_prefix, "parent": None
237262
})
238263

239264
# Ensure uniqueness again after adding suffixes
@@ -250,6 +275,7 @@ async def list_models(fastapi_request: Request, api_key: str = Depends(get_api_k
250275
if EXPERIMENTAL_MARKER in base_model_id_for_openai:
251276
display_model_id = f"{base_model_id_for_openai}{OPENAI_DIRECT_SUFFIX}"
252277
else:
278+
# OpenAI直接模式下也应该保持PAY前缀
253279
display_model_id = f"{PAY_PREFIX}{base_model_id_for_openai}{OPENAI_DIRECT_SUFFIX}"
254280

255281
# Check if already added (e.g. if remote config somehow already listed it or added as a base model)

0 commit comments

Comments
 (0)