
    ?h *                         d Z ddlZddlmZmZ ddlmZmZmZm	Z	m
Z
mZ ddlmZ ddlmZmZ ddlmZ ee	ee
f      Z G d d	e      Z G d
 de      Z G d de      Zy)z!
BatchRequest class definitions.
    N)ABCabstractmethod)ListSequenceOptionalDictAnyUnion)uuid4)get_valid_uuid
get_vector)UUIDc            	          e Zd ZdZddZdefdZdefdZddZ	ddede
fd	Zed
        Zedeeeeef      eeef   f   fd       Zededeee      deee      defd       Zedeeef   deee      deee      defd       Zy)BatchRequestzM
    BatchRequest abstract class used as a interface for batch requests.
    returnNc                     g | _         y N_itemsselfs    U/home/chris/cleankitchens-env/lib/python3.12/site-packages/weaviate/batch/requests.py__init__zBatchRequest.__init__   s	    ,.    c                 ,    t        | j                        S r   lenr   r   s    r   __len__zBatchRequest.__len__   s    4;;r   c                 2    t        | j                        dk(  S )z
        Check if BatchRequest is empty.

        Returns
        -------
        bool
            Whether the BatchRequest is empty.
        r   r   r   s    r   is_emptyzBatchRequest.is_empty   s     4;;1$$r   c                     g | _         y)z=
        Remove all the items from the BatchRequest.
        Nr   r   s    r   emptyzBatchRequest.empty&   s    
 r   indexc                 8    | j                   j                  |      S )a  
        Remove and return item at index (default last).

        Parameters
        ----------
        index : int, optional
            The index of the item to pop, by default -1 (last item).

        Returns
        -------
        dict
            The popped item.

        Raises
        -------
        IndexError
            If batch is empty or index is out of range.
        )r   pop)r   r#   s     r   r%   zBatchRequest.pop-   s    ( {{u%%r   c                      y)zAdd objects to BatchRequest.N )r   argskwargss      r   addzBatchRequest.addC       r   c                      y)zQReturn the request body to be digested by weaviate that contains all batch items.Nr'   r   s    r   get_request_bodyzBatchRequest.get_request_bodyG   r+   r   response_itemerrors_to_excludeerrors_to_includec                      y)a  Add failed items from a weaviate response.

        Parameters
        ----------
        response_item : BatchResponse
            Weaviate response that contains the status for all objects.
        errors_to_exclude : Optional[List[str]]
            Which errors should NOT be retried.
        errors_to_include : Optional[List[str]]
            Which errors should be retried.

        Returns
        ------
        BatchResponse: Contains responses form all successful object, eg. those that have not been added to this batch.
        Nr'   )r   r.   r/   r0   s       r    add_failed_objects_from_responsez-BatchRequest.add_failed_objects_from_responseK   r+   r   entryc                    t        | d         dk(  s(d| d   vs!d| d   d   vst        | d   d   d         dk(  ry|'| d   d   d   D ]  t        fd|D              s y y|'| d   d   d   D ]  t        fd|D              s y yy)	Nresultr   errorserrorTc              3   ,   K   | ]  }|d    v   ywmessageNr'   ).0exclerrs     r   	<genexpr>z3BatchRequest._skip_objects_retry.<locals>.<genexpr>s        L$ts9~-L   Fc              3   ,   K   | ]  }|d    v   ywr9   r'   )r;   inclr=   s     r   r>   z3BatchRequest._skip_objects_retry.<locals>.<genexpr>x   r?   r@   )r   any)r3   r/   r0   r=   s      @r   _skip_objects_retryz BatchRequest._skip_objects_retryb   s     h A%uX.eHoh775?8,W56!; (Xx09  L:KLL  *Xx09 !L:KLL ! r   )r   N))__name__
__module____qualname____doc__r   intr   boolr    r"   dictr%   r   r*   r
   r   r   strr	   r-   BatchResponser   r2   staticmethodrD   r'   r   r   r   r      s+   /   
