Spaces:
Sleeping
Sleeping
File size: 2,410 Bytes
65936f4 |
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 |
{
"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
}
|