File size: 8,065 Bytes
879cc1c | 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 | #!/usr/bin/env python3
"""
LLM API Middleware Server
Intercepts and logs all OpenAI-compatible API requests and responses.
"""
import json
import os
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, Optional
import httpx
import yaml
from fastapi import FastAPI, Request, Response
from fastapi.responses import StreamingResponse, JSONResponse
import uvicorn
class LLMMiddleware:
def __init__(self, config_path: str = "config.yaml"):
"""Initialize the middleware with configuration."""
self.config = self._load_config(config_path)
self.upstream_base_url = self.config["upstream"]["base_url"].rstrip("/")
self.upstream_api_key = self.config["upstream"]["api_key"]
self.log_dir = Path(self.config["logging"]["log_dir"])
self.logging_enabled = self.config["logging"]["enabled"]
self.console_output = self.config["logging"]["console_output"]
# Create log directory if it doesn't exist
if self.logging_enabled:
self.log_dir.mkdir(parents=True, exist_ok=True)
def _load_config(self, config_path: str) -> Dict[str, Any]:
"""Load configuration from YAML file."""
with open(config_path, 'r') as f:
return yaml.safe_load(f)
def _save_log(self, request_data: Dict[str, Any], response_data: Any,
endpoint: str, status_code: int):
"""Save request and response to a JSON file."""
if not self.logging_enabled:
return
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
log_entry = {
"timestamp": datetime.now().isoformat(),
"endpoint": endpoint,
"status_code": status_code,
"input": request_data,
"output": str(response_data) if not isinstance(response_data, (dict, list)) else response_data
}
log_file = self.log_dir / f"{timestamp}.json"
with open(log_file, 'w', encoding='utf-8') as f:
json.dump(log_entry, f, indent=2, ensure_ascii=False)
if self.console_output:
print(f"[{timestamp}] Logged API call to {log_file}")
async def proxy_request(self, request: Request, endpoint: str) -> Response:
"""Proxy the request to upstream API and log the interaction."""
# Parse request body
try:
body = await request.body()
request_data = json.loads(body) if body else {}
except json.JSONDecodeError:
request_data = {}
# Prepare headers
headers = {
"Authorization": f"Bearer {self.upstream_api_key}",
"Content-Type": "application/json",
}
# Add other headers from original request (excluding host and authorization)
for key, value in request.headers.items():
if key.lower() not in ["host", "authorization", "content-length"]:
headers[key] = value
# Construct upstream URL
upstream_url = f"{self.upstream_base_url}{endpoint}"
# Check if streaming is requested
is_streaming = request_data.get("stream", False)
try:
async with httpx.AsyncClient(timeout=300.0) as client:
if is_streaming:
# Handle streaming response
return await self._handle_streaming(
client, upstream_url, headers, request_data, endpoint
)
else:
# Handle non-streaming response
return await self._handle_non_streaming(
client, upstream_url, headers, request_data, endpoint
)
except Exception as e:
error_response = {
"error": {
"message": str(e),
"type": "middleware_error",
"code": "internal_error"
}
}
self._save_log(request_data, error_response, endpoint, 500)
return JSONResponse(content=error_response, status_code=500)
async def _handle_non_streaming(self, client: httpx.AsyncClient, url: str,
headers: Dict[str, str], request_data: Dict[str, Any],
endpoint: str) -> Response:
"""Handle non-streaming API requests."""
response = await client.post(url, headers=headers, json=request_data)
# Parse response
try:
response_data = response.json()
except json.JSONDecodeError:
response_data = response.text
# Log the interaction
self._save_log(request_data, response_data, endpoint, response.status_code)
# Filter out headers that should not be copied
filtered_headers = {
k: v for k, v in response.headers.items()
if k.lower() not in ['content-length', 'content-encoding', 'transfer-encoding']
}
# Return response
return JSONResponse(
content=response_data,
status_code=response.status_code,
headers=filtered_headers
)
async def _handle_streaming(self, client: httpx.AsyncClient, url: str,
headers: Dict[str, str], request_data: Dict[str, Any],
endpoint: str) -> StreamingResponse:
"""Handle streaming API requests."""
collected_chunks = []
async def stream_generator():
async with client.stream("POST", url, headers=headers, json=request_data) as response:
async for chunk in response.aiter_bytes():
collected_chunks.append(chunk)
yield chunk
# After streaming is complete, save the log
try:
# Reconstruct the full response from chunks
full_response = b"".join(collected_chunks).decode('utf-8')
self._save_log(request_data, full_response, endpoint, response.status_code)
except Exception as e:
if self.console_output:
print(f"Error logging streaming response: {e}")
return StreamingResponse(
stream_generator(),
media_type="text/event-stream"
)
# Initialize FastAPI app and middleware
app = FastAPI(title="LLM API Middleware", version="1.0.0")
middleware = LLMMiddleware()
@app.get("/")
async def root():
"""Health check endpoint."""
return {
"status": "running",
"service": "LLM API Middleware",
"version": "1.0.0"
}
@app.post("/v1/chat/completions")
async def chat_completions(request: Request):
"""Proxy chat completions endpoint."""
return await middleware.proxy_request(request, "/v1/chat/completions")
@app.post("/v1/completions")
async def completions(request: Request):
"""Proxy completions endpoint."""
return await middleware.proxy_request(request, "/v1/completions")
@app.post("/v1/embeddings")
async def embeddings(request: Request):
"""Proxy embeddings endpoint."""
return await middleware.proxy_request(request, "/v1/embeddings")
@app.get("/v1/models")
async def list_models(request: Request):
"""Proxy list models endpoint."""
return await middleware.proxy_request(request, "/v1/models")
@app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH"])
async def catch_all(request: Request, path: str):
"""Catch-all endpoint for other API routes."""
endpoint = f"/{path}"
return await middleware.proxy_request(request, endpoint)
def main():
"""Run the middleware server."""
config = middleware.config
host = config["server"]["host"]
port = config["server"]["port"]
print(f"Starting LLM API Middleware on {host}:{port}")
print(f"Proxying to: {middleware.upstream_base_url}")
print(f"Logs directory: {middleware.log_dir.absolute()}")
uvicorn.run(app, host=host, port=port)
if __name__ == "__main__":
main()
|