Skip to content

Commit 3e67879

Browse files
committed
generated file: dependencies/utils.py
1 parent 8eacf94 commit 3e67879

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

Diff for: dependencies/utils.py

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
from typing import Optional, Dict, Any, List
2+
import logging
3+
import os
4+
from datetime import datetime, timedelta
5+
6+
from fastapi import HTTPException, status
7+
8+
from .config import settings
9+
from .database import get_db
10+
from .models import User
11+
from .schemas.openai import OpenAIRequest, OpenAIResponse, OpenAIModel
12+
13+
logger = logging.getLogger(__name__)
14+
15+
# --- Constants ---
16+
17+
# ... (Constants specific to this file if any)
18+
19+
# --- Functions ---
20+
21+
def format_datetime(dt: datetime) -> str:
22+
"""Formats a datetime object into a human-readable string."""
23+
return dt.strftime("%Y-%m-%d %H:%M:%S")
24+
25+
def get_api_key(user: User) -> Optional[str]:
26+
"""Retrieves the OpenAI API key for the given user."""
27+
if user.api_key:
28+
return user.api_key
29+
else:
30+
return None
31+
32+
def generate_api_key():
33+
"""Generates a random API key."""
34+
# ... (Implementation for generating a random API key)
35+
36+
def log_api_usage(user: User, endpoint: str, response_time: float) -> None:
37+
"""Logs API usage statistics."""
38+
db = get_db()
39+
try:
40+
# ... (Implementation for logging API usage)
41+
except Exception as e:
42+
logger.error(f"Error logging API usage: {e}")
43+
44+
def format_openai_response(response: Dict[str, Any]) -> OpenAIResponse:
45+
"""Formats the OpenAI API response into a structured OpenAIResponse object."""
46+
# ... (Implementation for formatting the OpenAI API response)
47+
48+
async def get_openai_model(model_id: str, user: User) -> OpenAIModel:
49+
"""Retrieves information about a specific OpenAI model."""
50+
api_key = get_api_key(user)
51+
if not api_key:
52+
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="API key not found.")
53+
openai.api_key = api_key
54+
try:
55+
model = openai.Model.retrieve(model_id)
56+
return OpenAIModel(**model)
57+
except openai.error.APIError as e:
58+
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Error retrieving model: {e}")
59+
60+
# --- Classes ---
61+
62+
# ... (Classes specific to this file if any)
63+
64+
# --- Main ---
65+
66+
# ... (Main function or script execution logic if any)

0 commit comments

Comments
 (0)