
    h;                        d dl mZ d dlZd dlmZmZmZmZmZm	Z	m
Z
mZmZ erd dlZd dlZd dlmZmZmZmZ d dlmZ d dlmZ d dlmZ d dlmZ d	Zdd
ZddZ G d de      Zy)    )annotationsN)	TYPE_CHECKINGAnyCallableDictIterableListOptionalTupleType)ID	OneOrManyWhereWhereDocument)Document)
Embeddings)xor_args)VectorStore   c                J    t        |       D cg c]  \  }}|	 c}}S c c}}w N)_results_to_docs_and_scores)resultsdoc_s      d/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/langchain_community/vectorstores/bagel.py_results_to_docsr      s    9'BCFCCCCCs   c                    t        | d   d   | d   d   | d   d         D cg c]  }t        |d   |d   xs i       |d   f  c}S c c}w )N	documentsr   	metadatas	distances   )page_contentmetadata   )zipr   )r   results     r   r   r   !   sl     K #K #K #
 
vay6!9?	CVAYO  s   #Ac            	      0   e Zd ZU dZdZded<   edddddf	 	 	 	 	 	 	 	 	 	 	 	 	 ddZedd       Z e	d      	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 dd	       Z
	 	 	 d	 	 	 	 	 	 	 	 	 	 	 dd
