Spaces:
Sleeping
Sleeping
Update think block extraction and format inline code execution blocks
Browse files- gemini_web2api.py +9 -5
gemini_web2api.py
CHANGED
|
@@ -507,10 +507,10 @@ def gemini_stream_generate(prompt: str, model_info: dict) -> str:
|
|
| 507 |
|
| 508 |
def clean_gemini_text(text: str) -> str:
|
| 509 |
"""Remove internal code execution artifacts and image placeholders."""
|
| 510 |
-
#
|
| 511 |
text = re.sub(
|
| 512 |
-
r'```(
|
| 513 |
-
'', text
|
| 514 |
)
|
| 515 |
# Remove googleusercontent placeholder URLs (image gen/retrieval/collection)
|
| 516 |
text = re.sub(
|
|
@@ -571,7 +571,7 @@ def _scan_complete_wrb_frames(buf: str) -> list:
|
|
| 571 |
return frames
|
| 572 |
|
| 573 |
|
| 574 |
-
def extract_response_text(raw: str) -> str:
|
| 575 |
"""Parse StreamGenerate response to extract final text.
|
| 576 |
|
| 577 |
Uses the upstream's bracket-depth frame scanner for robustness.
|
|
@@ -607,6 +607,10 @@ def extract_response_text(raw: str) -> str:
|
|
| 607 |
break
|
| 608 |
|
| 609 |
final_response = clean_gemini_text(text)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 610 |
return final_response
|
| 611 |
|
| 612 |
|
|
@@ -792,7 +796,7 @@ class GeminiHandler(BaseHTTPRequestHandler):
|
|
| 792 |
|
| 793 |
def _call_gemini(self, prompt, model_info, tools):
|
| 794 |
raw = gemini_stream_generate(prompt, model_info)
|
| 795 |
-
text = extract_response_text(raw)
|
| 796 |
tool_calls = None
|
| 797 |
if tools and text:
|
| 798 |
text, tool_calls = parse_tool_calls(text)
|
|
|
|
| 507 |
|
| 508 |
def clean_gemini_text(text: str) -> str:
|
| 509 |
"""Remove internal code execution artifacts and image placeholders."""
|
| 510 |
+
# Convert code execution blocks to standard markdown
|
| 511 |
text = re.sub(
|
| 512 |
+
r'```(python|javascript|text)\?code_(?:reference|stdout)&code_event_index=\d+',
|
| 513 |
+
r'```\1', text
|
| 514 |
)
|
| 515 |
# Remove googleusercontent placeholder URLs (image gen/retrieval/collection)
|
| 516 |
text = re.sub(
|
|
|
|
| 571 |
return frames
|
| 572 |
|
| 573 |
|
| 574 |
+
def extract_response_text(raw: str, model_info: dict = None) -> str:
|
| 575 |
"""Parse StreamGenerate response to extract final text.
|
| 576 |
|
| 577 |
Uses the upstream's bracket-depth frame scanner for robustness.
|
|
|
|
| 607 |
break
|
| 608 |
|
| 609 |
final_response = clean_gemini_text(text)
|
| 610 |
+
|
| 611 |
+
if model_info and model_info.get("think") == 0:
|
| 612 |
+
final_response = f"<think>\nthink_mode: 0\nwe do not really recieve thinking block from gemini.\n</think>\n\n{final_response}"
|
| 613 |
+
|
| 614 |
return final_response
|
| 615 |
|
| 616 |
|
|
|
|
| 796 |
|
| 797 |
def _call_gemini(self, prompt, model_info, tools):
|
| 798 |
raw = gemini_stream_generate(prompt, model_info)
|
| 799 |
+
text = extract_response_text(raw, model_info)
|
| 800 |
tool_calls = None
|
| 801 |
if tools and text:
|
| 802 |
text, tool_calls = parse_tool_calls(text)
|