{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Create Vector database" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os\n", "from dotenv import load_dotenv\n", "import google.generativeai as genai\n", "from langchain_anthropic import ChatAnthropic\n", "from langchain_community.vectorstores.faiss import FAISS\n", "from langchain_google_genai import ChatGoogleGenerativeAI\n", "from langchain_google_genai import GoogleGenerativeAIEmbeddings\n", "from langchain.text_splitter import RecursiveCharacterTextSplitter" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "load_dotenv()\n", "genai.configure(api_key=os.getenv(\"GOOGLE_API_KEY\"))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def get_text_chunks(text):\n", " text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)\n", " chunks = text_splitter.split_text(text)\n", " return chunks" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "def get_vector_store(text_chunks):\n", " embeddings = GoogleGenerativeAIEmbeddings(model = \"models/embedding-001\")\n", " vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)\n", " vector_store.save_local(\"faiss_index\")" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "with open(\"all_hotels.txt\", \"r\", encoding=\"utf8\") as file:\n", " text = file.read()" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "chunks = get_text_chunks(text)\n", "get_vector_store(chunks)" ] } ], "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.11.3" } }, "nbformat": 4, "nbformat_minor": 2 }