|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": { |
| 6 | + "colab_type": "text", |
| 7 | + "id": "view-in-github" |
| 8 | + }, |
| 9 | + "source": [ |
| 10 | + "<a href=\"https://colab.research.google.com/github/RashmiLnu/aqa-test-tools/blob/Google_Collab_Rashmi/Version1.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "code", |
| 15 | + "execution_count": null, |
| 16 | + "metadata": { |
| 17 | + "id": "NW7h6VD2xC6a" |
| 18 | + }, |
| 19 | + "outputs": [], |
| 20 | + "source": [ |
| 21 | + "!pip install -q langchain together openai faiss-cpu tiktoken\n" |
| 22 | + ] |
| 23 | + }, |
| 24 | + { |
| 25 | + "cell_type": "code", |
| 26 | + "execution_count": null, |
| 27 | + "metadata": { |
| 28 | + "id": "NtPGB4JfxQqt" |
| 29 | + }, |
| 30 | + "outputs": [], |
| 31 | + "source": [ |
| 32 | + "import os\n", |
| 33 | + "from langchain.llms import Together\n", |
| 34 | + "\n", |
| 35 | + "# Paste your Together.ai API key here (inside quotes)\n", |
| 36 | + "os.environ[\"TOGETHER_API_KEY\"] = \"Token_KEY\"\n", |
| 37 | + "\n", |
| 38 | + "# Load the LLM from Together.ai (you can switch models later)\n", |
| 39 | + "llm = Together(\n", |
| 40 | + " model=\"mistralai/Mistral-7B-Instruct-v0.1\",\n", |
| 41 | + " temperature=0.3,\n", |
| 42 | + " max_tokens=512\n", |
| 43 | + ")\n" |
| 44 | + ] |
| 45 | + }, |
| 46 | + { |
| 47 | + "cell_type": "code", |
| 48 | + "execution_count": null, |
| 49 | + "metadata": { |
| 50 | + "id": "6e52bb9b" |
| 51 | + }, |
| 52 | + "outputs": [], |
| 53 | + "source": [ |
| 54 | + "!pip install -q langchain-community" |
| 55 | + ] |
| 56 | + }, |
| 57 | + { |
| 58 | + "cell_type": "code", |
| 59 | + "execution_count": null, |
| 60 | + "metadata": { |
| 61 | + "id": "f9e0ba93" |
| 62 | + }, |
| 63 | + "outputs": [], |
| 64 | + "source": [ |
| 65 | + "!pip install -U langchain-together" |
| 66 | + ] |
| 67 | + }, |
| 68 | + { |
| 69 | + "cell_type": "code", |
| 70 | + "execution_count": null, |
| 71 | + "metadata": { |
| 72 | + "id": "fe5a52e5" |
| 73 | + }, |
| 74 | + "outputs": [], |
| 75 | + "source": [ |
| 76 | + "import os\n", |
| 77 | + "from langchain_together import Together\n", |
| 78 | + "\n", |
| 79 | + "# Paste your Together.ai API key here (inside quotes)\n", |
| 80 | + "os.environ[\"TOGETHER_API_KEY\"] = \"Token\"\n", |
| 81 | + "\n", |
| 82 | + "# Load the LLM from Together.ai (you can switch models later)\n", |
| 83 | + "llm = Together(\n", |
| 84 | + " model=\"mistralai/Mistral-7B-Instruct-v0.1\",\n", |
| 85 | + " temperature=0.3,\n", |
| 86 | + " max_tokens=512\n", |
| 87 | + ")" |
| 88 | + ] |
| 89 | + }, |
| 90 | + { |
| 91 | + "cell_type": "code", |
| 92 | + "execution_count": null, |
| 93 | + "metadata": { |
| 94 | + "id": "5W6agZt6yPYW" |
| 95 | + }, |
| 96 | + "outputs": [], |
| 97 | + "source": [ |
| 98 | + "from langchain.prompts import PromptTemplate\n", |
| 99 | + "from langchain.chains import LLMChain\n", |
| 100 | + "\n", |
| 101 | + "# Define prompt template\n", |
| 102 | + "template = \"\"\"\n", |
| 103 | + "You are a debugging assistant.\n", |
| 104 | + "Given the following test failure and commit diff, determine whether the commit likely caused the failure.\n", |
| 105 | + "\n", |
| 106 | + "Test Failure:\n", |
| 107 | + "{failure}\n", |
| 108 | + "\n", |
| 109 | + "Commit Diff:\n", |
| 110 | + "{diff}\n", |
| 111 | + "\n", |
| 112 | + "Answer:\n", |
| 113 | + "\"\"\"\n", |
| 114 | + "\n", |
| 115 | + "prompt = PromptTemplate(\n", |
| 116 | + " input_variables=[\"failure\", \"diff\"],\n", |
| 117 | + " template=template,\n", |
| 118 | + ")\n", |
| 119 | + "\n", |
| 120 | + "test_failure = \"NullPointerException at NullHandler.java:42\"\n", |
| 121 | + "commit_diff = \"\"\"\n", |
| 122 | + "diff --git a/NullHandler.java b/NullHandler.java\n", |
| 123 | + "@@ -40,6 +40,7 @@\n", |
| 124 | + " public void handle() {\n", |
| 125 | + " Object obj = null;\n", |
| 126 | + " obj.toString(); // ← added line\n", |
| 127 | + " }\n", |
| 128 | + "\"\"\"\n", |
| 129 | + "\n", |
| 130 | + "\n", |
| 131 | + "chain = prompt | llm\n", |
| 132 | + "result = chain.invoke({\n", |
| 133 | + " \"failure\": test_failure,\n", |
| 134 | + " \"diff\": commit_diff\n", |
| 135 | + "})\n", |
| 136 | + "\n", |
| 137 | + "print(\"🤖 GPT Response:\\n\", result)\n" |
| 138 | + ] |
| 139 | + }, |
| 140 | + { |
| 141 | + "cell_type": "code", |
| 142 | + "execution_count": null, |
| 143 | + "metadata": { |
| 144 | + "id": "PnNaGqCW20Rt" |
| 145 | + }, |
| 146 | + "outputs": [], |
| 147 | + "source": [ |
| 148 | + "!pip install -q sentence-transformers\n" |
| 149 | + ] |
| 150 | + }, |
| 151 | + { |
| 152 | + "cell_type": "code", |
| 153 | + "execution_count": null, |
| 154 | + "metadata": { |
| 155 | + "id": "ni717d8U5e1C" |
| 156 | + }, |
| 157 | + "outputs": [], |
| 158 | + "source": [ |
| 159 | + "from sentence_transformers import SentenceTransformer\n", |
| 160 | + "import faiss\n", |
| 161 | + "import numpy as np\n", |
| 162 | + "\n", |
| 163 | + "# Load open-source embedding model\n", |
| 164 | + "embed_model = SentenceTransformer(\"all-MiniLM-L6-v2\") # or use bge-small-en\n", |
| 165 | + "\n", |
| 166 | + "# Simulate commit diffs\n", |
| 167 | + "commit_diffs = [\n", |
| 168 | + " (\"sha1\", \"Added null check to NullHandler.java\"),\n", |
| 169 | + " (\"sha2\", \"Refactored config loading\"),\n", |
| 170 | + " (\"sha3\", \"Removed unused function from TestRunner\"),\n", |
| 171 | + " (\"sha4\", \"Introduced obj.toString() in NullHandler.java\"),\n", |
| 172 | + " (\"sha5\", \"Added logging in exception handler\")\n", |
| 173 | + "]\n", |
| 174 | + "\n", |
| 175 | + "# Embed each diff\n", |
| 176 | + "texts = [text for _, text in commit_diffs]\n", |
| 177 | + "embeddings = embed_model.encode(texts, convert_to_numpy=True)\n", |
| 178 | + "\n", |
| 179 | + "# Build FAISS index\n", |
| 180 | + "dimension = embeddings.shape[1]\n", |
| 181 | + "index = faiss.IndexFlatL2(dimension)\n", |
| 182 | + "index.add(embeddings)\n", |
| 183 | + "\n", |
| 184 | + "# Store commit metadata separately\n", |
| 185 | + "sha_lookup = [sha for sha, _ in commit_diffs]\n" |
| 186 | + ] |
| 187 | + }, |
| 188 | + { |
| 189 | + "cell_type": "code", |
| 190 | + "execution_count": null, |
| 191 | + "metadata": { |
| 192 | + "id": "yQzf5L3l6HHN" |
| 193 | + }, |
| 194 | + "outputs": [], |
| 195 | + "source": [ |
| 196 | + "# Test failure text\n", |
| 197 | + "test_failure_context = \"NullPointerException at NullHandler.java:42 in handle()\"\n", |
| 198 | + "\n", |
| 199 | + "# Embed the failure\n", |
| 200 | + "query_embedding = embed_model.encode([test_failure_context])\n", |
| 201 | + "\n", |
| 202 | + "# Search for top 3 most similar diffs\n", |
| 203 | + "D, I = index.search(query_embedding, 3)\n", |
| 204 | + "top_matches = [(sha_lookup[i], commit_diffs[i][1]) for i in I[0]]\n", |
| 205 | + "\n", |
| 206 | + "print(\"🔍 Top relevant commit diffs:\\n\")\n", |
| 207 | + "for sha, diff in top_matches:\n", |
| 208 | + " print(f\"Commit {sha}:\\n{diff}\\n\")\n" |
| 209 | + ] |
| 210 | + }, |
| 211 | + { |
| 212 | + "cell_type": "code", |
| 213 | + "execution_count": null, |
| 214 | + "metadata": { |
| 215 | + "id": "0LiPUzbA7-WK" |
| 216 | + }, |
| 217 | + "outputs": [], |
| 218 | + "source": [ |
| 219 | + "from langchain.prompts import PromptTemplate\n", |
| 220 | + "\n", |
| 221 | + "template = \"\"\"\n", |
| 222 | + "You are a debugging assistant.\n", |
| 223 | + "\n", |
| 224 | + "A test failed with the following message:\n", |
| 225 | + "{failure}\n", |
| 226 | + "\n", |
| 227 | + "Here is a commit diff that was semantically similar:\n", |
| 228 | + "{diff}\n", |
| 229 | + "\n", |
| 230 | + "Do you think this commit caused the failure? Why or why not?\n", |
| 231 | + "\"\"\"\n", |
| 232 | + "\n", |
| 233 | + "prompt = PromptTemplate(\n", |
| 234 | + " input_variables=[\"failure\", \"diff\"],\n", |
| 235 | + " template=template,\n", |
| 236 | + ")\n", |
| 237 | + "\n", |
| 238 | + "chain = prompt | llm\n" |
| 239 | + ] |
| 240 | + }, |
| 241 | + { |
| 242 | + "cell_type": "code", |
| 243 | + "execution_count": null, |
| 244 | + "metadata": { |
| 245 | + "id": "I7C-duv58Bdx" |
| 246 | + }, |
| 247 | + "outputs": [], |
| 248 | + "source": [ |
| 249 | + "print(\"🤖 GPT-style analysis:\\n\")\n", |
| 250 | + "\n", |
| 251 | + "for sha, diff in top_matches:\n", |
| 252 | + " print(f\"🧩 Commit {sha}:\")\n", |
| 253 | + " response = chain.invoke({\n", |
| 254 | + " \"failure\": test_failure_context,\n", |
| 255 | + " \"diff\": diff\n", |
| 256 | + " })\n", |
| 257 | + " print(response)\n", |
| 258 | + " print(\"-\" * 60)\n" |
| 259 | + ] |
| 260 | + }, |
| 261 | + { |
| 262 | + "cell_type": "code", |
| 263 | + "execution_count": null, |
| 264 | + "metadata": { |
| 265 | + "id": "fGZLq7VdARod" |
| 266 | + }, |
| 267 | + "outputs": [], |
| 268 | + "source": [ |
| 269 | + "combined_diff_text = \"\"\n", |
| 270 | + "\n", |
| 271 | + "for sha, diff in top_matches:\n", |
| 272 | + " combined_diff_text += f\"--- Commit {sha} ---\\n{diff}\\n\\n\"\n" |
| 273 | + ] |
| 274 | + }, |
| 275 | + { |
| 276 | + "cell_type": "code", |
| 277 | + "execution_count": null, |
| 278 | + "metadata": { |
| 279 | + "id": "fWmt6Ii0ATHp" |
| 280 | + }, |
| 281 | + "outputs": [], |
| 282 | + "source": [ |
| 283 | + "from langchain.prompts import PromptTemplate\n", |
| 284 | + "\n", |
| 285 | + "combined_template = \"\"\"\n", |
| 286 | + "You are a debugging assistant helping identify the root cause of a test failure.\n", |
| 287 | + "\n", |
| 288 | + "Here is the test failure:\n", |
| 289 | + "{failure}\n", |
| 290 | + "\n", |
| 291 | + "Here are the top 3 semantically related commits:\n", |
| 292 | + "{diffs}\n", |
| 293 | + "\n", |
| 294 | + "Based on the diff content and the test failure, which commit is most likely responsible? Explain your reasoning.\n", |
| 295 | + "\"\"\"\n", |
| 296 | + "\n", |
| 297 | + "combined_prompt = PromptTemplate(\n", |
| 298 | + " input_variables=[\"failure\", \"diffs\"],\n", |
| 299 | + " template=combined_template\n", |
| 300 | + ")\n", |
| 301 | + "\n", |
| 302 | + "# Create runnable chain\n", |
| 303 | + "combined_chain = combined_prompt | llm\n" |
| 304 | + ] |
| 305 | + }, |
| 306 | + { |
| 307 | + "cell_type": "code", |
| 308 | + "execution_count": null, |
| 309 | + "metadata": { |
| 310 | + "id": "eQ6iKc4iAWSv" |
| 311 | + }, |
| 312 | + "outputs": [], |
| 313 | + "source": [ |
| 314 | + "result = combined_chain.invoke({\n", |
| 315 | + " \"failure\": test_failure_context,\n", |
| 316 | + " \"diffs\": combined_diff_text\n", |
| 317 | + "})\n", |
| 318 | + "\n", |
| 319 | + "print(\"🤖 Final Verdict:\\n\")\n", |
| 320 | + "print(result)\n" |
| 321 | + ] |
| 322 | + } |
| 323 | + ], |
| 324 | + "metadata": { |
| 325 | + "colab": { |
| 326 | + "include_colab_link": true, |
| 327 | + "provenance": [] |
| 328 | + }, |
| 329 | + "kernelspec": { |
| 330 | + "display_name": "Python 3", |
| 331 | + "name": "python3" |
| 332 | + }, |
| 333 | + "language_info": { |
| 334 | + "name": "python" |
| 335 | + } |
| 336 | + }, |
| 337 | + "nbformat": 4, |
| 338 | + "nbformat_minor": 0 |
| 339 | +} |
0 commit comments