{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "9ba5c49e", "metadata": {}, "outputs": [], "source": [ "import asyncio\n", "import os\n", "from typing import List, Dict, Any\n", "from dotenv import load_dotenv\n", "from code_ingestion import ingest_github_repo\n", "from model_service import get_model_response_async\n", "from code_evaluation import evaluate_code\n", "\n", "load_dotenv()\n", "\n", "async def get_full_response(model_name: str, prompt: str, context: Dict[str, Any]) -> str:\n", " \"\"\"\n", " Get a complete response from a model (non-streaming)\n", " \"\"\"\n", " response_text = \"\"\n", " async for chunk in get_model_response_async(model_name, prompt, context):\n", " response_text += chunk\n", " \n", " # Clean up the response by removing markdown code blocks\n", " cleaned_text = response_text.strip().removeprefix(\"```python\").removeprefix(\"```\").removesuffix(\"```\").strip()\n", " return cleaned_text\n", "\n", "async def process_single_query(query: str, context: Dict[str, Any]) -> Dict[str, Any]:\n", " \"\"\"\n", " Process a single query and return results for both models\n", " \"\"\"\n", " try:\n", " # Get responses from both models\n", " claude_response, qwen_response = await asyncio.gather(\n", " get_full_response(\"claude-4\", query, context),\n", " get_full_response(\"qwen3-coder\", query, context)\n", " )\n", " \n", " # Evaluate both responses\n", " claude_eval = evaluate_code(claude_response)\n", " qwen_eval = evaluate_code(qwen_response)\n", " \n", " # Return results in the specified format\n", " return {\n", " \"query\": query,\n", " \"Correctness_Qwen\": qwen_eval[\"detailed_metrics\"][\"correctness\"][\"score\"],\n", " \"Correctness_Sonnet\": claude_eval[\"detailed_metrics\"][\"correctness\"][\"score\"],\n", " \"readability_Qwen\": qwen_eval[\"detailed_metrics\"][\"readability\"][\"score\"],\n", " \"readability_Sonnet\": claude_eval[\"detailed_metrics\"][\"readability\"][\"score\"],\n", " \"bestpractices_Qwen\": qwen_eval[\"detailed_metrics\"][\"best_practices\"][\"score\"],\n", " \"bestpractices_Sonnet\": claude_eval[\"detailed_metrics\"][\"best_practices\"][\"score\"],\n", " # Additional fields for debugging/analysis\n", " \"claude_response\": claude_response,\n", " \"qwen_response\": qwen_response,\n", " \"claude_overall_score\": claude_eval[\"overall_score\"],\n", " \"qwen_overall_score\": qwen_eval[\"overall_score\"]\n", " }\n", " \n", " except Exception as e:\n", " print(f\"Error processing query '{query}': {str(e)}\")\n", " return {\n", " \"query\": query,\n", " \"Correctness_Qwen\": 0.0,\n", " \"Correctness_Sonnet\": 0.0,\n", " \"readability_Qwen\": 0.0,\n", " \"readability_Sonnet\": 0.0,\n", " \"bestpractices_Qwen\": 0.0,\n", " \"bestpractices_Sonnet\": 0.0,\n", " \"error\": str(e)\n", " }\n", "\n", "async def process_queries(queries: List[str], github_repo_url: str) -> List[Dict[str, Any]]:\n", " \"\"\"\n", " Process a list of queries against a GitHub repository\n", " \n", " Args:\n", " queries: List of query strings\n", " github_repo_url: URL of the GitHub repository to ingest\n", " \n", " Returns:\n", " List of dictionaries with evaluation results for each query\n", " \"\"\"\n", " # Ingest the repository to get context\n", " print(f\"Ingesting repository: {github_repo_url}\")\n", " context = ingest_github_repo(github_repo_url)\n", " print(\"Repository ingested successfully!\")\n", " \n", " # Process all queries\n", " results = []\n", " for i, query in enumerate(queries, 1):\n", " print(f\"Processing query {i}/{len(queries)}: {query[:50]}...\")\n", " result = await process_single_query(query, context)\n", " results.append(result)\n", " print(result)\n", " print(f\"Completed query {i}/{len(queries)}\")\n", " \n", " return results\n", "\n", "def process_queries_sync(queries: List[str], github_repo_url: str) -> List[Dict[str, Any]]:\n", " \"\"\"\n", " Synchronous wrapper for process_queries - works in Jupyter notebooks\n", " \"\"\"\n", " try:\n", " # Try to get the current event loop\n", " loop = asyncio.get_event_loop()\n", " if loop.is_running():\n", " # If we're in a running event loop (like Jupyter), use asyncio.create_task\n", " import nest_asyncio\n", " nest_asyncio.apply()\n", " return loop.run_until_complete(process_queries(queries, github_repo_url))\n", " else:\n", " # If no event loop is running, use asyncio.run\n", " return asyncio.run(process_queries(queries, github_repo_url))\n", " except RuntimeError:\n", " # Fallback for Jupyter notebooks\n", " import nest_asyncio\n", " nest_asyncio.apply()\n", " loop = asyncio.get_event_loop()\n", " return loop.run_until_complete(process_queries(queries, github_repo_url))\n", "\n", "# Example usage:\n", "if True:\n", " # Define your queries\n", " mcp_prompts = [\n", " \"Build an MCP server that fetches the latest tweets from a user and sends them as a daily digest email via Gmail.\",\n", " \"Build an MCP server that creates a new Notion page when someone drops a file into a specific Google Drive folder.\",\n", " \"Create a small MCP server that listens for incoming questions from Discord and uses OpenAI to generate answers.\",\n", " \"Build an MCP server that watches a GitHub repo for new issues and posts them into a Telegram group.\",\n", " \"Write an MCP agent that reads a URL, extracts metadata with BeautifulSoup, and logs it to a CSV file.\",\n", " \"Create an MCP endpoint that receives a YouTube link, downloads the audio, and sends it via email.\",\n", " \"Create an MCP server that takes a PDF uploaded via a web form, summarizes it using an LLM, and returns the summary.\",\n", " \"Write an MCP-compatible server that pulls job listings from LinkedIn and posts them to a Slack channel every hour.\",\n", " \"Build a server that monitors a subreddit for new posts, filters them by keyword, and logs them in a Notion database.\",\n", " \"Create a minimal MCP server that extracts tabular data from a URL and sends it to an Airtable base.\"\n", "]\n", " \n", " # Define the GitHub repository URL\n", " repo_url = \"https://github.com/jlowin/fastmcp\"\n", " \n", " # Process all queries\n", " results = process_queries_sync(mcp_prompts, repo_url)\n", " \n", " # Print results\n", " for result in results:\n", " print(f\"\\nQuery: {result['query']}\")\n", " print(f\"Qwen Correctness: {result['Correctness_Qwen']:.2f}\")\n", " print(f\"Sonnet Correctness: {result['Correctness_Sonnet']:.2f}\")\n", " print(f\"Qwen Readability: {result['readability_Qwen']:.2f}\")\n", " print(f\"Sonnet Readability: {result['readability_Sonnet']:.2f}\")\n", " print(f\"Qwen Best Practices: {result['bestpractices_Qwen']:.2f}\")\n", " print(f\"Sonnet Best Practices: {result['bestpractices_Sonnet']:.2f}\")\n", " if 'error' in result:\n", " print(f\"Error: {result['error']}\")" ] }, { "cell_type": "code", "execution_count": null, "id": "4f14722e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
| \n", " | Query | \n", "Correctness (Qwen) | \n", "Readability (Qwen) | \n", "Best Practices (Qwen) | \n", "Correctness (Claude) | \n", "Readability (Claude) | \n", "Best Practices (Claude) | \n", "Overall Score (Qwen) | \n", "Overall Score (Claude) | \n", "Winner | \n", "
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | \n", "Build an MCP server that fetches the latest tweets from a user and sends them as a daily digest email via Gmail. | \n", "0.914 | \n", "0.903 | \n", "0.710 | \n", "0.738 | \n", "0.900 | \n", "0.758 | \n", "0.842 | \n", "0.799 | \n", "Qwen | \n", "
| 1 | \n", "Build an MCP server that creates a new Notion page when someone drops a file into a specific Google Drive folder. | \n", "0.935 | \n", "0.910 | \n", "0.779 | \n", "0.890 | \n", "0.899 | \n", "0.734 | \n", "0.874 | \n", "0.841 | \n", "Qwen | \n", "
| 2 | \n", "Create a small MCP server that listens for incoming questions from Discord and uses OpenAI to generate answers. | \n", "0.920 | \n", "0.904 | \n", "0.755 | \n", "0.894 | \n", "0.905 | \n", "0.773 | \n", "0.860 | \n", "0.857 | \n", "Qwen | \n", "
| 3 | \n", "Build an MCP server that watches a GitHub repo for new issues and posts them into a Telegram group. | \n", "0.917 | \n", "0.913 | \n", "0.813 | \n", "0.741 | \n", "0.905 | \n", "0.796 | \n", "0.881 | \n", "0.814 | \n", "Qwen | \n", "
| 4 | \n", "Write an MCP agent that reads a URL, extracts metadata with BeautifulSoup, and logs it to a CSV file. | \n", "0.901 | \n", "0.906 | \n", "0.770 | \n", "0.762 | \n", "0.904 | \n", "0.754 | \n", "0.859 | \n", "0.807 | \n", "Qwen | \n", "
| 5 | \n", "Create an MCP endpoint that receives a YouTube link, downloads the audio, and sends it via email. | \n", "0.932 | \n", "0.908 | \n", "0.731 | \n", "0.900 | \n", "0.910 | \n", "0.713 | \n", "0.857 | \n", "0.841 | \n", "Qwen | \n", "
| 6 | \n", "Create an MCP server that takes a PDF uploaded via a web form, summarizes it using an LLM, and returns the summary. | \n", "0.920 | \n", "0.905 | \n", "0.703 | \n", "0.845 | \n", "0.908 | \n", "0.802 | \n", "0.843 | \n", "0.851 | \n", "Claude | \n", "
| 7 | \n", "Write an MCP-compatible server that pulls job listings from LinkedIn and posts them to a Slack channel every hour. | \n", "0.920 | \n", "0.780 | \n", "0.793 | \n", "0.694 | \n", "0.898 | \n", "0.708 | \n", "0.831 | \n", "0.767 | \n", "Qwen | \n", "
| 8 | \n", "Build a server that monitors a subreddit for new posts, filters them by keyword, and logs them in a Notion database. | \n", "0.919 | \n", "0.905 | \n", "0.718 | \n", "0.632 | \n", "0.906 | \n", "0.791 | \n", "0.847 | \n", "0.776 | \n", "Qwen | \n", "
| 9 | \n", "Create a minimal MCP server that extracts tabular data from a URL and sends it to an Airtable base. | \n", "0.920 | \n", "0.890 | \n", "0.860 | \n", "0.897 | \n", "0.905 | \n", "0.827 | \n", "0.890 | \n", "0.876 | \n", "Qwen | \n", "