Update api/utils.py
Browse files- api/utils.py +4 -12
api/utils.py
CHANGED
@@ -20,8 +20,7 @@ logger = setup_logger(__name__)
|
|
20 |
def create_chat_completion_data(
|
21 |
content: str, model: str, timestamp: int, finish_reason: Optional[str] = None
|
22 |
) -> Dict[str, Any]:
|
23 |
-
#
|
24 |
-
cleaned_content = '\n'.join([line.rstrip() for line in content.splitlines() if line.strip() != ''])
|
25 |
return {
|
26 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
27 |
"object": "chat.completion.chunk",
|
@@ -30,7 +29,7 @@ def create_chat_completion_data(
|
|
30 |
"choices": [
|
31 |
{
|
32 |
"index": 0,
|
33 |
-
"delta": {"content":
|
34 |
"finish_reason": finish_reason,
|
35 |
}
|
36 |
],
|
@@ -85,7 +84,7 @@ async def process_streaming_response(request: ChatRequest):
|
|
85 |
try:
|
86 |
async with client.stream(
|
87 |
"POST",
|
88 |
-
f"{BASE_URL}/api/chat",
|
89 |
headers=headers,
|
90 |
json=json_data,
|
91 |
timeout=100,
|
@@ -94,10 +93,7 @@ async def process_streaming_response(request: ChatRequest):
|
|
94 |
async for line in response.aiter_lines():
|
95 |
timestamp = int(datetime.now().timestamp())
|
96 |
if line:
|
97 |
-
|
98 |
-
content = line.strip()
|
99 |
-
content = '\n'.join([l.rstrip() for l in content.splitlines() if l.strip() != ''])
|
100 |
-
content += "\n"
|
101 |
if content.startswith("$@$v=undefined-rv1$@$"):
|
102 |
yield f"data: {json.dumps(create_chat_completion_data(content[21:], request.model, timestamp))}\n\n"
|
103 |
else:
|
@@ -154,10 +150,6 @@ async def process_non_streaming_response(request: ChatRequest):
|
|
154 |
if full_response.startswith("$@$v=undefined-rv1$@$"):
|
155 |
full_response = full_response[21:]
|
156 |
|
157 |
-
# Clean the content to remove extra spaces
|
158 |
-
full_response = full_response.strip()
|
159 |
-
full_response = '\n'.join([line.rstrip() for line in full_response.splitlines() if line.strip() != ''])
|
160 |
-
|
161 |
return {
|
162 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
163 |
"object": "chat.completion",
|
|
|
20 |
def create_chat_completion_data(
|
21 |
content: str, model: str, timestamp: int, finish_reason: Optional[str] = None
|
22 |
) -> Dict[str, Any]:
|
23 |
+
# Removed the content cleaning function
|
|
|
24 |
return {
|
25 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
26 |
"object": "chat.completion.chunk",
|
|
|
29 |
"choices": [
|
30 |
{
|
31 |
"index": 0,
|
32 |
+
"delta": {"content": content, "role": "assistant"},
|
33 |
"finish_reason": finish_reason,
|
34 |
}
|
35 |
],
|
|
|
84 |
try:
|
85 |
async with client.stream(
|
86 |
"POST",
|
87 |
+
f"{BASE_URL}/api/chat",
|
88 |
headers=headers,
|
89 |
json=json_data,
|
90 |
timeout=100,
|
|
|
93 |
async for line in response.aiter_lines():
|
94 |
timestamp = int(datetime.now().timestamp())
|
95 |
if line:
|
96 |
+
content = line + "\n"
|
|
|
|
|
|
|
97 |
if content.startswith("$@$v=undefined-rv1$@$"):
|
98 |
yield f"data: {json.dumps(create_chat_completion_data(content[21:], request.model, timestamp))}\n\n"
|
99 |
else:
|
|
|
150 |
if full_response.startswith("$@$v=undefined-rv1$@$"):
|
151 |
full_response = full_response[21:]
|
152 |
|
|
|
|
|
|
|
|
|
153 |
return {
|
154 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
155 |
"object": "chat.completion",
|