
    {h                         d Z ddlmZmZ ddlmZmZmZmZm	Z	m
Z
mZmZmZmZ ddlmZmZmZmZ ddlmZmZmZmZmZ ddlmZ  G d d	      Zg d
Zy)aA  
Unified Batch Processing API for Multiple Providers

This module provides a unified interface for batch processing across OpenAI and Anthropic
providers. The API uses a Maybe/Result-like pattern with custom_id
tracking for type-safe handling of batch results.

Supported Providers:
- OpenAI: 50% cost savings on batch requests
- Anthropic: 50% cost savings on batch requests (Message Batches API)

Features:
- Type-safe Maybe/Result pattern for handling successes and errors
- Custom ID tracking for correlating results to original requests
- Unified interface across all providers
- Helper functions for filtering and extracting results

Example usage:
    from instructor.batch import BatchProcessor, filter_successful, extract_results
    from pydantic import BaseModel

    class User(BaseModel):
        name: str
        age: int

    processor = BatchProcessor("openai/gpt-4o-mini", User)
    batch_id = processor.submit_batch("requests.jsonl")

    # Results are BatchSuccess[T] | BatchError union types
    all_results = processor.retrieve_results(batch_id)
    successful_results = filter_successful(all_results)
    extracted_users = extract_results(all_results)

Documentation:
- OpenAI Batch API: https://platform.openai.com/docs/guides/batch
- Anthropic Message Batches: https://docs.anthropic.com/en/api/creating-message-batches
    )AnyOptional   )
BatchSuccess
BatchErrorBatchStatusBatchTimestampsBatchRequestCountsBatchErrorInfo
BatchFilesBatchJobInfoBatchResultT)filter_successfulfilter_errorsextract_resultsget_results_by_custom_id)BatchRequestFunctionToolRequestBody
BatchModel)BatchProcessorc                       e Zd ZdZededee   dee	e   e	e
eef      f   fd       Zededee   dee	e   e	e
eef      f   fd       Zede
eef   dee
eef      fd	       Zy
)BatchJobz0Legacy BatchJob class for backward compatibility	file_pathresponse_modelreturnc                     t        |      5 }|j                         }d d d        | j                  |      S # 1 sw Y   xY w)N)openreadparse_from_string)clsr   r   filecontents        Z/var/www/html/hubwallet-dev/venv/lib/python3.12/site-packages/instructor/batch/__init__.pyparse_from_filezBatchJob.parse_from_fileI   s@     )_ 	"iikG	"$$Wn==	" 	"s	   7A r%   c                    ddl }g }g }|j                         j                  d      }|D ]d  }|j                         s	 |j                  |      }| j	                  |      }	|	r	  |di |	}
|j                  |
       n|j                  |       f ||fS # t        $ r |j                  |       Y "w xY w# t        $ r |j                  d|d       Y w xY w)z?Enhanced parser that works with all providers using JSON schemar   N
zFailed to parse JSON)errorraw_line )jsonstripsplitloads_extract_structured_dataappend	Exception)r#   r%   r   r-   res
error_objslineslinedataextracted_dataresults              r&   r"   zBatchJob.parse_from_stringQ   s    
 	+-
%%d+ 	WD::<Wzz$'!$!=!=d!C!0!/!A.!A

6* %%d+	W( J % 0"))$/0
  W!!,BPT"UVWs5   $B5$B=B5B2/B51B22B55CCr8   c                    ddl }	 d|v rd|d   v r||d   d   j                  dg       }|rb|d   j                  di       }d|v r&|d   }t        |t              r|j	                  |      S d|v r|d   d   }|j	                  |d	   d
         S d|v rd|d   v r|d   d   d   }t        |t
              r}t        |      dkD  ro|D ]*  }|j                  d      dk(  s|j                  di       c S  |D ];  }|j                  d      dk(  s|j                  dd      }|j	                  |      c S  y# t        $ r Y yw xY w)z>Extract structured data from various provider response formatsr   Nresponsebodychoicesmessager%   
tool_callsfunction	argumentsr:   typetool_useinputtext )r-   get
isinstancestrr0   listlenr3   )	r#   r8   r-   r>   r?   r%   	tool_callitemrF   s	            r&   r1   z!BatchJob._extract_structured_datar   s}    	!	T!fZ0@&@z*6266y"E%ajnnY;G !G+"))"4%gs3#'::g#66 $w.$+L$9!$<	#zz)J*?*LMM 4Ih$?x.3I>gt,W1A ' 988F+z9#'88GR#889 !( 488F+v5#'88FB#7D#'::d#334   		s7   A#D1 *"D1 AD1 D1 /D1 
$D1 /D1 1	D=<D=N)__name__
__module____qualname____doc__classmethodrJ   rC   r   tuplerK   dictr   r'   r"   r   r1   r,       r&   r   r   F   s    :>>-1!W>	tAwT#s(^,,	-> > +/7	tAwT#s(^,,	- @ 'DcN 'xSRUX?W ' 'rV   r   )r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   N)rR   typingr   r   modelsr   r   r   r	   r
   r   r   r   r   r   utilsr   r   r   r   requestr   r   r   r   r   	processorr   r   __all__r,   rV   r&   <module>r]      sL   $L !     &T TprV   