{ "cells": [ { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "from langchain_google_genai import ChatGoogleGenerativeAI\n", "from langchain_core.runnables import RunnablePassthrough\n", "import os\n", "from dotenv import load_dotenv\n", "load_dotenv()\n", "from langchain_community.document_loaders import WebBaseLoader\n", "from langchain.vectorstores import FAISS\n", "from langchain_core.output_parsers import StrOutputParser" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "from IPython.display import Markdown\n", "def to_Markdown(text):\n", " return Markdown(text)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "from langchain.text_splitter import RecursiveCharacterTextSplitter\n", "def VectorStore(data,embedding):\n", " splitter = RecursiveCharacterTextSplitter(chunk_size = 1000,chunk_overlap =500)\n", " chunks = splitter.split_documents(data)\n", " vector = FAISS.from_documents(chunks,embedding)\n", " retriever = vector.as_retriever()\n", " return retriever\n", "\n" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "llm = ChatGoogleGenerativeAI(model='gemini-1.5-flash')\n", "from langchain_huggingface import HuggingFaceEmbeddings\n", "embedding = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hi there! How can I help you today?'" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "llm.invoke(\"hi\").content" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "def data_ingestion(path):\n", " loader = WebBaseLoader(path) \n", " data = loader.load()\n", " return data" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "from langchain.prompts import ChatPromptTemplate\n", "def prompt_helper():\n", " template = \"\"\" Answer Based on the following context:\n", " {context}\n", " Question: {question}\n", " provide only helpful information.\n", " \"\"\"\n", " prompt = ChatPromptTemplate.from_template(template)\n", " return prompt" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "def main():\n", " path = input(\"Enter the website link: \")\n", " data = data_ingestion(path)\n", " retriever = VectorStore(data,embedding)\n", " prompt = prompt_helper()\n", " chain = (\n", " {'context': retriever , 'question': RunnablePassthrough()}\n", " | prompt\n", " |llm\n", " |StrOutputParser()\n", " )\n", " question = input(\"Enter the question from the link: \")\n", " response = chain.invoke(question)\n", " print(response)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The provided text does not contain any information about the color blue.\n" ] } ], "source": [ "main()" ] }, { "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.9.6" } }, "nbformat": 4, "nbformat_minor": 2 }