
    {hJ                        d dl Z d dlmZ d dlmZ d dlmZ d dlZd dlZd dl	Z	d dl
Z
d dlmZmZ d dlmZ  ej                         Z e       Zdee   defd	ZdLdededee   fdZej/                  dd       ej0                  d
d       ej0                  d
d       ej0                  dd       ej0                  dd       ej0                  dd       ej0                  dd      fdedededededefd       Zej/                  d       ej0                  d        ej0                  d!d"       ej0                  d#d$       ej0                  d%d&       ej0                  dd      fd'ed(ed)ed*edef
d+       Zej/                  d,       ej0                  d-       ej0                  dd       ej0                  dd.      fd/ededefd0       Zej/                  d1       ej0                  d2       ej0                  dd      fd/edefd3       Zej/                  d4       ej0                  d5       ej0                  d6       ej0                  dd      fd/ed7edefd8       Zej/                  d9       ej0                  d:       ej0                  d;       ej0                  d!d"      fd/ed<ed(efd=       Zej/                  d>       ej0                  d?       ej0                  d!d"       ej0                  d@       ej0                  dAdB       ej0                  dCdD       ej0                  dEdF      fdGed(edHed<edIedJe fdK       Z!y)M    N)Console)Table)Live)BatchProcessorBatchJobInfo)tqdm
batch_jobsproviderc                    t        |j                          d      }|j                  dddd       |j                  dd	
       |j                  ddd	       |j                  ddd	       |j                  ddd       |dk(  r=|j                  ddd       |j                  ddd       |j                  ddd       nA|dk(  r<|j                  ddd       |j                  ddd       |j                  ddd       | D ]5  }dddd d d d!j                  |j                  j
                  d"      }d#| d$|j                  j
                   d%| d$}|j                  j                  r%|j                  j                  j                  d&      nd'}|j                  j                  r%|j                  j                  j                  d&      nd'}d'}|j                  j                  rv|j                  j                  r`|j                  j                  |j                  j                  z
  }	|	j                         d(z  }
|
d(k  rt        |
       d)}n|
d(z  }|d*d+}n|j                  j                  r|j                  j
                  d,k(  rld-d.lm}m} |j                  |j                         |j                  j                  z
  }	|	j                         d(z  }
|
d(k  rt        |
       d)}n|
d(z  }|d*d+}t#        |j$                        }t'        |      d/kD  r|d0d1 d2z   }|dk(  r}|j)                  |||||t#        |j*                  j,                  xs d-      t#        |j*                  j.                  xs d-      t#        |j*                  j0                  xs d-             |dk(  s|j)                  t#        |j$                        ||||t#        |j*                  j2                  xs d-      t#        |j*                  j4                  xs d-      t#        |j*                  j6                  xs d-             8 |S )3zIGenerate enhanced table for batch jobs using unified BatchJobInfo objectsz Batch Jobs)titlezBatch IDdim   T)style	max_widthno_wrapStatus
   )	min_widthCreated)r   r   StartedDuration   openai	Completedright   )justifyr   Failed   Total	anthropic	SucceededErrored
Processing	   yellowbluegreenred)pending
processing	completedfailed	cancelledexpiredwhite[]z[/z%m/%d %H:%MzN/A<   mz.1fhr+   r   )datetimetimezone   N   z...)r   r   
add_columngetstatusvalue
timestamps
created_atstrftime
started_atcompleted_attotal_secondsintr6   r7   nowutcstridlenadd_rowrequest_countsr,   r-   total	succeedederroredr+   )r	   r
   table	batch_jobstatus_colorcolored_statuscreated_strstarted_strduration_strdurationtotal_minuteshoursr6   r7   batch_id_displays                  U/var/www/html/hubwallet-dev/venv/lib/python3.12/site-packages/instructor/cli/batch.pygenerate_tabler[      s-   8>>+,K89E	ZuDI	X,	Yer:	Yer:	Zu: 8gC7a@'Q?	[	 gCGqAw!D J	    
 #i$$g
. 	 \N!I,<,<,B,B+C2l^STU
 ##..   ++44]C 	 ##..   ++44]C 	 **y/C/C/P/P$$11I4H4H4S4SS  %224r9Mr!"%m"4!5Q7%*"'A!!,,1A1A1G1G<1W3||HLL1I4H4H4S4SSH$224r9Mr!"%m"4!5Q7%*"'A y||, 2%/4u<xMM I,,66;!<I,,338q9I,,227a8	 $MMILL!I,,66;!<I,,449:I,,77<1=	CJX L    r   r   limitreturnc                     ddd}||vrt        d|       ddlm}  G d d|      }	 t        ||   |      }|j	                  | 	      S # t
        $ r(}t        j                  d
| d| d       g cY d}~S d}~ww xY w)z>Get batch jobs for the specified provider using BatchProcessoropenai/gpt-4o-minianthropic/claude-3-sonnetr   r!   zUnsupported provider: r   	BaseModelc                       e Zd ZU dZeed<   y)get_jobs.<locals>.DummyModeldummyN__name__
__module____qualname__rg   rG   __annotations__ r\   rZ   
DummyModelrf      s    sr\   rn   )r]   z[red]Error listing z batch jobs: [/red]N)
ValueErrorpydanticrd   r   list_batches	Exceptionconsoleprint)r]   r
   	model_maprd   rn   	processores          rZ   get_jobsry   u   s     '0I
 y 1(<== #Y "9X#6
C	%%E%22 +H:]1#VLM	s    A 	A<A71A<7A<listzSee all existing batch jobs)namehelpz"Total number of batch jobs to show)r|   z5Time in seconds to wait for the batch job to completeFzEnable or disable screen outputz4Enable live polling to continuously update the tablez-Provider to use (e.g., 'openai', 'anthropic')zE[DEPRECATED] Use --model instead. Use Anthropic API instead of OpenAIpollscreenliveuse_anthropicc                    | t        j                  dt        d       |rd}ddd}||v r8t        j                  ||         s t
        j                  d	||    d
