
    dhzC                        S SK Jr  S SKrS SKrS SKJrJrJrJrJ	r	J
r
JrJrJrJr  S SKJr  S SKJr  S SKJr  \(       a  S SKr " S S\5      rg)	    )annotationsN)
TYPE_CHECKINGAnyCallableDictIterableListOptionalTupleTypeUnion)Document)
Embeddings)VectorStorec                     \ rS rSrSr   S         SS jjr\SS j5       r S       SS jjr S       SS jjr	 S     SS jjr
 S       SS	 jjr S       SS
 jjr    SS jr    SS jr S     S S jjr S     S!S jjr\ S         S"S jj5       r\          S#                         S$S jj5       rS%S jrS&S jrSrg)'Marqo   aS  `Marqo` vector store.

Marqo indexes have their own models associated with them to generate your
embeddings. This means that you can selected from a range of different models
and also use CLIP models to create multimodal indexes
with images and text together.

Marqo also supports more advanced queries with multiple weighted terms, see See
https://docs.marqo.ai/latest/#searching-using-weights-in-queries.
This class can flexibly take strings or dictionaries for weighted queries
in its similarity search methods.

To use, you should have the `marqo` python package installed, you can do this with
`pip install marqo`.

Example:
    .. code-block:: python

        import marqo
        from langchain_community.vectorstores import Marqo
        client = marqo.Client(url=os.environ["MARQO_URL"], ...)
        vectorstore = Marqo(client, index_name)

