
    h;                        d dl mZ d dlZd dlZd dlZd dlZd dlmZmZm	Z	m
Z
mZmZmZ d dlmZ d dlmZ d dlmZ erd dlZ ej*                  e      Zd
dZ G d d	e      Zy)    )annotationsN)TYPE_CHECKINGAnyIterableListOptionalTupleType)Document)
Embeddings)VectorStorec                F    t        j                  dt        |       z  g|  S )zSerializes a list of floats into a compact "raw bytes" format

    Source: https://github.com/asg017/sqlite-vec/blob/21c5a14fc71c83f135f5b00c84115139fd12c492/examples/simple-python/demo.py#L8-L10
    z%sf)structpacklen)vectors    h/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/langchain_community/vectorstores/sqlitevec.pyserialize_f32r      s!    
 ;;us6{*4V44    c                     e Zd ZdZ	 d	 	 	 	 	 	 	 ddZddZ	 d	 	 	 	 	 	 	 ddZ	 d	 	 	 	 	 	 	 ddZ	 d	 	 	 	 	 	 	 ddZ	 d	 	 	 	 	 	 	 ddZ		 d	 	 	 	 	 	 	 dd	Z
e	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd
       Zedd       ZddZy)	SQLiteVecaL  SQLite with Vec extension as a vector database.

    To use, you should have the ``sqlite-vec`` python package installed.
    Example:
        .. code-block:: python
            from langchain_community.vectorstores import SQLiteVec
            from langchain_community.embeddings.openai import OpenAIEmbeddings
            ...
    c                    	 ddl }|s| j                  |      }t        |t              st        j                  d       || _        || _        || _	        | j                          y# t        $ r t        d      w xY w)z1Initialize with sqlite client with vss extension.r   Nz\Could not import sqlite-vec python package. Please install it with `pip install sqlite-vec`.z+embeddings input must be Embeddings object.)
sqlite_vecImportErrorcreate_connection
isinstancer   warningswarn_connection_table
_embeddingcreate_table_if_not_exists)selftable
connection	embeddingdb_filer   s         r   __init__zSQLiteVec.__init__.   s|    	 //8J)Z0MMGH%#'')!  	C 	s   A$ $A9c           	        | j                   j                  d| j                   d       | j                   j                  d| j                   d| j                          d       | j                   j                  d| j                   d| j                   d| j                   d	       | j                   j	                          y )
Nz(
            CREATE TABLE IF NOT EXISTS z
            (
                rowid INTEGER PRIMARY KEY AUTOINCREMENT,
                text TEXT,
                metadata BLOB,
                text_embedding BLOB
            )
            ;
            z0
            CREATE VIRTUAL TABLE IF NOT EXISTS za_vec USING vec0(
                rowid INTEGER PRIMARY KEY,
                text_embedding float[z*]
            )
            ;
            z.
                CREATE TRIGGER IF NOT EXISTS z-_embed_text 
                AFTER INSERT ON z7
                BEGIN
                    INSERT INTO z_vec(rowid, text_embedding)
                    VALUES (new.rowid, new.text_embedding) 
                    ;
                END;
            )r   executer    get_dimensionalitycommit)r#   s    r   r"   z$SQLiteVec.create_table_if_not_existsJ   s      ((,} 5		
 	  004} =&&*&=&=&?%@ A	
 	  ..2kk] ;!!% .!!% .	
	
 	!r   Nc           
        | j                   j                  d| j                         j                         d   }|d}| j                  j                  t        |            }|s|D cg c]  }i  }}t        |||      D 	cg c]'  \  }}}	|t        j                  |      t        |	      f) }
}}}	| j                   j                  d| j                   d|
       | j                   j                          | j                   j                  d| j                   d|       }|D cg c]  }|d   	 c}S c c}w c c}	}}w c c}w )a  Add more texts to the vectorstore index.
        Args:
            texts: Iterable of strings to add to the vectorstore.
            metadatas: Optional list of metadatas associated with the texts.
            kwargs: vectorstore specific parameters
        z SELECT max(rowid) as rowid FROM rowidr   zINSERT INTO z/(text, metadata, text_embedding) VALUES (?,?,?)zSELECT rowid FROM z WHERE rowid > )r   r*   r    fetchoner!   embed_documentslistzipjsondumpsr   executemanyr,   )r#   texts	metadataskwargsmax_idembeds_textmetadataembed
