feat: Initialize FastAPI AI Gateway project structure with authentication, module management, and LLM API routing.
This commit is contained in:
66
app/api/endpoints/admin.py
Normal file
66
app/api/endpoints/admin.py
Normal file
@@ -0,0 +1,66 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
from app.core.database import get_db
|
||||
from app.api.deps import get_api_key
|
||||
from app.models.module import Module
|
||||
import secrets
|
||||
from pydantic import BaseModel
|
||||
from datetime import datetime
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
class ModuleCreate(BaseModel):
|
||||
name: str
|
||||
|
||||
class ModuleResponse(BaseModel):
|
||||
id: int
|
||||
name: str
|
||||
secret_key: str
|
||||
is_active: bool
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
@router.post("/modules", response_model=ModuleResponse)
|
||||
def create_module(
|
||||
module_in: ModuleCreate,
|
||||
db: Session = Depends(get_db),
|
||||
api_key: str = Depends(get_api_key) # Only global admin key should be allowed here ideally
|
||||
):
|
||||
# Check if exists
|
||||
db_module = db.query(Module).filter(Module.name == module_in.name).first()
|
||||
if db_module:
|
||||
raise HTTPException(status_code=400, detail="Module already exists")
|
||||
|
||||
new_key = secrets.token_hex(32)
|
||||
db_module = Module(
|
||||
name=module_in.name,
|
||||
secret_key=new_key
|
||||
)
|
||||
db.add(db_module)
|
||||
db.commit()
|
||||
db.refresh(db_module)
|
||||
return db_module
|
||||
|
||||
@router.post("/modules/{module_id}/rotate", response_model=ModuleResponse)
|
||||
def rotate_module_key(
|
||||
module_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
api_key: str = Depends(get_api_key)
|
||||
):
|
||||
db_module = db.query(Module).filter(Module.id == module_id).first()
|
||||
if not db_module:
|
||||
raise HTTPException(status_code=404, detail="Module not found")
|
||||
|
||||
db_module.secret_key = secrets.token_hex(32)
|
||||
db_module.last_rotated_at = datetime.utcnow()
|
||||
db.commit()
|
||||
db.refresh(db_module)
|
||||
return db_module
|
||||
|
||||
@router.get("/modules", response_model=list[ModuleResponse])
|
||||
def get_modules(
|
||||
db: Session = Depends(get_db),
|
||||
api_key: str = Depends(get_api_key)
|
||||
):
|
||||
return db.query(Module).all()
|
||||
26
app/api/endpoints/auth.py
Normal file
26
app/api/endpoints/auth.py
Normal file
@@ -0,0 +1,26 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from fastapi.security import OAuth2PasswordRequestForm
|
||||
from app.core import security
|
||||
from app.core.config import settings
|
||||
from pydantic import BaseModel
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
class Token(BaseModel):
|
||||
access_token: str
|
||||
token_type: str
|
||||
|
||||
@router.post("/login", response_model=Token)
|
||||
async def login(form_data: OAuth2PasswordRequestForm = Depends()):
|
||||
# Simple admin check - in a real app, query a User table
|
||||
if form_data.username == "admin" and form_data.password == settings.ADMIN_PASSWORD:
|
||||
access_token = security.create_access_token(
|
||||
data={"sub": form_data.username}
|
||||
)
|
||||
return {"access_token": access_token, "token_type": "bearer"}
|
||||
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Incorrect username or password",
|
||||
headers={"WWW-Authenticate": "Bearer"},
|
||||
)
|
||||
54
app/api/endpoints/gemini.py
Normal file
54
app/api/endpoints/gemini.py
Normal file
@@ -0,0 +1,54 @@
|
||||
from fastapi import APIRouter, Depends, Request
|
||||
from app.api.deps import get_api_key
|
||||
from app.core.limiter import limiter
|
||||
from app.core.config import settings
|
||||
from pydantic import BaseModel
|
||||
from google import genai
|
||||
import asyncio
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
class LLMRequest(BaseModel):
|
||||
prompt: str
|
||||
context: str = ""
|
||||
|
||||
# Shared client instance (global)
|
||||
_client = None
|
||||
|
||||
def get_gemini_client():
|
||||
global _client
|
||||
if _client is None and settings.GOOGLE_API_KEY and settings.GOOGLE_API_KEY != "your-google-api-key":
|
||||
_client = genai.Client(api_key=settings.GOOGLE_API_KEY, http_options={'api_version': 'v1alpha'})
|
||||
return _client
|
||||
|
||||
@router.post("/chat")
|
||||
@limiter.limit(settings.RATE_LIMIT)
|
||||
async def gemini_chat(
|
||||
request: Request,
|
||||
chat_data: LLMRequest,
|
||||
api_key: str = Depends(get_api_key)
|
||||
):
|
||||
client = get_gemini_client()
|
||||
|
||||
try:
|
||||
if not client:
|
||||
return {
|
||||
"status": "mock",
|
||||
"model": "gemini",
|
||||
"response": f"MOCK: Gemini response to '{chat_data.prompt}'"
|
||||
}
|
||||
|
||||
# Using the async generation method provided by the new google-genai library
|
||||
# We use await to ensure we don't block the event loop
|
||||
response = await client.aio.models.generate_content(
|
||||
model="gemini-2.0-flash",
|
||||
contents=chat_data.prompt
|
||||
)
|
||||
|
||||
return {
|
||||
"status": "success",
|
||||
"model": "gemini",
|
||||
"response": response.text
|
||||
}
|
||||
except Exception as e:
|
||||
return {"status": "error", "detail": str(e)}
|
||||
46
app/api/endpoints/openai.py
Normal file
46
app/api/endpoints/openai.py
Normal file
@@ -0,0 +1,46 @@
|
||||
from fastapi import APIRouter, Depends, Request
|
||||
from app.api.deps import get_api_key
|
||||
from app.core.limiter import limiter
|
||||
from app.core.config import settings
|
||||
from pydantic import BaseModel
|
||||
from openai import AsyncOpenAI
|
||||
import asyncio
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
class LLMRequest(BaseModel):
|
||||
prompt: str
|
||||
context: str = ""
|
||||
|
||||
# Initialize Async client
|
||||
client = None
|
||||
if settings.OPENAI_API_KEY and settings.OPENAI_API_KEY != "your-openai-api-key":
|
||||
client = AsyncOpenAI(api_key=settings.OPENAI_API_KEY)
|
||||
|
||||
@router.post("/chat")
|
||||
@limiter.limit(settings.RATE_LIMIT)
|
||||
async def openai_chat(
|
||||
request: Request,
|
||||
chat_data: LLMRequest,
|
||||
api_key: str = Depends(get_api_key)
|
||||
):
|
||||
try:
|
||||
if not client:
|
||||
return {
|
||||
"status": "mock",
|
||||
"model": "openai",
|
||||
"response": f"MOCK: OpenAI response to '{chat_data.prompt}'"
|
||||
}
|
||||
|
||||
# Perform Async call to OpenAI
|
||||
response = await client.chat.completions.create(
|
||||
model="gpt-3.5-turbo",
|
||||
messages=[{"role": "user", "content": chat_data.prompt}]
|
||||
)
|
||||
return {
|
||||
"status": "success",
|
||||
"model": "openai",
|
||||
"response": response.choices[0].message.content
|
||||
}
|
||||
except Exception as e:
|
||||
return {"status": "error", "detail": str(e)}
|
||||
33
app/api/endpoints/storyline.py
Normal file
33
app/api/endpoints/storyline.py
Normal file
@@ -0,0 +1,33 @@
|
||||
from fastapi import APIRouter, Depends, Request
|
||||
from app.api.deps import get_api_key
|
||||
from app.core.limiter import limiter
|
||||
from app.core.config import settings
|
||||
from pydantic import BaseModel
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
class ChatRequest(BaseModel):
|
||||
prompt: str
|
||||
context: str = ""
|
||||
|
||||
@router.post("/chat")
|
||||
@limiter.limit(settings.RATE_LIMIT)
|
||||
async def story_chat(
|
||||
request: Request,
|
||||
chat_data: ChatRequest,
|
||||
api_key: str = Depends(get_api_key)
|
||||
):
|
||||
# This is where you would call your LLM (OpenAI, Anthropic, etc.)
|
||||
# For now, we return a mock response
|
||||
return {
|
||||
"status": "success",
|
||||
"response": f"Processed prompt: {chat_data.prompt}",
|
||||
"metadata": {
|
||||
"characters_received": len(chat_data.prompt),
|
||||
"context_length": len(chat_data.context)
|
||||
}
|
||||
}
|
||||
|
||||
@router.get("/health")
|
||||
async def health_check():
|
||||
return {"status": "healthy"}
|
||||
Reference in New Issue
Block a user