| d       yt        | |      }t        ||      }|st
        j                  |       yt        |d|      5 }		 t        | |      }t        ||      }|	j                  |       t        j                  |       ?# 1 sw Y   yxY w)z:
    Monitor the status of the most recent batch jobs
    NB--use-anthropic is deprecated. Use --provider 'anthropic' instead.   
stacklevelr!   ANTHROPIC_API_KEYOPENAI_API_KEY)r!   r   z[red]Error: z" environment variable not set for ro   )refresh_per_secondr~   )warningswarnDeprecationWarningosgetenvrt   ru   ry   r[   r   updatetimesleep)
r]   r}   r~   r   r
   r   required_keysr	   rO   
live_tables
             rZ   watchr      s   0  P	

 "H )"M
 = =3J)K=233UV^U__ef	
 	%*J:x0Ee 
e&	9 Z!%2J":x8Ee$JJt	  s   "AC##C,zCreate a batch job from a filez&File containing the batch job requestsr`   zYModel in format 'provider/model-name' (e.g., 'openai/gpt-4', 'anthropic/claude-3-sonnet')zInstructor batch jobz&Description/metadata for the batch job24hz1Completion window for the batch job (OpenAI only)	file_pathmodeldescriptioncompletion_windowc                    | t        j                  dt        d       |rd}	 ddlm}  G d d	|      }t        ||      }d
|i}t        j                  dd      5  |j                  | ||      }	ddd       t        j                  d	 d       |j                  dd      d   }
t        dddd|
       y# 1 sw Y   HxY w# t        $ r#}t        j                  d| d       Y d}~yd}~ww xY w)z?Create a batch job from a file using the unified BatchProcessorNzO--use-anthropic is deprecated. Use --model 'anthropic/claude-3-sonnet' instead.r   r   ra   r   rc   c                       e Zd ZU dZeed<   y)$create_from_file.<locals>.DummyModelrg   Nrh   rm   r\   rZ   rn   r           E3 r\   rn   r   z#[bold green]Submitting batch job...dotsspinner)metadatar   z'[bold green]Batch job created with ID: [/bold green]/      Fr]   r}   r~   r   r
   z$[bold red]Error creating batch job: [/bold red])r   r   r   rq   rd   r   rt   r<   submit_batchru   splitr   rs   )r   r   r   r   r   rd   rn   rw   r   batch_idprovider_namerx   s               rZ   create_from_filer      s
   2  ]	

 /EM&	! 	! #5*5	 ;
 ^^AF^S 	 --H@Q . H	
 	?zWX C+A.AAe%-P	 	  M<QC{KLLMs0   8C B81AC 8C=C 	C0C++C0zCancel a batch jobzBatch job ID to cancelzT[DEPRECATED] Use --provider 'anthropic' instead. Use Anthropic API instead of OpenAIr   c                 \   | t        j                  dt        d       |rd}	 ddlm}  G d d	|      }d
