Update api/utils.py
Browse files- api/utils.py +23 -13
api/utils.py
CHANGED
@@ -10,8 +10,6 @@ import httpx
|
|
10 |
from fastapi import HTTPException
|
11 |
from api.config import (
|
12 |
MODEL_MAPPING,
|
13 |
-
get_headers_api_chat,
|
14 |
-
get_headers_chat,
|
15 |
BASE_URL,
|
16 |
AGENT_MODE,
|
17 |
TRENDING_AGENT_MODE,
|
@@ -80,7 +78,7 @@ def strip_model_prefix(content: str, model_prefix: Optional[str] = None) -> str:
|
|
80 |
logger.debug("No prefix to strip from content.")
|
81 |
return content
|
82 |
|
83 |
-
# Process streaming response with headers
|
84 |
async def process_streaming_response(request: ChatRequest):
|
85 |
chat_id = generate_chat_id()
|
86 |
logger.info(f"Generated Chat ID: {chat_id}")
|
@@ -91,10 +89,12 @@ async def process_streaming_response(request: ChatRequest):
|
|
91 |
referer_path = MODEL_REFERERS.get(request.model, f"/?model={request.model}")
|
92 |
referer_url = f"{BASE_URL}{referer_path}"
|
93 |
|
94 |
-
#
|
95 |
-
|
96 |
-
|
97 |
-
|
|
|
|
|
98 |
|
99 |
# Introduce delay for 'o1-preview' model
|
100 |
if request.model == 'o1-preview':
|
@@ -155,7 +155,7 @@ async def process_streaming_response(request: ChatRequest):
|
|
155 |
logger.error(f"Error occurred during request for Chat ID {chat_id}: {e}")
|
156 |
raise HTTPException(status_code=500, detail=str(e))
|
157 |
|
158 |
-
# Process non-streaming response with headers
|
159 |
async def process_non_streaming_response(request: ChatRequest):
|
160 |
chat_id = generate_chat_id()
|
161 |
logger.info(f"Generated Chat ID: {chat_id}")
|
@@ -167,11 +167,21 @@ async def process_non_streaming_response(request: ChatRequest):
|
|
167 |
referer_url = f"{BASE_URL}{referer_path}"
|
168 |
chat_url = f"{BASE_URL}/chat/{chat_id}?model={request.model}"
|
169 |
|
170 |
-
#
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
|
176 |
# Introduce delay for 'o1-preview' model
|
177 |
if request.model == 'o1-preview':
|
|
|
10 |
from fastapi import HTTPException
|
11 |
from api.config import (
|
12 |
MODEL_MAPPING,
|
|
|
|
|
13 |
BASE_URL,
|
14 |
AGENT_MODE,
|
15 |
TRENDING_AGENT_MODE,
|
|
|
78 |
logger.debug("No prefix to strip from content.")
|
79 |
return content
|
80 |
|
81 |
+
# Process streaming response with combined headers
|
82 |
async def process_streaming_response(request: ChatRequest):
|
83 |
chat_id = generate_chat_id()
|
84 |
logger.info(f"Generated Chat ID: {chat_id}")
|
|
|
89 |
referer_path = MODEL_REFERERS.get(request.model, f"/?model={request.model}")
|
90 |
referer_url = f"{BASE_URL}{referer_path}"
|
91 |
|
92 |
+
# Combined headers for API chat
|
93 |
+
headers_api_chat = {
|
94 |
+
**common_headers,
|
95 |
+
'Content-Type': 'application/json',
|
96 |
+
'Referer': referer_url
|
97 |
+
}
|
98 |
|
99 |
# Introduce delay for 'o1-preview' model
|
100 |
if request.model == 'o1-preview':
|
|
|
155 |
logger.error(f"Error occurred during request for Chat ID {chat_id}: {e}")
|
156 |
raise HTTPException(status_code=500, detail=str(e))
|
157 |
|
158 |
+
# Process non-streaming response with combined headers
|
159 |
async def process_non_streaming_response(request: ChatRequest):
|
160 |
chat_id = generate_chat_id()
|
161 |
logger.info(f"Generated Chat ID: {chat_id}")
|
|
|
167 |
referer_url = f"{BASE_URL}{referer_path}"
|
168 |
chat_url = f"{BASE_URL}/chat/{chat_id}?model={request.model}"
|
169 |
|
170 |
+
# Combined headers for API chat and chat request
|
171 |
+
headers_api_chat = {
|
172 |
+
**common_headers,
|
173 |
+
'Content-Type': 'application/json',
|
174 |
+
'Referer': referer_url
|
175 |
+
}
|
176 |
+
headers_chat = {
|
177 |
+
**common_headers,
|
178 |
+
'Accept': 'text/x-component',
|
179 |
+
'Content-Type': 'text/plain;charset=UTF-8',
|
180 |
+
'Referer': chat_url,
|
181 |
+
'next-action': str(uuid.uuid4()),
|
182 |
+
'next-router-state-tree': json.dumps([""]),
|
183 |
+
'next-url': '/'
|
184 |
+
}
|
185 |
|
186 |
# Introduce delay for 'o1-preview' model
|
187 |
if request.model == 'o1-preview':
|