Zedf	 	 	 	 	 	 	 	 	 ddZedf	 	 	 	 	 	 	 	 	 ddZedddeddddf	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd       ZddZedf	 	 	 	 	 	 	 	 	 d dZedf	 	 	 	 	 	 	 	 	 d!dZd"dZeddedddf	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d#d       Zd$dZ	 	 	 	 	 	 d%	 	 	 	 	 	 	 	 	 	 	 	 	 d&dZd'd(dZy))Bagela"  ``Bagel.net`` Inference platform.

    To use, you should have the ``bagelML`` python package installed.

    Example:
        .. code-block:: python

                from langchain_community.vectorstores import Bagel
                vectorstore = Bagel(cluster_name="langchain_store")
    	langchainstr_LANGCHAIN_DEFAULT_CLUSTER_NAMENc                H   	 ddl }ddl}||| _        || _        n@|r|}n|j
                  j                  dd      }|| _         |j                  |      | _        | j                  j                  ||      | _	        || _
        || _        y# t        $ r t        d      w xY w)zInitialize with bagel clientr   N+Please install bagel `pip install bagelML`.restzapi.bageldb.ai)bagel_api_implbagel_server_host)namer$   )bagelbagel.configImportError_client_settings_clientconfigSettingsClientget_or_create_cluster_clusteroverride_relevance_score_fn_embedding_function)	selfcluster_nameclient_settingsembedding_functioncluster_metadataclientrelevance_score_fnr3   r6   s	            r   __init__zBagel.__init__:   s    	M $3D!!DL#2 #(<<#8#8#)&6 $9 $  %5D!'5<<(89DL::% ; 
 ,>(#5 +  	MKLL	Ms   B B!c                    | j                   S r   )r>   r?   s    r   
embeddingszBagel.embeddings^   s    '''    )query_textsquery_embeddingsc                    	 ddl }| j                  r,|*|r(t        |      }| j                  j	                  |      }d} | j
                  j                  d||||d|S # t        $ r t        d      w xY w)z9Query the Bagel cluster based on the provided parameters.r   Nr.   )rK   rL   	n_resultswhere )r3   r5   r>   listembed_documentsr<   find)r?   rK   rL   rN   rO   kwargsr3   textss           r   __query_clusterzBagel.__query_clusterb   s    	M ##(8(@[%E#77GGNK!t}}!! 
#-	

 
 	
  	MKLL	Ms   A A4c                   |*|D cg c]  }t        t        j                               ! }}t        |      }| j                  r||r| j                  j                  |      }|r)t        |      t        |      z
  }|r	|i g|z  z   }g }g }	t        |      D ]*  \  }
}|r|	j                  |
       |j                  |
       , |	rl|	D 
cg c]  }
||
   	 }}
|	D 
cg c]  }
||
   	 }}
|r|	D 
cg c]  }
||
   	 c}
nd}|	D 
cg c]  }
||
   	 }}
| j                  j                  ||||       |rY|D cg c]  }||   	 }}|r|D cg c]  }||   	 c}nd}|D cg c]  }||   	 }}| j                  j                  |||       |S i gt        |      z  }| j                  j                  ||||       |S c c}w c c}
w c c}
w c c}
w c c}
w c c}w c c}w c c}w )a  
        Add texts along with their corresponding embeddings and optional
        metadata to the Bagel cluster.

        Args:
            texts (Iterable[str]): Texts to be added.
            embeddings (Optional[List[float]]): List of embeddingvectors
            metadatas (Optional[List[dict]]): Optional list of metadatas.
            ids (Optional[List[str]]): List of unique ID for the texts.

        Returns:
            List[str]: List of unique ID representing the added texts.
        N)rI   r    r   ids)rI   r   rX   )rI   r   r    rX   )r+   uuiduuid4rQ   r>   rR   len	enumerateappendr<   upsert)r?   rU   r    rX   rI   rT   r   length_diff	empty_idsnon_empty_idsidxr$   texts_with_metadatasembeddings_with_metadatasids_with_metadatajtexts_without_metadatasembeddings_without_metadatasids_without_metadatass                      r   	add_textszBagel.add_texts~   s%   , ;.343tzz|$4C4U##
(:u11AA%HJe*s9~5K%{(::	IM!*9!5 *X!((-$$S)	*
 7DEYs^E	E>K'Lsc
'L$'LBL>Z_>RV * :G$G#SX$G!$G$$8'2)	 %  =F*G58*G'*G:DI6qZ]6$ - :C(CAQ(C%(C$$;5- %  
 s5z)IMM  %#	 !  
a 5" F'L>$G +H6(Cs/   $F?
GG	0GG8GG!G"c                `    | j                  |||      }|D cg c]  \  }}|	 c}}S c c}}w )a  
        Run a similarity search with Bagel.

        Args:
            query (str): The query text to search for similar documents/texts.
            k (int): The number of results to return.
            where (Optional[Dict[str, str]]): Metadata filters to narrow down.

        Returns:
            List[Document]: List of documents objects representing
            the documents most similar to the query text.
        )rO   )similarity_search_with_score)r?   querykrO   rT   docs_and_scoresr   r   s           r   similarity_searchzBagel.similarity_search   s3    & ;;E1E;R"12Q222s   *c                B    | j                  |g||      }t        |      S )a  
        Run a similarity search with Bagel and return documents with their
        corresponding similarity scores.

        Args:
            query (str): The query text to search for similar documents.
            k (int): The number of results to return.
            where (Optional[Dict[str, str]]): Filter using metadata.

        Returns:
            List[Tuple[Document, float]]: List of tuples, each containing a
            Document object representing a similar document and its
            corresponding similarity score.

        )rK   rN   rO   _Bagel__query_clusterr   )r?   rm   rn   rO   rT   r   s         r   rl   z"Bagel.similarity_search_with_score   s(    , &&E7au&U*733rJ   c
           	     L     | d|||||d|
}|j                  ||	||      }|S )a  
        Create and initialize a Bagel instance from list of texts.

        Args:
            texts (List[str]): List of text content to be added.
            cluster_name (str): The name of the Bagel cluster.
            client_settings (Optional[bagel.config.Settings]): Client settings.
            cluster_metadata (Optional[Dict]): Metadata of the cluster.
            embeddings (Optional[Embeddings]): List of embedding.
            metadatas (Optional[List[dict]]): List of metadata.
            ids (Optional[List[str]]): List of unique ID. Defaults to None.
            client (Optional[bagel.Client]): Bagel client instance.

        Returns:
            Bagel: Bagel vectorstore.
        )r@   rB   rA   rD   rC   )rU   rI   r    rX   rP   )rj   )clsrU   	embeddingr    rX   r@   rA   rC   rD   text_embeddingsrT   bagel_clusterr   s                r   
from_textszBagel.from_texts   sT    <  
%(+-
 
 ##Oyc $ 
 rJ   c                b    | j                   j                  | j                  j                         y)zDelete the cluster.N)r7   delete_clusterr<   r2   rH   s    r   r{   zBagel.delete_cluster!  s    ##DMM$6$67rJ   c                @    | j                  |||      }t        |      S )zT
        Return docs most similar to embedding vector and similarity score.
        rL   rN   rO   rr   )r?   rL   rn   rO   rT   r   s         r   1similarity_search_by_vector_with_relevance_scoresz7Bagel.similarity_search_by_vector_with_relevance_scores%  s-     &&-% ' 
 +733rJ   c                @    | j                  |||      }t        |      S )z-Return docs most similar to embedding vector.r}   )rs   r   )r?   rv   rn   rO   rT   r   s         r   similarity_search_by_vectorz!Bagel.similarity_search_by_vector4  s-     &&&!5 ' 
  ((rJ   c                    | j                   r| j                   S d}d}| j                  j                  }|r	||v r||   }|dk(  r| j                  S |dk(  r| j                  S |dk(  r| j
                  S t        d| d      )z
        Select and return the appropriate relevance score function based
        on the distance metric used in the Bagel cluster.
        l2z
hnsw:spacecosineipzANo supported normalization function for distance metric of type: z=. Consider providing relevance_score_fn to Bagel constructor.)r=   r<   r$   _cosine_relevance_score_fn_euclidean_relevance_score_fn%_max_inner_product_relevance_score_fn
ValueError)r?   distancedistance_keyr$   s       r   _select_relevance_score_fnz Bagel._select_relevance_score_fnA  s    
 ++333#==))0-Hx222555===$$,: .<< rJ   c                    |D 	cg c]  }	|	j                    }
}	|D 	cg c]  }	|	j                   }}	 | j                  d|
|||||||d|S c c}	w c c}	w )a  
        Create a Bagel vectorstore from a list of documents.

        Args:
            documents (List[Document]): List of Document objects to add to the
                                        Bagel vectorstore.
            embedding (Optional[List[float]]): List of embedding.
            ids (Optional[List[str]]): List of IDs. Defaults to None.
            cluster_name (str): The name of the Bagel cluster.
            client_settings (Optional[bagel.config.Settings]): Client settings.
            client (Optional[bagel.Client]): Bagel client instance.
            cluster_metadata (Optional[Dict]): Metadata associated with the
                                               Bagel cluster. Defaults to None.

        Returns:
            Bagel: Bagel vectorstore.
        )rU   rv   r    rX   r@   rA   rD   rC   rP   )r#   r$   ry   )ru   r   rv   rX   r@   rA   rD   rC   rT   r   rU   r    s               r   from_documentszBagel.from_documents]  sv    : .77c!!77-67cS\\7	7s~~ 

%+-

 

 
	
 87s
   AAc                v    |j                   }|j                  }| j                  j                  |g|g|g       y)zUpdate a document in the cluster.

        Args:
            document_id (str): ID of the document to update.
            document (Document): Document to update.
        )rX   r   r    N)r#   r$   r<   update)r?   document_iddocumenttextr$   s        r   update_documentzBagel.update_document  s@     $$$$fj 	 	
rJ   c                X    |||||d}|||d<    | j                   j                  di |S )zGets the collection.)rX   rO   limitoffsetwhere_documentincluderP   )r<   get)r?   rX   rO   r   r   r   r   rT   s           r   r   z	Bagel.get  sF     ,
  'F9 t}}  *6**rJ   c                <    | j                   j                  |       y)zW
        Delete by IDs.

        Args:
            ids: List of ids to delete.
        )rX   N)r<   delete)r?   rX   rT   s      r   r   zBagel.delete  s     	%rJ   )r@   r+   rA   Optional[bagel.config.Settings]rB   Optional[Embeddings]rC   Optional[Dict]rD   Optional[bagel.Client]rE   z"Optional[Callable[[float], float]]returnNone)r   r   )NN   N)rK   Optional[List[str]]rL   Optional[List[List[float]]]rN   intrO   Optional[Dict[str, str]]rT   r   r   List[Document])NNN)rU   zIterable[str]r    Optional[List[dict]]rX   r   rI   r   rT   r   r   	List[str])
rm   r+   rn   r   rO   r   rT   r   r   r   )
rm   r+   rn   r   rO   r   rT   r   r   List[Tuple[Document, float]])ru   Type[Bagel]rU   r   rv   r   r    r   rX   r   r@   r+   rA   r   rC   r   rD   r   rw   r   rT   r   r   r)   )r   r   )
rL   List[float]rn   r   rO   r   rT   r   r   r   )
rv   r   rn   r   rO   r   rT   r   r   r   )r   zCallable[[float], float])ru   r   r   r   rv   r   rX   r   r@   r+   rA   r   rD   r   rC   r   rT   r   r   r)   )r   r+   r   r   r   r   )NNNNNN)rX   zOptional[OneOrMany[ID]]rO   zOptional[Where]r   Optional[int]r   r   r   zOptional[WhereDocument]r   r   r   zDict[str, Any]r   )rX   r   rT   r   r   r   )__name__
__module____qualname____doc__r,   __annotations__rF   propertyrI   r   rs   rj   	DEFAULT_Krp   rl   classmethodry   r{   r~   r   r   r   r   r   r   rP   rJ   r   r)   r)   ,   s   	 ,7#S6 <;?37+/)-AE"6"6 9"6 1	"6
 )"6 '"6 ?"6 
"6H ( ( 12 ,08<*.
(
 6
 	

 (
 
 

 3
< +/#'26GG (G !	G
 0G G 
GX *.	33 3 (	3
 3 
32 *.	44 4 (	4
 4 
&42  +/*.#';;?+/)-7;((( (( (	(
 !( ( 9( )( '( 5( ( 
( (T8 *.	4%4 4 (	4
 4 
&4$ *.	)) ) (	)
 ) 
)8  +/#';;?)-+/(
(
!(
 ((
 !	(

 (
 9(
 '(
 )(
 (
 
(
 (
T
" (,!%# $26'++$+ + 	+
 + 0+ %+ 
+.&rJ   r)   )r   r   r   r   )r   r   r   r   ) 
__future__r   rY   typingr   r   r   r   r   r	   r
   r   r   r3   r4   bagel.api.typesr   r   r   r   langchain_core.documentsr   langchain_core.embeddingsr   langchain_core.utilsr   langchain_core.vectorstoresr   r   r   r   r)   rP   rJ   r   <module>r      sU    " 
 
 
 CC - 0 ) 3	DI&K I&rJ   