Update api/utils.py
Browse files- api/utils.py +46 -4
api/utils.py
CHANGED
@@ -25,9 +25,13 @@ logger = setup_logger(__name__)
|
|
25 |
# Initialize the HTTPBearer security scheme
|
26 |
bearer_scheme = HTTPBearer()
|
27 |
|
|
|
28 |
def create_chat_completion_data(
|
29 |
content: str, model: str, timestamp: int, finish_reason: Optional[str] = None
|
30 |
) -> Dict[str, Any]:
|
|
|
|
|
|
|
31 |
return {
|
32 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
33 |
"object": "chat.completion.chunk",
|
@@ -43,13 +47,18 @@ def create_chat_completion_data(
|
|
43 |
"usage": None,
|
44 |
}
|
45 |
|
|
|
46 |
def verify_app_secret(credentials: HTTPAuthorizationCredentials = Depends(bearer_scheme)):
|
|
|
|
|
|
|
47 |
if credentials.credentials != APP_SECRET:
|
48 |
logger.warning("Invalid APP_SECRET provided.")
|
49 |
raise HTTPException(status_code=403, detail="Invalid APP_SECRET")
|
50 |
logger.debug("APP_SECRET verified successfully.")
|
51 |
return credentials.credentials
|
52 |
|
|
|
53 |
def message_to_dict(message):
|
54 |
"""
|
55 |
Convert a message object to a dictionary suitable for the API request.
|
@@ -84,9 +93,35 @@ def message_to_dict(message):
|
|
84 |
|
85 |
return message_dict
|
86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
async def process_streaming_response(request: ChatRequest):
|
88 |
-
|
89 |
-
|
|
|
|
|
|
|
90 |
|
91 |
# Log reduced information
|
92 |
logger.info(
|
@@ -149,10 +184,17 @@ async def process_streaming_response(request: ChatRequest):
|
|
149 |
except httpx.RequestError as e:
|
150 |
logger.error(f"Request error occurred: {e}")
|
151 |
raise HTTPException(status_code=500, detail="Internal server error.")
|
|
|
|
|
|
|
|
|
152 |
|
153 |
async def process_non_streaming_response(request: ChatRequest):
|
154 |
-
|
155 |
-
|
|
|
|
|
|
|
156 |
|
157 |
# Log reduced information
|
158 |
logger.info(
|
|
|
25 |
# Initialize the HTTPBearer security scheme
|
26 |
bearer_scheme = HTTPBearer()
|
27 |
|
28 |
+
|
29 |
def create_chat_completion_data(
|
30 |
content: str, model: str, timestamp: int, finish_reason: Optional[str] = None
|
31 |
) -> Dict[str, Any]:
|
32 |
+
"""
|
33 |
+
Create a dictionary representing a chat completion chunk.
|
34 |
+
"""
|
35 |
return {
|
36 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
37 |
"object": "chat.completion.chunk",
|
|
|
47 |
"usage": None,
|
48 |
}
|
49 |
|
50 |
+
|
51 |
def verify_app_secret(credentials: HTTPAuthorizationCredentials = Depends(bearer_scheme)):
|
52 |
+
"""
|
53 |
+
Verify the APP_SECRET from the authorization credentials.
|
54 |
+
"""
|
55 |
if credentials.credentials != APP_SECRET:
|
56 |
logger.warning("Invalid APP_SECRET provided.")
|
57 |
raise HTTPException(status_code=403, detail="Invalid APP_SECRET")
|
58 |
logger.debug("APP_SECRET verified successfully.")
|
59 |
return credentials.credentials
|
60 |
|
61 |
+
|
62 |
def message_to_dict(message):
|
63 |
"""
|
64 |
Convert a message object to a dictionary suitable for the API request.
|
|
|
93 |
|
94 |
return message_dict
|
95 |
|
96 |
+
|
97 |
+
def get_agent_mode(model: str) -> Dict[str, Any]:
|
98 |
+
"""
|
99 |
+
Retrieves the agent mode configuration.
|
100 |
+
Falls back to an empty dictionary if not found.
|
101 |
+
"""
|
102 |
+
agent_mode = AGENT_MODE.get(model, {})
|
103 |
+
if not agent_mode:
|
104 |
+
logger.warning(f"No AGENT_MODE configuration found for model: {model}")
|
105 |
+
return agent_mode
|
106 |
+
|
107 |
+
|
108 |
+
def get_trending_agent_mode(model: str) -> Dict[str, Any]:
|
109 |
+
"""
|
110 |
+
Retrieves the trending agent mode configuration.
|
111 |
+
Falls back to an empty dictionary if not found.
|
112 |
+
"""
|
113 |
+
trending_agent_mode = TRENDING_AGENT_MODE.get(model, {})
|
114 |
+
if not trending_agent_mode:
|
115 |
+
logger.warning(f"No TRENDING_AGENT_MODE configuration found for model: {model}")
|
116 |
+
return trending_agent_mode
|
117 |
+
|
118 |
+
|
119 |
async def process_streaming_response(request: ChatRequest):
|
120 |
+
"""
|
121 |
+
Process a streaming response for a chat completion request.
|
122 |
+
"""
|
123 |
+
agent_mode = get_agent_mode(request.model)
|
124 |
+
trending_agent_mode = get_trending_agent_mode(request.model)
|
125 |
|
126 |
# Log reduced information
|
127 |
logger.info(
|
|
|
184 |
except httpx.RequestError as e:
|
185 |
logger.error(f"Request error occurred: {e}")
|
186 |
raise HTTPException(status_code=500, detail="Internal server error.")
|
187 |
+
except Exception as e:
|
188 |
+
logger.error(f"Unexpected error: {e}")
|
189 |
+
raise HTTPException(status_code=500, detail="Internal server error.")
|
190 |
+
|
191 |
|
192 |
async def process_non_streaming_response(request: ChatRequest):
|
193 |
+
"""
|
194 |
+
Process a non-streaming response for a chat completion request.
|
195 |
+
"""
|
196 |
+
agent_mode = get_agent_mode(request.model)
|
197 |
+
trending_agent_mode = get_trending_agent_mode(request.model)
|
198 |
|
199 |
# Log reduced information
|
200 |
logger.info(
|