Update api/utils.py
Browse files- api/utils.py +31 -33
api/utils.py
CHANGED
@@ -18,7 +18,7 @@ from api.config import (
|
|
18 |
MODEL_PREFIXES,
|
19 |
MODEL_REFERERS
|
20 |
)
|
21 |
-
from api.models import ChatRequest
|
22 |
from api.logger import (
|
23 |
log_generated_chat_id,
|
24 |
log_model_delay,
|
@@ -27,7 +27,7 @@ from api.logger import (
|
|
27 |
log_strip_prefix
|
28 |
)
|
29 |
|
30 |
-
# Helper function to
|
31 |
def generate_chat_id(length: int = 7) -> str:
|
32 |
characters = string.ascii_letters + string.digits
|
33 |
return ''.join(random.choices(characters, k=length))
|
@@ -78,33 +78,30 @@ def message_to_dict(message, model_prefix: Optional[str] = None):
|
|
78 |
def strip_model_prefix(content: str, model_prefix: Optional[str] = None) -> str:
|
79 |
"""Remove the model prefix from the response content if present."""
|
80 |
if model_prefix and content.startswith(model_prefix):
|
81 |
-
log_strip_prefix(model_prefix)
|
82 |
return content[len(model_prefix):].strip()
|
83 |
return content
|
84 |
|
85 |
# Process streaming response with headers from config.py
|
86 |
async def process_streaming_response(request: ChatRequest):
|
87 |
-
|
88 |
-
|
89 |
-
chat_id = generate_chat_id()
|
90 |
-
chat_url = f"/chat/{chat_id}?model={request.model}"
|
91 |
-
log_generated_chat_id(chat_id, chat_url)
|
92 |
-
|
93 |
-
referer_path = MODEL_REFERERS[request.model]
|
94 |
-
referer_url = f"{BASE_URL}{referer_path}"
|
95 |
-
else:
|
96 |
-
chat_id = None
|
97 |
-
referer_url = BASE_URL # Use base URL for models not in MODEL_REFERERS
|
98 |
|
99 |
agent_mode = AGENT_MODE.get(request.model, {})
|
100 |
trending_agent_mode = TRENDING_AGENT_MODE.get(request.model, {})
|
101 |
model_prefix = MODEL_PREFIXES.get(request.model, "")
|
|
|
|
|
|
|
|
|
102 |
|
|
|
103 |
headers_api_chat = get_headers_api_chat(referer_url)
|
104 |
|
|
|
105 |
if request.model == 'o1-preview':
|
106 |
delay_seconds = random.randint(20, 60)
|
107 |
-
log_model_delay(delay_seconds,
|
108 |
await asyncio.sleep(delay_seconds)
|
109 |
|
110 |
json_data = {
|
@@ -114,7 +111,7 @@ async def process_streaming_response(request: ChatRequest):
|
|
114 |
"clickedForceWebSearch": False,
|
115 |
"codeModelMode": True,
|
116 |
"githubToken": None,
|
117 |
-
"id": chat_id
|
118 |
"isChromeExt": False,
|
119 |
"isMicMode": False,
|
120 |
"maxTokens": request.max_tokens,
|
@@ -147,41 +144,41 @@ async def process_streaming_response(request: ChatRequest):
|
|
147 |
content = line
|
148 |
if content.startswith("$@$v=undefined-rv1$@$"):
|
149 |
content = content[21:]
|
|
|
150 |
cleaned_content = strip_model_prefix(content, model_prefix)
|
151 |
yield f"data: {json.dumps(create_chat_completion_data(cleaned_content, request.model, timestamp))}\n\n"
|
152 |
|
153 |
yield f"data: {json.dumps(create_chat_completion_data('', request.model, timestamp, 'stop'))}\n\n"
|
154 |
yield "data: [DONE]\n\n"
|
155 |
except httpx.HTTPStatusError as e:
|
156 |
-
log_http_error(
|
157 |
raise HTTPException(status_code=e.response.status_code, detail=str(e))
|
158 |
except httpx.RequestError as e:
|
159 |
-
log_request_error(
|
160 |
raise HTTPException(status_code=500, detail=str(e))
|
161 |
|
162 |
# Process non-streaming response with headers from config.py
|
163 |
async def process_non_streaming_response(request: ChatRequest):
|
164 |
-
|
165 |
-
|
166 |
-
chat_url = f"/chat/{chat_id}?model={request.model}"
|
167 |
-
log_generated_chat_id(chat_id, chat_url)
|
168 |
-
|
169 |
-
referer_path = MODEL_REFERERS[request.model]
|
170 |
-
referer_url = f"{BASE_URL}{referer_path}"
|
171 |
-
else:
|
172 |
-
chat_id = None
|
173 |
-
referer_url = BASE_URL
|
174 |
|
175 |
agent_mode = AGENT_MODE.get(request.model, {})
|
176 |
trending_agent_mode = TRENDING_AGENT_MODE.get(request.model, {})
|
177 |
model_prefix = MODEL_PREFIXES.get(request.model, "")
|
178 |
|
|
|
|
|
|
|
|
|
|
|
|
|
179 |
headers_api_chat = get_headers_api_chat(referer_url)
|
180 |
-
headers_chat = get_headers_chat(chat_url, next_action=str(uuid.uuid4()), next_router_state_tree=json.dumps([""]))
|
181 |
|
|
|
182 |
if request.model == 'o1-preview':
|
183 |
delay_seconds = random.randint(20, 60)
|
184 |
-
log_model_delay(delay_seconds,
|
185 |
await asyncio.sleep(delay_seconds)
|
186 |
|
187 |
json_data = {
|
@@ -191,7 +188,7 @@ async def process_non_streaming_response(request: ChatRequest):
|
|
191 |
"clickedForceWebSearch": False,
|
192 |
"codeModelMode": True,
|
193 |
"githubToken": None,
|
194 |
-
"id": chat_id
|
195 |
"isChromeExt": False,
|
196 |
"isMicMode": False,
|
197 |
"maxTokens": request.max_tokens,
|
@@ -218,14 +215,15 @@ async def process_non_streaming_response(request: ChatRequest):
|
|
218 |
async for chunk in response.aiter_text():
|
219 |
full_response += chunk
|
220 |
except httpx.HTTPStatusError as e:
|
221 |
-
log_http_error(
|
222 |
raise HTTPException(status_code=e.response.status_code, detail=str(e))
|
223 |
except httpx.RequestError as e:
|
224 |
-
log_request_error(
|
225 |
raise HTTPException(status_code=500, detail=str(e))
|
226 |
if full_response.startswith("$@$v=undefined-rv1$@$"):
|
227 |
full_response = full_response[21:]
|
228 |
|
|
|
229 |
cleaned_full_response = strip_model_prefix(full_response, model_prefix)
|
230 |
|
231 |
return {
|
|
|
18 |
MODEL_PREFIXES,
|
19 |
MODEL_REFERERS
|
20 |
)
|
21 |
+
from api.models import ChatRequest
|
22 |
from api.logger import (
|
23 |
log_generated_chat_id,
|
24 |
log_model_delay,
|
|
|
27 |
log_strip_prefix
|
28 |
)
|
29 |
|
30 |
+
# Helper function to generate a random alphanumeric chat ID
|
31 |
def generate_chat_id(length: int = 7) -> str:
|
32 |
characters = string.ascii_letters + string.digits
|
33 |
return ''.join(random.choices(characters, k=length))
|
|
|
78 |
def strip_model_prefix(content: str, model_prefix: Optional[str] = None) -> str:
|
79 |
"""Remove the model prefix from the response content if present."""
|
80 |
if model_prefix and content.startswith(model_prefix):
|
81 |
+
log_strip_prefix(model_prefix, content)
|
82 |
return content[len(model_prefix):].strip()
|
83 |
return content
|
84 |
|
85 |
# Process streaming response with headers from config.py
|
86 |
async def process_streaming_response(request: ChatRequest):
|
87 |
+
chat_id = generate_chat_id()
|
88 |
+
log_generated_chat_id(chat_id, request.model)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
|
90 |
agent_mode = AGENT_MODE.get(request.model, {})
|
91 |
trending_agent_mode = TRENDING_AGENT_MODE.get(request.model, {})
|
92 |
model_prefix = MODEL_PREFIXES.get(request.model, "")
|
93 |
+
|
94 |
+
# Check if the model is in MODEL_REFERERS, else no referer path
|
95 |
+
referer_path = MODEL_REFERERS.get(request.model, "")
|
96 |
+
referer_url = f"{BASE_URL}{referer_path}" if referer_path else BASE_URL
|
97 |
|
98 |
+
# Generate headers for API chat request with dynamic Referer
|
99 |
headers_api_chat = get_headers_api_chat(referer_url)
|
100 |
|
101 |
+
# Introduce delay for 'o1-preview' model
|
102 |
if request.model == 'o1-preview':
|
103 |
delay_seconds = random.randint(20, 60)
|
104 |
+
log_model_delay(delay_seconds, request.model, chat_id)
|
105 |
await asyncio.sleep(delay_seconds)
|
106 |
|
107 |
json_data = {
|
|
|
111 |
"clickedForceWebSearch": False,
|
112 |
"codeModelMode": True,
|
113 |
"githubToken": None,
|
114 |
+
"id": chat_id,
|
115 |
"isChromeExt": False,
|
116 |
"isMicMode": False,
|
117 |
"maxTokens": request.max_tokens,
|
|
|
144 |
content = line
|
145 |
if content.startswith("$@$v=undefined-rv1$@$"):
|
146 |
content = content[21:]
|
147 |
+
# Strip the model prefix from the response content
|
148 |
cleaned_content = strip_model_prefix(content, model_prefix)
|
149 |
yield f"data: {json.dumps(create_chat_completion_data(cleaned_content, request.model, timestamp))}\n\n"
|
150 |
|
151 |
yield f"data: {json.dumps(create_chat_completion_data('', request.model, timestamp, 'stop'))}\n\n"
|
152 |
yield "data: [DONE]\n\n"
|
153 |
except httpx.HTTPStatusError as e:
|
154 |
+
log_http_error(e, chat_id)
|
155 |
raise HTTPException(status_code=e.response.status_code, detail=str(e))
|
156 |
except httpx.RequestError as e:
|
157 |
+
log_request_error(e, chat_id)
|
158 |
raise HTTPException(status_code=500, detail=str(e))
|
159 |
|
160 |
# Process non-streaming response with headers from config.py
|
161 |
async def process_non_streaming_response(request: ChatRequest):
|
162 |
+
chat_id = generate_chat_id()
|
163 |
+
log_generated_chat_id(chat_id, request.model)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
164 |
|
165 |
agent_mode = AGENT_MODE.get(request.model, {})
|
166 |
trending_agent_mode = TRENDING_AGENT_MODE.get(request.model, {})
|
167 |
model_prefix = MODEL_PREFIXES.get(request.model, "")
|
168 |
|
169 |
+
# Check if the model is in MODEL_REFERERS, else no referer path
|
170 |
+
referer_path = MODEL_REFERERS.get(request.model, "")
|
171 |
+
referer_url = f"{BASE_URL}{referer_path}" if referer_path else BASE_URL
|
172 |
+
chat_url = f"{referer_url}/chat/{chat_id}?model={request.model}"
|
173 |
+
|
174 |
+
# Generate headers for API chat request and chat request with dynamic Referer
|
175 |
headers_api_chat = get_headers_api_chat(referer_url)
|
176 |
+
headers_chat = get_headers_chat(chat_url, next_action=str(uuid.uuid4()), next_router_state_tree=json.dumps([""]))
|
177 |
|
178 |
+
# Introduce delay for 'o1-preview' model
|
179 |
if request.model == 'o1-preview':
|
180 |
delay_seconds = random.randint(20, 60)
|
181 |
+
log_model_delay(delay_seconds, request.model, chat_id)
|
182 |
await asyncio.sleep(delay_seconds)
|
183 |
|
184 |
json_data = {
|
|
|
188 |
"clickedForceWebSearch": False,
|
189 |
"codeModelMode": True,
|
190 |
"githubToken": None,
|
191 |
+
"id": chat_id,
|
192 |
"isChromeExt": False,
|
193 |
"isMicMode": False,
|
194 |
"maxTokens": request.max_tokens,
|
|
|
215 |
async for chunk in response.aiter_text():
|
216 |
full_response += chunk
|
217 |
except httpx.HTTPStatusError as e:
|
218 |
+
log_http_error(e, chat_id)
|
219 |
raise HTTPException(status_code=e.response.status_code, detail=str(e))
|
220 |
except httpx.RequestError as e:
|
221 |
+
log_request_error(e, chat_id)
|
222 |
raise HTTPException(status_code=500, detail=str(e))
|
223 |
if full_response.startswith("$@$v=undefined-rv1$@$"):
|
224 |
full_response = full_response[21:]
|
225 |
|
226 |
+
# Strip the model prefix from the full response
|
227 |
cleaned_full_response = strip_model_prefix(full_response, model_prefix)
|
228 |
|
229 |
return {
|