Nc                    SSK n[        XR                  5      (       d  [	        S[        U5       35      eXl        X l        Uc  0 OUU l        X@l	        XPl
        S/U l        SU l        g! [         a    [        S5      ef = f)zInitialize with Marqo client.r   NRCould not import marqo python package. Please install it with `pip install marqo`.z2client should be an instance of marqo.Client, got texti   )marqoImportError
isinstanceClient
ValueErrortype_client_index_name_add_documents_settings_searchable_attributespage_content_buildertensor_fields_document_batch_size)selfclient
index_nameadd_documents_settingssearchable_attributesr!   r   s          ^/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/vectorstores/marqo.py__init__Marqo.__init__4   s    	 &,,//DT&\NS  %(0B6L 	$ '<#$8!$X$(!'  	> 	s   A+ +Bc                    g N r$   s    r)   
embeddingsMarqo.embeddingsT   s        c                "   U R                   R                  U R                  5      R                  5       nSU;   a  US   S   (       d  UR	                  S5      (       a  [        S5      e/ nSn[        U5       HS  u  pxUU(       a  [        R                  " X'   5      O[        R                  " 0 5      S.n	UR                  U	5        US-  nMU     / n
[        SX`R                  5       H  nU R                   R                  U R                  5      R                  " XWXpR                  -    4SU R                  0U R                  D6nUS   (       a   S	U S
XpR                  -    S3n[        U5      eXS    Vs/ sH  oS   PM	     sn-  n
M     U
$ s  snf )a%  Upload texts with metadata (properties) to Marqo.

You can either have marqo generate ids for each document or you can provide
your own by including a "_id" field in the metadata objects.

Args:
    texts (Iterable[str]): am iterator of texts - assumed to preserve an
    order that matches the metadatas.
    metadatas (Optional[List[dict]], optional): a list of metadatas.

Raises:
    ValueError: if metadatas is provided and the number of metadatas differs
    from the number of texts.

Returns:
    List[str]: The list of ids that were added.
index_defaults!treat_urls_and_pointers_as_imageszMarqo.add_texts is disabled for multimodal indexes. To add documents with a multimodal index use the Python client for Marqo directly.r   )r   metadata   r"   errorsz.Error in upload for documents in index range [,z], check Marqo logs.items_id)r   indexr   get_settingsgetr   	enumeratejsondumpsappendranger#   add_documentsr"   r   RuntimeError)r$   texts	metadataskwargssettings	documentsnum_docsir   docidsresponseerr_msgitems                 r)   	add_textsMarqo.add_textsX   s   0 <<%%d&6&67DDF()*+NO||?@@T  +-	 'GA8ADJJy|4tzzRT~C S!MH ( q($=$=>A||))$*:*:;IIa";";;<"00 ..H
 !DQCq4445 6() 
 #7++W,=>,=DK,=>>C ?  
 ?s   4Fc                F    U R                  XS9nU R                  U5      nU$ )aC  Search the marqo index for the most similar documents.

Args:
    query (Union[str, Dict[str, float]]): The query for the search, either
    as a string or a weighted query.
    k (int, optional): The number of documents to return. Defaults to 4.

Returns:
    List[Document]: k documents ordered from best to worst match.
queryk)marqo_similarity_search/_construct_documents_from_results_without_score)r$   rV   rW   rH   resultsrJ   s         r)   similarity_searchMarqo.similarity_search   s-      ..U.@HHQ	r2   c                F    U R                  XS9nU R                  U5      nU$ )ap  Return documents from Marqo that are similar to the query as well
as their scores.

Args:
    query (str): The query to search with, either as a string or a weighted
    query.
    k (int, optional): The number of documents to return. Defaults to 4.

Returns:
    List[Tuple[Document, float]]: The matching documents and their scores,
    ordered by descending score.
rU   )rX   ,_construct_documents_from_results_with_score)r$   rV   rW   rZ   scored_documentss        r)   similarity_search_with_score"Marqo.similarity_search_with_score   s.    " ..U.@LLWUr2   c                    U R                  XS9n/ nUS    H%  nU R                  U5      nUR                  U5        M'     U$ )a  Search the marqo index for the most similar documents in bulk with multiple
queries.

Args:
    queries (Iterable[Union[str, Dict[str, float]]]): An iterable of queries to
    execute in bulk, queries in the list can be strings or dictionaries of
    weighted queries.
    k (int, optional): The number of documents to return for each query.
    Defaults to 4.

Returns:
    List[List[Document]]: A list of results for each query.
queriesrW   result)marqo_bulk_similarity_searchrY   rB   r$   rd   rW   rH   bulk_resultsbulk_documentsrZ   rJ   s           r)   bulk_similarity_searchMarqo.bulk_similarity_search   sQ    & 888N/1#H-GLLWUI!!), . r2   c                    U R                  XS9n/ nUS    H%  nU R                  U5      nUR                  U5        M'     U$ )a  Return documents from Marqo that are similar to the query as well as
their scores using a batch of queries.

Args:
    query (Iterable[Union[str, Dict[str, float]]]): An iterable of queries
    to execute in bulk, queries in the list can be strings or dictionaries
    of weighted queries.
    k (int, optional): The number of documents to return. Defaults to 4.

Returns:
    List[Tuple[Document, float]]: A list of lists of the matching
    documents and their scores for each query
rc   re   )rf   r^   rB   rg   s           r)   !bulk_similarity_search_with_score'Marqo.bulk_similarity_search_with_score   sQ    & 888N=?#H-GII'RI!!), . r2   c                    / nUS    Hj  nU R                   c  US   nOU R                  U5      n[        R                  " UR                  SS5      5      nUR	                  [        XES9US   45        Ml     U$ )c  Helper to convert Marqo results into documents.

Args:
    results (List[dict]): A marqo results object with the 'hits'.
    include_scores (bool, optional): Include scores alongside documents.
    Defaults to False.

Returns:
    Union[List[Document], List[Tuple[Document, float]]]: The documents or
    document score pairs if `include_scores` is true.
hitsr   r6   {}page_contentr6   _scorer!   r@   loadsr>   rB   r   r$   rZ   rJ   resr   r6   s         r)   r^   2Marqo._construct_documents_from_results_with_score   s     13	6?C((06{005zz#''*d";<H$BM # r2   c                    / nUS    He  nU R                   c  US   nOU R                  U5      n[        R                  " UR                  SS5      5      nUR	                  [        XES95        Mg     U$ )rp   rq   r   r6   rr   rs   rv   rx   s         r)   rY   5Marqo._construct_documents_from_results_without_score  sp     %'	6?C((06{005zz#''*d";<HX4KL # r2   c                    U R                   R                  U R                  5      R                  XR                  US9nU$ )zReturn documents from Marqo exposing Marqo's output directly

Args:
    query (str): The query to search with.
    k (int, optional): The number of documents to return. Defaults to 4.

Returns:
    List[Dict[str, Any]]: This hits from marqo.
qr(   limitr   r<   r   searchr    )r$   rV   rW   rZ   s       r)   rX   Marqo.marqo_similarity_search0  s@     ,,$$T%5%56==+F+Fa > 
 r2   c           
         SU Vs/ sH@  nU R                   R                  U R                  5      R                  X0R                  US9PMB     sn0nU$ s  snf )a^  Return documents from Marqo using a bulk search, exposes Marqo's
output directly

Args:
    queries (Iterable[Union[str, Dict[str, float]]]): A list of queries.
    k (int, optional): The number of documents to return for each query.
    Defaults to 4.

Returns:
    Dict[str, Dict[List[Dict[str, Dict[str, Any]]]]]: A bulk search results
    object
re   r~   r   )r$   rd   rW   rV   rh   s        r)   rf   "Marqo.marqo_bulk_similarity_searchC  sm       %	 %E ""4#3#34;;3N3NVW <  %	
 s   AAc                    U Vs/ sH  oDR                   PM     nnU Vs/ sH  oDR                  PM     nnU R                  " U4SU0UD6$ s  snf s  snf )aT  Return VectorStore initialized from documents. Note that Marqo does not
need embeddings, we retain the parameter to adhere to the Liskov substitution
principle.


Args:
    documents (List[Document]): Input documents
    embedding (Any, optional): Embeddings (not required). Defaults to None.

Returns:
    VectorStore: A Marqo vectorstore
rG   )rt   r6   
from_texts)clsrJ   	embeddingrH   drF   rG   s          r)   from_documentsMarqo.from_documents]  sQ    & *33A3)23AZZ	3~~eCyCFCC 43s
   A
Ac                    SSK nU(       d  [        [        R                  " 5       5      nUR
                  " XVS9n UR                  XJ=(       d    0 S9  U(       a  [        SU S35        U " UUUU=(       d    0 U	S
9nUR                  X5        U$ ! [         a    [        S5      ef = f! [         a    U(       a  [        SU S	35         Naf = f)a  Return Marqo initialized from texts. Note that Marqo does not need
embeddings, we retain the parameter to adhere to the Liskov
substitution principle.

This is a quick way to get started with marqo - simply provide your texts and
metadatas and this will create an instance of the data store and index the
provided data.

To know the ids of your documents with this approach you will need to include
them in under the key "_id" in your metadatas for each text

Example:
.. code-block:: python

        from langchain_community.vectorstores import Marqo

        datastore = Marqo(texts=['text'], index_name='my-first-index',
        url='http://localhost:8882')

Args:
    texts (List[str]): A list of texts to index into marqo upon creation.
    embedding (Any, optional): Embeddings (not required). Defaults to None.
    index_name (str, optional): The name of the index to use, if none is
    provided then one will be created with a UUID. Defaults to None.
    url (str, optional): The URL for Marqo. Defaults to "http://localhost:8882".
    api_key (str, optional): The API key for Marqo. Defaults to "".
    metadatas (Optional[List[dict]], optional): A list of metadatas, to
    accompany the texts. Defaults to None.
    this is only used when a new index is being created. Defaults to "cpu". Can
    be "cpu" or "cuda".
    add_documents_settings (Optional[Dict[str, Any]], optional): Settings
    for adding documents, see
    https://docs.marqo.ai/0.0.16/API-Reference/documents/#query-parameters.
    Defaults to {}.
    index_settings (Optional[Dict[str, Any]], optional): Index settings if
    the index doesn't exist, see
    https://docs.marqo.ai/0.0.16/API-Reference/indexes/#index-defaults-object.
    Defaults to {}.

Returns:
    Marqo: An instance of the Marqo vector store
r   Nr   )urlapi_key)settings_dictzCreated z successfully.zIndex z exists.)r(   r'   r!   )
r   r   struuiduuid4r   create_indexprint	ExceptionrR   )r   rF   r   rG   r&   r   r   r'   r(   r!   index_settingsverboserH   r   r%   instances                   r)   r   Marqo.from_textst  s    t	 TZZ\*J#7	5
:NBON;<
 "7#9#?R!5
 	5,7  	> 	  	5zl(34	5s   B .B) B&) CCc                <    U R                   R                  5       S   $ )zHelper to see your available indexes in marqo, useful if the
from_texts method was used without an index name specified

Returns:
    List[Dict[str, str]]: The list of indexes
rZ   )r   get_indexesr/   s    r)   r   Marqo.get_indexes  s     ||''))44r2   c                n    U R                   R                  U R                  5      R                  5       S   $ )z^Helper to see the number of documents in the index

Returns:
    int: The number of documents
numberOfDocuments)r   r<   r   	get_statsr/   s    r)   get_number_of_documentsMarqo.get_number_of_documents  s.     ||!!$"2"23==?@STTr2   )r   r   r#   r   r    r!   r"   )NNN)
r%   zmarqo.Clientr&   r   r'   Optional[Dict[str, Any]]r(   Optional[List[str]]r!   z)Optional[Callable[[Dict[str, Any]], str]])returnzOptional[Embeddings]r-   )rF   zIterable[str]rG   Optional[List[dict]]rH   r   r   	List[str])   )rV   Union[str, Dict[str, float]]rW   intrH   r   r   List[Document])rV   r   rW   r   r   zList[Tuple[Document, float]])rd   &Iterable[Union[str, Dict[str, float]]]rW   r   rH   r   r   zList[List[Document]])rd   r   rW   r   rH   r   r   z"List[List[Tuple[Document, float]]])rZ   Dict[str, List[Dict[str, str]]]r   zList[Tuple[Document, Any]])rZ   r   r   r   )rV   r   rW   r   r   r   )rd   r   rW   r   r   z0Dict[str, List[Dict[str, List[Dict[str, str]]]]])
r   zType[Marqo]rJ   r   r   zUnion[Embeddings, None]rH   r   r   r   )
NN zhttp://localhost:8882r   NNNNT)rF   r   r   r   rG   r   r&   r   r   r   r   r   r'   r   r(   r   r!   z)Optional[Callable[[Dict[str, str]], str]]r   r   r   boolrH   r   r   r   )r   zList[Dict[str, str]])r   r   )__name__
__module____qualname____firstlineno____doc__r*   propertyr0   rR   r[   r`   rj   rm   r^   rY   rX   rf   classmethodr   r   r   r   __static_attributes__r.   r2   r)   r   r      s   : <@59JN)) ) !9	)
  3) H)@   +/>> (> 	>
 
>F +  	
 
0  +    
&	 2 7  	
 
< 7  	
 
,66	#<6	8 +  
)	( IJ=BE	94  .2DD!D +D 	D
 
D D,  *.*;?59JN37VV V (	V
 V V V !9V  3V HV 1V V V 
V Vp5Ur2   r   )
__future__r   r@   r   typingr   r   r   r   r   r	   r
   r   r   r   langchain_core.documentsr   langchain_core.embeddingsr   langchain_core.vectorstoresr   r   r   r.   r2   r)   <module>r      s>    "     . 0 3BUK BUr2   