{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"authorship_tag":"ABX9TyPzimatngGy+QfyDaAQmAxa"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"code","source":["from IPython.core.display import display, Javascript\n","\n","def wrap_text():\n"," display(Javascript('''\n"," function changeCss(cssFile) {\n"," let head = document.getElementsByTagName(\"head\")[0];\n"," let link = document.createElement(\"link\");\n"," link.rel = \"stylesheet\";\n"," link.type = \"text/css\";\n"," link.href = cssFile;\n"," head.appendChild(link);\n"," }\n"," changeCss(\"https://raw.githubusercontent.com/colab-resources/colab-text-wrap/main/colab-text-wrap.css\");\n"," '''))\n","\n","wrap_text()\n"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":17},"id":"X2-OAM60OZ76","executionInfo":{"status":"ok","timestamp":1739381718040,"user_tz":-330,"elapsed":397,"user":{"displayName":"Aryan Garg","userId":"14741919013574129820"}},"outputId":"47102a7a-bc6d-44a0-da39-03cb9344854a"},"execution_count":33,"outputs":[{"output_type":"display_data","data":{"text/plain":[""],"application/javascript":["\n"," function changeCss(cssFile) {\n"," let head = document.getElementsByTagName(\"head\")[0];\n"," let link = document.createElement(\"link\");\n"," link.rel = \"stylesheet\";\n"," link.type = \"text/css\";\n"," link.href = cssFile;\n"," head.appendChild(link);\n"," }\n"," changeCss(\"https://raw.githubusercontent.com/colab-resources/colab-text-wrap/main/colab-text-wrap.css\");\n"," "]},"metadata":{}}]},{"cell_type":"code","source":["! pip install langchain-groq"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"iSymqg8wIapv","executionInfo":{"status":"ok","timestamp":1739380088393,"user_tz":-330,"elapsed":3881,"user":{"displayName":"Aryan Garg","userId":"14741919013574129820"}},"outputId":"23168c07-fa36-4dc1-983d-4055c10562ef"},"execution_count":4,"outputs":[{"output_type":"stream","name":"stdout","text":["Collecting langchain-groq\n"," Downloading langchain_groq-0.2.4-py3-none-any.whl.metadata (3.0 kB)\n","Collecting groq<1,>=0.4.1 (from langchain-groq)\n"," Downloading groq-0.18.0-py3-none-any.whl.metadata (14 kB)\n","Requirement already satisfied: langchain-core<0.4.0,>=0.3.33 in /usr/local/lib/python3.11/dist-packages (from langchain-groq) (0.3.33)\n","Requirement already satisfied: anyio<5,>=3.5.0 in /usr/local/lib/python3.11/dist-packages (from groq<1,>=0.4.1->langchain-groq) (3.7.1)\n","Requirement already satisfied: distro<2,>=1.7.0 in /usr/local/lib/python3.11/dist-packages (from groq<1,>=0.4.1->langchain-groq) (1.9.0)\n","Requirement already satisfied: httpx<1,>=0.23.0 in /usr/local/lib/python3.11/dist-packages (from groq<1,>=0.4.1->langchain-groq) (0.28.1)\n","Requirement already satisfied: pydantic<3,>=1.9.0 in /usr/local/lib/python3.11/dist-packages (from groq<1,>=0.4.1->langchain-groq) (2.10.6)\n","Requirement already satisfied: sniffio in /usr/local/lib/python3.11/dist-packages (from groq<1,>=0.4.1->langchain-groq) (1.3.1)\n","Requirement already satisfied: typing-extensions<5,>=4.10 in /usr/local/lib/python3.11/dist-packages (from groq<1,>=0.4.1->langchain-groq) (4.12.2)\n","Requirement already satisfied: PyYAML>=5.3 in /usr/local/lib/python3.11/dist-packages (from langchain-core<0.4.0,>=0.3.33->langchain-groq) (6.0.2)\n","Requirement already satisfied: jsonpatch<2.0,>=1.33 in /usr/local/lib/python3.11/dist-packages (from langchain-core<0.4.0,>=0.3.33->langchain-groq) (1.33)\n","Requirement already satisfied: langsmith<0.4,>=0.1.125 in /usr/local/lib/python3.11/dist-packages (from langchain-core<0.4.0,>=0.3.33->langchain-groq) (0.3.6)\n","Requirement already satisfied: packaging<25,>=23.2 in /usr/local/lib/python3.11/dist-packages (from langchain-core<0.4.0,>=0.3.33->langchain-groq) (24.2)\n","Requirement already satisfied: tenacity!=8.4.0,<10.0.0,>=8.1.0 in /usr/local/lib/python3.11/dist-packages (from langchain-core<0.4.0,>=0.3.33->langchain-groq) (9.0.0)\n","Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.11/dist-packages (from anyio<5,>=3.5.0->groq<1,>=0.4.1->langchain-groq) (3.10)\n","Requirement already satisfied: certifi in /usr/local/lib/python3.11/dist-packages (from httpx<1,>=0.23.0->groq<1,>=0.4.1->langchain-groq) (2025.1.31)\n","Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.11/dist-packages (from httpx<1,>=0.23.0->groq<1,>=0.4.1->langchain-groq) (1.0.7)\n","Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.11/dist-packages (from httpcore==1.*->httpx<1,>=0.23.0->groq<1,>=0.4.1->langchain-groq) (0.14.0)\n","Requirement already satisfied: jsonpointer>=1.9 in /usr/local/lib/python3.11/dist-packages (from jsonpatch<2.0,>=1.33->langchain-core<0.4.0,>=0.3.33->langchain-groq) (3.0.0)\n","Requirement already satisfied: orjson<4.0.0,>=3.9.14 in /usr/local/lib/python3.11/dist-packages (from langsmith<0.4,>=0.1.125->langchain-core<0.4.0,>=0.3.33->langchain-groq) (3.10.15)\n","Requirement already satisfied: requests<3,>=2 in /usr/local/lib/python3.11/dist-packages (from langsmith<0.4,>=0.1.125->langchain-core<0.4.0,>=0.3.33->langchain-groq) (2.32.3)\n","Requirement already satisfied: requests-toolbelt<2.0.0,>=1.0.0 in /usr/local/lib/python3.11/dist-packages (from langsmith<0.4,>=0.1.125->langchain-core<0.4.0,>=0.3.33->langchain-groq) (1.0.0)\n","Requirement already satisfied: zstandard<0.24.0,>=0.23.0 in /usr/local/lib/python3.11/dist-packages (from langsmith<0.4,>=0.1.125->langchain-core<0.4.0,>=0.3.33->langchain-groq) (0.23.0)\n","Requirement already satisfied: annotated-types>=0.6.0 in /usr/local/lib/python3.11/dist-packages (from pydantic<3,>=1.9.0->groq<1,>=0.4.1->langchain-groq) (0.7.0)\n","Requirement already satisfied: pydantic-core==2.27.2 in /usr/local/lib/python3.11/dist-packages (from pydantic<3,>=1.9.0->groq<1,>=0.4.1->langchain-groq) (2.27.2)\n","Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.11/dist-packages (from requests<3,>=2->langsmith<0.4,>=0.1.125->langchain-core<0.4.0,>=0.3.33->langchain-groq) (3.4.1)\n","Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.11/dist-packages (from requests<3,>=2->langsmith<0.4,>=0.1.125->langchain-core<0.4.0,>=0.3.33->langchain-groq) (2.3.0)\n","Downloading langchain_groq-0.2.4-py3-none-any.whl (14 kB)\n","Downloading groq-0.18.0-py3-none-any.whl (121 kB)\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m121.9/121.9 kB\u001b[0m \u001b[31m3.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hInstalling collected packages: groq, langchain-groq\n","Successfully installed groq-0.18.0 langchain-groq-0.2.4\n"]}]},{"cell_type":"code","source":["! pip install python-docx"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"orwcBUNgLAHA","executionInfo":{"status":"ok","timestamp":1739380877692,"user_tz":-330,"elapsed":3291,"user":{"displayName":"Aryan Garg","userId":"14741919013574129820"}},"outputId":"937668c1-4065-46e0-b99f-b8c5b102dcc7"},"execution_count":16,"outputs":[{"output_type":"stream","name":"stdout","text":["Collecting python-docx\n"," Downloading python_docx-1.1.2-py3-none-any.whl.metadata (2.0 kB)\n","Requirement already satisfied: lxml>=3.1.0 in /usr/local/lib/python3.11/dist-packages (from python-docx) (5.3.0)\n","Requirement already satisfied: typing-extensions>=4.9.0 in /usr/local/lib/python3.11/dist-packages (from python-docx) (4.12.2)\n","Downloading python_docx-1.1.2-py3-none-any.whl (244 kB)\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m244.3/244.3 kB\u001b[0m \u001b[31m4.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hInstalling collected packages: python-docx\n","Successfully installed python-docx-1.1.2\n"]}]},{"cell_type":"code","source":["!pip install faiss-cpu"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"eB1APAEtNNFT","executionInfo":{"status":"ok","timestamp":1739381397316,"user_tz":-330,"elapsed":6986,"user":{"displayName":"Aryan Garg","userId":"14741919013574129820"}},"outputId":"a7d28afb-689c-43cd-b4cf-bf13fbe62f01"},"execution_count":25,"outputs":[{"output_type":"stream","name":"stdout","text":["Collecting faiss-cpu\n"," Downloading faiss_cpu-1.10.0-cp311-cp311-manylinux_2_28_x86_64.whl.metadata (4.4 kB)\n","Requirement already satisfied: numpy<3.0,>=1.25.0 in /usr/local/lib/python3.11/dist-packages (from faiss-cpu) (1.26.4)\n","Requirement already satisfied: packaging in /usr/local/lib/python3.11/dist-packages (from faiss-cpu) (24.2)\n","Downloading faiss_cpu-1.10.0-cp311-cp311-manylinux_2_28_x86_64.whl (30.7 MB)\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m30.7/30.7 MB\u001b[0m \u001b[31m25.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hInstalling collected packages: faiss-cpu\n","Successfully installed faiss-cpu-1.10.0\n"]}]},{"cell_type":"code","source":["from docx import Document\n","from langchain_core.prompts import ChatPromptTemplate\n","from langchain_groq import ChatGroq"],"metadata":{"id":"QzWB6nYeGj5h","executionInfo":{"status":"ok","timestamp":1739388962112,"user_tz":-330,"elapsed":377,"user":{"displayName":"Aryan Garg","userId":"14741919013574129820"}}},"execution_count":50,"outputs":[]},{"cell_type":"code","source":["from google.colab import files\n","uploaded = files.upload()"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":73},"id":"Dbe2V8b4MFrQ","executionInfo":{"status":"ok","timestamp":1739381061627,"user_tz":-330,"elapsed":11625,"user":{"displayName":"Aryan Garg","userId":"14741919013574129820"}},"outputId":"4bdeb89c-2d18-497c-d5e8-825853c150a7"},"execution_count":18,"outputs":[{"output_type":"display_data","data":{"text/plain":[""],"text/html":["\n"," \n"," \n"," Upload widget is only available when the cell has been executed in the\n"," current browser session. Please rerun this cell to enable.\n"," \n"," "]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["Saving solar panel technology.docx to solar panel technology.docx\n"]}]},{"cell_type":"code","source":["def extract_text_from_docx(file_path):\n"," doc = Document(file_path)\n"," text = \"\\n\".join([para.text for para in doc.paragraphs if para.text.strip()])\n"," return text\n","\n","text_data = extract_text_from_docx(\"solar panel technology.docx\")\n","print(text_data[:500])"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"s3OGuSONK-GL","executionInfo":{"status":"ok","timestamp":1739388818701,"user_tz":-330,"elapsed":383,"user":{"displayName":"Aryan Garg","userId":"14741919013574129820"}},"outputId":"da339b73-97cd-4221-92f6-6a66fa23edd6"},"execution_count":48,"outputs":[{"output_type":"stream","name":"stdout","text":["New Solar Panel Technology Trends Shaping the Future\n","Efficiency Skyrockets With New Solar Panel Technologies\n","Solar panel efficiency has seen remarkable advancements over the past two to three decades. In the early days, solar panels had a conversion efficiency of around 10%, meaning they could only convert about a tenth of the sunlight they captured into usable electricity. However, solar panel efficiency rates have increased dramatically thanks to continuous research, development, and technolog\n"]}]},{"cell_type":"code","source":["from sentence_transformers import SentenceTransformer\n","model = SentenceTransformer(\"all-MiniLM-L6-v2\")\n","def split_text(text, chunk_size=300):\n"," \"\"\"Splits text into smaller chunks for vector indexing.\"\"\"\n"," sentences = text.split(\". \")\n"," chunks, current_chunk = [], \"\"\n","\n"," for sentence in sentences:\n"," if len(current_chunk) + len(sentence) < chunk_size:\n"," current_chunk += sentence + \". \"\n"," else:\n"," chunks.append(current_chunk.strip())\n"," current_chunk = sentence + \". \"\n","\n"," if current_chunk:\n"," chunks.append(current_chunk.strip())\n","\n"," return chunks\n","\n","chunks = split_text(text_data)\n","embeddings = model.encode(chunks)\n"],"metadata":{"id":"HuGL68uzMP06","executionInfo":{"status":"ok","timestamp":1739385400247,"user_tz":-330,"elapsed":8728,"user":{"displayName":"Aryan Garg","userId":"14741919013574129820"}}},"execution_count":45,"outputs":[]},{"cell_type":"code","source":["chunks[0]"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":53},"id":"5pgVH7OKcjE9","executionInfo":{"status":"ok","timestamp":1739385405049,"user_tz":-330,"elapsed":621,"user":{"displayName":"Aryan Garg","userId":"14741919013574129820"}},"outputId":"04b8bcba-cc8c-4269-9192-faeaf359e004"},"execution_count":46,"outputs":[{"output_type":"execute_result","data":{"text/plain":["'New Solar Panel Technology Trends Shaping the Future\\nEfficiency Skyrockets With New Solar Panel Technologies\\nSolar panel efficiency has seen remarkable advancements over the past two to three decades.'"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"string"}},"metadata":{},"execution_count":46}]},{"cell_type":"code","source":["import pickle\n","with open(\"chunks.pkl\", \"wb\") as f:\n"," pickle.dump(chunks, f)"],"metadata":{"id":"b2C2Oqixbr9i","executionInfo":{"status":"ok","timestamp":1739385538317,"user_tz":-330,"elapsed":363,"user":{"displayName":"Aryan Garg","userId":"14741919013574129820"}}},"execution_count":47,"outputs":[]},{"cell_type":"code","source":["embeddings.shape"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"5DJ8EsfxM0a4","executionInfo":{"status":"ok","timestamp":1739381308078,"user_tz":-330,"elapsed":355,"user":{"displayName":"Aryan Garg","userId":"14741919013574129820"}},"outputId":"29ddf446-6d10-4cbe-8ae6-d01d62da42a8"},"execution_count":22,"outputs":[{"output_type":"execute_result","data":{"text/plain":["(74, 384)"]},"metadata":{},"execution_count":22}]},{"cell_type":"code","source":["import faiss\n","import numpy as np\n","\n","vector_dim = embeddings.shape[1]\n","\n","index = faiss.IndexFlatL2(vector_dim)\n","index.add(np.array(embeddings))\n","faiss.write_index(index, \"solar_vectors.index\")\n"],"metadata":{"id":"KsRXY2DWNFcE","executionInfo":{"status":"ok","timestamp":1739381403064,"user_tz":-330,"elapsed":357,"user":{"displayName":"Aryan Garg","userId":"14741919013574129820"}}},"execution_count":26,"outputs":[]},{"cell_type":"code","source":["index"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"bvw2S-QANenF","executionInfo":{"status":"ok","timestamp":1739381424305,"user_tz":-330,"elapsed":383,"user":{"displayName":"Aryan Garg","userId":"14741919013574129820"}},"outputId":"38973b49-6707-4577-d12d-ad0728d3c885"},"execution_count":27,"outputs":[{"output_type":"execute_result","data":{"text/plain":[" >"]},"metadata":{},"execution_count":27}]},{"cell_type":"code","source":["index = faiss.read_index(\"solar_vectors.index\")\n","\n","def retrieve_relevant_text(query, top_k=1):\n"," query_embedding = model.encode([query])\n"," distances, indices = index.search(np.array(query_embedding), top_k)\n"," return [chunks[i] for i in indices[0]]\n","\n"],"metadata":{"id":"uEzCuXokNjjS","executionInfo":{"status":"ok","timestamp":1739381511722,"user_tz":-330,"elapsed":345,"user":{"displayName":"Aryan Garg","userId":"14741919013574129820"}}},"execution_count":28,"outputs":[]},{"cell_type":"code","source":["llm=ChatGroq(model=\"mixtral-8x7b-32768\",temperature=0.2)"],"metadata":{"id":"aioSsUnlqN94","executionInfo":{"status":"ok","timestamp":1739388968534,"user_tz":-330,"elapsed":632,"user":{"displayName":"Aryan Garg","userId":"14741919013574129820"}}},"execution_count":51,"outputs":[]},{"cell_type":"code","source":["def generate_response(user_query):\n"," \"\"\"Fetch relevant data from FAISS and pass to LLM.\"\"\"\n"," retrieved_text = retrieve_relevant_text(user_query, top_k=2)\n"," print(f\"Retrieved Text: {len(retrieved_text)}\")\n"," # Create prompt with retrieved text\n"," system_message = \"You are an intelligent assistant that provides accurate, helpful information about solar energy based on the information provided(if not, answer according to your knowledge).\"\n"," prompt_template = ChatPromptTemplate.from_messages([\n"," (\"system\", system_message),\n"," (\"human\", f\"Use the following information to answer: {retrieved_text} \\n\\nUser Query: {user_query}\")\n"," ])\n","\n"," # Generate response\n"," chain = prompt_template | llm\n"," response = chain.invoke({\"text\": user_query})\n","\n"," return response.content\n","\n","# Example Usage\n","user_input = \"What is the installation process in detail\"\n","answer = generate_response(user_input)\n","print(answer)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"-aYh1jbFN5JW","executionInfo":{"status":"ok","timestamp":1739381985869,"user_tz":-330,"elapsed":1175,"user":{"displayName":"Aryan Garg","userId":"14741919013574129820"}},"outputId":"ff466587-e283-4b4e-df82-141eb77feb8a"},"execution_count":41,"outputs":[{"output_type":"stream","name":"stdout","text":["Retrieved Text: 2\n","Based on the information provided, here is a detailed solar panel installation process:\n","\n","1. Obtain Permits and Grants:\n"," - Finalize your installation plan.\n"," - Obtain necessary permits to legalize the installation.\n"," - Explore potential subsidies or incentives for solar panel installation available in your area.\n","\n","2. Install Racking System:\n"," - For Pitched Roofs - Use flashings, screws, and bolts to attach rails by drilling the roof. Apply sealant to prevent water leakage.\n"," - For Flat Roofs - Installers use either ballasts or concrete blocks to rack the solar panels.\n"," - Secure the structures into place properly.\n"," - Install the racking rails.\n","\n","3. Install Solar Panels:\n"," - Place solar panels onto the racking system.\n"," - Connect the panels to the racking system using clamps.\n"," - Ensure that the panels are level and secure.\n","\n","4. Install Conduit and Wiring:\n"," - Run conduit from the solar array to the inverter location.\n"," - Connect the panels using MC4 (Multi-Contact) connectors.\n"," - Ensure that all wiring is properly insulated and secured.\n","\n","5. Install Inverter and Monitoring System:\n"," - Install the inverter near the main electrical panel.\n"," - Connect the inverter to the solar array and the main electrical panel.\n"," - Install a monitoring system to track the solar panel performance.\n","\n","6. Final Inspection and Commissioning:\n"," - Schedule a final inspection with the local permitting authority.\n"," - Ensure that all work is done according to the local codes and regulations.\n"," - After passing the inspection, commission the system and start generating clean, renewable energy.\n"]}]},{"cell_type":"code","source":[],"metadata":{"id":"C0gwHVI-OA30"},"execution_count":null,"outputs":[]}]}