-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
267 lines (225 loc) · 10.2 KB
/
main.py
File metadata and controls
267 lines (225 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
from fastapi import FastAPI, Request, Response, HTTPException
from fastapi.responses import JSONResponse
import os
import csv
from datetime import datetime
from threading import Lock
import logging
import json
import httpx
from dotenv import load_dotenv
import openai
from functools import lru_cache
from typing import Dict, List, Optional
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
filename='service.log'
)
app = FastAPI(docs_url=None, redoc_url=None)
# Lock for thread-safe writing to the log file
log_lock = Lock()
# Function to load API keys and server details from the .env file
def load_config(file_path):
load_dotenv(file_path)
api_keys = {}
servers = {}
# Extract API keys that start with "username_"
for key, value in os.environ.items():
if key.startswith("username_"):
api_keys[key.split('username_')[1]] = value
elif key.startswith("SERVER_"):
server_name = key.split('SERVER_')[1].lower()
server_config = value.split(',')
servers[server_name] = {
'url': server_config[0].strip(),
'api_key': server_config[1].strip() if len(server_config) > 1 else None
}
# Ensure OpenAI is always present as the default server
if 'openai' not in servers:
servers['openai'] = {
'url': "https://api.openai.com",
'api_key': os.getenv("OPENAI_API_KEY")
}
return api_keys, servers
@lru_cache(maxsize=128)
def get_server_models(server_name: str) -> List[str]:
server = SERVERS.get(server_name)
if not server:
return []
with httpx.Client() as client:
headers = {"Content-Type": "application/json"}
if server['api_key']:
headers["Authorization"] = f"Bearer {server['api_key']}"
response = client.get(
f"{server['url']}/v1/models",
headers=headers,
)
if response.status_code == 200:
return [model['id'] for model in response.json().get("data", [])]
else:
print(f"Error fetching models for {server_name}: {response.content}")
return []
# Load configuration from .env
API_KEYS_FILE_PATH = ".env"
VALID_API_KEYS, SERVERS = load_config(API_KEYS_FILE_PATH)
# Define a middleware function to log requests
@app.middleware("http")
async def log_requests(request: Request, call_next):
# Log the request details
log_line = f"Time: {datetime.now()}, Method: {request.method}, Path: {request.url.path}\n"
logging.info(log_line)
# Proceed with the request
response = await call_next(request)
return response
# Function to log API usage
def log_api_usage(api_key, endpoint, request_headers="none", request_body="none"):
log_file_path = "./api_usage.json"
username = next((user for user, key in VALID_API_KEYS.items() if key == api_key), None)
with log_lock:
try:
# Read existing data
try:
with open(log_file_path, "r") as log_file:
log_data = json.load(log_file)
except (FileNotFoundError, json.JSONDecodeError):
log_data = []
# Append new entry
log_data.append({
"timestamp": str(datetime.now()),
"username": username,
"api_key": api_key,
"endpoint": endpoint,
"request_headers": request_headers,
"request_body": request_body
})
# Write updated data
with open(log_file_path, "w") as log_file:
json.dump(log_data, log_file, indent=2)
except Exception as e:
logging.error(f"Error writing to api_usage.json: {e}")
# Function to log invalid API usage
def log_invalid_api_usage(api_key, endpoint, request_headers="none", request_body="none"):
log_file_path = "./invalid_api_usage.json"
with log_lock:
try:
# Read existing data
try:
with open(log_file_path, "r") as log_file:
log_data = json.load(log_file)
except (FileNotFoundError, json.JSONDecodeError):
log_data = []
# Append new entry
log_data.append({
"timestamp": str(datetime.now()),
"api_key": api_key,
"endpoint": endpoint,
"request_headers": request_headers,
"request_body": request_body
})
# Write updated data
with open(log_file_path, "w") as log_file:
json.dump(log_data, log_file, indent=2)
except Exception as e:
logging.error(f"Error writing to invalid_api_usage.json: {e}")
# Function to get the API usage logs
@app.api_route("/api_usage", methods=["GET"])
async def get_api_usage(request: Request):
authorization: str = request.headers.get("Authorization", "")
if not authorization.startswith("Bearer "):
log_invalid_api_usage(api_key="no_api_key", endpoint="/validate")
return Response("Invalid API Key format", status_code=400, headers={"Proxy-Status": "invalid_api_key_format"})
api_key = authorization[7:] # Remove the 'Bearer ' prefix
if api_key in VALID_API_KEYS.values():
log_file_path = "./api_usage.json"
try:
with log_lock:
with open(log_file_path, "r") as log_file:
log_data = json.load(log_file)
log_api_usage(api_key, "/api_usage")
return JSONResponse({"data": log_data}, status_code=200)
except Exception as e:
logging.error(f"Error reading from api_usage.json: {e}")
raise HTTPException(status_code=500, detail="Failed to read usage data")
else:
log_invalid_api_usage(api_key, "/api_usage")
return Response("Invalid API Key", status_code=401, headers={"Proxy-Status": "invalid_api_key"})
@app.api_route("/service_log", methods=["GET"])
async def get_service_log(request: Request):
authorization: str = request.headers.get("Authorization", "")
if not authorization:
log_invalid_api_usage(api_key="no_api_key", endpoint="/service_log")
return Response("Invalid API Key format", status_code=400, headers={"Proxy-Status": "invalid_api_key_format"})
api_key = authorization[7:]
if api_key in VALID_API_KEYS.values():
log_api_usage(api_key, "/service_log")
try:
with open("service.log", "r") as log_file:
log_data = log_file.read()
return Response(log_data, status_code=200)
except Exception as e:
logging.error(f"Error reading service.log: {e}")
raise HTTPException(status_code=500, detail="Failed to read service log")
else:
log_invalid_api_usage(api_key, "/service_log")
return Response("Invalid API Key", status_code=401, headers={"Proxy-Status": "invalid_api"})
@app.api_route("/models", methods=["GET"])
async def get_models(request: Request):
authorization: str = request.headers.get("Authorization", "")
if not authorization:
log_invalid_api_usage(api_key="no_api_key", endpoint="/models")
return Response("Invalid API Key format", status_code=400, headers={"Proxy-Status": "invalid_api_key_format"})
api_key = authorization[7:] # Remove the 'Bearer ' prefix
if api_key.replace('"', '') in VALID_API_KEYS.values():
log_api_usage(api_key, "/models")
all_models = {server: get_server_models(server) for server in SERVERS}
return JSONResponse(all_models, status_code=200)
else:
log_invalid_api_usage(api_key, "/models")
return Response("Invalid API Key", status_code=401, headers={"Proxy-Status": "invalid_api"})
@app.api_route("/ping", methods=["GET"])
async def ping(request: Request):
return Response("Pong", status_code=200)
@app.api_route("/", methods=["GET"])
async def index(request: Request):
return Response('{"status": "OK"}', status_code=200)
# Proxy endpoint
@app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE"])
async def proxy(request: Request):
authorization: str = request.headers.get("Authorization", "")
request_headers = dict(request.headers)
request_body = await request.body()
if not authorization.startswith("Bearer "):
log_invalid_api_usage(api_key="no_api_key", endpoint=request.url.path, request_headers=request_headers, request_body=request_body)
return Response("Invalid API Key format", status_code=400, headers={"Proxy-Status": "invalid_api_key_format"})
api_key = authorization[7:] # Remove the 'Bearer ' prefix
if not request_body:
return Response("Access to this endpoint is restricted", status_code=400, headers={"Proxy-Status": "empty_request_body"})
if api_key.replace('"', '') in VALID_API_KEYS.values():
log_api_usage(api_key, request.url.path, request_headers=request_headers, request_body=request_body.decode())
# Forward the request to all servers and return the first successful response
for server_name, server in SERVERS.items():
async with httpx.AsyncClient() as client:
headers = {
"Content-Type": "application/json",
}
if server['api_key']:
headers["Authorization"] = f"Bearer {server['api_key']}"
try:
response = await client.request(
method=request.method,
url=f"{server['url']}{request.url.path}",
headers=headers,
content=request_body,
timeout=30.0
)
if response.status_code == 200:
return Response(content=response.content.decode("utf-8"))
except httpx.RequestError as e:
print(e)
return Response(status_code=500, content=f"Error: {e}")
return Response("No server could process the request", status_code=500, headers={"Proxy-Status": "all_servers_failed"})
else:
log_invalid_api_usage(api_key, request.url.path)
return Response("Invalid API Key", status_code=401, headers={"Proxy-Status": "invalid_api_key"})