Update api/utils.py
Browse files- api/utils.py +45 -7
api/utils.py
CHANGED
|
@@ -6,7 +6,13 @@ import uuid
|
|
| 6 |
|
| 7 |
import httpx
|
| 8 |
from api import validate
|
| 9 |
-
from api.config import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
from fastapi import Depends, security
|
| 11 |
from fastapi.security import HTTPAuthorizationCredentials
|
| 12 |
|
|
@@ -56,6 +62,10 @@ def message_to_dict(message):
|
|
| 56 |
else:
|
| 57 |
return {"role": message.role, "content": message.content}
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
async def process_streaming_response(request: ChatRequest):
|
| 60 |
agent_mode = AGENT_MODE.get(request.model, {})
|
| 61 |
trending_agent_mode = TRENDING_AGENT_MODE.get(request.model, {})
|
|
@@ -89,6 +99,11 @@ async def process_streaming_response(request: ChatRequest):
|
|
| 89 |
"validated": validate.getHid()
|
| 90 |
}
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
async with httpx.AsyncClient() as client:
|
| 93 |
try:
|
| 94 |
async with client.stream(
|
|
@@ -106,14 +121,25 @@ async def process_streaming_response(request: ChatRequest):
|
|
| 106 |
if "https://www.blackbox.ai" in content:
|
| 107 |
validate.getHid(True)
|
| 108 |
content = "hid已刷新,重新对话即可\n"
|
| 109 |
-
|
|
|
|
|
|
|
| 110 |
break
|
| 111 |
if content.startswith("$@$v=undefined-rv1$@$"):
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
| 113 |
else:
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
yield "data: [DONE]\n\n"
|
| 118 |
except httpx.HTTPStatusError as e:
|
| 119 |
logger.error(f"HTTP error occurred: {e}")
|
|
@@ -155,6 +181,11 @@ async def process_non_streaming_response(request: ChatRequest):
|
|
| 155 |
"validated": validate.getHid()
|
| 156 |
}
|
| 157 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
full_response = ""
|
| 159 |
async with httpx.AsyncClient() as client:
|
| 160 |
async with client.stream(
|
|
@@ -162,11 +193,18 @@ async def process_non_streaming_response(request: ChatRequest):
|
|
| 162 |
) as response:
|
| 163 |
async for chunk in response.aiter_text():
|
| 164 |
full_response += chunk
|
|
|
|
| 165 |
if "https://www.blackbox.ai" in full_response:
|
| 166 |
validate.getHid(True)
|
| 167 |
full_response = "hid已刷新,重新对话即可"
|
|
|
|
| 168 |
if full_response.startswith("$@$v=undefined-rv1$@$"):
|
| 169 |
full_response = full_response[21:]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
return {
|
| 171 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
| 172 |
"object": "chat.completion",
|
|
@@ -180,4 +218,4 @@ async def process_non_streaming_response(request: ChatRequest):
|
|
| 180 |
}
|
| 181 |
],
|
| 182 |
"usage": None,
|
| 183 |
-
}
|
|
|
|
| 6 |
|
| 7 |
import httpx
|
| 8 |
from api import validate
|
| 9 |
+
from api.config import (
|
| 10 |
+
MODEL_MAPPING,
|
| 11 |
+
MODEL_PREFIXES, # Import MODEL_PREFIXES
|
| 12 |
+
headers,
|
| 13 |
+
AGENT_MODE,
|
| 14 |
+
TRENDING_AGENT_MODE
|
| 15 |
+
)
|
| 16 |
from fastapi import Depends, security
|
| 17 |
from fastapi.security import HTTPAuthorizationCredentials
|
| 18 |
|
|
|
|
| 62 |
else:
|
| 63 |
return {"role": message.role, "content": message.content}
|
| 64 |
|
| 65 |
+
def get_model_prefix(model: str) -> str:
|
| 66 |
+
"""Retrieve the prefix for a given model."""
|
| 67 |
+
return MODEL_PREFIXES.get(model, "")
|
| 68 |
+
|
| 69 |
async def process_streaming_response(request: ChatRequest):
|
| 70 |
agent_mode = AGENT_MODE.get(request.model, {})
|
| 71 |
trending_agent_mode = TRENDING_AGENT_MODE.get(request.model, {})
|
|
|
|
| 99 |
"validated": validate.getHid()
|
| 100 |
}
|
| 101 |
|
| 102 |
+
# Get the model prefix
|
| 103 |
+
prefix = get_model_prefix(request.model)
|
| 104 |
+
if prefix:
|
| 105 |
+
prefix += " " # Add space after prefix
|
| 106 |
+
|
| 107 |
async with httpx.AsyncClient() as client:
|
| 108 |
try:
|
| 109 |
async with client.stream(
|
|
|
|
| 121 |
if "https://www.blackbox.ai" in content:
|
| 122 |
validate.getHid(True)
|
| 123 |
content = "hid已刷新,重新对话即可\n"
|
| 124 |
+
# Prepend prefix to the content
|
| 125 |
+
prefixed_content = prefix + content if prefix else content
|
| 126 |
+
yield f"data: {json.dumps(create_chat_completion_data(prefixed_content, request.model, timestamp))}\n\n"
|
| 127 |
break
|
| 128 |
if content.startswith("$@$v=undefined-rv1$@$"):
|
| 129 |
+
# Remove the special prefix and prepend model prefix
|
| 130 |
+
actual_content = content[21:]
|
| 131 |
+
prefixed_content = prefix + actual_content if prefix else actual_content
|
| 132 |
+
yield f"data: {json.dumps(create_chat_completion_data(prefixed_content, request.model, timestamp))}\n\n"
|
| 133 |
else:
|
| 134 |
+
# Prepend model prefix to the content
|
| 135 |
+
prefixed_content = prefix + content if prefix else content
|
| 136 |
+
yield f"data: {json.dumps(create_chat_completion_data(prefixed_content, request.model, timestamp))}\n\n"
|
| 137 |
+
|
| 138 |
+
# Indicate the end of the streaming response with the prefix
|
| 139 |
+
finish_content = ''
|
| 140 |
+
if prefix:
|
| 141 |
+
finish_content = prefix
|
| 142 |
+
yield f"data: {json.dumps(create_chat_completion_data(finish_content, request.model, timestamp, 'stop'))}\n\n"
|
| 143 |
yield "data: [DONE]\n\n"
|
| 144 |
except httpx.HTTPStatusError as e:
|
| 145 |
logger.error(f"HTTP error occurred: {e}")
|
|
|
|
| 181 |
"validated": validate.getHid()
|
| 182 |
}
|
| 183 |
|
| 184 |
+
# Get the model prefix
|
| 185 |
+
prefix = get_model_prefix(request.model)
|
| 186 |
+
if prefix:
|
| 187 |
+
prefix += " " # Add space after prefix
|
| 188 |
+
|
| 189 |
full_response = ""
|
| 190 |
async with httpx.AsyncClient() as client:
|
| 191 |
async with client.stream(
|
|
|
|
| 193 |
) as response:
|
| 194 |
async for chunk in response.aiter_text():
|
| 195 |
full_response += chunk
|
| 196 |
+
|
| 197 |
if "https://www.blackbox.ai" in full_response:
|
| 198 |
validate.getHid(True)
|
| 199 |
full_response = "hid已刷新,重新对话即可"
|
| 200 |
+
|
| 201 |
if full_response.startswith("$@$v=undefined-rv1$@$"):
|
| 202 |
full_response = full_response[21:]
|
| 203 |
+
|
| 204 |
+
# Prepend model prefix to the full response
|
| 205 |
+
if prefix:
|
| 206 |
+
full_response = prefix + full_response
|
| 207 |
+
|
| 208 |
return {
|
| 209 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
| 210 |
"object": "chat.completion",
|
|
|
|
| 218 |
}
|
| 219 |
],
|
| 220 |
"usage": None,
|
| 221 |
+
}
|