
    6if#                         d Z ddlZddlmZmZmZ ddlZddlZddlm	Z	 ddl
mZ  ej                  e      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Union   )BatchProvider   )BatchJobInfoc                       e Zd ZdZ	 ddeeej                  f   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_or_buffermetadatareturnc                    |}	 ddl }|j                         }|rt        d|        	 |j                  j                  }t        |t              rHt        |      5 }|D 	cg c](  }	|	j                         st        j                  |	      * }
}	ddd       nt        |t        j                        rs|j                  d       |j!                         j#                  d      }|j%                  d      D 	cg c](  }	|	j                         st        j                  |	      * }
}	nt'        dt)        |             |j+                  
      }|j,                  S # t
        $ r$ |j                  j                  j                  }Y Fw xY wc c}	w # 1 sw Y   \xY wc c}	w # t&        t.        f$ r}t0        j3                  d|         d}~wt4        $ r}t7        d	|       |d}~ww xY w)
zSubmit Anthropic batch jobr   Nz:Note: Anthropic batches don't support metadata. Ignoring: zutf-8
z&Unsupported file_path_or_buffer type: )requestsz0Validation error in Anthropic batch submission: z"Failed to submit Anthropic batch: )	anthropic	AnthropicprintmessagesbatchesAttributeErrorbeta
isinstancestropenstripjsonloadsioBytesIOseekreaddecodesplit
ValueErrortypecreateid	TypeErrorloggererror	ExceptionRuntimeError)selfr   r   kwargs_r   clientbatches_clientfliner   contentbatches                 o/root/.openclaw/workspace/visionaryfx/venv/lib/python3.12/site-packages/instructor/batch/providers/anthropic.pysubmit_batchzAnthropicProvider.submit_batch   s    '	P((*F PQYPZ[
>!'!8!8 -s3-. P!=>OT$**,

4 0OHOP P/<#((+-224;;GD18t1D)-

DJJt$  !<TBU=V<WX  #))8)<E88O% " >!'!5!5!=!=>
  PP P
 I& 	LLKA3OP 	P!CA3GHaO	Ps   $F E F FF	5F	FA&F 4F
F!7F )FF FF 	FFF G'.GG'G""G'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   r1   r2   r6   r7   s          r8   
get_statuszAnthropicProvider.get_statusE   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                    	 ddl }|j                         }	 |j                  j                  }|j                  |      }|j                  dv rt        d|j                         |j                  dk7  rt        d|j                         t        |dd      }|r@t        |dd      }t        |d	d      }t        |d
d      }	|dkD  r|dk(  rt        d|	 d      |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: r<   	succeedederroredtotalAll 5 batch requests failed. No results will be available.r   z&Failed to retrieve Anthropic results: )r   r   r   r   r   r   r?   r@   r,   rA   r-   resultsappendmodel_dump_jsonjoin)r.   r:   r   r1   r2   r6   r<   rL   rM   rN   rQ   results_linesresultr7   s                 r8   retrieve_resultsz"AnthropicProvider.retrieve_results\   s   +	Q((*F>!'!8!8 #++H5E &&*LL4U5L5L4MN  &&'13E4K4K3LM 
 %U,<dCN#NKC	!.)Q?;Q;9>&ug%Z[  %,,X6GM! ?$$V%;%;%=>? 99]++C " >!'!5!5!=!=>D  	QDQCHIqP	Qs:   E D$ C6E $)EE EE 	E1E,,E1	file_pathc                    	 ddl }|j                         }	 |j                  j                  }|j                  |      }|j                  dv rt        d|j                         |j                  dk7  rt        d|j                         t        |dd      }|r@t        |dd      }t        |d	d      }	t        |d
d      }
|	dkD  r|dk(  rt        d|
 d      |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   NrE   rI   rJ   rK   r<   rL   rM   rN   rO   rP   wr   z&Failed to download Anthropic results: )r   r   r   r   r   r   r?   r@   r,   rA   r-   rQ   r   writerS   )r.   r:   rX   r   r1   r2   r6   r<   rL   rM   rN   rQ   r3   rV   r7   s                  r8   download_resultsz"AnthropicProvider.download_results   s   )	Q((*F>!'!8!8 #++H5E &&*LL4U5L5L4MN  &&'13E4K4K3LM 
 %U,<dCN#NKC	!.)Q?;Q;9>&ug%Z[  %,,X6Gi% =% =FGGF224t;<== =; " >!'!5!5!=!=>:= =  	QDQCHIqP	QsX   E' D+ C
E' 7*E"E' +)EE' EE' E$ E' $E' '	F0E??F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,   rB   s          r8   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,   rB   s          r8   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)rd   z"Failed to list Anthropic batches: )r   r   r   r   r   r   listdatar	   from_anthropicr_   r,   )r.   rd   r   r1   r2   r   r6   r7   s           r8   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__r   r   r   r    r   dictr   r9   rC   rW   r\   r`   rc   intrf   r	   ri        r8   r   r      s    -
 .2/P"3

?3/P 4S>*/P
 
/PbQ3 Q4S> Q.-Q -Q -Q^+Q +Q +Q +QZMS MT#s(^ M$MS MT#s(^ M,M# MtL/A Mrr   r   )rn   r   typingr   r   r   r   loggingbaser   modelsr	   	getLoggerrk   r*   r   rq   rr   r8   <module>rx      sC     ' ' 	   !			8	$bM bMrr   