Update api/utils.py
Browse files- api/utils.py +16 -10
api/utils.py
CHANGED
|
@@ -9,8 +9,7 @@ import httpx
|
|
| 9 |
from fastapi import HTTPException
|
| 10 |
from api.config import (
|
| 11 |
MODEL_MAPPING,
|
| 12 |
-
|
| 13 |
-
get_headers_chat,
|
| 14 |
BASE_URL,
|
| 15 |
AGENT_MODE,
|
| 16 |
TRENDING_AGENT_MODE,
|
|
@@ -68,7 +67,7 @@ def strip_model_prefix(content: str, model_prefix: Optional[str] = None) -> str:
|
|
| 68 |
return content[len(model_prefix):].strip()
|
| 69 |
return content
|
| 70 |
|
| 71 |
-
# Process streaming response
|
| 72 |
async def process_streaming_response(request: ChatRequest):
|
| 73 |
# Generate a unique ID for this request
|
| 74 |
request_id = f"chatcmpl-{uuid.uuid4()}"
|
|
@@ -78,8 +77,8 @@ async def process_streaming_response(request: ChatRequest):
|
|
| 78 |
trending_agent_mode = TRENDING_AGENT_MODE.get(request.model, {})
|
| 79 |
model_prefix = MODEL_PREFIXES.get(request.model, "")
|
| 80 |
|
| 81 |
-
#
|
| 82 |
-
headers_api_chat =
|
| 83 |
|
| 84 |
if request.model == 'o1-preview':
|
| 85 |
delay_seconds = random.randint(1, 60)
|
|
@@ -144,7 +143,7 @@ async def process_streaming_response(request: ChatRequest):
|
|
| 144 |
logger.error(f"Error occurred during request for Request ID {request_id}: {e}")
|
| 145 |
raise HTTPException(status_code=500, detail=str(e))
|
| 146 |
|
| 147 |
-
# Process non-streaming response
|
| 148 |
async def process_non_streaming_response(request: ChatRequest):
|
| 149 |
# Generate a unique ID for this request
|
| 150 |
request_id = f"chatcmpl-{uuid.uuid4()}"
|
|
@@ -154,9 +153,16 @@ async def process_non_streaming_response(request: ChatRequest):
|
|
| 154 |
trending_agent_mode = TRENDING_AGENT_MODE.get(request.model, {})
|
| 155 |
model_prefix = MODEL_PREFIXES.get(request.model, "")
|
| 156 |
|
| 157 |
-
#
|
| 158 |
-
headers_api_chat =
|
| 159 |
-
headers_chat =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
if request.model == 'o1-preview':
|
| 162 |
delay_seconds = random.randint(20, 60)
|
|
@@ -208,7 +214,7 @@ async def process_non_streaming_response(request: ChatRequest):
|
|
| 208 |
except httpx.RequestError as e:
|
| 209 |
logger.error(f"Error occurred during request for Request ID {request_id}: {e}")
|
| 210 |
raise HTTPException(status_code=500, detail=str(e))
|
| 211 |
-
|
| 212 |
if full_response.startswith("$@$v=undefined-rv1$@$"):
|
| 213 |
full_response = full_response[21:]
|
| 214 |
|
|
|
|
| 9 |
from fastapi import HTTPException
|
| 10 |
from api.config import (
|
| 11 |
MODEL_MAPPING,
|
| 12 |
+
common_headers, # Import common_headers directly
|
|
|
|
| 13 |
BASE_URL,
|
| 14 |
AGENT_MODE,
|
| 15 |
TRENDING_AGENT_MODE,
|
|
|
|
| 67 |
return content[len(model_prefix):].strip()
|
| 68 |
return content
|
| 69 |
|
| 70 |
+
# Process streaming response without header functions
|
| 71 |
async def process_streaming_response(request: ChatRequest):
|
| 72 |
# Generate a unique ID for this request
|
| 73 |
request_id = f"chatcmpl-{uuid.uuid4()}"
|
|
|
|
| 77 |
trending_agent_mode = TRENDING_AGENT_MODE.get(request.model, {})
|
| 78 |
model_prefix = MODEL_PREFIXES.get(request.model, "")
|
| 79 |
|
| 80 |
+
# Define headers directly
|
| 81 |
+
headers_api_chat = {**common_headers, 'Content-Type': 'application/json'}
|
| 82 |
|
| 83 |
if request.model == 'o1-preview':
|
| 84 |
delay_seconds = random.randint(1, 60)
|
|
|
|
| 143 |
logger.error(f"Error occurred during request for Request ID {request_id}: {e}")
|
| 144 |
raise HTTPException(status_code=500, detail=str(e))
|
| 145 |
|
| 146 |
+
# Process non-streaming response without header functions
|
| 147 |
async def process_non_streaming_response(request: ChatRequest):
|
| 148 |
# Generate a unique ID for this request
|
| 149 |
request_id = f"chatcmpl-{uuid.uuid4()}"
|
|
|
|
| 153 |
trending_agent_mode = TRENDING_AGENT_MODE.get(request.model, {})
|
| 154 |
model_prefix = MODEL_PREFIXES.get(request.model, "")
|
| 155 |
|
| 156 |
+
# Define headers directly
|
| 157 |
+
headers_api_chat = {**common_headers, 'Content-Type': 'application/json'}
|
| 158 |
+
headers_chat = {
|
| 159 |
+
**common_headers,
|
| 160 |
+
'Accept': 'text/x-component',
|
| 161 |
+
'Content-Type': 'text/plain;charset=UTF-8',
|
| 162 |
+
'next-action': str(uuid.uuid4()),
|
| 163 |
+
'next-router-state-tree': json.dumps([""]),
|
| 164 |
+
'next-url': '/',
|
| 165 |
+
}
|
| 166 |
|
| 167 |
if request.model == 'o1-preview':
|
| 168 |
delay_seconds = random.randint(20, 60)
|
|
|
|
| 214 |
except httpx.RequestError as e:
|
| 215 |
logger.error(f"Error occurred during request for Request ID {request_id}: {e}")
|
| 216 |
raise HTTPException(status_code=500, detail=str(e))
|
| 217 |
+
|
| 218 |
if full_response.startswith("$@$v=undefined-rv1$@$"):
|
| 219 |
full_response = full_response[21:]
|
| 220 |
|