-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
66 lines (50 loc) · 2.04 KB
/
utils.py
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
from typing import Optional, Dict, Any, List
import logging
import os
from datetime import datetime, timedelta
from fastapi import HTTPException, status
from .config import settings
from .database import get_db
from .models import User
from .schemas.openai import OpenAIRequest, OpenAIResponse, OpenAIModel
logger = logging.getLogger(__name__)
# --- Constants ---
# ... (Constants specific to this file if any)
# --- Functions ---
def format_datetime(dt: datetime) -> str:
"""Formats a datetime object into a human-readable string."""
return dt.strftime("%Y-%m-%d %H:%M:%S")
def get_api_key(user: User) -> Optional[str]:
"""Retrieves the OpenAI API key for the given user."""
if user.api_key:
return user.api_key
else:
return None
def generate_api_key():
"""Generates a random API key."""
# ... (Implementation for generating a random API key)
def log_api_usage(user: User, endpoint: str, response_time: float) -> None:
"""Logs API usage statistics."""
db = get_db()
try:
# ... (Implementation for logging API usage)
except Exception as e:
logger.error(f"Error logging API usage: {e}")
def format_openai_response(response: Dict[str, Any]) -> OpenAIResponse:
"""Formats the OpenAI API response into a structured OpenAIResponse object."""
# ... (Implementation for formatting the OpenAI API response)
async def get_openai_model(model_id: str, user: User) -> OpenAIModel:
"""Retrieves information about a specific OpenAI model."""
api_key = get_api_key(user)
if not api_key:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="API key not found.")
openai.api_key = api_key
try:
model = openai.Model.retrieve(model_id)
return OpenAIModel(**model)
except openai.error.APIError as e:
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Error retrieving model: {e}")
# --- Classes ---
# ... (Classes specific to this file if any)
# --- Main ---
# ... (Main function or script execution logic if any)