Update api/utils.py
Browse files- api/utils.py +6 -26
api/utils.py
CHANGED
@@ -24,7 +24,6 @@ from api.logger import (
|
|
24 |
log_model_delay,
|
25 |
log_http_error,
|
26 |
log_request_error,
|
27 |
-
log_strip_prefix,
|
28 |
)
|
29 |
|
30 |
# Helper function to generate a random alphanumeric chat ID
|
@@ -71,15 +70,7 @@ def message_to_dict(message, model_prefix: Optional[str] = None):
|
|
71 |
else:
|
72 |
return {"role": message.role, "content": content}
|
73 |
|
74 |
-
#
|
75 |
-
def strip_model_prefix(content: str, model_prefix: Optional[str] = None) -> str:
|
76 |
-
"""Remove the model prefix from the response content if present."""
|
77 |
-
if model_prefix and content.startswith(model_prefix):
|
78 |
-
log_strip_prefix(model_prefix, content)
|
79 |
-
return content[len(model_prefix):].strip()
|
80 |
-
return content
|
81 |
-
|
82 |
-
# Streaming response processing with headers from config.py
|
83 |
async def process_streaming_response(request: ChatRequest):
|
84 |
chat_id = generate_chat_id() if request.model in MODEL_REFERERS else None
|
85 |
referer_path = MODEL_REFERERS.get(request.model, "")
|
@@ -92,10 +83,8 @@ async def process_streaming_response(request: ChatRequest):
|
|
92 |
trending_agent_mode = TRENDING_AGENT_MODE.get(request.model, {})
|
93 |
model_prefix = MODEL_PREFIXES.get(request.model, "")
|
94 |
|
95 |
-
# Generate headers for API chat request with dynamic Referer
|
96 |
headers_api_chat = get_headers_api_chat(referer_url)
|
97 |
|
98 |
-
# Introduce delay for 'o1-preview' model
|
99 |
if request.model == 'o1-preview':
|
100 |
delay_seconds = random.randint(20, 60)
|
101 |
log_model_delay(delay_seconds, request.model, chat_id)
|
@@ -137,10 +126,8 @@ async def process_streaming_response(request: ChatRequest):
|
|
137 |
response.raise_for_status()
|
138 |
async for line in response.aiter_lines():
|
139 |
timestamp = int(datetime.now().timestamp())
|
140 |
-
if
|
141 |
-
|
142 |
-
cleaned_content = strip_model_prefix(content, model_prefix)
|
143 |
-
yield f"data: {json.dumps(create_chat_completion_data(cleaned_content, request.model, timestamp))}\n\n"
|
144 |
|
145 |
yield f"data: {json.dumps(create_chat_completion_data('', request.model, timestamp, 'stop'))}\n\n"
|
146 |
yield "data: [DONE]\n\n"
|
@@ -151,24 +138,21 @@ async def process_streaming_response(request: ChatRequest):
|
|
151 |
log_request_error(e, chat_id)
|
152 |
raise HTTPException(status_code=500, detail=str(e))
|
153 |
|
154 |
-
# Non-streaming response processing
|
155 |
async def process_non_streaming_response(request: ChatRequest):
|
156 |
chat_id = generate_chat_id() if request.model in MODEL_REFERERS else None
|
157 |
referer_path = MODEL_REFERERS.get(request.model, "")
|
158 |
referer_url = f"{BASE_URL}/chat/{chat_id}?model={request.model}" if chat_id else BASE_URL
|
159 |
|
160 |
-
# Log with chat ID, model, and referer URL if applicable
|
161 |
log_generated_chat_id_with_referer(chat_id, request.model, referer_url)
|
162 |
|
163 |
agent_mode = AGENT_MODE.get(request.model, {})
|
164 |
trending_agent_mode = TRENDING_AGENT_MODE.get(request.model, {})
|
165 |
model_prefix = MODEL_PREFIXES.get(request.model, "")
|
166 |
|
167 |
-
# Generate headers for API chat request and secondary chat request with dynamic Referer
|
168 |
headers_api_chat = get_headers_api_chat(referer_url)
|
169 |
headers_chat = get_headers_chat(referer_url, next_action=str(uuid.uuid4()), next_router_state_tree=json.dumps([""]))
|
170 |
|
171 |
-
# Introduce delay for 'o1-preview' model
|
172 |
if request.model == 'o1-preview':
|
173 |
delay_seconds = random.randint(20, 60)
|
174 |
log_model_delay(delay_seconds, request.model, chat_id)
|
@@ -214,11 +198,7 @@ async def process_non_streaming_response(request: ChatRequest):
|
|
214 |
log_request_error(e, chat_id)
|
215 |
raise HTTPException(status_code=500, detail=str(e))
|
216 |
|
217 |
-
|
218 |
-
full_response = full_response[21:]
|
219 |
-
|
220 |
-
# Strip the model prefix from the full response
|
221 |
-
cleaned_full_response = strip_model_prefix(full_response, model_prefix)
|
222 |
|
223 |
return {
|
224 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
@@ -228,7 +208,7 @@ async def process_non_streaming_response(request: ChatRequest):
|
|
228 |
"choices": [
|
229 |
{
|
230 |
"index": 0,
|
231 |
-
"message": {"role": "assistant", "content":
|
232 |
"finish_reason": "stop",
|
233 |
}
|
234 |
],
|
|
|
24 |
log_model_delay,
|
25 |
log_http_error,
|
26 |
log_request_error,
|
|
|
27 |
)
|
28 |
|
29 |
# Helper function to generate a random alphanumeric chat ID
|
|
|
70 |
else:
|
71 |
return {"role": message.role, "content": content}
|
72 |
|
73 |
+
# Streaming response processing
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
async def process_streaming_response(request: ChatRequest):
|
75 |
chat_id = generate_chat_id() if request.model in MODEL_REFERERS else None
|
76 |
referer_path = MODEL_REFERERS.get(request.model, "")
|
|
|
83 |
trending_agent_mode = TRENDING_AGENT_MODE.get(request.model, {})
|
84 |
model_prefix = MODEL_PREFIXES.get(request.model, "")
|
85 |
|
|
|
86 |
headers_api_chat = get_headers_api_chat(referer_url)
|
87 |
|
|
|
88 |
if request.model == 'o1-preview':
|
89 |
delay_seconds = random.randint(20, 60)
|
90 |
log_model_delay(delay_seconds, request.model, chat_id)
|
|
|
126 |
response.raise_for_status()
|
127 |
async for line in response.aiter_lines():
|
128 |
timestamp = int(datetime.now().timestamp())
|
129 |
+
content = line.lstrip("$@$v=undefined-rv1$@$") # Trim only if needed
|
130 |
+
yield f"data: {json.dumps(create_chat_completion_data(content, request.model, timestamp))}\n\n"
|
|
|
|
|
131 |
|
132 |
yield f"data: {json.dumps(create_chat_completion_data('', request.model, timestamp, 'stop'))}\n\n"
|
133 |
yield "data: [DONE]\n\n"
|
|
|
138 |
log_request_error(e, chat_id)
|
139 |
raise HTTPException(status_code=500, detail=str(e))
|
140 |
|
141 |
+
# Non-streaming response processing
|
142 |
async def process_non_streaming_response(request: ChatRequest):
|
143 |
chat_id = generate_chat_id() if request.model in MODEL_REFERERS else None
|
144 |
referer_path = MODEL_REFERERS.get(request.model, "")
|
145 |
referer_url = f"{BASE_URL}/chat/{chat_id}?model={request.model}" if chat_id else BASE_URL
|
146 |
|
|
|
147 |
log_generated_chat_id_with_referer(chat_id, request.model, referer_url)
|
148 |
|
149 |
agent_mode = AGENT_MODE.get(request.model, {})
|
150 |
trending_agent_mode = TRENDING_AGENT_MODE.get(request.model, {})
|
151 |
model_prefix = MODEL_PREFIXES.get(request.model, "")
|
152 |
|
|
|
153 |
headers_api_chat = get_headers_api_chat(referer_url)
|
154 |
headers_chat = get_headers_chat(referer_url, next_action=str(uuid.uuid4()), next_router_state_tree=json.dumps([""]))
|
155 |
|
|
|
156 |
if request.model == 'o1-preview':
|
157 |
delay_seconds = random.randint(20, 60)
|
158 |
log_model_delay(delay_seconds, request.model, chat_id)
|
|
|
198 |
log_request_error(e, chat_id)
|
199 |
raise HTTPException(status_code=500, detail=str(e))
|
200 |
|
201 |
+
full_response = full_response.lstrip("$@$v=undefined-rv1$@$") # Trim only if needed
|
|
|
|
|
|
|
|
|
202 |
|
203 |
return {
|
204 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
|
|
208 |
"choices": [
|
209 |
{
|
210 |
"index": 0,
|
211 |
+
"message": {"role": "assistant", "content": full_response},
|
212 |
"finish_reason": "stop",
|
213 |
}
|
214 |
],
|