%$ 
%& &d &, + + `%T#s(^(<d38n(L"M ` ` $ $DI. $DI.	
 
 , CH~#DI. $DI. 
	 r   r   c                       e Zd ZdZ	 	 ddededededee   dee   d	dfd
Zd	ee	ee
f      fdZdedeee      deee      d	efdZy)ReferenceBatchRequestz
    Collect Weaviate-object references to add them in one request to Weaviate.
    Caution this request will miss some validations to be faster.
    Nfrom_object_class_namefrom_object_uuidfrom_property_nameto_object_uuidto_object_class_nametenantr   c                 b   t        |t              st        d      t        |t              st        d      |t        |t              st        d      t        |      }t        |      }|	d| d| }nd| }d|z   dz   |z   dz   |z   |d}|||d<   | j                  j                  |       y)	a&  
        Add one Weaviate-object reference to this batch. Does NOT validate the consistency of the
        reference against the class schema. Checks the arguments' type and UUIDs' format.

        Parameters
        ----------
        from_object_class_name : str
            The name of the class that should reference another object.
        from_object_uuid : str
            The UUID or URL of the object that should reference another object.
        from_property_name : str
            The name of the property that contains the reference.
        to_object_uuid : str
            The UUID or URL of the object that is actually referenced.
        to_object_class_name : Optional[str], optional
            The referenced object class name to which to add the reference (with UUID
            `to_object_uuid`), it is included in Weaviate 1.14.0, where all objects are namespaced
            by class name.
            STRONGLY recommended to set it with Weaviate >= 1.14.0. It will be required in future
            versions of Weaviate Server and Clients. Use None value ONLY for Weaviate < v1.14.0,
            by default None

        Raises
        ------
        TypeError
            If arguments are not of type str.
        ValueError
            If 'uuid' is not valid or cannot be extracted.
        z5'from_object_class_name' argument must be of type strz1'from_property_name' argument must be of type strNz3'to_object_class_name' argument must be of type strzweaviate://localhost//fromtorW   )
isinstancerM   	TypeErrorr   r   append)	r   rR   rS   rT   rU   rV   rW   	to_beaconitems	            r   r*   zReferenceBatchRequest.add   s    N 0#6STT,c2OPP+J?SUX4YQRR'7)*:;+/0D/EQ~FVWI//?@I ,$%  	
 !! 
 #DN4 r   c                     | j                   S )z
        Get request body as a list of dictionaries, where each dictionary
        is a Weaviate-object reference.

        Returns
        -------
        List[dict]
            A list of Weaviate-objects references as dictionaries.
        r   r   s    r   r-   z&ReferenceBatchRequest.get_request_body   s     {{r   responser/   r0   c                     g }|D ]K  }| j                  |||      r|j                  |       (| j                  j                  |d   |d   d       M |S )Nr[   r\   rZ   )rD   r_   r   )r   rc   r/   r0   successful_responsesrefs         r   r2   z6ReferenceBatchRequest.add_failed_objects_from_response   sg      " 	GC''->@QR$++C0KKF3t9EF		G
 $#r   )NN)rF   rG   rH   rI   rM   r   r   r*   r   r   r	   r-   rN   r2   r'   r   r   rQ   rQ   ~   s     /3 $E! #E! E!  	E!
 E! 'smE! E! 
E!N$tCH~"6 $$ $DI.$ $DI.	$
 
$r   rQ   c                       e Zd ZdZ	 	 	 ddededee   dee   dee   defd	Z	de
eef   fd
Zdedeee      deee      defdZy)ObjectsBatchRequestz{
    Collect objects for one batch request to weaviate.
    Caution this batch will not be validated through weaviate.
    Ndata_object
class_nameuuidvectorrW   r   c                 R   t        |t              st        d      t        |t              st        d      |t	        j
                  |      d}|t        |      }nt        t                     }||d<   |t        |      |d<   |||d<   | j                  j                  |       |S )aK  
        Add one object to this batch. Does NOT validate the consistency of the object against
        the client's schema. Checks the arguments' type and UUIDs' format.

        Parameters
        ----------
        class_name : str
            The name of the class this object belongs to.
        data_object : dict
            Object to be added as a dict datatype.
        uuid : str or None, optional
            UUID of the object as a string, by default None
        vector: Sequence or None, optional
            The embedding of the object that should be validated.
            Can be used when:
             - a class does not have a vectorization module.
             - The given vector was generated using the _identical_ vectorization module that is configured for the
             class. In this case this vector takes precedence.

            Supported types are `list`, 'numpy.ndarray`, `torch.Tensor` and `tf.Tensor`,
            by default None.
        tenant: str, optional
            Tenant of the object

        Returns
        -------
        str
            The UUID of the added object. If one was not provided a UUIDv3 will be generated.

        Raises
        ------
        TypeError
            If an argument passed is not of an appropriate type.
        ValueError
            If 'uuid' is not of a proper form.
        zObject must be of type dictzClass name must be of type str)class
propertiesidrl   rW   )r]   rL   r^   rM   copydeepcopyr   r   r   r   r_   )r   ri   rj   rk   rl   rW   
batch_item
valid_uuids           r   r*   zObjectsBatchRequest.add   s    Z +t,9::*c*<==){9ST
'-J'0J%
4#-f#5Jx #)Jx :&r   c                 "    dg| j                   dS )z
        Get the request body as it is needed for the Weaviate server.

        Returns
        -------
        dict
            The request body as a dict.
        ALL)fieldsobjectsr   r   s    r   r-   z$ObjectsBatchRequest.get_request_body0  s     !'dkk::r   rc   r/   r0   c                     g }|D ]f  }| j                  |||      r|j                  |       (| j                  |d   |d   |d   |j                  dd       |j                  dd              h |S )Nro   rn   rp   rl   rW   )ri   rj   rk   rl   rW   )rD   r_   r*   get)r   rc   r/   r0   re   objs         r   r2   z4ObjectsBatchRequest.add_failed_objects_from_response<  s      " 
	C''->@QR$++C0HH-w<Ywwx.wwx.  	
	 $#r   )NNN)rF   rG   rH   rI   rL   rM   r   r   r   r*   r   r	   r-   rN   r   r2   r'   r   r   rh   rh      s      $%) $@@ @ tn	@
 "@ @ 
@D
;$sCx. 
;$$ $DI.$ $DI.	$
 
$r   rh   )rI   rq   abcr   r   typingr   r   r   r   r	   r
   rk   r   weaviate.utilr   r   weaviate.typesr   rM   rN   r   rQ   rh   r'   r   r   <module>r      s_     # = =  4 T#s(^$l3 l^g$L g$Tg$, g$r   