
    {hP                     N    d Z ddlZddlmZmZ ddlmZ ddlmZ  G d d	e      Z	y)
zz
Anthropic-specific batch processing implementation.

This module contains the Anthropic batch processing provider class.
    N)AnyOptional   )BatchProvider   )BatchJobInfoc                       e Zd ZdZ	 ddedeeeef      defdZdedeeef   fdZ	dedefd	Z
dededdfd
Zdedeeef   fdZdedeeef   fdZddedee   fdZy)AnthropicProviderz#Anthropic batch processing providerN	file_pathmetadatareturnc                    |}	 ddl }|j                         }|rt        d|        	 |j                  j                  }t        |      5 }|D 	cg c](  }	|	j                         st        j                  |	      * }
}	ddd       |j                  
      }|j                  S # t
        $ r# |j                  j                  j                  }Y w xY wc c}	w # 1 sw Y   [xY w# t        $ r}t        d|       |d}~ww xY w)zSubmit Anthropic batch jobr   Nz:Note: Anthropic batches don't support metadata. Ignoring: )requestsz"Failed to submit Anthropic batch: )	anthropic	AnthropicprintmessagesbatchesAttributeErrorbetaopenstripjsonloadscreateid	Exception)selfr   r   kwargs_r   clientbatches_clientfliner   batches                e/var/www/html/hubwallet-dev/venv/lib/python3.12/site-packages/instructor/batch/providers/anthropic.pysubmit_batchzAnthropicProvider.submit_batch   s    	M((*F PQYPZ[
>!'!8!8 i LA9:KdjjlDJJt,KKL #))8)<E88O " >!'!5!5!=!=> LL L
  	M@DE1L	Msi   $C$ B$ C$ 
CC%C<C>%C$ $)CC$ CC$ CC!C$ $	D-C<<Dbatch_idc                 z   	 ddl }|j                         }	 |j                  j                  }|j                  |      }|j                  |j                  |j                  t        |di       dS # t        $ r# |j
                  j                  j                  }Y lw xY w# t        $ r}t        d|       |d}~ww xY w)zGet Anthropic batch statusr   Nrequest_counts)r   status
created_atr+   z&Failed to get Anthropic batch status: )r   r   r   r   r   r   retriever   processing_statusr-   getattrr   r   r)   r   r!   r"   r%   r&   s          r'   
get_statuszAnthropicProvider.get_status/   s    	Q((*F>!'!8!8 #++H5Ehh11#..")%1A2"F	 	 " >!'!5!5!=!=>  	QDQCHIqP	Qs:   B A. A B .)BB BB 	B:&B55B:c                 H   	 ddl }|j                         }	 |j                  j                  }|j                  |      }|j                  dv rt        d|j                         |j                  dk7  rt        d|j                         |j                  |      }g }|D ]!  }|j                  |j                                # dj                  |      S # t        $ r# |j
                  j                  j                  }Y w xY w# t        $ r}	t        d|	       |	d}	~	ww xY w)	z Retrieve Anthropic batch resultsr   Nfailed	cancelledexpiredBatch job failed with status: endedBatch not completed, status: 
z&Failed to retrieve Anthropic results: )r   r   r   r   r   r   r.   r/   r   resultsappendmodel_dump_jsonjoin)
r   r)   r   r!   r"   r%   r<   results_linesresultr&   s
             r'   retrieve_resultsz"AnthropicProvider.retrieve_resultsF   s8   	Q((*F>!'!8!8 #++H5E &&*LL4U5L5L4MN  &&'13E4K4K3LM  %,,X6GM! ?$$V%;%;%=>? 99]+++ " >!'!5!5!=!=>,  	QDQCHIqP	Qs:   D C B'D )D>D  DD 	D!DD!c                 n   	 ddl }|j                         }	 |j                  j                  }|j                  |      }|j                  dv rt        d|j                         |j                  dk7  rt        d|j                         |j                  |      }t        |d      5 }|D ]$  }	|j                  |	j                         dz          & 	 ddd       y# t        $ r# |j
                  j                  j                  }Y w xY w# 1 sw Y   yxY w# t        $ r}
t        d	|
       |
d}
~
ww xY w)
z*Download Anthropic batch results to a filer   Nr4   r8   r9   r:   wr;   z&Failed to download Anthropic results: )r   r   r   r   r   r   r.   r/   r   r<   r   writer>   )r   r)   r   r   r!   r"   r%   r<   r#   rA   r&   s              r'   download_resultsz"AnthropicProvider.download_resultsi   sK   	Q((*F>!'!8!8 #++H5E &&*LL4U5L5L4MN  &&'13E4K4K3LM  %,,X6Gi% =% =FGGF224t;<== =# " >!'!5!5!=!=>"= =  	QDQCHIqP	QsX   D C A;D (*DD )DD DD DD D 	D4 D//D4c                 :   	 ddl }|j                         }	 |j                  j                  }|j                  |      }|j                         S # t        $ r# |j
                  j                  j                  }Y Lw xY w# t        $ r}t        d|       |d}~ww xY w)zCancel Anthropic batch jobr   Nz"Failed to cancel Anthropic batch: )	r   r   r   r   r   r   cancel
model_dumpr   r1   s          r'   cancel_batchzAnthropicProvider.cancel_batch   s    	M((*F>!'!8!8 #))(3E##%%	 " >!'!5!5!=!=>
  	M@DE1L	Ms9   A= A  A= )A:7A= 9A::A= =	BBBc                 N   	 ddl }|j                         }	 |j                  j                  }|j                  |      }|j                  |j                  ddS # t        $ r# |j
                  j                  j                  }Y Vw xY w# t        $ r}t        d|       |d}~ww xY w)zDelete Anthropic batch jobr   Nz)Anthropic does not support batch deletion)r   r,   messagez"Failed to delete Anthropic batch: )
r   r   r   r   r   r   r.   r   r/   r   r1   s          r'   delete_batchzAnthropicProvider.delete_batch   s    	M((*F>!'!8!8 #++H5Ehh11F 	 " >!'!5!5!=!=>  	M@DE1L	Ms9   B A *B )BB BB 	B$BB$limitc                    	 ddl }|j                         }	 |j                  j                  }|j                  |      }|j                  D cg c]%  }t        j                  |j                               ' c}S # t        $ r# |j
                  j                  j                  }Y xw xY wc c}w # t        $ r}t        d|       |d}~ww xY w)zList Anthropic batch jobsr   N)rN   z"Failed to list Anthropic batches: )r   r   r   r   r   r   listdatar   from_anthropicrI   r   )r   rN   r   r!   r"   r   r%   r&   s           r'   list_batcheszAnthropicProvider.list_batches   s    	M((*F>!'!8!8 %)))6G %\\ ++E,<,<,>? 	 " >!'!5!5!=!=>  	M@DE1L	MsE   B. A:  B. *B)7B. :)B&#B. %B&&B. .	C7CC)N)
   )__name__
__module____qualname____doc__strr   dictr   r(   r2   rB   rF   rJ   rM   intrP   r   rS        r'   r
   r
      s    - DHMM(0c3h(@M	M>Q3 Q4S> Q.!Q !Q !QFQ Q Q QBMS MT#s(^ M$MS MT#s(^ M,M# MtL/A Mr]   r
   )
rX   r   typingr   r   baser   modelsr   r
   r\   r]   r'   <module>ra      s(        !xM xMr]   