Update api/utils.py
Browse files- api/utils.py +4 -7
api/utils.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
from datetime import datetime
|
| 2 |
-
import json
|
| 3 |
import uuid
|
| 4 |
import asyncio
|
| 5 |
import random
|
|
@@ -47,7 +46,6 @@ def message_to_dict(message, model_prefix: Optional[str] = None):
|
|
| 47 |
if model_prefix:
|
| 48 |
content = f"{model_prefix} {content}"
|
| 49 |
if isinstance(message.content, list) and len(message.content) == 2 and "image_url" in message.content[1]:
|
| 50 |
-
# Ensure base64 images are always included for all models
|
| 51 |
return {
|
| 52 |
"role": message.role,
|
| 53 |
"content": content,
|
|
@@ -68,7 +66,7 @@ async def process_streaming_response(request: ChatRequest):
|
|
| 68 |
model_prefix = MODEL_PREFIXES.get(request.model, "")
|
| 69 |
|
| 70 |
headers_api_chat = get_headers_api_chat(BASE_URL)
|
| 71 |
-
validated_token = validate.getHid()
|
| 72 |
logger.info(f"Retrieved validated token: {validated_token}")
|
| 73 |
|
| 74 |
if request.model == 'o1-preview':
|
|
@@ -112,10 +110,9 @@ async def process_streaming_response(request: ChatRequest):
|
|
| 112 |
async for line in response.aiter_lines():
|
| 113 |
timestamp = int(datetime.now().timestamp())
|
| 114 |
if line:
|
| 115 |
-
#
|
| 116 |
-
yield f"data: {
|
| 117 |
|
| 118 |
-
yield f"data: {json.dumps(create_chat_completion_data('', request.model, timestamp, 'stop'))}\n\n"
|
| 119 |
yield "data: [DONE]\n\n"
|
| 120 |
except httpx.HTTPStatusError as e:
|
| 121 |
logger.error(f"HTTP error occurred: {e}")
|
|
@@ -134,7 +131,7 @@ async def process_non_streaming_response(request: ChatRequest):
|
|
| 134 |
|
| 135 |
headers_api_chat = get_headers_api_chat(BASE_URL)
|
| 136 |
headers_chat = get_headers_chat(BASE_URL, next_action=str(uuid.uuid4()), next_router_state_tree=json.dumps([""]))
|
| 137 |
-
validated_token = validate.getHid()
|
| 138 |
|
| 139 |
if request.model == 'o1-preview':
|
| 140 |
delay_seconds = random.randint(20, 60)
|
|
|
|
| 1 |
from datetime import datetime
|
|
|
|
| 2 |
import uuid
|
| 3 |
import asyncio
|
| 4 |
import random
|
|
|
|
| 46 |
if model_prefix:
|
| 47 |
content = f"{model_prefix} {content}"
|
| 48 |
if isinstance(message.content, list) and len(message.content) == 2 and "image_url" in message.content[1]:
|
|
|
|
| 49 |
return {
|
| 50 |
"role": message.role,
|
| 51 |
"content": content,
|
|
|
|
| 66 |
model_prefix = MODEL_PREFIXES.get(request.model, "")
|
| 67 |
|
| 68 |
headers_api_chat = get_headers_api_chat(BASE_URL)
|
| 69 |
+
validated_token = validate.getHid()
|
| 70 |
logger.info(f"Retrieved validated token: {validated_token}")
|
| 71 |
|
| 72 |
if request.model == 'o1-preview':
|
|
|
|
| 110 |
async for line in response.aiter_lines():
|
| 111 |
timestamp = int(datetime.now().timestamp())
|
| 112 |
if line:
|
| 113 |
+
# Stream line directly without additional processing or transformation
|
| 114 |
+
yield f"data: {line}\n\n"
|
| 115 |
|
|
|
|
| 116 |
yield "data: [DONE]\n\n"
|
| 117 |
except httpx.HTTPStatusError as e:
|
| 118 |
logger.error(f"HTTP error occurred: {e}")
|
|
|
|
| 131 |
|
| 132 |
headers_api_chat = get_headers_api_chat(BASE_URL)
|
| 133 |
headers_chat = get_headers_chat(BASE_URL, next_action=str(uuid.uuid4()), next_router_state_tree=json.dumps([""]))
|
| 134 |
+
validated_token = validate.getHid()
|
| 135 |
|
| 136 |
if request.model == 'o1-preview':
|
| 137 |
delay_seconds = random.randint(20, 60)
|