Spaces:
Running
Running
fix system prompt issue
Browse files- anycoder_app/deploy.py +12 -1
- anycoder_app/docs_manager.py +13 -10
- anycoder_app/models.py +12 -0
- anycoder_app/parsers.py +7 -1
- anycoder_app/prompts.py +6 -0
anycoder_app/deploy.py
CHANGED
|
@@ -34,7 +34,8 @@ from .parsers import (
|
|
| 34 |
from .models import (
|
| 35 |
get_inference_client, get_real_model_id, history_to_messages,
|
| 36 |
history_to_chatbot_messages, strip_placeholder_thinking,
|
| 37 |
-
is_placeholder_thinking_only, extract_last_thinking_line
|
|
|
|
| 38 |
)
|
| 39 |
from . import prompts
|
| 40 |
from .prompts import (
|
|
@@ -231,8 +232,10 @@ Generate the exact search/replace blocks needed to make these changes."""
|
|
| 231 |
# Update system prompts if needed
|
| 232 |
if language == "gradio":
|
| 233 |
update_gradio_system_prompts()
|
|
|
|
| 234 |
elif language == "json":
|
| 235 |
update_json_system_prompts()
|
|
|
|
| 236 |
|
| 237 |
# Choose system prompt based on context
|
| 238 |
# Special case: If user is asking about model identity, use neutral prompt
|
|
@@ -269,6 +272,11 @@ Generate the exact search/replace blocks needed to make these changes."""
|
|
| 269 |
system_prompt = get_comfyui_system_prompt()
|
| 270 |
else:
|
| 271 |
system_prompt = GENERIC_SYSTEM_PROMPT.format(language=language)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
|
| 273 |
messages = history_to_messages(_history, system_prompt)
|
| 274 |
|
|
@@ -491,6 +499,9 @@ Generate the exact search/replace blocks needed to make these changes."""
|
|
| 491 |
# This is a structured thinking chunk, skip it to avoid polluting output
|
| 492 |
continue
|
| 493 |
chunk_content = chunk_str
|
|
|
|
|
|
|
|
|
|
| 494 |
if _current_model["id"] == "gpt-5":
|
| 495 |
# If this chunk is only placeholder thinking, surface a status update without polluting content
|
| 496 |
if is_placeholder_thinking_only(chunk_content):
|
|
|
|
| 34 |
from .models import (
|
| 35 |
get_inference_client, get_real_model_id, history_to_messages,
|
| 36 |
history_to_chatbot_messages, strip_placeholder_thinking,
|
| 37 |
+
is_placeholder_thinking_only, extract_last_thinking_line,
|
| 38 |
+
strip_thinking_tags
|
| 39 |
)
|
| 40 |
from . import prompts
|
| 41 |
from .prompts import (
|
|
|
|
| 232 |
# Update system prompts if needed
|
| 233 |
if language == "gradio":
|
| 234 |
update_gradio_system_prompts()
|
| 235 |
+
print(f"[Generation] Updated Gradio system prompt (length: {len(prompts.GRADIO_SYSTEM_PROMPT)} chars)")
|
| 236 |
elif language == "json":
|
| 237 |
update_json_system_prompts()
|
| 238 |
+
print(f"[Generation] Updated JSON system prompt (length: {len(prompts.JSON_SYSTEM_PROMPT)} chars)")
|
| 239 |
|
| 240 |
# Choose system prompt based on context
|
| 241 |
# Special case: If user is asking about model identity, use neutral prompt
|
|
|
|
| 272 |
system_prompt = get_comfyui_system_prompt()
|
| 273 |
else:
|
| 274 |
system_prompt = GENERIC_SYSTEM_PROMPT.format(language=language)
|
| 275 |
+
|
| 276 |
+
# Debug: Log system prompt info
|
| 277 |
+
prompt_preview = system_prompt[:200] if system_prompt else "None"
|
| 278 |
+
print(f"[Generation] Using system prompt (first 200 chars): {prompt_preview}...")
|
| 279 |
+
print(f"[Generation] System prompt total length: {len(system_prompt) if system_prompt else 0} chars")
|
| 280 |
|
| 281 |
messages = history_to_messages(_history, system_prompt)
|
| 282 |
|
|
|
|
| 499 |
# This is a structured thinking chunk, skip it to avoid polluting output
|
| 500 |
continue
|
| 501 |
chunk_content = chunk_str
|
| 502 |
+
|
| 503 |
+
# Strip thinking tags and tool call markers from all streaming chunks
|
| 504 |
+
chunk_content = strip_thinking_tags(chunk_content)
|
| 505 |
if _current_model["id"] == "gpt-5":
|
| 506 |
# If this chunk is only placeholder thinking, surface a status update without polluting content
|
| 507 |
if is_placeholder_thinking_only(chunk_content):
|
anycoder_app/docs_manager.py
CHANGED
|
@@ -16,6 +16,7 @@ from .config import (
|
|
| 16 |
FASTRTC_LLMS_TXT_URL, FASTRTC_DOCS_CACHE_FILE, FASTRTC_DOCS_LAST_UPDATE_FILE,
|
| 17 |
FASTRTC_DOCS_UPDATE_ON_APP_UPDATE, _fastrtc_docs_content, _fastrtc_docs_last_fetched
|
| 18 |
)
|
|
|
|
| 19 |
|
| 20 |
def fetch_gradio_docs() -> Optional[str]:
|
| 21 |
"""Fetch the latest Gradio documentation from llms.txt"""
|
|
@@ -370,14 +371,18 @@ def get_fastrtc_docs_content() -> str:
|
|
| 370 |
|
| 371 |
def update_gradio_system_prompts():
|
| 372 |
"""Update the global Gradio system prompts with latest documentation"""
|
| 373 |
-
global GRADIO_SYSTEM_PROMPT, GRADIO_SYSTEM_PROMPT_WITH_SEARCH
|
| 374 |
-
|
| 375 |
docs_content = get_gradio_docs_content()
|
| 376 |
fastrtc_content = get_fastrtc_docs_content()
|
| 377 |
|
| 378 |
# Base system prompt
|
| 379 |
base_prompt = """You are an expert Gradio developer. Create a complete, working Gradio application based on the user's request. Generate all necessary code to make the application functional and runnable.
|
| 380 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 381 |
## Multi-File Application Structure
|
| 382 |
|
| 383 |
When creating complex Gradio applications, organize your code into multiple files for better maintainability:
|
|
@@ -1280,14 +1285,12 @@ This reference is automatically synced from https://fastrtc.org/llms.txt to ensu
|
|
| 1280 |
base_prompt += fastrtc_section
|
| 1281 |
search_prompt += fastrtc_section
|
| 1282 |
|
| 1283 |
-
# Update the prompts
|
| 1284 |
-
GRADIO_SYSTEM_PROMPT = base_prompt + docs_content + "\n\nAlways use the exact function signatures from this API reference and follow modern Gradio patterns.\n\nIMPORTANT: Always include \"Built with anycoder\" as clickable text in the header/top section of your application that links to https://huggingface.co/spaces/akhaliq/anycoder"
|
| 1285 |
-
GRADIO_SYSTEM_PROMPT_WITH_SEARCH = search_prompt + docs_content + "\n\nAlways use the exact function signatures from this API reference and follow modern Gradio patterns.\n\nIMPORTANT: Always include \"Built with anycoder\" as clickable text in the header/top section of your application that links to https://huggingface.co/spaces/akhaliq/anycoder"
|
| 1286 |
|
| 1287 |
def update_json_system_prompts():
|
| 1288 |
"""Update the global JSON system prompts with latest ComfyUI documentation"""
|
| 1289 |
-
global JSON_SYSTEM_PROMPT, JSON_SYSTEM_PROMPT_WITH_SEARCH
|
| 1290 |
-
|
| 1291 |
docs_content = get_comfyui_docs_content()
|
| 1292 |
|
| 1293 |
# Base system prompt for regular JSON
|
|
@@ -1329,9 +1332,9 @@ This reference is automatically synced from https://docs.comfy.org/llms.txt to e
|
|
| 1329 |
base_prompt += comfyui_section
|
| 1330 |
search_prompt += comfyui_section
|
| 1331 |
|
| 1332 |
-
# Update the prompts
|
| 1333 |
-
JSON_SYSTEM_PROMPT = base_prompt
|
| 1334 |
-
JSON_SYSTEM_PROMPT_WITH_SEARCH = search_prompt
|
| 1335 |
|
| 1336 |
def get_comfyui_system_prompt():
|
| 1337 |
"""Get ComfyUI-specific system prompt with enhanced guidance"""
|
|
|
|
| 16 |
FASTRTC_LLMS_TXT_URL, FASTRTC_DOCS_CACHE_FILE, FASTRTC_DOCS_LAST_UPDATE_FILE,
|
| 17 |
FASTRTC_DOCS_UPDATE_ON_APP_UPDATE, _fastrtc_docs_content, _fastrtc_docs_last_fetched
|
| 18 |
)
|
| 19 |
+
from . import prompts
|
| 20 |
|
| 21 |
def fetch_gradio_docs() -> Optional[str]:
|
| 22 |
"""Fetch the latest Gradio documentation from llms.txt"""
|
|
|
|
| 371 |
|
| 372 |
def update_gradio_system_prompts():
|
| 373 |
"""Update the global Gradio system prompts with latest documentation"""
|
|
|
|
|
|
|
| 374 |
docs_content = get_gradio_docs_content()
|
| 375 |
fastrtc_content = get_fastrtc_docs_content()
|
| 376 |
|
| 377 |
# Base system prompt
|
| 378 |
base_prompt = """You are an expert Gradio developer. Create a complete, working Gradio application based on the user's request. Generate all necessary code to make the application functional and runnable.
|
| 379 |
|
| 380 |
+
π¨ CRITICAL OUTPUT RULES:
|
| 381 |
+
- DO NOT use <think> tags or thinking blocks in your output
|
| 382 |
+
- DO NOT use [TOOL_CALL] or any tool call markers
|
| 383 |
+
- Generate ONLY the requested code files and requirements.txt
|
| 384 |
+
- No explanatory text outside the code blocks
|
| 385 |
+
|
| 386 |
## Multi-File Application Structure
|
| 387 |
|
| 388 |
When creating complex Gradio applications, organize your code into multiple files for better maintainability:
|
|
|
|
| 1285 |
base_prompt += fastrtc_section
|
| 1286 |
search_prompt += fastrtc_section
|
| 1287 |
|
| 1288 |
+
# Update the prompts in the prompts module
|
| 1289 |
+
prompts.GRADIO_SYSTEM_PROMPT = base_prompt + docs_content + "\n\nAlways use the exact function signatures from this API reference and follow modern Gradio patterns.\n\nIMPORTANT: Always include \"Built with anycoder\" as clickable text in the header/top section of your application that links to https://huggingface.co/spaces/akhaliq/anycoder"
|
| 1290 |
+
prompts.GRADIO_SYSTEM_PROMPT_WITH_SEARCH = search_prompt + docs_content + "\n\nAlways use the exact function signatures from this API reference and follow modern Gradio patterns.\n\nIMPORTANT: Always include \"Built with anycoder\" as clickable text in the header/top section of your application that links to https://huggingface.co/spaces/akhaliq/anycoder"
|
| 1291 |
|
| 1292 |
def update_json_system_prompts():
|
| 1293 |
"""Update the global JSON system prompts with latest ComfyUI documentation"""
|
|
|
|
|
|
|
| 1294 |
docs_content = get_comfyui_docs_content()
|
| 1295 |
|
| 1296 |
# Base system prompt for regular JSON
|
|
|
|
| 1332 |
base_prompt += comfyui_section
|
| 1333 |
search_prompt += comfyui_section
|
| 1334 |
|
| 1335 |
+
# Update the prompts in the prompts module
|
| 1336 |
+
prompts.JSON_SYSTEM_PROMPT = base_prompt
|
| 1337 |
+
prompts.JSON_SYSTEM_PROMPT_WITH_SEARCH = search_prompt
|
| 1338 |
|
| 1339 |
def get_comfyui_system_prompt():
|
| 1340 |
"""Get ComfyUI-specific system prompt with enhanced guidance"""
|
anycoder_app/models.py
CHANGED
|
@@ -279,6 +279,18 @@ def remove_code_block(text):
|
|
| 279 |
|
| 280 |
## React CDN compatibility fixer removed per user preference
|
| 281 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 282 |
def strip_placeholder_thinking(text: str) -> str:
|
| 283 |
"""Remove placeholder 'Thinking...' status lines from streamed text."""
|
| 284 |
if not text:
|
|
|
|
| 279 |
|
| 280 |
## React CDN compatibility fixer removed per user preference
|
| 281 |
|
| 282 |
+
def strip_thinking_tags(text: str) -> str:
|
| 283 |
+
"""Strip <think> tags and [TOOL_CALL] markers from streaming output."""
|
| 284 |
+
if not text:
|
| 285 |
+
return text
|
| 286 |
+
# Remove <think> opening tags
|
| 287 |
+
text = re.sub(r'<think>', '', text, flags=re.IGNORECASE)
|
| 288 |
+
# Remove </think> closing tags
|
| 289 |
+
text = re.sub(r'</think>', '', text, flags=re.IGNORECASE)
|
| 290 |
+
# Remove [TOOL_CALL] markers
|
| 291 |
+
text = re.sub(r'\[/?TOOL_CALL\]', '', text, flags=re.IGNORECASE)
|
| 292 |
+
return text
|
| 293 |
+
|
| 294 |
def strip_placeholder_thinking(text: str) -> str:
|
| 295 |
"""Remove placeholder 'Thinking...' status lines from streamed text."""
|
| 296 |
if not text:
|
anycoder_app/parsers.py
CHANGED
|
@@ -16,11 +16,17 @@ from .config import SEARCH_START, DIVIDER, REPLACE_END
|
|
| 16 |
History = List[Dict[str, str]]
|
| 17 |
|
| 18 |
def strip_tool_call_markers(text):
|
| 19 |
-
"""Remove TOOL_CALL markers that some LLMs
|
| 20 |
if not text:
|
| 21 |
return text
|
| 22 |
# Remove [TOOL_CALL] and [/TOOL_CALL] markers
|
| 23 |
text = re.sub(r'\[/?TOOL_CALL\]', '', text, flags=re.IGNORECASE)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
# Remove standalone }} that appears with tool calls
|
| 25 |
# Only remove if it's on its own line or at the end
|
| 26 |
text = re.sub(r'^\s*\}\}\s*$', '', text, flags=re.MULTILINE)
|
|
|
|
| 16 |
History = List[Dict[str, str]]
|
| 17 |
|
| 18 |
def strip_tool_call_markers(text):
|
| 19 |
+
"""Remove TOOL_CALL markers and thinking tags that some LLMs add to their output."""
|
| 20 |
if not text:
|
| 21 |
return text
|
| 22 |
# Remove [TOOL_CALL] and [/TOOL_CALL] markers
|
| 23 |
text = re.sub(r'\[/?TOOL_CALL\]', '', text, flags=re.IGNORECASE)
|
| 24 |
+
# Remove <think> and </think> tags and their content
|
| 25 |
+
text = re.sub(r'<think>[\s\S]*?</think>', '', text, flags=re.IGNORECASE)
|
| 26 |
+
# Remove any remaining unclosed <think> tags at the start
|
| 27 |
+
text = re.sub(r'^<think>[\s\S]*?(?=\n|$)', '', text, flags=re.IGNORECASE | re.MULTILINE)
|
| 28 |
+
# Remove any remaining </think> tags
|
| 29 |
+
text = re.sub(r'</think>', '', text, flags=re.IGNORECASE)
|
| 30 |
# Remove standalone }} that appears with tool calls
|
| 31 |
# Only remove if it's on its own line or at the end
|
| 32 |
text = re.sub(r'^\s*\}\}\s*$', '', text, flags=re.MULTILINE)
|
anycoder_app/prompts.py
CHANGED
|
@@ -521,6 +521,12 @@ CRITICAL: For imported spaces that lack anycoder attribution, you MUST add it as
|
|
| 521 |
GradioFollowUpSystemPrompt = """You are an expert Gradio developer modifying an existing Gradio application.
|
| 522 |
The user wants to apply changes based on their request.
|
| 523 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 524 |
π¨ CRITICAL INSTRUCTION: You MUST maintain the original multi-file structure when making modifications.
|
| 525 |
β Do NOT use SEARCH/REPLACE blocks.
|
| 526 |
β Do NOT output everything in one combined block.
|
|
|
|
| 521 |
GradioFollowUpSystemPrompt = """You are an expert Gradio developer modifying an existing Gradio application.
|
| 522 |
The user wants to apply changes based on their request.
|
| 523 |
|
| 524 |
+
π¨ CRITICAL OUTPUT RULES:
|
| 525 |
+
- DO NOT use <think> tags or thinking blocks in your output
|
| 526 |
+
- DO NOT use [TOOL_CALL] or any tool call markers
|
| 527 |
+
- Generate ONLY the requested code files
|
| 528 |
+
- No explanatory text outside the code blocks
|
| 529 |
+
|
| 530 |
π¨ CRITICAL INSTRUCTION: You MUST maintain the original multi-file structure when making modifications.
|
| 531 |
β Do NOT use SEARCH/REPLACE blocks.
|
| 532 |
β Do NOT output everything in one combined block.
|