
    NgE                    >   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mZ d dlZd dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZmZ erd dl m!Z!  ej"        e#          Z$ddZ%ddZ& eddd           G d de                      Z'dS )    )annotationsN)TYPE_CHECKINGAnyCallableIterableListOptionalTupleUnion)
deprecated)Document)
Embeddings)batch_iterate)VectorStore)version)DistanceStrategymaximal_marginal_relevance)Indexreturnr   c                 V    	 dd l } n"# t          $ r}t          d          |d }~ww xY w| S )Nr   z_Could not import pinecone python package. Please install it with `pip install pinecone-client`.)pineconeImportError)r   es     e/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/vectorstores/pinecone.py_import_pineconer      sU       D
 
 	
 Os    
&!&boolc                     t                      } | j        }t          j        |          t          j        d          k    S )Nz	3.0.0.dev)r   __version__r   parse)r   pinecone_client_versions     r   _is_pinecone_v3r!   '   s6    !!H&2=011W];5O5OOO    z0.0.18z1.0zlangchain_pinecone.Pinecone)sinceremovalalternative_importc                  Z   e Zd ZdZdej        fdGdZedHd            ZdIdZ	dJdZ
	 	 	 	 	 dKdLd#Z	 	 	 dMdNd*Zd$ddd+dOd,Z	 	 	 dMdPd.ZdQd0ZedRd3            Z	 	 	 	 	 dSdTd8Z	 	 	 	 	 dSdUd9Ze	 dVdWd=            Ze	 	 	 	 	 	 	 	 	 dXdYdA            Ze	 	 	 dZd[dB            Z	 	 	 	 d\d]dFZdS )^Pineconez`Pinecone` vector store.

    To use, you should have the ``pinecone-client`` python package installed.

    This version of Pinecone is deprecated. Please use `langchain_pinecone.Pinecone`
    instead.
    Nindexr   	embeddingUnion[Embeddings, Callable]text_keystr	namespaceOptional[str]distance_strategyOptional[DistanceStrategy]c                "   t                      }t          |t                    st          j        d           t          ||j                  st          dt          |                     || _        || _	        || _
        || _        || _        dS )z Initialize with Pinecone client.z`Passing in `embedding` as a Callable is deprecated. Please pass in an Embeddings object instead.z4client should be an instance of pinecone.Index, got N)r   
isinstancer   warningswarnr   
ValueErrortype_index
_embedding	_text_key
_namespacer/   )selfr(   r)   r+   r-   r/   r   s          r   __init__zPinecone.__init__9   s     $%%)Z00 	M.   %00 	X4PU;;XX   #!#!2r"   r   Optional[Embeddings]c                H    t          | j        t                    r| j        S dS )z/Access the query embedding object if available.N)r2   r8   r   r;   s    r   
embeddingszPinecone.embeddingsR   s%     doz22 	#?"tr"   textsIterable[str]List[List[float]]c                     t           j        t                    r' j                            t	          |                    S  fd|D             S )zEmbed search docs.c                :    g | ]}                     |          S  )r8   ).0tr;   s     r   
<listcomp>z-Pinecone._embed_documents.<locals>.<listcomp>]   s%    222q""222r"   )r2   r8   r   embed_documentslist)r;   rA   s   ` r   _embed_documentszPinecone._embed_documentsY   sN    doz22 	@?224;;???2222E2222r"   textList[float]c                    t          | j        t                    r| j                            |          S |                     |          S )zEmbed query text.)r2   r8   r   embed_query)r;   rM   s     r   _embed_queryzPinecone._embed_query_   s>    doz22 	5?..t444t$$$r"         	metadatasOptional[List[dict]]idsOptional[List[str]]
batch_sizeintembedding_chunk_sizekwargs	List[str]c           
          j         t          |          }|pd |D             }|pd |D             }t          ||          D ]\  }}	|	| j        <   t	          dt          |          |          D ]w}
||
|
|z            }||
|
|z            }||
|
|z            }                     |          } fdt          |t          |||                    D             }d |D              x|S )a  Run more texts through the embeddings and add to the vectorstore.

        Upsert optimization is done by chunking the embeddings and upserting them.
        This is done to avoid memory issues and optimize using HTTP based embeddings.
        For OpenAI embeddings, use pool_threads>4 when constructing the pinecone.Index,
        embedding_chunk_size>1000 and batch_size~64 for best performance.
        Args:
            texts: Iterable of strings to add to the vectorstore.
            metadatas: Optional list of metadatas associated with the texts.
            ids: Optional list of ids to associate with the texts.
            namespace: Optional pinecone namespace to add the texts to.
            batch_size: Batch size to use when adding the texts to the vectorstore.
            embedding_chunk_size: Chunk size to use when embedding the texts.

        Returns:
            List of ids from adding the texts into the vectorstore.

        Nc                N    g | ]"}t          t          j                              #S rF   )r,   uuiduuid4rG   _s     r   rI   z&Pinecone.add_texts.<locals>.<listcomp>   s&    777Ac$*,,''777r"   c                    g | ]}i S rF   rF   ra   s     r   rI   z&Pinecone.add_texts.<locals>.<listcomp>   s    !4!4!4"!4!4!4r"   r   c           	     <    g | ]} j         j        d|d dS )T)vectorsr-   	async_reqrF   )r7   upsert)rG   batchr[   r-   r;   s     r   rI   z&Pinecone.add_texts.<locals>.<listcomp>   sY     
 
 
  #" !'"  	 
 
 
r"   c                6    g | ]}|                                 S rF   )get)rG   ress     r   rI   z&Pinecone.add_texts.<locals>.<listcomp>   s     ,,,3SWWYY,,,r"   )r:   rK   zipr9   rangelenrL   r   )r;   rA   rT   rV   r-   rX   rZ   r[   metadatarM   ichunk_texts	chunk_idschunk_metadatasr@   	async_ress   `   `  `        r   	add_textszPinecone.add_textse   s`   8 IU7777774!4!4e!4!4!4	!)U33 	, 	,NHd'+HT^$$
 q#e**&:;; 	- 	-AA(<$< <=KA$8 889I'A0D,D(DEO..{;;J
 
 
 
 
 
 +Iz? K K 
 
 
I -,),,,,,
r"      querykfilterOptional[dict]List[Tuple[Document, float]]c                Z    |                      |                     |          |||          S )a  Return pinecone documents most similar to query, along with scores.

        Args:
            query: Text to look up documents similar to.
            k: Number of Documents to return. Defaults to 4.
            filter: Dictionary of argument(s) to filter on metadata
            namespace: Namespace to search in. Default will search in '' namespace.

        Returns:
            List of Documents most similar to the query and score for each
        rx   ry   r-   )&similarity_search_by_vector_with_scorerQ   )r;   rw   rx   ry   r-   s        r   similarity_search_with_scorez%Pinecone.similarity_search_with_score   s8    $ ::e$$&I ; 
 
 	
r"   r}   c               l   || j         }g }| j                            |g|d||          }|d         D ]}|d         }| j        |v rI|                    | j                  }	|d         }
|                    t          |	|          |
f           \t                              d| j         d	           |S )
zGReturn pinecone documents most similar to embedding, along with scores.NT)vectortop_kinclude_metadatar-   ry   matchesro   scorepage_contentro   zFound document with no `z` key. Skipping.)	r:   r7   rw   r9   popappendr   loggerwarning)r;   r)   rx   ry   r-   docsresultsrk   ro   rM   r   s              r   r~   z/Pinecone.similarity_search_by_vector_with_score   s     I+##;! $ 
 
 9% 		 		C:H~))||DN33GX4(KKKUSTTTTOt~OOO    r"   List[Document]c                >     | j         |f|||d|}d |D             S )a  Return pinecone documents most similar to query.

        Args:
            query: Text to look up documents similar to.
            k: Number of Documents to return. Defaults to 4.
            filter: Dictionary of argument(s) to filter on metadata
            namespace: Namespace to search in. Default will search in '' namespace.

        Returns:
            List of Documents most similar to the query and score for each
        r}   c                    g | ]\  }}|S rF   rF   )rG   docrb   s      r   rI   z.Pinecone.similarity_search.<locals>.<listcomp>   s    222Q222r"   )r   )r;   rw   rx   ry   r-   r[   docs_and_scoress          r   similarity_searchzPinecone.similarity_search   sJ    & <$;
v
 
>D
 
 32/2222r"   Callable[[float], float]c                    | j         t          j        k    r| j        S | j         t          j        k    r| j        S | j         t          j        k    r| j        S t          d          )a8  
        The 'correct' relevance function
        may differ depending on a few things, including:
        - the distance / similarity metric used by the VectorStore
        - the scale of your embeddings (OpenAI's are unit normed. Many others are not!)
        - embedding dimensionality
        - etc.
        zXUnknown distance strategy, must be cosine, max_inner_product (dot product), or euclidean)	r/   r   COSINE_cosine_relevance_score_fnMAX_INNER_PRODUCT%_max_inner_product_relevance_score_fnEUCLIDEAN_DISTANCE_euclidean_relevance_score_fnr5   r?   s    r   _select_relevance_score_fnz#Pinecone._select_relevance_score_fn   sl     !%5%<<<22#'7'III==#'7'JJJ55.  r"   r   floatc                    | dz   dz  S )z8Pinecone returns cosine similarity scores between [-1,1]      rF   )r   s    r   r   z#Pinecone._cosine_relevance_score_fn  s     	Qr"            ?fetch_klambda_multc                   
 | j         } j                            |g|dd||          
t          t	          j        |gt          j                  d 
d         D             ||          }
fd|D             }	 fd	|	D             S )
a  Return docs selected using the maximal marginal relevance.

        Maximal marginal relevance optimizes for similarity to query AND diversity
        among selected documents.

        Args:
            embedding: Embedding to look up documents similar to.
            k: Number of Documents to return. Defaults to 4.
            fetch_k: Number of Documents to fetch to pass to MMR algorithm.
            lambda_mult: Number between 0 and 1 that determines the degree
                        of diversity among the results with 0 corresponding
                        to maximum diversity and 1 to minimum diversity.
                        Defaults to 0.5.
        Returns:
            List of Documents selected by maximal marginal relevance.
        NT)r   r   include_valuesr   r-   ry   )dtypec                    g | ]
}|d          S )valuesrF   )rG   items     r   rI   zDPinecone.max_marginal_relevance_search_by_vector.<locals>.<listcomp>0  s    ;;;T(^;;;r"   r   )rx   r   c                8    g | ]}d          |         d         S )r   ro   rF   )rG   rp   r   s     r   rI   zDPinecone.max_marginal_relevance_search_by_vector.<locals>.<listcomp>4  s(    LLL!GI&q)*5LLLr"   c                b    g | ]+}t          |                    j                  |           ,S )r   )r   r   r9   )rG   ro   r;   s     r   rI   zDPinecone.max_marginal_relevance_search_by_vector.<locals>.<listcomp>5  sD     
 
 
 (,,"@"@8TTT
 
 
r"   )r:   r7   rw   r   nparrayfloat32)r;   r)   rx   r   r   ry   r-   r[   mmr_selectedselectedr   s   `         @r   'max_marginal_relevance_search_by_vectorz0Pinecone.max_marginal_relevance_search_by_vector
  s    4 I+##;! $ 
 
 2Hi[
333;;	(:;;;#	
 
 
 MLLL|LLL
 
 
 
$
 
 
 	
r"   c                `    |                      |          }|                     ||||||          S )a  Return docs selected using the maximal marginal relevance.

        Maximal marginal relevance optimizes for similarity to query AND diversity
        among selected documents.

        Args:
            query: Text to look up documents similar to.
            k: Number of Documents to return. Defaults to 4.
            fetch_k: Number of Documents to fetch to pass to MMR algorithm.
            lambda_mult: Number between 0 and 1 that determines the degree
                        of diversity among the results with 0 corresponding
                        to maximum diversity and 1 to minimum diversity.
                        Defaults to 0.5.
        Returns:
            List of Documents selected by maximal marginal relevance.
        )rQ   r   )	r;   rw   rx   r   r   ry   r-   r[   r)   s	            r   max_marginal_relevance_searchz&Pinecone.max_marginal_relevance_search:  s<    4 %%e,,	;;q';	
 
 	
r"   
index_namepool_threadsr   c                8   t                      }t                      r`|                    t          j                            d          |          }|                                }d |j        d         D             }n|                                }||v r;t                      r|                    |          n|                    ||          }nJt          |          dk    rt          d          t          d| d	d
                    |                     |S )zReturn a Pinecone Index instance.

        Args:
            index_name: Name of the index to use.
            pool_threads: Number of threads to use for index upsert.
        Returns:
            Pinecone Index instance.PINECONE_API_KEY)api_keyr   c                    g | ]	}|j         
S rF   )name)rG   rp   s     r   rI   z/Pinecone.get_pinecone_index.<locals>.<listcomp>n  s    IIIa16IIIr"   indexes)r   r   zNo active indexes found in your Pinecone project, are you sure you're using the right Pinecone API key and Environment? Please double check your Pinecone dashboard.zIndex 'zQ' not found in your Pinecone project. Did you mean one of the following indexes: z, )r   r!   r'   osenvironrj   list_indexes
index_listr   rn   r5   join)clsr   r   r   pinecone_instancer   index_namesr(   s           r   get_pinecone_indexzPinecone.get_pinecone_indexY  sM    $%% 	2 ( 1 1
'9:: !2 ! ! (4466GII7+=i+HIIIKK"//11K$$ #$$K!''
333^^J\^JJ E
 ""?   W* W W>Bii>T>TW W   r"   r   upsert_kwargsembeddings_chunk_sizec           	     v    |                      ||
          } | ||||fi |} |j        |f|||||d|	pi  |S )a  
        DEPRECATED: use langchain_pinecone.PineconeVectorStore.from_texts instead:
        Construct Pinecone wrapper from raw documents.

        This is a user friendly interface that:
            1. Embeds documents.
            2. Adds the documents to a provided Pinecone index

        This is intended to be a quick way to get started.

        The `pool_threads` affects the speed of the upsert operations.

        Example:
            .. code-block:: python

                from langchain_pinecone import PineconeVectorStore
                from langchain_openai import OpenAIEmbeddings

                embeddings = OpenAIEmbeddings()
                index_name = "my-index"
                namespace = "my-namespace"
                vectorstore = Pinecone(
                    index_name=index_name,
                    embedding=embedding,
                    namespace=namespace,
                )
        )rT   rV   r-   rX   rZ   )r   ru   )r   rA   r)   rT   rV   rX   r+   r-   r   r   r   r   r[   pinecone_indexr   s                  r   
from_textszPinecone.from_texts  s    V //
LII3~y(IPPPP	
!!6	
 	
 "	
 	
 	
 r"   c                J    |                      ||          } | ||||          S )z*Load pinecone vectorstore from index name.)r   )r   r   r)   r+   r-   r   r   s          r   from_existing_indexzPinecone.from_existing_index  s/     //
LIIs>9h	BBBr"   
delete_allOptional[bool]Nonec                &   || j         }|r | j        j        d	d|d| no|Fd}t          dt	          |          |          D ]$}||||z            } | j        j        d	||d| %n'| | j        j        d	||d| nt          d          dS )
zDelete by vector IDs or filter.
        Args:
            ids: List of ids to delete.
            filter: Dictionary of conditions to filter vectors to delete.
        NT)r   r-   rS   r   )rV   r-   )ry   r-   z3Either ids, delete_all, or filter must be provided.rF   )r:   r7   deleterm   rn   r5   )	r;   rV   r   r-   ry   r[   
chunk_sizerp   chunks	            r   r   zPinecone.delete  s     I 
	TDKN$)NNvNNNN_J1c#hh
33 M MAJ./""Lu	LLVLLLLM DKLf	LLVLLLLRSSStr"   )
r(   r   r)   r*   r+   r,   r-   r.   r/   r0   )r   r=   )rA   rB   r   rC   )rM   r,   r   rN   )NNNrR   rS   )rA   rB   rT   rU   rV   rW   r-   r.   rX   rY   rZ   rY   r[   r   r   r\   )rv   NN)
rw   r,   rx   rY   ry   rz   r-   r.   r   r{   )
r)   rN   rx   rY   ry   rz   r-   r.   r   r{   )rw   r,   rx   rY   ry   rz   r-   r.   r[   r   r   r   )r   r   )r   r   r   r   )rv   r   r   NN)r)   rN   rx   rY   r   rY   r   r   ry   rz   r-   r.   r[   r   r   r   )rw   r,   rx   rY   r   rY   r   r   ry   rz   r-   r.   r[   r   r   r   )rv   )r   r.   r   rY   r   r   )	NNrR   rM   NNNrv   rS   )rA   r\   r)   r   rT   rU   rV   rW   rX   rY   r+   r,   r-   r.   r   r.   r   rz   r   rY   r   rY   r[   r   r   r'   )rM   Nrv   )r   r,   r)   r   r+   r,   r-   r.   r   rY   r   r'   )NNNN)rV   rW   r   r   r-   r.   ry   rz   r[   r   r   r   )__name__
__module____qualname____doc__r   r   r<   propertyr@   rL   rQ   ru   r   r~   r   r   staticmethodr   r   r   classmethodr   r   r   r   rF   r"   r   r'   r'   -   sz         $(8H8O3 3 3 3 32    X3 3 3 3% % % % +/#'#'$(: : : : :~ !%#'
 
 
 
 
4 !%#'     F !%#'3 3 3 3 30   ,    \  !%#'.
 .
 .
 .
 .
f  !%#'
 
 
 
 
>  ) ) ) ) [)V 
 +/#'#'$((,%)6 6 6 6 [6p 
 #'
C 
C 
C 
C [
C $(%)#'!%      r"   r'   )r   r   )r   r   )(
__future__r   loggingr   r_   r3   typingr   r   r   r   r   r	   r
   r   numpyr   langchain_core._api.deprecationr   langchain_core.documentsr   langchain_core.embeddingsr   langchain_core.utils.iterr   langchain_core.vectorstoresr   	packagingr   &langchain_community.vectorstores.utilsr   r   r   r   	getLoggerr   r   r   r!   r'   rF   r"   r   <module>r      s   " " " " " "  				   W W W W W W W W W W W W W W W W W W W W     6 6 6 6 6 6 - - - - - - 0 0 0 0 0 0 3 3 3 3 3 3 3 3 3 3 3 3             
  		8	$	$   P P P P 
E6S  x x x x x{ x x x x xr"   