
    6i                        d Z ddlmZ ddlmZmZ ddlmZmZm	Z	 ddl
Z
ddlZddlmZ  G d d	e      Z G d
 de      Z G d de      Z G d de      Z G d deee         Zy)z
Batch request models and schema utilities.

This module contains the BatchRequest class and related models for creating
provider-specific batch requests with JSON schema generation.
    )annotations)AnyGeneric)	BaseModelField
ConfigDictN   )Tc                  ,    e Zd ZU ded<   ded<   ded<   y)Functionstrnamedescriptionr   
parametersN__name__
__module____qualname____annotations__     c/root/.openclaw/workspace/visionaryfx/venv/lib/python3.12/site-packages/instructor/batch/request.pyr   r      s    
IOr   r   c                  "    e Zd ZU ded<   ded<   y)Toolr   typer   functionNr   r   r   r   r   r      s    
Ir   r   c                  n    e Zd ZU ded<   ded<    ed      Zded<    ed	      Zd
ed<   ded<   ded<   y)RequestBodyr   modellist[dict[str, Any]]messages  default
int | None
max_tokensg      ?float | Nonetemperaturezlist[Tool] | Nonetoolszdict[str, Any] | Nonetool_choiceN)r   r   r   r   r   r&   r(   r   r   r   r   r      s7    J"""40J
0 %c 2K2&&r   r   c                  6    e Zd ZU ded<   ded<   ded<   ded<   y)
BatchModelr   	custom_idr   bodyurlmethodNr   r   r   r   r,   r,   $   s    N
	HKr   r,   c                      e Zd ZU dZded<   ded<   ded<   ded<    ed	
      Zded<    ed
      Zded<    ed      Z	ddZ
ddZddZ	 	 	 	 	 	 ddZy)BatchRequestzGUnified batch request that works across all providers using JSON schemar   r-   r    r!   ztype[T]response_modelr   r"   r#   r%   r&   g?r'   r(   T)arbitrary_types_allowedc                6    | j                   j                         S )z(Generate JSON schema from response_model)r3   model_json_schema)selfs    r   get_json_schemazBatchRequest.get_json_schema7   s    ""4466r   c                   | j                         }fd |j                               }| j                  dd| j                  | j                  | j
                  | j                  d| j                  j                  d|dddd	S )
z/Convert to OpenAI batch format with JSON schemac                D   t        | t              rd| v r(| d   dk(  rd| d<   n| d   dk(  rd| v r | d         | d<   d| v r)| d   j                         D ]  \  }} |      | d   |<    dD ]0  }|| v s| |   j                         D ]  \  }} |      | |   |<    2 | S )	zBRecursively add additionalProperties: false for OpenAI strict moder   objectFadditionalPropertiesarrayitems
properties)definitionsz$defs)
isinstancedictr>   )schema_dict	prop_nameprop_schemakeydef_name
def_schemamake_strict_schemas         r   rI   z9BatchRequest.to_openai_format.<locals>.make_strict_schema@   s    +t,[("6*h6>C$:;$V,7G{<R/A+gBV/WG,  ;.2=l2K2Q2Q2S .	;?Q'@L1)< 4 XCk)4?4D4J4J4L X0Hj9KJ9WK,X6XX
 r   POSTz/v1/chat/completionsjson_schemaT)r   strictschema)r   rK   )r   r!   r&   r(   response_format)r-   r0   r/   r.   )	r8   copyr-   r   r!   r&   r(   r3   r   )r7   rM   strict_schemarI   s      @r   to_openai_formatzBatchRequest.to_openai_format;   s    %%'	0 +6;;=9 ) MM"oo#//) $ 3 3 < <"&"/$$	
 	
r   c                   | j                         }d|vrd|d<   d|vrd|d<   d}g }| j                  D ]:  }|j                  d      dk(  r|j                  dd	      }*|j                  |       < | j                  | j
                  | j                  |d
d| j                  j                   d|dgdd
dd}|r||d<   | j                  |dS )z2Convert to Anthropic batch format with JSON schemar   r;   r<   FNrolesystemcontent extract_datazExtract data matching the z schema)r   r   input_schematool)r   r   )r   r&   r(   r!   r)   r*   )r-   params)
r8   r!   getappendr   r&   r(   r3   r   r-   )r7   rM   system_messagefiltered_messagesmessagerZ   s         r   to_anthropic_formatz BatchRequest.to_anthropic_formatn   s   %%' %F6N!/-2F)* }} 	2G{{6"h.!(Y!;!((1		2 ZZ//++) +%?@S@S@\@\?]]d#e$* %+NC
  -F8 
 	
r   c                   |dk(  r| j                         }n$|dk(  r| j                         }nt        d|       t        j                  |      dz   }t        |t              r't        |d      5 }|j                  |       ddd       yt        |t        j                        r!|j                  |j                  d             yt        dt        |             # 1 sw Y   yxY w)	zHSave batch request to file or BytesIO buffer in provider-specific formatopenai	anthropiczUnsupported provider: 
aNzutf-8z&Unsupported file_path_or_buffer type: )rQ   r`   
ValueErrorjsondumpsrA   r   openwriteioBytesIOencoder   )r7   file_path_or_bufferproviderdata	json_linefs         r   save_to_filezBatchRequest.save_to_file   s     x((*D$++-D5hZ@AAJJt$t+	)3/)3/ #1	"# #+RZZ8%%i&6&6w&?@8>Q9R8ST # #s   /CC%N)returnzdict[str, Any])rn   zstr | io.BytesIOro   r   rt   None)r   r   r   __doc__r   r   r&   r(   r   model_configr8   rQ   r`   rs   r   r   r   r2   r2   +   sq    QN""J"40J
0 %c 2K2d;L71
f*
X#3?B	r   r2   )rv   
__future__r   typingr   r   pydanticr   r   r   rg   rk   modelsr
   r   r   r   r,   r2   r   r   r   <module>r|      se    #  1 1  	 y 9 
') ' D9gaj Dr   