kshitijthakkar commited on
Commit
807fe76
Β·
1 Parent(s): f4dfe1f

feat: Add generate_prompt_template MCP tool

Browse files

- New MCP tool generates customized smolagents prompt templates
- Fetches base templates from smolagents GitHub (code_agent.yaml or toolcalling_agent.yaml)
- Uses Gemini AI to adapt templates for specific domains and tools
- Added UI tab in app.py for easy template generation
- Returns YAML template with usage instructions
- Enables complete end-to-end workflow: generate dataset + matching prompt template

Files changed (2) hide show
  1. app.py +78 -4
  2. mcp_tools.py +177 -0
app.py CHANGED
@@ -2,7 +2,7 @@
2
  TraceMind MCP Server - Hugging Face Space Entry Point (Track 1)
3
 
4
  This file serves as the entry point for HuggingFace Space deployment.
5
- Exposes 7 AI-powered MCP tools + 3 Resources + 3 Prompts via Gradio's native MCP support.
6
 
7
  Built on Open Source Foundation:
8
  πŸ”­ TraceVerde (genai_otel_instrument) - Automatic OpenTelemetry instrumentation
@@ -36,6 +36,7 @@ Tools Provided:
36
  πŸ“ˆ get_leaderboard_summary - Get leaderboard overview statistics
37
  πŸ“¦ get_dataset - Load SMOLTRACE datasets as JSON
38
  πŸ§ͺ generate_synthetic_dataset - Create domain-specific test datasets
 
39
  πŸ“€ push_dataset_to_hub - Upload datasets to HuggingFace Hub
40
 
41
  Compatible with:
@@ -71,6 +72,7 @@ from mcp_tools import (
71
  get_leaderboard_summary,
72
  get_dataset,
73
  generate_synthetic_dataset,
 
74
  push_dataset_to_hub
75
  )
76
 
@@ -91,7 +93,7 @@ def create_gradio_ui():
91
 
92
  **AI-Powered Analysis for Agent Evaluation Data**
93
 
94
- This server provides **9 MCP Tools + 3 MCP Resources + 3 MCP Prompts**:
95
 
96
  ### MCP Tools (AI-Powered & Optimized)
97
  - πŸ“Š **Analyze Leaderboard**: Get AI-powered insights from evaluation results
@@ -102,6 +104,7 @@ def create_gradio_ui():
102
  - πŸ“ˆ **Get Leaderboard Summary**: Get high-level leaderboard statistics (optimized for overview)
103
  - πŸ“¦ **Get Dataset**: Load any HuggingFace dataset as JSON for flexible analysis
104
  - πŸ§ͺ **Generate Synthetic Dataset**: Create domain-specific test datasets for SMOLTRACE
 
105
  - πŸ“€ **Push to Hub**: Upload generated datasets to HuggingFace Hub
106
 
107
  ### MCP Resources (Data Access)
@@ -638,6 +641,77 @@ def create_gradio_ui():
638
  outputs=[synth_output]
639
  )
640
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
641
  # Tab 7: Push Dataset to Hub
642
  with gr.Tab("πŸ“€ Push to Hub"):
