Spaces:
Runtime error
Runtime error
import os | |
import json | |
DEFAULT_SYSTEM_PROMPT = os.getenv( | |
"DEFAULT_SYSTEM_PROMPT", | |
"Your Name is Node. You are a Helpful AI Assistant that can answer questions, perform research, and build software on Hugging Face. You can use tools like web search and a Hugging Face Space builder. When providing information or reporting tool results, be clear and concise." | |
) | |
METRICS_SYSTEM_PROMPT = "You are a precise JSON output agent. Output a single JSON object containing interaction metrics as requested by the user. Do not include any explanatory text before or after the JSON object." | |
TOOL_SYSTEM_PROMPT = """You are a precise routing agent. Your task is to select the most appropriate action to respond to a user's query and provide the required inputs as a single JSON object. | |
Available Actions and their inputs: | |
- "create_huggingface_space": Creates a new HF space. Requires: "owner", "space_name", "sdk", "description". | |
- "list_space_files": Lists all files in an existing HF space. Requires: "owner", "space_name". | |
- "get_space_file_content": Retrieves the content of a specific file in a space. Requires: "owner", "space_name", "file_path". | |
- "update_huggingface_space_file": Updates a file in an existing HF space. Requires: "owner", "space_name", "file_path", "new_content", "commit_message". | |
- "search_duckduckgo_and_report": Searches the web. Requires: "search_engine_query". | |
- "scrape_url_and_report": Scrapes a single URL. Requires: "url". | |
- "answer_using_conversation_memory": Answers from memory. | |
- "quick_respond": For simple conversation. | |
Example for listing files: | |
{"action": "list_space_files", "action_input": {"owner": "test-user", "space_name": "my-app"}} | |
Example for reading a file: | |
{"action": "get_space_file_content", "action_input": {"owner": "test-user", "space_name": "my-app", "file_path": "app.py"}} | |
Example for creating a space: | |
{"action": "create_huggingface_space", "action_input": {"owner": "test-user", "space_name": "my-translator-app", "sdk": "gradio", "description": "a simple Gradio app that translates english text to french text"}} | |
Example for updating a file: | |
{"action": "update_huggingface_space_file", "action_input": {"owner": "test-user", "space_name": "my-translator-app", "file_path": "app.py", "new_content": "import gradio as gr\\n# Updated code\\ndef translate(text):\\n return f'Translated: {text}'\\n\\ndemo = gr.Interface(fn=translate, inputs='text', outputs='text')\\ndemo.launch()\\n", "commit_message": "Improve translation logic"}} | |
Extract the owner's username from the user's prompt. Output only the JSON object. | |
""" | |
SPACE_GENERATION_SYSTEM_PROMPT = """You generate program files for a Hugging Face Space project as a single plain text string, strictly adhering to the specified markdown format. Every single line, including backticks, language identifiers, file content, and empty lines, MUST be prefixed with '# ' to comment it out. This is critical. The output must include a complete file structure and the contents of each file, with all necessary code and configurations for a functional project. Do not deviate from this format. | |
The format is as follows: | |
# # Space: [owner/project-name] | |
# ## File Structure | |
# ``` | |
# π Root | |
# π [file1] | |
# π [file2] | |
# ``` | |
# | |
# # Below are the contents of all files in the space: | |
# | |
# ### File: [file1] | |
# ```[language] | |
# [content line 1] | |
# [content line 2] | |
# ``` | |
# | |
# ### File: [file2] | |
# ```[language] | |
# [content line 1] | |
# ``` | |
Every line you generate must start with '# '. | |
""" | |
def get_space_generation_user_prompt(description: str, owner: str, space_name: str) -> str: | |
return f"""Generate the complete file structure and content for the following Hugging Face Space project, following the strict '# ' formatting rules. | |
Project Details: | |
- Owner: {owner} | |
- Space Name: {space_name} | |
- Description: {description} | |
Ensure the output is a single, complete, and functional project definition ready to be used. | |
""" | |
def get_metrics_user_prompt(user_input: str, bot_response: str) -> str: | |
return f"User: \"{user_input}\"\nAI: \"{bot_response}\"\nMetrics: \"takeaway\" (3-7 words), \"response_success_score\" (0.0-1.0), \"future_confidence_score\" (0.0-1.0). Output JSON ONLY." | |
def get_tool_user_prompt(user_input: str, history_snippet: str, guideline_snippet: str) -> str: | |
return f"""User Query: "{user_input}" | |
Recent History: | |
{history_snippet} | |
Guidelines: {guideline_snippet}... | |
Task: Based on the user query and available actions, construct the appropriate JSON object to call the correct tool. If the user wants to build, create, modify, or update a Hugging Face Space, use the space builder tools. | |
""" | |
def get_final_response_prompt(history_str: str, insights_str: str, user_input: str, context_str: str = None) -> str: | |
base = f"History:\n{history_str}\n\nGuidelines:\n{insights_str}" | |
if context_str: | |
base += f"\n\nContext from Tool Execution:\n{context_str}" | |
base += f"\n\nQuery: \"{user_input}\"\n\nResponse:" | |
return base | |
def get_insight_user_prompt(summary: str, existing_rules_ctx: str, insights_reflected: list[dict]) -> str: | |
return f"""Interaction Summary:\n{summary}\n | |
Potentially Relevant Existing Rules:\n{existing_rules_ctx}\n | |
Guiding principles considered during THIS interaction:\n{json.dumps([p['original'] for p in insights_reflected if 'original' in p]) if insights_reflected else "None"}\n | |
Task: Based on your reflection process, generate a single, valid XML structure of operations to refine the AI's rules. Output XML ONLY.""" |