dd}||vrt
        j                  d| d       yt        ||   |      }t
        j                  d| dd      5  |j                  |        ddd       t
        j                  d|  d       t        dddd|       y# 1 sw Y   3xY w# t        $ r#}t
        j                  d| d       Y d}~yd}~wt        $ r&}t
        j                  d|  d| d       Y d}~yd}~ww xY w)z3Cancel a batch job using the unified BatchProcessorNr   r   r   r!   r   rc   c                       e Zd ZU dZeed<   y)cancel.<locals>.DummyModelrg   Nrh   rm   r\   rZ   rn   r   ,  r   r\   rn   r`   ra   rb   [red]Unsupported provider: ro   z[bold yellow]Cancelling  batch job...r   r   [bold green]Batch z% cancelled successfully![/bold green]r   Fr   [yellow]Note: 	[/yellow]z![bold red]Error cancelling batch : r   )r   r   r   rq   rd   rt   ru   r   r<   cancel_batchr   NotImplementedErrorrs   )r   r
   r   rd   rn   rv   rw   rx   s           rZ   cancelr     sG     P	

 "H#V&	! 	!
 +4
	
 9$MM7zHI #9X#6
C	^^&xj>  
 	- ""8,	-
 	 
*OP	

 	AAe%(K	- 	-  5qc344 V9(2aSTUUVsA   3C *C C1C CC 	D+C99D+D&&D+zDelete a completed batch jobzBatch job ID to deletec                    	 ddl m}  G d d|      }ddd}||vrt        j                  d| d	       y
t	        ||   |      }t        j                  d| dd      5  |j                  |        d
d
d
       t        j                  d|  d       t        dddd|       y
# 1 sw Y   3xY w# t        $ r#}t        j                  d| d       Y d
}~y
d
}~wt        $ r&}t        j                  d|  d| d       Y d
}~y
d
}~ww xY w)z3Delete a batch job using the unified BatchProcessorr   rc   c                       e Zd ZU dZeed<   y)delete.<locals>.DummyModelrg   Nrh   rm   r\   rZ   rn   r   [  r   r\   rn   r`   ra   rb   r   ro   Nz[bold yellow]Deleting r   r   r   r   z# deleted successfully![/bold green]r   r   Fr   r   r   z[bold red]Error deleting batch r   r   )
rq   rd   rt   ru   r   r<   delete_batchr   r   rs   )r   r
   rd   rn   rv   rw   rx   s          rZ   deleter   N  s    #T&	! 	!
 +4
	
 9$MM7zHI #9X#6
C	^^$XJm<f  
 	- ""8,	-
 	 
*MN	

 	AAe%(K	- 	-  5qc344 T7zA3kRSSTs@   3B0 *B0  B$21B0 $B-)B0 0	D	9CD	#DD	z-Download the file associated with a batch jobzBatch job ID to downloadzPath to download file todownload_file_pathc                     	 |dk(  rddl m}  |       }	 |j                  j                  }|j                  |       }|j                  dk7  rt        d      |j                  }|st        d      t        |d      5 }t        |j                  j                  j                  |             D ]7  }	|j                  t        j                  |	j!                               dz          9 	 d d d        y dd	lm}
  |
       }|j                  j                  | 
      }|j&                  }|dk7  rt        d      |j(                  }|s
J d|         |j*                  j-                  |      }t        |d      5 }|j                  |j.                         d d d        y # t        $ r$ |j
                  j                  j                  }Y w xY w# 1 sw Y   y xY w# 1 sw Y   y xY w# t0        $ r%}t2        j5                  d|  d|        Y d }~y d }~ww xY w)Nr!   r   	Anthropicendedz%Only completed Jobs can be downloadedzResults URL not availablew
OpenAIr   r,   z%Equivalent Output File not found for z%[bold red]Error downloading file for r   )r!   r   messagesbatchesAttributeErrorbetaretrieveprocessing_statusrp   results_urlopenr   resultswritejsondumps
model_dumpr   r   r<   output_file_idfilescontenttextrs   rt   log)r   r   r
   r   clientbatches_clientbatchr   fileresultr   r<   file_idfile_responserx   s                  rZ   download_filer   }  s   (M{"+[F>!'!8!8 #++H5E&&'1 !HII++K !<==(#. G$"6??#:#:#B#B8#LM GFJJtzz&*;*;*=>EFGG G &XFNN++X+>E\\F$ !HII**GNCH:NN7"LL009M(#. /$

=--./ /9 " >!'!5!5!=!=>G G$/ /  M;H:RsKLLMs~   G F AG ;A)G%G .BG 2GG )G G GG GG G GG G 	H(HHz!Retrieve results from a batch jobz Batch job ID to get results fromzFile to save the results tooutput_filec                    |j                  dd      \  }}	 |dk(  rddlm}  |       }|j                  j	                  |       }|j
                  dk7  r$t        j                  d|j
                   d	       y
|j                  }|st        j                  d       y
|j                  j                  |      }	t        |d      5 }
|
j                  |	j                         d
d
d
       t        j                  d| d       y