643
  gr.Markdown("""
@@ -1259,8 +1333,8 @@ def create_gradio_ui():
1259
 
1260
  ### What's Exposed via MCP:
1261
 
1262
- #### 9 MCP Tools (AI-Powered & Optimized)
1263
- The nine tools above (`analyze_leaderboard`, `debug_trace`, `estimate_cost`, `compare_runs`, `get_top_performers`, `get_leaderboard_summary`, `get_dataset`, `generate_synthetic_dataset`, `push_dataset_to_hub`)
1264
  are automatically exposed as MCP tools and can be called from any MCP client.
1265
 
1266
  #### 3 MCP Resources (Data Access)
 
2
  TraceMind MCP Server - Hugging Face Space Entry Point (Track 1)
3
 
4
  This file serves as the entry point for HuggingFace Space deployment.
5
+ Exposes 10 AI-powered MCP tools + 3 Resources + 3 Prompts via Gradio's native MCP support.
6
 
7
  Built on Open Source Foundation:
8
  πŸ”­ TraceVerde (genai_otel_instrument) - Automatic OpenTelemetry instrumentation
 
36
  πŸ“ˆ get_leaderboard_summary - Get leaderboard overview statistics
37
  πŸ“¦ get_dataset - Load SMOLTRACE datasets as JSON
38
  πŸ§ͺ generate_synthetic_dataset - Create domain-specific test datasets
39
+ πŸ“ generate_prompt_template - Generate customized smolagents prompt templates
40
  πŸ“€ push_dataset_to_hub - Upload datasets to HuggingFace Hub
41
 
42
  Compatible with:
 
72
  get_leaderboard_summary,
73
  get_dataset,
74
  generate_synthetic_dataset,
75
+ generate_prompt_template,
76
  push_dataset_to_hub
77
  )
78
 
 
93
 
94
  **AI-Powered Analysis for Agent Evaluation Data**
95
 
96
+ This server provides **10 MCP Tools + 3 MCP Resources + 3 MCP Prompts**:
97
 
98
  ### MCP Tools (AI-Powered & Optimized)
99
  - πŸ“Š **Analyze Leaderboard**: Get AI-powered insights from evaluation results
 
104
  - πŸ“ˆ **Get Leaderboard Summary**: Get high-level leaderboard statistics (optimized for overview)
105
  - πŸ“¦ **Get Dataset**: Load any HuggingFace dataset as JSON for flexible analysis
106
  - πŸ§ͺ **Generate Synthetic Dataset**: Create domain-specific test datasets for SMOLTRACE
107
+ - πŸ“ **Generate Prompt Template**: Create customized smolagents prompt templates for your domain
108
  - πŸ“€ **Push to Hub**: Upload generated datasets to HuggingFace Hub
109
 
110
  ### MCP Resources (Data Access)
 
641
  outputs=[synth_output]
642
  )
643
 
644
+ # Tab: Generate Prompt Template
645
+ with gr.Tab("πŸ“ Generate Prompt Template"):
646
+ gr.Markdown("""
647
+ ## Create Customized Agent Prompt Template
648
+
649
+ Generate a domain-specific prompt template based on smolagents templates.
650
+ This template can be used with your synthetic dataset to run SMOLTRACE evaluations.
651
+
652
+ **🎯 Use Case**: After generating a synthetic dataset, create a matching prompt template
653
+ that agents can use during evaluation. This ensures your evaluation setup is complete.
654
+
655
+ **Output**: Customized YAML prompt template ready for use with smolagents
656
+ """)
657
+
658
+ with gr.Row():
659
+ with gr.Column():
660
+ prompt_domain = gr.Textbox(
661
+ label="Domain",
662
+ placeholder="e.g., finance, healthcare, customer_support",
663
+ value="travel",
664
+ info="The domain/industry for the prompt template"
665
+ )
666
+ prompt_tools = gr.Textbox(
667
+ label="Tool Names (comma-separated)",
668
+ placeholder="e.g., get_weather,search_flights,book_hotel",
669
+ value="get_weather,search_flights,book_hotel",
670
+ info="Names of tools the agent will use",
671
+ lines=2
672
+ )
673
+ prompt_agent_type = gr.Dropdown(
674
+ label="Agent Type",
675
+ choices=["tool", "code"],
676
+ value="tool",
677
+ info="ToolCallingAgent (tool) or CodeAgent (code)"
678
+ )
679
+ prompt_button = gr.Button("πŸ“ Generate Prompt Template", variant="primary", size="lg")
680
+
681
+ with gr.Column():
682
+ prompt_output = gr.JSON(label="Generated Prompt Template (JSON)")
683
+
684
+ gr.Markdown("""
685
+ ### πŸ“ Next Steps
686
+
687
+ After generation:
688
+ 1. **Copy the `prompt_template`** from the JSON output above
689
+ 2. **Save it as a YAML file** (e.g., `{domain}_agent.yaml`)
690
+ 3. **Include it in your HuggingFace dataset** card or repository
691
+ 4. **Use it with SMOLTRACE** when running evaluations
692
+
693
+ **πŸ’‘ Tip**: This template is AI-customized for your domain and tools!
694
+ """)
695
+
696
+ async def run_generate_prompt_template(domain, tools, agent_type):
697
+ """Generate prompt template with async support."""
698
+ try:
699
+ import json
700
+ result = await generate_prompt_template(
701
+ domain=domain,
702
+ tool_names=tools,
703
+ agent_type=agent_type
704
+ )
705
+ return json.loads(result)
706
+ except Exception as e:
707
+ return {"error": str(e)}
708
+
709
+ prompt_button.click(
710
+ fn=run_generate_prompt_template,
711
+ inputs=[prompt_domain, prompt_tools, prompt_agent_type],
712
+ outputs=[prompt_output]
713
+ )
714
+
715
  # Tab 7: Push Dataset to Hub
716
  with gr.Tab("πŸ“€ Push to Hub"):
717
  gr.Markdown("""
 
1333
 
1334
  ### What's Exposed via MCP:
1335
 
1336
+ #### 10 MCP Tools (AI-Powered & Optimized)
1337
+ The ten tools above (`analyze_leaderboard`, `debug_trace`, `estimate_cost`, `compare_runs`, `get_top_performers`, `get_leaderboard_summary`, `get_dataset`, `generate_synthetic_dataset`, `generate_prompt_template`, `push_dataset_to_hub`)
1338
  are automatically exposed as MCP tools and can be called from any MCP client.
1339
 
1340
  #### 3 MCP Resources (Data Access)
mcp_tools.py CHANGED
@@ -1916,3 +1916,180 @@ def _calculate_agent_type_distribution(num_tasks: int, agent_type: str) -> dict:
1916
  tool_count = num_tasks // 2
1917
  code_count = num_tasks - tool_count
1918
  return {"tool": tool_count, "code": code_count}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1916
  tool_count = num_tasks // 2
1917
  code_count = num_tasks - tool_count
1918
  return {"tool": tool_count, "code": code_count}
1919
+
1920
+
1921
+ @gr.mcp.tool()
1922
+ async def generate_prompt_template(
1923
+ domain: str,
1924
+ tool_names: str,
1925
+ agent_type: str = "tool"
1926
+ ) -> str:
1927
+ """
1928
+ Generate customized smolagents prompt template for a specific domain and tool set.
1929
+
1930
+ This tool fetches the base prompt template from smolagents GitHub repository and uses
1931
+ Gemini AI to adapt it for your specific domain and tools. The result is a ready-to-use
1932
+ prompt template that you can use with SMOLTRACE evaluations.
1933
+
1934
+ **Use Case**: When you generate synthetic datasets with `generate_synthetic_dataset`,
1935
+ use this tool to create a matching prompt template that agents can use during evaluation.
1936
+ This ensures your evaluation setup is complete and ready to run.
1937
+
1938
+ **Integration**: The generated prompt template can be included in your HuggingFace dataset
1939
+ card, making it easy for anyone to run evaluations with your dataset.
1940
+
1941
+ Args:
1942
+ domain (str): The domain for the prompt template (e.g., "finance", "healthcare", "customer_support")
1943
+ tool_names (str): Comma-separated list of tool names (e.g., "get_stock_price,calculate_roi,fetch_company_info")
1944
+ agent_type (str): Agent type - "tool" for ToolCallingAgent or "code" for CodeAgent. Default: "tool"
1945
+
1946
+ Returns:
1947
+ str: JSON response containing the customized YAML prompt template and metadata
1948
+ """
1949
+ try:
1950
+ import aiohttp
1951
+
1952
+ # Initialize Gemini client
1953
+ gemini_client = GeminiClient()
1954
+
1955
+ # Validate agent_type
1956
+ if agent_type not in ["tool", "code"]:
1957
+ return json.dumps({
1958
+ "error": "agent_type must be 'tool' or 'code'",
1959
+ "agent_type_provided": agent_type
1960
+ }, indent=2)
1961
+
1962
+ # Parse tool names
1963
+ tools = [tool.strip() for tool in tool_names.split(",") if tool.strip()]
1964
+ if len(tools) == 0:
1965
+ return json.dumps({
1966
+ "error": "At least one tool name must be provided",
1967
+ "tool_names_provided": tool_names
1968
+ }, indent=2)
1969
+
1970
+ # Determine which template to fetch
1971
+ if agent_type == "tool":
1972
+ template_url = "https://raw.githubusercontent.com/huggingface/smolagents/refs/heads/main/src/smolagents/prompts/toolcalling_agent.yaml"
1973
+ template_name = "ToolCallingAgent"
1974
+ else: # code
1975
+ template_url = "https://raw.githubusercontent.com/huggingface/smolagents/refs/heads/main/src/smolagents/prompts/code_agent.yaml"
1976
+ template_name = "CodeAgent"
1977
+
1978
+ # Fetch the base template from GitHub
1979
+ async with aiohttp.ClientSession() as session:
1980
+ async with session.get(template_url) as response:
1981
+ if response.status != 200:
1982
+ return json.dumps({
1983
+ "error": f"Failed to fetch template from GitHub (status {response.status})",
1984
+ "template_url": template_url
1985
+ }, indent=2)
1986
+
1987
+ base_template = await response.text()
1988
+
1989
+ # Create customization prompt for Gemini
1990
+ customization_prompt = f"""You are an expert at creating agent prompt templates for smolagents.
1991
+
1992
+ I have a base {template_name} prompt template and need to customize it for a specific domain and set of tools.
1993
+
1994
+ **Domain**: {domain}
1995
+ **Tools Available**: {", ".join(tools)}
1996
+ **Agent Type**: {template_name}
1997
+
1998
+ **Base Template**:
1999
+ ```yaml
2000
+ {base_template}
2001
+ ```
2002
+
2003
+ **Your Task**:
2004
+ 1. Analyze the base template structure
2005
+ 2. Customize it for the {domain} domain
2006
+ 3. Integrate the provided tools ({", ".join(tools)}) into the template
2007
+ 4. Add domain-specific instructions and examples
2008
+ 5. Ensure the tool descriptions are clear and domain-relevant
2009
+
2010
+ **Customization Guidelines**:
2011
+ - Keep the YAML structure intact
2012
+ - Update the introduction/system message to be domain-specific
2013
+ - Add clear descriptions for each tool in the context of the {domain} domain
2014
+ - Include domain-specific examples where appropriate
2015
+ - Maintain the same placeholder variables (e.g., {{{{tool_descriptions}}}}, {{{{tools}}}})
2016
+ - Ensure the template is immediately usable with SMOLTRACE
2017
+
2018
+ **Output Format**: Return ONLY the customized YAML template. No explanations, no markdown code blocks, just the raw YAML content.
2019
+
2020
+ Start your response with the YAML content immediately."""
2021
+
2022
+ # Call Gemini to customize the template
2023
+ customized_template = await gemini_client.generate_content(
2024
+ customization_prompt,
2025
+ temperature=0.3, # Lower temperature for more consistent formatting
2026
+ max_output_tokens=4096
2027
+ )
2028
+
2029
+ # Clean up the response (remove any markdown formatting if present)
2030
+ customized_template = customized_template.strip()
2031
+ if customized_template.startswith("```yaml"):
2032
+ customized_template = customized_template.replace("```yaml\n", "").replace("```", "").strip()
2033
+ elif customized_template.startswith("```"):
2034
+ customized_template = customized_template.replace("```\n", "").replace("```", "").strip()
2035
+
2036
+ # Return response with metadata
2037
+ return json.dumps({
2038
+ "template_info": {
2039
+ "domain": domain,
2040
+ "tools": tools,
2041
+ "agent_type": agent_type,
2042
+ "template_name": template_name,
2043
+ "base_template_url": template_url,
2044
+ "customization_method": "Google Gemini 2.5 Pro"
2045
+ },
2046
+ "prompt_template": customized_template,
2047
+ "usage_instructions": f"""
2048
+ # How to Use This Prompt Template
2049
+
2050
+ ## In SMOLTRACE Evaluations
2051
+
2052
+ 1. Save this template to a file (e.g., `{domain}_{agent_type}_agent.yaml`)
2053
+ 2. Use it with SMOLTRACE:
2054
+ ```python
2055
+ from smolagents import {template_name}
2056
+
2057
+ agent = {template_name}(
2058
+ tools=[...], # Your tools: {", ".join(tools)}
2059
+ model="openai/gpt-4", # Or your preferred model
2060
+ system_prompt_path="{domain}_{agent_type}_agent.yaml"
2061
+ )
2062
+ ```
2063
+
2064
+ ## In HuggingFace Dataset Card
2065
+
2066
+ Add this template to your dataset's README.md:
2067
+ ```markdown
2068
+ ## Agent Prompt Template
2069
+
2070
+ This dataset was designed for the following agent configuration:
2071
+
2072
+ - **Agent Type**: {template_name}
2073
+ - **Domain**: {domain}
2074
+ - **Tools**: {", ".join(tools)}
2075
+
2076
+ ### Prompt Template (YAML)
2077
+
2078
+ See the `prompt_template.yaml` file in this repository.
2079
+ ```
2080
+
2081
+ ## Testing the Template
2082
+
2083
+ Use this template when evaluating with the synthetic dataset you generated.
2084
+ The template is pre-configured for the {domain} domain and includes all necessary
2085
+ tool descriptions and examples.
2086
+ """
2087
+ }, indent=2)
2088
+
2089
+ except Exception as e:
2090
+ import traceback
2091
+ error_details = traceback.format_exc()
2092
+ return json.dumps({
2093
+ "error": f"Failed to generate prompt template: {str(e)}",
2094
+ "error_details": error_details
2095
+ }, indent=2)