diff --git a/week5/community-contributions/solisoma/intelligentRag.ipynb b/week5/community-contributions/solisoma/intelligentRag.ipynb index 846660b..c5bd481 100644 --- a/week5/community-contributions/solisoma/intelligentRag.ipynb +++ b/week5/community-contributions/solisoma/intelligentRag.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 6, + "execution_count": 51, "id": "3d526a96", "metadata": {}, "outputs": [], @@ -25,7 +25,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 52, "id": "3d14dedb", "metadata": {}, "outputs": [], @@ -69,7 +69,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "id": "fa781fd3", "metadata": {}, "outputs": [], @@ -127,19 +127,39 @@ " \n", " def get_file(self, file_name: str):\n", " matches = []\n", + " \n", + " # Normalize the input file name\n", + " file_name = file_name.strip()\n", + " \n", " for root, dirs, files in os.walk(self.root_folder):\n", - " print(files, flush=True)\n", " for f in files:\n", + " # Case-insensitive comparison\n", " if \".\" in file_name:\n", - " if f == file_name:\n", + " # Exact match for files with extension (case-insensitive)\n", + " if f.lower() == file_name.lower():\n", " matches.append(os.path.join(root, f))\n", " else:\n", - " base_file_name = file_name.split(\".\")[0]\n", - " if file_name == base_file_name:\n", + " # Match base name without extension (case-insensitive)\n", + " base_name = f.split(\".\")[0]\n", + " if file_name.lower() == base_name.lower():\n", " matches.append(os.path.join(root, f))\n", "\n", " if not matches:\n", - " return f\"No file named '{file_name}' found in the repository.\"\n", + " # Try fuzzy matching for better results\n", + " fuzzy_matches = []\n", + " for root, dirs, files in os.walk(self.root_folder):\n", + " for f in files:\n", + " if file_name.lower() in f.lower():\n", + " fuzzy_matches.append(os.path.join(root, f))\n", + " \n", + " if fuzzy_matches:\n", + " listed = \"\\n\".join(fuzzy_matches[:10]) # Limit to 10 results\n", + " return (\n", + " f\"No exact match for '{file_name}' found. \"\n", + " f\"Did you mean one of these?\\n\\n{listed}\"\n", + " )\n", + " else:\n", + " return f\"No file named '{file_name}' found in the repository.\"\n", "\n", " if len(matches) > 1:\n", " listed = \"\\n\".join(matches)\n", @@ -152,9 +172,9 @@ " try:\n", " with open(file_path, \"r\", encoding=\"utf-8\") as file:\n", " content = file.read()\n", - " return f\"Content of '{file_name}':\\n\\n{content}\"\n", + " return f\"Content of '{file_name}' (from {file_path}):\\n\\n{content}\"\n", " except Exception as e:\n", - " return f\"Error reading file: {e}\"\n", + " return f\"Error reading file '{file_path}': {e}\"\n", " \n", " \n", " def _get_all_folders_recursive(self, root_path):\n", @@ -280,50 +300,188 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 54, "id": "075a4612", "metadata": {}, "outputs": [], "source": [ - "G = RepoIntelligentAssistant(root_path=\"C:/Users/hp/projects/gen-ai/llm_engineering/week5/community-contributions/tourist-guide/\")" + "root = os.path.abspath(\"../tourist-guide\")\n", + "G = RepoIntelligentAssistant(root_path=root)" ] }, { "cell_type": "code", - "execution_count": null, - "id": "4f16318f", + "execution_count": 55, + "id": "8c48444a", "metadata": {}, "outputs": [], "source": [ - "interface = gr.ChatInterface(G.run, type=\"messages\")\n", - "interface.launch()" + "def chatInterface(fn, chat_type=\"messages\"):\n", + " with gr.Blocks(\n", + " title=\"π€ Intelligent Codebase Assistant\",\n", + " theme=gr.themes.Soft(),\n", + " css=\"\"\"\n", + " .gradio-container {\n", + " max-width: 1200px !important;\n", + " margin: auto !important;\n", + " }\n", + " .chat-message {\n", + " font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;\n", + " }\n", + " \"\"\"\n", + " ) as ui:\n", + " \n", + " # Header Section\n", + " with gr.Row():\n", + " gr.Markdown(\"\"\"\n", + " # π€ Intelligent Codebase Assistant\n", + " \n", + " **Your AI-powered coding companion for understanding and improving codebases**\n", + " \n", + " ---\n", + " \"\"\")\n", + " \n", + " # Main Chat Interface\n", + " with gr.Row(height=\"77dvh\", equal_height=True):\n", + " with gr.Column(scale=4):\n", + " gr.ChatInterface(\n", + " fn, \n", + " type=chat_type,\n", + " title=\"Chat with your codebase\",\n", + " description=\"Ask questions about your code, request explanations, or get improvement suggestions\",\n", + " examples=[\n", + " \"What is the codebase about?\",\n", + " \"Explain the key file in the codebase\",\n", + " \"Explain the architecture of the codebase\",\n", + " \"Suggest improvements for the codebase\",\n", + " \"What design patterns are used?\"\n", + " ],\n", + " cache_examples=False,\n", + " fill_height=True\n", + " )\n", + " \n", + " # Sidebar with info\n", + " with gr.Column(scale=1):\n", + " gr.Markdown(\"\"\"\n", + " ## π‘ Tips\n", + " \n", + " **Ask about:**\n", + " - Specific functions or classes\n", + " - Code explanations\n", + " - Architecture questions\n", + " - Improvement suggestions\n", + " - Design patterns\n", + " \n", + " **Examples:**\n", + " - \"What does the login function do?\"\n", + " - \"Explain this code line by line\"\n", + " - \"How can I improve this function?\"\n", + " - \"What's the overall architecture?\"\n", + " \"\"\")\n", + " \n", + " gr.Markdown(\"\"\"\n", + " ## π οΈ Capabilities\n", + " \n", + " β **Code Analysis** \n", + " β **Line-by-line Explanations** \n", + " β **Improvement Suggestions** \n", + " β **Multi-language Support** \n", + " β **Memory Persistence** \n", + " β **Repository Context** \n", + " \"\"\")\n", + "\n", + " ui.launch(inbrowser=True)" ] }, - { - "cell_type": "markdown", - "id": "c0cf22c8", - "metadata": {}, - "source": [] - }, { "cell_type": "code", - "execution_count": null, - "id": "b81ef168", + "execution_count": 56, + "id": "4f16318f", "metadata": {}, "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "* Running on local URL: http://127.0.0.1:7877\n", + "* To create a public link, set `share=True` in `launch()`.\n" + ] + }, { "data": { + "text/html": [ + "
" + ], "text/plain": [ - "'..\\\\day 4 no_langchain'" + "