|dk(  r
ddlm}  |       }|j                   j"                  j                  j	                  |       }|j$                  dk7  r$t        j                  d|j$                   d       y
|j                   j"                  j                  j'                  |       }t        |d      5 }
|D ]7  }|
j                  t)        j*                  |j-                               dz          9 	 d
d
d
       t        j                  d| d       y
t        j                  d| d       y
# 1 sw Y   NxY w# 1 sw Y   JxY w# t.        $ r%}t        j1                  d|  d|        Y d
}~y
d
}~ww xY w)z#Retrieve and save batch job resultsr   r   r   r   r   r   r,   z[yellow]Batch status is 'z', not completed[/yellow]Nz#[red]No output file available[/red]r   z[bold green]Results saved to: r   r!   r   r   z', not ended[/yellow]r   r   ro   z'[bold red]Error retrieving results for r   )r   r   r   r   r   r<   rt   ru   r   r   r   r   r   r   r!   r   r   r   r   r   r   r   r   rs   r   )r   r   r   r
   _r   r   r   r   r   fr   results_iterr   rx   s                  rZ   r   r     s0    ++c1%KHa/Ox%XFNN++X+>E||{*/~=VW **GCD"LL009Mk3' ,1**+,MM:;-}UV$+[FKK((0099(CE&&'1/0G0G/HH]^  ";;//77??ILk3' D1* DFGGDJJv'8'8':;dBCDD MM:;-}UV MM7zHI3, ,&D D  O=hZr!MNNOsg   A I 8#I 'I H+!I A4I 6;I 1=H8/!I I +H50I 8I=I 	I2I--I2z%Create batch job using BatchProcessorz%JSONL file with message conversationsz<Python class path for response model (e.g., 'examples.User')zbatch_requests.jsonlzOutput file for batch requestsi  zMaximum tokens per requestg?zTemperature for generationmessages_fileresponse_model
max_tokenstemperaturec                    	 |j                  dd      \  }}ddl}|j                  |      }	t        |	|      }
g }t	        |       5 }|D ]7  }|j                         s|j                  t        j                  |             9 	 ddd       t        ||
      }t        j                  dt        |       dd      5  |j                  ||||       ddd       t        j                  d	| d
       t        j                  d| d       y# 1 sw Y   xY w# 1 sw Y   HxY w# t        $ r"}t        j!                  d|        Y d}~yd}~ww xY w)z3Create a batch job using the unified BatchProcessor.r   r   Nz%[bold green]Creating batch file with z requests...r   r   z [bold green]Batch file created: r   z;[yellow]Use 'instructor batch create-from-file --file-path z' to submit the batch[/yellow]z [bold red]Error creating batch: )rsplit	importlibimport_modulegetattrr   stripappendr   loadsr   rt   r<   rI   create_batch_from_messagesru   rs   r   )r   r   r   r   r   r   module_path
class_namer   moduleresponse_classmessages_listr   linerw   rx   s                   rZ   creater     sO   "!<"0"7"7Q"?Z((5 4 -  	;A ;::<!((D)9:;	; #5.9	 ^^3C4F3G|T  
 	 00{J		 	8]STI+Vtu	
%	; 	;	 	  <6qc:;;<sN   AD# D&D8D# ;D:D# DD# D D# #	E,E		E)r   r   )"r   rich.consoler   
rich.tabler   	rich.liver   typerr   r   r   instructor.batchr   r   r   Typerapprt   rz   rG   r[   rD   ry   commandOptionboolr   r   r   r   r   r   floatr   rm   r\   rZ   <module>r     s   	         9 ekkm
)`tL1 `S `FC  4;M : &<=b'KL
H  5<<,MNJ !ELL<
 '%,,T::
:
 : : : : >:z 	)   "U\\'OPh $u||5 *U\\@
 '%,,T:M:M:M :M :M :M:Mz &' ELL&>? ELL<
 '%,,c:V:V:V :V (:Vz 01 ELL&>? ELL<+T+T+T 2+T\ AB ELL&@A*ell0JK ELL<0M0M0M 0M C0Mf 56 ELL&HI#u||)FGh:O:O:O :O 7:Oz 9:%+RSh '%,,K $u||%E #ell4.JK%c0LM1<1<1< 1< 1< 1< 1< ;1<r\   