data_inputresultsrows                r   	add_textszSQLiteVec.add_textsm   sF    !!)).t{{m<

(*W >F00e=%*++I+ *-UIv)F
 
%h 4::h'u)=>

 
 	$$4;;-'VW	
 	!""** _VHE
 )00G00 ,
 1s   (	D6,D;'Ec                d   d| j                    d| j                    d}| j                  j                         }|j                  |t	        |      |g       |j                         }g }|D ]D  }t        j                  |d         xs i }	t        |d   |	      }
|j                  |
|d   f       F |S )Nzo
            SELECT 
                text,
                metadata,
                distance
            FROM z AS e
            INNER JOIN z_vec AS v on v.rowid = e.rowid  
            WHERE
                v.text_embedding MATCH ?
                AND k = ?
            ORDER BY distance
        r=   r<   )page_contentr=   distance)
r    r   cursorr*   r   fetchallr3   loadsr   append)r#   r&   kr8   	sql_queryrF   r@   	documentsrA   r=   docs              r   &similarity_search_with_score_by_vectorz0SQLiteVec.similarity_search_with_score_by_vector   s    
 ++ } %		 !!((*9%q)	
 //#	 	5Czz#j/28bHFhGCc3z?34	5
 r   c                    | j                   j                  |      }| j                  ||      }|D cg c]  \  }}|	 c}}S c c}}w z"Return docs most similar to query.r&   rJ   r!   embed_queryrN   )r#   queryrJ   r8   r&   rL   rM   r;   s           r   similarity_searchzSQLiteVec.similarity_search   sM     OO//6	??1 @ 
	 #,,Q,,,s   Ac                b    | j                   j                  |      }| j                  ||      }|S rP   rR   )r#   rT   rJ   r8   r&   rL   s         r   similarity_search_with_scorez&SQLiteVec.similarity_search_with_score   s;     OO//6	??1 @ 
	 r   c                ^    | j                  ||      }|D cg c]  \  }}|	 c}}S c c}}w )NrQ   )rN   )r#   r&   rJ   r8   rL   rM   r;   s          r   similarity_search_by_vectorz%SQLiteVec.similarity_search_by_vector   s9     ??1 @ 
	 #,,Q,,,s   )c                f    | j                  |      } | ||||      }|j                  ||       |S )z9Return VectorStore initialized from texts and embeddings.)r$   r%   r'   r&   )r6   r7   )r   rB   )	clsr6   r&   r7   r$   r'   r8   r%   vecs	            r   
from_textszSQLiteVec.from_texts   s>     **73
J9
 	EY7
r   c                    dd l }dd l} |j                  |       }|j                  |_        |j                  d       |j                  |       |j                  d       |S )Nr   TF)sqlite3r   connectRowrow_factoryenable_load_extensionload)r'   r_   r   r%   s       r   r   zSQLiteVec.create_connection   sR    $W__W-
!(
((.
#((/r   c                R    d}| j                   j                  |      }t        |      S )z
        Function that does a dummy embedding to figure out how many dimensions
        this embedding function returns. Needed for the virtual table DDL.
        zThis is a dummy text)r!   rS   r   )r#   
dummy_textdummy_embeddings      r   r+   zSQLiteVec.get_dimensionality   s(    
 ,
//55jA?##r   )vec.db)r$   strr%   zOptional[sqlite3.Connection]r&   r   r'   ri   )returnNone)N)r6   zIterable[str]r7   Optional[List[dict]]r8   r   rj   	List[str])   )r&   List[float]rJ   intr8   r   rj   List[Tuple[Document, float]])rT   ri   rJ   rp   r8   r   rj   List[Document])rT   ri   rJ   rp   r8   r   rj   rq   )r&   ro   rJ   rp   r8   r   rj   rr   )N	langchainrh   )r[   zType[SQLiteVec]r6   rm   r&   r   r7   rl   r$   ri   r'   ri   r8   r   rj   r   )r'   ri   rj   zsqlite3.Connection)rj   rp   )__name__
__module____qualname____doc__r(   r"   rB   rN   rU   rW   rY   classmethodr]   staticmethodr   r+    r   r   r   r   #   s     ** 1* 	*
 *8!"L +/"1"1 ("1 	"1
 
"1J 01$),<?	%> $%-- -03-	- $% 03	% 01-$-),-<?-	- 
 +/   (	
    
 " 
 
$r   r   )r   ro   rj   bytes)
__future__r   r3   loggingr   r   typingr   r   r   r   r   r	   r
   langchain_core.documentsr   langchain_core.embeddingsr   langchain_core.vectorstoresr   r_   	getLoggerrt   loggerr   r   rz   r   r   <module>r      sY    "       . 0 3			8	$5N$ N$r   