tanbushi commited on
Commit
3375e89
·
1 Parent(s): c250ca9
Files changed (1) hide show
  1. app.py +20 -5
app.py CHANGED
@@ -55,6 +55,9 @@ async def health_check():
55
  @app.api_route("/v1/{protocol}/{host}/{path:path}", methods=["GET", "POST", "PUT", "DELETE"])
56
  # @app.api_route("/v1/{protocol}/{host}/{path:path}", methods=["POST"])
57
  async def proxy(request: Request, protocol: ProtocolType, host:str, path: str, proxy_api_key: str = Depends(verify_proxy_api_key)): # 添加代理认证依赖
 
 
 
58
  real_url = f"{protocol.value}://{host}/{path}"
59
 
60
  # 提取客户端请求的headers和body,透传给Gemini
@@ -114,26 +117,38 @@ async def proxy(request: Request, protocol: ProtocolType, host:str, path: str, p
114
  )
115
 
116
  except httpx.RequestError as e:
 
 
 
 
 
117
  raise HTTPException(status_code=500, detail=f"转发请求失败:{str(e)}")
118
  except httpx.HTTPStatusError as e:
 
 
 
119
  raise HTTPException(status_code=e.response.status_code, detail=f"目标API返回错误:{e.response.text}")
120
  except Exception as e:
 
 
 
 
121
  raise HTTPException(status_code=500, detail=f"转发失败:{str(e)}")
122
 
123
  async def get_api_key(model: str = None):
124
  try:
125
  # 根据用户提供的SQL语句修改,从 'airs_model_api_keys_view' 视图中获取 'api_key'
126
  if model:
127
- response = supabase.from_('airs_model_api_keys_view').select('api_key').eq('model_name', model).execute()
128
  # print(response.data)
129
  else:
130
  # 如果没有提供model,则获取所有模型的api_key
131
- response = supabase.from_('airs_model_api_keys_view').select('api_key').execute()
 
132
 
133
  if response.data:
134
- # 随机选择一个API密钥
135
- import random
136
- return random.choice([item['api_key'] for item in response.data])
137
  else:
138
  return None
139
  except Exception as e:
 
55
  @app.api_route("/v1/{protocol}/{host}/{path:path}", methods=["GET", "POST", "PUT", "DELETE"])
56
  # @app.api_route("/v1/{protocol}/{host}/{path:path}", methods=["POST"])
57
  async def proxy(request: Request, protocol: ProtocolType, host:str, path: str, proxy_api_key: str = Depends(verify_proxy_api_key)): # 添加代理认证依赖
58
+
59
+ print('\n\n======================================')
60
+
61
  real_url = f"{protocol.value}://{host}/{path}"
62
 
63
  # 提取客户端请求的headers和body,透传给Gemini
 
117
  )
118
 
119
  except httpx.RequestError as e:
120
+ print('\n\n----------------- 1 ------------------')
121
+ print(str(e))
122
+ print('\n\n----------------- 2 ------------------')
123
+ # return
124
+ # raise HTTPException(status_code=e.response.status_code, detail=f"转发请求失败:{str(e)}")
125
  raise HTTPException(status_code=500, detail=f"转发请求失败:{str(e)}")
126
  except httpx.HTTPStatusError as e:
127
+ print('\n\n----------------- 3 ------------------')
128
+ print(str(e))
129
+ print('\n\n----------------- 4 ------------------')
130
  raise HTTPException(status_code=e.response.status_code, detail=f"目标API返回错误:{e.response.text}")
131
  except Exception as e:
132
+ print('\n\n----------------- 5 ------------------')
133
+ print(str(e))
134
+ print('\n\n----------------- 6 ------------------')
135
+ # raise HTTPException(status_code=e.response.status_code, detail=f"转发失败:{str(e)}")
136
  raise HTTPException(status_code=500, detail=f"转发失败:{str(e)}")
137
 
138
  async def get_api_key(model: str = None):
139
  try:
140
  # 根据用户提供的SQL语句修改,从 'airs_model_api_keys_view' 视图中获取 'api_key'
141
  if model:
142
+ response = supabase.from_('airs_model_api_keys_view').select('api_key').eq('model_name', model).order('api_key_ran_at', desc=False).limit(1).execute()
143
  # print(response.data)
144
  else:
145
  # 如果没有提供model,则获取所有模型的api_key
146
+ raise HTTPException(status_code=400, detail="请提供模型名称!")
147
+ # response = supabase.from_('airs_model_api_keys_view').select('api_key').order('api_key_ran_at', desc=False).limit(1).execute()
148
 
149
  if response.data:
150
+ # 由于已经limit(1),直接返回第一条记录
151
+ return response.data[0]['api_key']
 
152
  else:
153
  return None
154
  except Exception as e: