
    3fiQ6                        d dl mZ d dlZd dlZd dl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  ej,                  e      Z G d	 d
e      Zy)    )annotationsN)AnyIterableListOptionalTuple)Document)
Embeddings)get_from_env)VectorStore)maximal_marginal_relevancec                     e Zd ZdZ	 	 	 	 	 	 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
	 	 	 d	 	 	 	 	 	 	 	 	 	 	 dd
Z	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZe	 	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Zy)
DashVectorad  `DashVector` vector store.

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

    Example:
        .. code-block:: python

            from langchain_community.vectorstores import DashVector
            from langchain_community.embeddings.openai import OpenAIEmbeddings
            import dashvector

            client = dashvector.Client(api_key="***")
            client.create("langchain", dimension=1024)
            collection = client.get("langchain")
            embeddings = OpenAIEmbeddings()
            vectorstore = DashVector(collection, embeddings.embed_query, "text")
    c                    	 ddl }t        ||j                        st	        dt        |             || _        || _        || _        y# t        $ r t        d      w xY w)z&Initialize with DashVector collection.r   N\Could not import dashvector python package. Please install it with `pip install dashvector`.zCcollection should be an instance of dashvector.Collection, bug got )	
dashvectorImportError
isinstance
Collection
ValueErrortype_collection
_embedding_text_field)self
collection	embedding
text_fieldr   s        i/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_community/vectorstores/dashvector.py__init__zDashVector.__init__+   s{    	 *j&;&;<
+,. 
 &#%  	C 	s   A	 	Ac                :    | j                   j                  |       y)z)Create a Partition in current Collection.N)r   create_partition)r   	partitions     r   _create_partition_if_not_existsz*DashVector._create_partition_if_not_existsE   s    )))4    Nc                @   | j                   j                  ||||      }|s"t        d| j                   j                         g }|D ]S  }|j                  }|j                  | j                        }	|j                  }
|j                  t        |	|      |
f       U |S )z;Return docs most similar to query vector, along with scores)topkfilterr#   $Fail to query docs by vector, error page_contentmetadata)
r   queryr   messagefieldspopr   scoreappendr	   )r   r   kr(   r#   retdocsdocr,   textr1   s              r   '_similarity_search_with_score_by_vectorz2DashVector._similarity_search_with_score_by_vectorI   s     $$Af	 % 
 6t7G7G7O7O6PQ   	QCzzH<< 0 01DIIEKKthGOP		Q
 r%   c                   | j                  |       |xs5 |D cg c])  }t        t        j                         j                        + c}}t        |      }t        dt        |      |      D ]  }	t        |	|z   t        |            }
||	|
 }||	|
 }| j                  j                  t        |            }|r||	|
 }nt        |	|
      D cg c]  }i  }}t        ||      D ]  \  }}||| j                  <    t        t        |||            }| j                  j                  ||      }|rt        d|j                           |S c c}w c c}w )a2  Run more texts through the embeddings and add to the vectorstore.

        Args:
            texts: Iterable of strings to add to the vectorstore.
            metadatas: Optional list of metadatas associated with the texts.
            ids: Optional list of ids associated with the texts.
            batch_size: Optional batch size to upsert docs.
            partition: a partition name in collection. [optional].
            kwargs: vectorstore specific parameters

        Returns:
            List of ids from adding the texts into the vectorstore.
        r   r#   z9Fail to upsert docs to dashvector vector database,Error: )r$   struuiduuid4hexlistrangelenminr   embed_documentszipr   r   upsertr   r.   )r   texts	metadatasids
batch_sizer#   kwargs_	text_listiendbatch_texts	batch_idsbatch_embeddingsbatch_metadatasr,   r7   r5   r4   s                      r   	add_textszDashVector.add_textsc   s[   , 	,,Y7;U;c$**,**+;K	q#i.*5 	Aa*nc)n5C#Ac*KAc
I#>>tK?PQ "+Ac"2/4Q}"=!2"="="%o{"C 2$-1))*2 I'7IJD""))$))DC !kk], )	0 
5 < #>s   .E
	Ec                N    t        | j                  j                  ||            S )zDelete by vector ID.

        Args:
            ids: List of ids to delete.
            partition: a partition name in collection. [optional].

        Returns:
            True if deletion is successful,
            False otherwise.
        r:   )boolr   delete)r   rH   r#   rJ   s       r   rV   zDashVector.delete   s%     D$$++C9+EFFr%   c                `    | j                  ||||      }|D cg c]  \  }}|	 c}}S c c}}w )a  Return docs most similar to query.

        Args:
            query: Text to search documents similar to.
            k: Number of documents to return. Default to 4.
            filter: Doc fields filter conditions that meet the SQL where clause
                    specification.
            partition: a partition name in collection. [optional].

        Returns:
            List of Documents most similar to the query text.
        )'similarity_search_with_relevance_scores)	r   r-   r3   r(   r#   rJ   docs_and_scoresr6   rK   s	            r   similarity_searchzDashVector.similarity_search   s7    * FF1fi
 #22Q222   *c                b    | j                   j                  |      }| j                  ||||      S )a  Return docs most similar to query text , alone with relevance scores.

        Less is more similar, more is more dissimilar.

        Args:
            query: input text
            k: Number of Documents to return. Defaults to 4.
            filter: Doc fields filter conditions that meet the SQL where clause
                    specification.
            partition: a partition name in collection. [optional].

        Returns:
            List of Tuples of (doc, similarity_score)
        )r3   r(   r#   )r   embed_queryr8   )r   r-   r3   r(   r#   rJ   r   s          r   rX   z2DashVector.similarity_search_with_relevance_scores   s9    . OO//6	;;6Y < 
 	
r%   c                `    | j                  ||||      }|D cg c]  \  }}|	 c}}S c c}}w )a  Return docs most similar to embedding vector.

        Args:
            embedding: Embedding to look up documents similar to.
            k: Number of Documents to return. Defaults to 4.
            filter: Doc fields filter conditions that meet the SQL where clause
                    specification.
            partition: a partition name in collection. [optional].

        Returns:
            List of Documents most similar to the query vector.
        )r8   )	r   r   r3   r(   r#   rJ   rY   r6   rK   s	            r   similarity_search_by_vectorz&DashVector.similarity_search_by_vector   s7    ( FFq&)
 #22Q222r[   c                d    | j                   j                  |      }| j                  ||||||      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.
            filter: Doc fields filter conditions that meet the SQL where clause
                    specification.
            partition: a partition name in collection. [optional].

        Returns:
            List of Documents selected by maximal marginal relevance.
        )r   r]   'max_marginal_relevance_search_by_vector)	r   r-   r3   fetch_klambda_multr(   r#   rJ   r   s	            r   max_marginal_relevance_searchz(DashVector.max_marginal_relevance_search   s8    < OO//6	;;q';	
 	
r%   c                   | j                   j                  ||||d      }|s"t        d| j                   j                         |D 	cg c]  }	|	j                   }
}	t        t        j                  |      |
||      }|D cg c]  }|j                  |   j                   }}|D cg c](  }t        |j                  | j                        |      * c}S c c}	w c c}w c c}w )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.
            filter: Doc fields filter conditions that meet the SQL where clause
                    specification.
            partition: a partition name in collection. [optional].

        Returns:
            List of Documents selected by maximal marginal relevance.
        T)r'   r(   r#   include_vectorr)   r*   )r   r-   r   r.   vectorr   nparrayoutputr/   r	   r0   r   )r   r   r3   rb   rc   r(   r#   rJ   r4   r6   candidate_embeddingsmmr_selectedrM   rG   r,   s                  r   ra   z2DashVector.max_marginal_relevance_search_by_vector  s    @ $$ % 
 6t7G7G7O7O6PQ  7::s

::1HHY!5{A
 4@@aSZZ]))@	@ &
 (,,t/?/?"@8T
 	
  ;
 A
s   	C C)-C#c
                   	 ddl }|xs t        dd      }|xs t        ddd	      }|j                  ||
      }|j	                  |       |j                  |      }|s]t        |j                  |d               }|j                  ||      }|r|j                  |      }nt        d|j                   d       | |||      }|j                  |||	|       |S # t        $ r t        d      w xY w)aT  Return DashVector VectorStore initialized from texts and embeddings.

        This is the quick way to get started with dashvector vector store.

        Example:
            .. code-block:: python

            from langchain_community.vectorstores import DashVector
            from langchain_community.embeddings import OpenAIEmbeddings
            import dashvector

            embeddings = OpenAIEmbeddings()
            dashvector = DashVector.from_documents(
                docs,
                embeddings,
                dashvector_api_key="{DASHVECTOR_API_KEY}"
            )
        r   Nr   dashvector_api_keyDASHVECTOR_API_KEYdashvector_endpointDASHVECTOR_ENDPOINTz#dashvector.cn-hangzhou.aliyuncs.com)default)api_keyendpoint)	dimensionz"Fail to create collection. Error: .)r   r   r   ClientrV   getrA   r]   creater   r.   rS   )clsrF   r   rG   rn   rp   collection_namer   rI   rH   rJ   r   dashvector_clientr   dimrespdashvector_vector_dbs                    r   
from_textszDashVector.from_textsN  s+   @	 0 
< "64
 2 
\!!96

 '--&1D . 
 	  1&**?;
i++E!H56C$++Os+KD.22?C
 #Edll^ST!UVV":y*E&&uijI##?  	C 	s   C C1)r   r   r   r
   r   r;   )r#   r;   returnNone)   Nrr   )
r   List[float]r3   intr(   Optional[str]r#   r;   r   List[Tuple[Document, float]])NN   rr   )rF   zIterable[str]rG   Optional[List[dict]]rH   Optional[List[str]]rI   r   r#   r;   rJ   r   r   	List[str])Nrr   )rH   r   r#   r;   rJ   r   r   rU   )r-   r;   r3   r   r(   r   r#   r;   rJ   r   r   List[Document])r-   r;   r3   r   r(   r   r#   r;   rJ   r   r   r   )r   r   r3   r   r(   r   r#   r;   rJ   r   r   r   )r      g      ?Nrr   )r-   r;   r3   r   rb   r   rc   floatr(   Optional[dict]r#   r;   rJ   r   r   r   )r   r   r3   r   rb   r   rc   r   r(   r   r#   r;   rJ   r   r   r   )NNN	langchainr7   r   N)rF   r   r   r
   rG   r   rn   r   rp   r   r{   r;   r   r;   rI   r   rH   r   rJ   r   r   r   )__name__
__module____qualname____doc__r    r$   r8   rS   rV   rZ   rX   r_   rd   ra   classmethodr    r%   r   r   r      sB   $&& & 	&45  $"  	
  
&: +/#'"11 (1 !	1
 1 1 1 
1h AJG&G:=GUXG	G$  $"33 3 	3
 3 3 
3:  $"

 
 	

 
 
 
&
>  $"33 3 	3
 3 3 
38  !%"!
!
 !
 	!

 !
 !
 !
 !
 
!
L  !%"5
5
 5
 	5

 5
 5
 5
 5
 
5
n 
 +/,0-1* #'@$@$ @$ (	@$
 *@$ +@$ @$ @$ @$ !@$ @$ 
@$ @$r%   r   )
__future__r   loggingr<   typingr   r   r   r   r   numpyrh   langchain_core.documentsr	   langchain_core.embeddingsr
   langchain_core.utilsr   langchain_core.vectorstoresr   &langchain_community.vectorstores.utilsr   	getLoggerr   loggerr   r   r%   r   <module>r      sL    "     - 0 - 3 M			8	$w$ w$r%   