Spaces:
Paused
Paused
Update proxy_handler.py
Browse files- proxy_handler.py +22 -1
proxy_handler.py
CHANGED
|
@@ -26,7 +26,7 @@ class ProxyHandler:
|
|
| 26 |
if not self.client.is_closed:
|
| 27 |
await self.client.aclose()
|
| 28 |
|
| 29 |
-
def _clean_thinking_content(self, text: str) -> str:
|
| 30 |
"""
|
| 31 |
Aggressively cleans raw thinking content strings.
|
| 32 |
Removes tool calls, HTML-like tags, and other metadata.
|
|
@@ -70,6 +70,27 @@ def _clean_thinking_content(self, text: str) -> str:
|
|
| 70 |
# Final trim
|
| 71 |
return cleaned_text.strip()
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
async def _prep_upstream(self, req: ChatCompletionRequest) -> Tuple[Dict[str, Any], Dict[str, str], str]:
|
| 74 |
"""Prepares the request body, headers, and cookie for the upstream API."""
|
| 75 |
ck = await cookie_manager.get_next_cookie()
|
|
|
|
| 26 |
if not self.client.is_closed:
|
| 27 |
await self.client.aclose()
|
| 28 |
|
| 29 |
+
def _clean_thinking_content(self, text: str) -> str:
|
| 30 |
"""
|
| 31 |
Aggressively cleans raw thinking content strings.
|
| 32 |
Removes tool calls, HTML-like tags, and other metadata.
|
|
|
|
| 70 |
# Final trim
|
| 71 |
return cleaned_text.strip()
|
| 72 |
|
| 73 |
+
def _clean_answer_content(self, text: str) -> str:
|
| 74 |
+
"""
|
| 75 |
+
Cleans only <glm_block> tags from answer content.
|
| 76 |
+
Does NOT strip whitespace to preserve markdown in streams.
|
| 77 |
+
"""
|
| 78 |
+
if not text:
|
| 79 |
+
return ""
|
| 80 |
+
# Remove only tool call blocks
|
| 81 |
+
cleaned_text = re.sub(r'<glm_block.*?</glm_block>', '', text, flags=re.DOTALL)
|
| 82 |
+
return cleaned_text
|
| 83 |
+
|
| 84 |
+
def _serialize_msgs(self, msgs) -> list:
|
| 85 |
+
"""Converts message objects to a list of dictionaries."""
|
| 86 |
+
out = []
|
| 87 |
+
for m in msgs:
|
| 88 |
+
if hasattr(m, "dict"): out.append(m.dict())
|
| 89 |
+
elif hasattr(m, "model_dump"): out.append(m.model_dump())
|
| 90 |
+
elif isinstance(m, dict): out.append(m)
|
| 91 |
+
else: out.append({"role": getattr(m, "role", "user"), "content": getattr(m, "content", str(m))})
|
| 92 |
+
return out
|
| 93 |
+
|
| 94 |
async def _prep_upstream(self, req: ChatCompletionRequest) -> Tuple[Dict[str, Any], Dict[str, str], str]:
|
| 95 |
"""Prepares the request body, headers, and cookie for the upstream API."""
|
| 96 |
ck = await cookie_manager.get_next_cookie()
|