Spaces:
Sleeping
Sleeping
File size: 4,205 Bytes
bea947b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | {
"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
}
|