tanbushi commited on
Commit
29bd9ed
·
1 Parent(s): e9670f4
Files changed (2) hide show
  1. api_key_sb.py +27 -0
  2. app.py +7 -0
api_key_sb.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from supabase import create_client, Client
2
+ import os
3
+ from fastapi import FastAPI, Request, HTTPException, Depends, status
4
+
5
+ # Supabase 配置
6
+ SUPABASE_URL = os.getenv("SUPABASE_URL")
7
+ SUPABASE_KEY = os.getenv("SUPABASE_KEY")
8
+
9
+ # 初始化 Supabase 客户端
10
+ supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
11
+ async def get_api_key_info(model: str = None):
12
+ try:
13
+ # 根据用户提供的SQL语句修改,从 'airs_model_api_keys_view' 视图中获取 'api_key'
14
+ if model:
15
+ response = supabase.from_('airs_model_api_keys_view').select('api_key', 'api_key_id').eq('model_name', model).order('api_key_ran_at', desc=False).limit(1).execute()
16
+ else:
17
+ # 如果没有提供model,则获取所有模型的api_key
18
+ raise HTTPException(status_code=400, detail="请提供模型名称!")
19
+
20
+ if response.data:
21
+ api_key_info = response.data[0]
22
+ return api_key_info
23
+ else:
24
+ return None
25
+ except Exception as e:
26
+ print(f"从Supabase获取API密钥失败: {e}")
27
+ return None
app.py CHANGED
@@ -8,6 +8,7 @@ import os, json
8
  from dotenv import load_dotenv
9
  from enum import Enum
10
  from proxy import do_proxy
 
11
 
12
  # 加载环境变量
13
  load_dotenv()
@@ -67,8 +68,14 @@ async def proxy_gemini(request: Request, protocol: ProtocolType, host:str, path:
67
 
68
  # 移除FastAPI自带的Host头,避免Gemini校验报错
69
  client_headers.pop("host", None)
 
70
  # 将User-Agent改为curl/8.7.1,以模拟curl请求
71
  client_headers["User-Agent"] = "curl/8.7.1"
 
 
 
 
 
72
 
73
  # 处理Gemini API密钥轮换和Authorization头
74
  global key_index # 声明使用全局变量
 
8
  from dotenv import load_dotenv
9
  from enum import Enum
10
  from proxy import do_proxy
11
+ from api_key_sb import get_api_key_info
12
 
13
  # 加载环境变量
14
  load_dotenv()
 
68
 
69
  # 移除FastAPI自带的Host头,避免Gemini校验报错
70
  client_headers.pop("host", None)
71
+
72
  # 将User-Agent改为curl/8.7.1,以模拟curl请求
73
  client_headers["User-Agent"] = "curl/8.7.1"
74
+
75
+ # 获取API密钥信息
76
+ api_key_info = await get_api_key_info()
77
+
78
+ return api_key_info
79
 
80
  # 处理Gemini API密钥轮换和Authorization头
81
  global key_index # 声明使用全局变量