{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## AI Code Reviewer with Langchain and Groq" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from dotenv import load_dotenv\n", "load_dotenv()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from langchain_groq import ChatGroq \n", "from langchain.chains import RetrievalQA\n", "from langchain.chains import LLMChain\n", "\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import os \n", "os.environ[\"GROQ_API_KEY\"]=os.getenv(\"GROQ_API_KEY\")\n", "os.environ[\"LANGSMITH_TRACING_V2\"]=\"true\"\n", "os.environ[\"LANGSMITH_ENDPOINT\"]=\"https://api.smith.langchain.com\"\n", "os.environ[\"LANGCHAIN_API_KEY\"]=os.getenv(\"LANGCHAIN_API_KEY\")\n", "os.environ[\"LANGSMITH_PROJECT\"]=\"AI Code Reviewer\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Loading The Model" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "GROQ_API_KEY=os.getenv(\"GROQ_API_KEY\")\n", "llm=ChatGroq(api_key=GROQ_API_KEY,model_name=\"gemma2-9b-it\")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "response=llm.invoke(\"import numy as np\")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'It seems like you\\'re trying to import the NumPy library. \\n\\nHowever, there\\'s a slight typo in your code. \"numy\" should be \"numpy\".\\n\\nHere\\'s the corrected import statement:\\n\\n```python\\nimport numpy as np\\n```\\n\\nThis line of code imports the NumPy library and gives it the alias \"np\". This is a common convention in Python, allowing you to use \"np\" instead of writing out \"numpy\" every time you need to use a NumPy function or object.\\n\\n\\n\\nLet me know if you have any other questions or need help with NumPy!\\n'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "response.content" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Trying out with Different Prompts" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "from langchain.prompts import FewShotPromptTemplate, PromptTemplate\n", "\n", "# Define example responses for few-shot prompting\n", "examples = [\n", " {\n", " \"input\": \"def add(a, b):\\nreturn a + b\",\n", " \"output\": \"Your function 'add' is missing proper indentation. Here's a corrected version:\\n\\ndef add(a, b):\\n return a + b\\n\"\n", " },\n", " {\n", " \"input\": \"def divide(a, b):\\n return a / b\",\n", " \"output\": \"Potential bug detected: Division by zero error. You should handle this case:\\n\\ndef divide(a, b):\\n if b == 0:\\n return 'Error: Division by zero'\\n return a / b\\n\"\n", " }\n", "]\n", "\n", "# Define example template\n", "example_template = PromptTemplate(\n", " input_variables=[\"input\", \"output\"],\n", " template=\"Code: \\n{input}\\n\\nFeedback:\\n{output}\\n\"\n", ")\n", "prefix=\"\"\"You are a highly skilled Python code reviewer. \n", "Your task is to analyze the given Python code, identify potential bugs, suggest improvements, and provide a corrected version of the code if necessary. Ensure that your feedback is clear, precise, and actionable.\n", "First you have to specify where and what the error is.\n", "Next give the correct code\n", "\n", "\"\"\"\n", "\n", "# Create a few-shot prompt template\n", "few_shot_prompt = FewShotPromptTemplate(\n", " examples=examples,\n", " example_prompt=example_template,\n", " prefix=prefix,\n", " suffix=\"Code:\\n{input}\\n\\nFeedback:\",\n", " input_variables=[\"input\"]\n", ")\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "C:\\Users\\saipr\\AppData\\Local\\Temp\\ipykernel_28632\\3821535042.py:2: LangChainDeprecationWarning: The class `LLMChain` was deprecated in LangChain 0.1.17 and will be removed in 1.0. Use :meth:`~RunnableSequence, e.g., `prompt | llm`` instead.\n", " llm_chain = LLMChain(llm=llm, prompt=few_shot_prompt)\n" ] } ], "source": [ "# Create the LLMChain\n", "llm_chain = LLMChain(llm=llm, prompt=few_shot_prompt)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "C:\\Users\\saipr\\AppData\\Local\\Temp\\ipykernel_28632\\911030789.py:3: LangChainDeprecationWarning: The method `Chain.run` was deprecated in langchain 0.1.0 and will be removed in 1.0. Use :meth:`~invoke` instead.\n", " response = llm_chain.run(input=code_snippet)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "The error is a simple typo. \n", "\n", "`nump` should be `numpy`. \n", "\n", "Here's the corrected code:\n", "\n", "```python\n", "import numpy as np\n", "``` \n", "\n" ] } ], "source": [ "\n", "# Example usage\n", "code_snippet = \"import nump as np\"\n", "response = llm_chain.run(input=code_snippet)\n", "print(response)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.0" } }, "nbformat": 4, "nbformat_minor": 2 }