Update api/utils.py
Browse files- api/utils.py +20 -1
api/utils.py
CHANGED
@@ -84,10 +84,23 @@ def strip_model_prefix(content: str, model_prefix: Optional[str] = None) -> str:
|
|
84 |
# Function to remove content between '$~~~$' tags
|
85 |
def remove_message_between_special_tags(content: str) -> str:
|
86 |
"""Remove any content that starts with '$~~~$' and ends with '$~~~$', including the tags."""
|
87 |
-
# Use regex to remove the entire block between '$~~~$' tags
|
88 |
content = re.sub(r'\$~~~\$.*?\$~~~\$', '', content, flags=re.DOTALL)
|
89 |
return content
|
90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
# Process streaming response with headers from config.py
|
92 |
async def process_streaming_response(request: ChatRequest):
|
93 |
# Generate a unique ID for this request
|
@@ -172,6 +185,9 @@ async def process_streaming_response(request: ChatRequest):
|
|
172 |
|
173 |
# Remove content between special tags
|
174 |
content = remove_message_between_special_tags(content)
|
|
|
|
|
|
|
175 |
|
176 |
cleaned_content = strip_model_prefix(content, model_prefix)
|
177 |
yield f"data: {json.dumps(create_chat_completion_data(cleaned_content, request.model, timestamp))}\n\n"
|
@@ -287,6 +303,9 @@ async def process_non_streaming_response(request: ChatRequest):
|
|
287 |
# Remove content between special tags
|
288 |
full_response = remove_message_between_special_tags(full_response)
|
289 |
|
|
|
|
|
|
|
290 |
cleaned_full_response = strip_model_prefix(full_response, model_prefix)
|
291 |
|
292 |
return {
|
|
|
84 |
# Function to remove content between '$~~~$' tags
|
85 |
def remove_message_between_special_tags(content: str) -> str:
|
86 |
"""Remove any content that starts with '$~~~$' and ends with '$~~~$', including the tags."""
|
|
|
87 |
content = re.sub(r'\$~~~\$.*?\$~~~\$', '', content, flags=re.DOTALL)
|
88 |
return content
|
89 |
|
90 |
+
# Function to remove search results but keep the links
|
91 |
+
def remove_search_results(content: str) -> str:
|
92 |
+
"""Remove search result sections, keeping the URLs intact."""
|
93 |
+
# Regex to match search result JSON (structured with `title`, `snippet`, `link`)
|
94 |
+
search_result_pattern = re.compile(r'(\{.*?"link":\s*"https?://[^\}]+.*?\})', re.DOTALL)
|
95 |
+
|
96 |
+
# Remove all search results but keep URLs intact by using a regex
|
97 |
+
cleaned_content = re.sub(search_result_pattern, '', content)
|
98 |
+
|
99 |
+
# Optional: clean up any unnecessary newlines or extra spaces left by the removal
|
100 |
+
cleaned_content = cleaned_content.strip()
|
101 |
+
|
102 |
+
return cleaned_content
|
103 |
+
|
104 |
# Process streaming response with headers from config.py
|
105 |
async def process_streaming_response(request: ChatRequest):
|
106 |
# Generate a unique ID for this request
|
|
|
185 |
|
186 |
# Remove content between special tags
|
187 |
content = remove_message_between_special_tags(content)
|
188 |
+
|
189 |
+
# Remove search results but keep the links intact
|
190 |
+
content = remove_search_results(content)
|
191 |
|
192 |
cleaned_content = strip_model_prefix(content, model_prefix)
|
193 |
yield f"data: {json.dumps(create_chat_completion_data(cleaned_content, request.model, timestamp))}\n\n"
|
|
|
303 |
# Remove content between special tags
|
304 |
full_response = remove_message_between_special_tags(full_response)
|
305 |
|
306 |
+
# Remove search results but keep the links intact
|
307 |
+
full_response = remove_search_results(full_response)
|
308 |
+
|
309 |
cleaned_full_response = strip_model_prefix(full_response, model_prefix)
|
310 |
|
311 |
return {
|