CoolFace
Apppublic

Sahil1694/Atlan

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
vector_store_script.cpython-311.pyc48 linesDownload Raw Back to __pycache__
1�

2C�h*��4�ddlZddlZddlmZddlmZddlmZddlm	Z	dZ3dZdZd	Z
d4Zdedefd
�Zdedefd�Zdedefd�Zdedefd�ZedkrGed��ee5��Zer"ee��Zee��Zeee��ed��dSdS)�N)�RecursiveCharacterTextSplitter)�Document)�HuggingFaceEmbeddings)�Chromazdata/knowledge_base.jsonzdata/chroma_dbi��dzall-MiniLM-L6-v2�filepath�returnc�^�	t|dd���5}tj|��}ddd��n#1swxYwYtdt	|���d|�d���|S#t6$rtd|�d	���gcYStj$rtd7|�d���gcYSwxYw)z*Loads the scraped data from the JSON file.�rzutf-8)�encodingNu✅ Successfully loaded z documents from 'z'.u🚨 Error: The file 'z' was not found.u(🚨 Error: Could not decode JSON from ')�open�json�load�print�len�FileNotFoundError�JSONDecodeError)r�f�datas   �TC:\Users\Sahil\OneDrive\Desktop\Atlan Project\backend\scripts\vector_store_script.py�load_knowledge_basers��8�
�(�C�'�
2�
2�
2�	 �a��9�Q�<�<�D�	 �	 �	 �	 �	 �	 �	 �	 �	 �	 �	 ����	 �	 �	 �	 �
�Q��T���Q�Q�X�Q�Q�Q�R�R�R��������
�A�x�A�A�A�B�B�B��	�	�	������
�E��E�E�E�F�F�F��	�	�	����s2�A%�5�A%�9�A%�9�'A%�%B,�#B,�+B,rc�^�d�|D��}tdt|���d���|S)z7Converts the raw data into LangChain's Document format.c�v�g|]6}|�d���t|dd|di�����7S)�content�source�url)�page_content�metadata)�getr)�.0�items  r�9<listcomp>z.create_documents_from_data.<locals>.<listcomp>'sS������D�H�H�Y�/�/���d�9�o��4��;�8O�P�P�P����u✅ Created z LangChain documents.)rr)r�	documentss  r�create_documents_from_datar%%sH��������I�10�11>��Y���12>�13>�14>�?�?�?��r#r$c���ttttd���}|�|��}tdt|���dt|���d���|S)zFSplits the documents into smaller chunks using the recursive strategy.T)�15chunk_size�
chunk_overlap�length_function�add_start_indexu16✅ Split z documents into z chunks.)r�17CHUNK_SIZE�
CHUNK_OVERLAPr�split_documentsr)r$�
text_splitter�chunked_documentss   rr-r-.so��2��#���	���M�&�5�5�i�@�@��	�18W�s�9�~�~�19W�20W�s�;L�7M�7M�21W�22W�23W�X�X�X��r#�chunks�	save_pathc��|std��dStd��tt���}td��tj|||���}td|�d���td	��dS)24zj25    Creates embeddings for the chunks and builds a ChromaDB vector store,26    then saves it to disk.27    u7🚨 No chunks to process. Aborting vector store build.Nu$🧠 Initializing embedding model...)�28model_nameuG🛠️ Building ChromaDB vector store... (This may take a few minutes))r$�	embedding�persist_directoryu 💾 Vector store persisted to '�'u.✅ Vector store built and saved successfully!)rr�EMBEDDING_MODEL_NAMEr�from_documents)r0r1�29embeddings�vector_stores    r�build_and_save_vector_storer;:s���30��
�G�H�H�H���	�310�1�1�1�&�2F�G�G�G�J�	�32S�T�T�T��(���#����L�33�349�Y�359�369�379�:�:�:�	�38:�;�;�;�;�;r#�__main__u+🚀 Starting Vector Store Build Process...u39✨ Process complete!)�osr�langchain.text_splitterr�langchain.docstore.documentr�langchain_community.embeddingsr� langchain_community.vectorstoresr�KNOWLEDGE_BASE_PATH�VECTOR_STORE_SAVE_PATHr+r,r7�str�listrr%r-r;�__name__r�scraped_datar$�chunked_docs�r#r�<module>rJs���	�	�	�	�����B�B�B�B�B�B�0�0�0�0�0�0�@�@�@�@�@�@�3�3�3�3�3�3�1��)���40��
�*���#��$������T��d�����41�t�42��43�44�45�46�<��<��<�<�<�<�6�z���	�E�477�8�8�8�'�&�':�;�;�L��J�.�.�|�<�<�	�'��y�1�1��	$�#�L�2H�I�I�I�	�E�48#�$�$�$�$�$�!�r#