{ "cells": [ { "cell_type": "code", "execution_count": 22, "metadata": { "id": "nhHdJaHG29x5" }, "outputs": [], "source": [ "!pip install langchain==0.2.16 \\\n", " langchain-google-genai==1.0.10 \\\n", " langchain-community==0.2.16 \\\n", " google-generativeai==0.7.2 \\\n", " pygithub==2.3.0 \\\n", " pydantic==2.7.4 \\\n", " streamlit==1.36.0 \\\n", " python-dotenv==1.0.1 \\\n", " -q" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "1viqpBCv6_NI", "outputId": "4963ed41-64c1-459e-fd3d-80e2134651a4" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "langchain: 0.2.16\n", "streamlit: 1.36.0\n", "All imports OK\n" ] } ], "source": [ "import langchain\n", "import langchain_google_genai\n", "from github import Github\n", "from pydantic import BaseModel\n", "import streamlit\n", "\n", "print(\"langchain:\", langchain.__version__)\n", "print(\"streamlit:\", streamlit.__version__)\n", "print(\"All imports OK\")" ] }, { "cell_type": "markdown", "metadata": { "id": "wfFRTyu17O_C" }, "source": [ "Block 1 — Project structure + API connections" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "id": "Be3QxnnmBwtZ" }, "outputs": [], "source": [ "!pip install langchain-groq==0.1.10 -q" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "RD_IqA5W7Or3", "outputId": "e849872f-5ac8-4a39-f36f-6771595a3314" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Connections working.\n" ] } ], "source": [ "from langchain_groq import ChatGroq\n", "from google.colab import userdata\n", "\n", "GROQ_API_KEY = userdata.get('GROQ_API_KEY')\n", "\n", "llm = ChatGroq(\n", " model=\"llama-3.3-70b-versatile\",\n", " api_key=GROQ_API_KEY,\n", " temperature=0\n", ")\n", "\n", "# Quick test\n", "response = llm.invoke(\"Say: connections working\")\n", "print(response.content)" ] }, { "cell_type": "markdown", "metadata": { "id": "kAtYQu-MChEq" }, "source": [ "Block 2 — The three tools\n", "This is the core of the agent. Three Python functions that fetch data from GitHub" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "CWS4xmDA-U3R", "outputId": "a6c76350-91a4-420c-fb1a-140be738b9c5" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Tools defined: ['get_pr_metadata', 'get_pr_diff', 'get_file_content']\n" ] } ], "source": [ "from langchain.tools import tool\n", "\n", "@tool\n", "def get_pr_metadata(pr_url: str) -> str:\n", " \"\"\"Get the title, description and author of a GitHub PR\"\"\"\n", " try:\n", " # Extract repo and PR number from URL\n", " # URL format: https://github.com/owner/repo/pull/123\n", " parts = pr_url.strip(\"/\").split(\"/\")\n", " owner = parts[-4]\n", " repo_name = parts[-3]\n", " pr_number = int(parts[-1])\n", "\n", " repo = github_client.get_repo(f\"{owner}/{repo_name}\")\n", " pr = repo.get_pull(pr_number)\n", "\n", " return f\"Title: {pr.title}\\nAuthor: {pr.user.login}\\nDescription: {pr.body}\"\n", " except Exception as e:\n", " return f\"Error fetching PR metadata: {str(e)}\"\n", "\n", "@tool\n", "def get_pr_diff(pr_url: str) -> str:\n", " \"\"\"Get the code changes (diff) from a GitHub PR\"\"\"\n", " try:\n", " parts = pr_url.strip(\"/\").split(\"/\")\n", " owner = parts[-4]\n", " repo_name = parts[-3]\n", " pr_number = int(parts[-1])\n", "\n", " repo = github_client.get_repo(f\"{owner}/{repo_name}\")\n", " pr = repo.get_pull(pr_number)\n", "\n", " diff_text = \"\"\n", " for file in pr.get_files():\n", " diff_text += f\"\\nFile: {file.filename}\\n\"\n", " diff_text += f\"Changes: +{file.additions} additions, -{file.deletions} deletions\\n\"\n", " if file.patch:\n", " diff_text += f\"{file.patch}\\n\"\n", "\n", " return diff_text[:8000] # Limit to avoid context overflow\n", " except Exception as e:\n", " return f\"Error fetching PR diff: {str(e)}\"\n", "\n", "@tool\n", "def get_file_content(repo_full_name: str, file_path: str) -> str:\n", " \"\"\"Get the full content of a file from a GitHub repository\"\"\"\n", " try:\n", " repo = github_client.get_repo(repo_full_name)\n", " content = repo.get_contents(file_path)\n", " return content.decoded_content.decode('utf-8')[:5000]\n", " except Exception as e:\n", " return f\"Error fetching file: {str(e)}\"\n", "\n", "print(\"Tools defined:\", [get_pr_metadata.name, get_pr_diff.name, get_file_content.name])" ] }, { "cell_type": "markdown", "metadata": { "id": "uhc4KMimCU9z" }, "source": [ "Block 3 — Building of the Agent" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Ho4w9NBz-mw4", "outputId": "498ecec0-868d-4829-8d90-7c6315a1d506" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Agent ready\n" ] } ], "source": [ "from langchain.agents import AgentExecutor, create_tool_calling_agent\n", "from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder\n", "\n", "# Define tools list\n", "tools = [get_pr_metadata, get_pr_diff, get_file_content]\n", "\n", "# System prompt — tells the agent its job\n", "prompt = ChatPromptTemplate.from_messages([\n", " (\"system\", \"\"\"You are an expert code reviewer.\n", " When given a GitHub PR URL, you must:\n", " 1. First fetch the PR metadata to understand its purpose\n", " 2. Then fetch the PR diff to see what changed\n", " 3. Analyze the changes for: bugs, security issues, code quality, naming problems\n", " 4. Return a structured review with clear issues and fixes\n", "\n", " Be specific. Always mention the filename and line where the issue exists.\"\"\"),\n", " (\"human\", \"{input}\"),\n", " MessagesPlaceholder(variable_name=\"agent_scratchpad\")\n", "])\n", "\n", "# Create the agent\n", "agent = create_tool_calling_agent(llm, tools, prompt)\n", "\n", "# Create the executor — this runs the agent loop\n", "agent_executor = AgentExecutor(\n", " agent=agent,\n", " tools=tools,\n", " verbose=True, # Shows the agent's thinking steps\n", " max_iterations=10\n", ")\n", "\n", "print(\"Agent ready\")" ] }, { "cell_type": "markdown", "metadata": { "id": "vmgVa2osCPbJ" }, "source": [ "Block 4 — Testing the agent on a real PR" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "3z2nXxQm_B-J", "outputId": "7b1cd5b1-d5da-4247-930f-281faf60a79d" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", "\u001b[32;1m\u001b[1;3m\n", "Invoking: `get_pr_metadata` with `{'pr_url': 'https://github.com/langchain-ai/langchain/pull/1'}`\n", "\n", "\n", "\u001b[0m\u001b[36;1m\u001b[1;3mError fetching PR metadata: name 'github_client' is not defined\u001b[0m\u001b[32;1m\u001b[1;3m\n", "Invoking: `get_pr_diff` with `{'pr_url': 'https://github.com/langchain-ai/langchain/pull/1'}`\n", "\n", "\n", "\u001b[0m\u001b[33;1m\u001b[1;3mError fetching PR diff: name 'github_client' is not defined\u001b[0m\u001b[32;1m\u001b[1;3mI'll provide a general review based on the information available. \n", "\n", "To provide a detailed review, I would need the PR metadata and diff. However, I can guide you through a general review process.\n", "\n", "1. **PR Metadata**: The PR metadata would provide the title, description, and author of the PR. This information is crucial in understanding the purpose of the PR and what changes are being proposed.\n", "\n", "2. **PR Diff**: The PR diff would show the actual code changes being proposed. This is where the bulk of the review happens. \n", "\n", "3. **Analysis**: \n", " - **Bugs**: Look for any changes that could potentially introduce bugs. This could be anything from off-by-one errors to incorrect function calls.\n", " - **Security Issues**: Check for any changes that could potentially introduce security vulnerabilities. This could be anything from SQL injection to cross-site scripting (XSS).\n", " - **Code Quality**: Check for any changes that could improve or degrade code quality. This could be anything from improving variable names to reducing complexity.\n", " - **Naming Problems**: Check for any changes that could improve or degrade naming conventions. This could be anything from improving variable names to improving function names.\n", "\n", "Without the actual PR metadata and diff, it's impossible to provide a detailed review. However, I can provide a general outline of what a review might look like:\n", "\n", "* In `filename.py` at line 10, there is a potential bug. The fix would be to change `line 10` to `fixed_line 10`.\n", "* In `filename.py` at line 20, there is a potential security issue. The fix would be to change `line 20` to `fixed_line 20`.\n", "* In `filename.py` at line 30, there is a potential code quality issue. The fix would be to change `line 30` to `fixed_line 30`.\n", "* In `filename.py` at line 40, there is a potential naming problem. The fix would be to change `line 40` to `fixed_line 40`.\n", "\n", "Again, this is just a general outline and a real review would require the actual PR metadata and diff.\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n", "\n", "=== FINAL REVIEW ===\n", "I'll provide a general review based on the information available. \n", "\n", "To provide a detailed review, I would need the PR metadata and diff. However, I can guide you through a general review process.\n", "\n", "1. **PR Metadata**: The PR metadata would provide the title, description, and author of the PR. This information is crucial in understanding the purpose of the PR and what changes are being proposed.\n", "\n", "2. **PR Diff**: The PR diff would show the actual code changes being proposed. This is where the bulk of the review happens. \n", "\n", "3. **Analysis**: \n", " - **Bugs**: Look for any changes that could potentially introduce bugs. This could be anything from off-by-one errors to incorrect function calls.\n", " - **Security Issues**: Check for any changes that could potentially introduce security vulnerabilities. This could be anything from SQL injection to cross-site scripting (XSS).\n", " - **Code Quality**: Check for any changes that could improve or degrade code quality. This could be anything from improving variable names to reducing complexity.\n", " - **Naming Problems**: Check for any changes that could improve or degrade naming conventions. This could be anything from improving variable names to improving function names.\n", "\n", "Without the actual PR metadata and diff, it's impossible to provide a detailed review. However, I can provide a general outline of what a review might look like:\n", "\n", "* In `filename.py` at line 10, there is a potential bug. The fix would be to change `line 10` to `fixed_line 10`.\n", "* In `filename.py` at line 20, there is a potential security issue. The fix would be to change `line 20` to `fixed_line 20`.\n", "* In `filename.py` at line 30, there is a potential code quality issue. The fix would be to change `line 30` to `fixed_line 30`.\n", "* In `filename.py` at line 40, there is a potential naming problem. The fix would be to change `line 40` to `fixed_line 40`.\n", "\n", "Again, this is just a general outline and a real review would require the actual PR metadata and diff.\n" ] } ], "source": [ "test_pr_url = \"https://github.com/langchain-ai/langchain/pull/1\"\n", "\n", "result = agent_executor.invoke({\n", " \"input\": f\"Please review this pull request: {test_pr_url}\"\n", "})\n", "\n", "print(\"\\n=== FINAL REVIEW ===\")\n", "print(result['output'])" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "id": "I7y2R-WPKnMQ" }, "outputs": [], "source": [ "!pip install plotly -q" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "id": "JwgdpO9fQCcN" }, "outputs": [], "source": [ "\n", "import os\n", "os.environ[\"GROQ_API_KEY\"] = userdata.get('GROQ_API_KEY')" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "id": "ZbiTsq9wDGSx" }, "outputs": [], "source": [ "import streamlit as st\n", "import plotly.graph_objects as go\n", "import re\n", "from langchain.agents import AgentExecutor, create_tool_calling_agent\n", "from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder\n", "from langchain.tools import tool\n", "from github import Github\n", "\n", "st.set_page_config(page_title=\"AI Code Review Agent\", page_icon=\"🔬\", layout=\"wide\", initial_sidebar_state=\"expanded\")\n", "\n", "st.markdown(\"\"\"\n", "\n", "\"\"\", unsafe_allow_html=True)\n", "\n", "# ─── Session state init ──────────────────────────────────────────\n", "for key in [\"saved_provider\", \"saved_model\", \"saved_api_key\", \"saved_github_token\"]:\n", " if key not in st.session_state:\n", " st.session_state[key] = \"\"\n", "\n", "# ─── LLM factory ─────────────────────────────────────────────────\n", "def get_llm(provider, api_key, model_name):\n", " if provider == \"Groq\":\n", " from langchain_groq import ChatGroq\n", " return ChatGroq(model=model_name, api_key=api_key, temperature=0)\n", " elif provider == \"Google Gemini\":\n", " from langchain_google_genai import ChatGoogleGenerativeAI\n", " return ChatGoogleGenerativeAI(model=model_name, google_api_key=api_key, temperature=0)\n", " elif provider == \"OpenAI\":\n", " from langchain_openai import ChatOpenAI\n", " return ChatOpenAI(model=model_name, api_key=api_key, temperature=0)\n", " else:\n", " raise ValueError(f\"Unknown provider: {provider}\")\n", "\n", "# ─── Diff helpers ─────────────────────────────────────────────────\n", "def render_diff_html(patch_text):\n", " if not patch_text:\n", " return \"
No diff available
\"\n", " html = \"\"\n", " for line in patch_text.split(\"\\n\")[:60]:\n", " if line.startswith(\"+\") and not line.startswith(\"+++\"):\n", " html += f'✓ API key active from session
', unsafe_allow_html=True)\n", " if st.session_state.saved_github_token:\n", " st.markdown('✓ GitHub token active from session
', unsafe_allow_html=True)\n", "\n", " st.markdown(\"---\")\n", " st.markdown(\"**🔄 Agent Flow**\")\n", " st.markdown(\"1. Fetch PR metadata\\n2. Fetch code diff\\n3. Analyze changes\\n4. Generate review\")\n", " st.caption(\"Built with LangChain + Streamlit\")\n", "\n", "# ─── Main UI ──────────────────────────────────────────────────────\n", "st.markdown('🔬 AI Code Review Agent
', unsafe_allow_html=True)\n", "st.markdown('Autonomous PR analysis — paste any GitHub PR URL below
', unsafe_allow_html=True)\n", "st.markdown(\"---\")\n", "\n", "pr_url = st.text_input(\"GitHub PR URL\", placeholder=\"https://github.com/owner/repo/pull/123\", label_visibility=\"collapsed\")\n", "\n", "col_btn, col_info = st.columns([1, 3])\n", "with col_btn:\n", " analyze_btn = st.button(\"🚀 Analyze PR\")\n", "with col_info:\n", " st.caption(\"The agent autonomously decides which tools to call and in what order.\")\n", "\n", "# Use saved values as fallback\n", "active_api_key = api_key or st.session_state.saved_api_key\n", "active_github_token = github_token or st.session_state.saved_github_token\n", "active_model = model_name or st.session_state.saved_model\n", "active_provider = provider\n", "\n", "# ─── Agent execution ──────────────────────────────────────────────\n", "if analyze_btn:\n", " if not active_api_key or not active_github_token:\n", " st.error(\"⚠️ Enter API keys in the sidebar and click 💾 Save Configuration.\")\n", " elif not active_model:\n", " st.error(\"⚠️ Enter a model name in the sidebar.\")\n", " elif not pr_url:\n", " st.error(\"⚠️ Enter a PR URL.\")\n", " else:\n", " @st.cache_resource\n", " def load_github_client(token):\n", " return Github(token)\n", "\n", " github_client = load_github_client(active_github_token)\n", " files_data = []\n", "\n", " @tool\n", " def get_pr_metadata(pr_url: str) -> str:\n", " \"\"\"Get the title, description and author of a GitHub PR\"\"\"\n", " try:\n", " parts = pr_url.strip(\"/\").split(\"/\")\n", " owner, repo_name, pr_number = parts[-4], parts[-3], int(parts[-1])\n", " repo = github_client.get_repo(f\"{owner}/{repo_name}\")\n", " pr = repo.get_pull(pr_number)\n", " return f\"Title: {pr.title}\\nAuthor: {pr.user.login}\\nDescription: {pr.body}\\nBase: {pr.base.ref} -> Head: {pr.head.ref}\"\n", " except Exception as e:\n", " return f\"Error: {str(e)}\"\n", "\n", " @tool\n", " def get_pr_diff(pr_url: str) -> str:\n", " \"\"\"Get the code changes (diff) from a GitHub PR\"\"\"\n", " try:\n", " parts = pr_url.strip(\"/\").split(\"/\")\n", " owner, repo_name, pr_number = parts[-4], parts[-3], int(parts[-1])\n", " repo = github_client.get_repo(f\"{owner}/{repo_name}\")\n", " pr = repo.get_pull(pr_number)\n", " diff_text = \"\"\n", " for file in pr.get_files():\n", " files_data.append({\n", " \"name\": file.filename.split(\"/\")[-1],\n", " \"additions\": file.additions,\n", " \"deletions\": file.deletions,\n", " \"patch\": file.patch or \"\"\n", " })\n", " diff_text += f\"\\nFile: {file.filename}\\n+{file.additions} -{file.deletions}\\n\"\n", " if file.patch:\n", " diff_text += file.patch + \"\\n\"\n", " return diff_text[:4000]\n", " except Exception as e:\n", " return f\"Error: {str(e)}\"\n", "\n", " @tool\n", " def get_file_content(repo_full_name: str, file_path: str) -> str:\n", " \"\"\"Get the full content of a file from a GitHub repository\"\"\"\n", " try:\n", " repo = github_client.get_repo(repo_full_name)\n", " content = repo.get_contents(file_path)\n", " return content.decoded_content.decode(\"utf-8\")[:2000]\n", " except Exception as e:\n", " return f\"Error: {str(e)}\"\n", "\n", " tools = [get_pr_metadata, get_pr_diff, get_file_content]\n", "\n", " try:\n", " llm = get_llm(active_provider, active_api_key, active_model)\n", " except Exception as e:\n", " st.error(f\"Failed to initialize LLM: {e}\")\n", " st.stop()\n", "\n", " prompt = ChatPromptTemplate.from_messages([\n", " (\"system\", \"\"\"You are an expert code reviewer. When given a GitHub PR URL:\n", "1. Fetch PR metadata to understand its purpose\n", "2. Fetch the PR diff to see what changed\n", "3. Analyze for: bugs, security issues, code quality, naming problems\n", "4. Return structured review with this exact format for each issue:\n", "\n", "ISSUE: [short title]\n", "FILE: [filename]\n", "SEVERITY: [High/Medium/Low]\n", "PROBLEM: [what is wrong]\n", "FIX: [how to fix it]\n", "---\n", "\n", "IMPORTANT: You MUST put --- on its own line between every single issue. No exceptions.\n", "\n", "End with a SUMMARY section.\"\"\"),\n", " (\"human\", \"{input}\"),\n", " MessagesPlaceholder(variable_name=\"agent_scratchpad\")\n", " ])\n", "\n", " agent = create_tool_calling_agent(llm, tools, prompt)\n", " agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=False, max_iterations=10)\n", "\n", " status_box = st.empty()\n", " with status_box.container():\n", " st.info(\"🤖 Agent is analyzing the PR... this may take 15–30 seconds.\")\n", "\n", " try:\n", " result = agent_executor.invoke({\"input\": f\"Review this PR: {pr_url}\"})\n", " status_box.empty()\n", " output = result[\"output\"]\n", "\n", " import gc\n", " gc.collect()\n", "\n", " high = output.upper().count(\"SEVERITY: HIGH\")\n", " medium = output.upper().count(\"SEVERITY: MEDIUM\")\n", " low = output.upper().count(\"SEVERITY: LOW\")\n", " total = high + medium + low\n", "\n", " st.markdown(\"### 📊 Review Summary\")\n", " m1, m2, m3, m4 = st.columns(4)\n", " with m1: st.markdown(f'{block}'\n",
" f'{output}'\n",
" f'