o
    ưi                     @   s   d dl Z d dlZd dlmZ d dlmZmZ d dlmZ d dl	m
Z
mZ G dd de jZded	ee fd
dZded	efddZde
ded	e
fddZdee d	efddZdS )    N)PathLike)ListOptional)verbose_logger)	FileTypesOpenAIFilesPurposec                       s.   e Zd Z	ddededef fddZ  ZS )InMemoryFileapplication/jsonlcontentnamecontent_typec                    s   t  | || _|| _d S N)super__init__r   r   )selfr
   r   r   	__class__ W/home/app/Keep/.python/lib/python3.10/site-packages/litellm/router_utils/batch_utils.pyr      s   
zInMemoryFile.__init__)r	   )__name__
__module____qualname__bytesstrr   __classcell__r   r   r   r   r   
   s    r   r
   returnc              
   C   s   g }d}| D ]%}||7 }|dkr+zt | }|| d}W q t jy*   Y qw q| r^zt | }|| W |S  t jy] } ztd|dd  d|  |d}~ww |S )ab  
    Parse JSONL content that may contain JSON objects with embedded newlines in string values.

    Unlike splitlines(), this function properly handles cases where JSON string values
    contain literal newline characters, which would otherwise break simple line-based parsing.

    Args:
        content: The JSONL file content as a string

    Returns:
        List of parsed JSON objects

    Example:
        >>> content = '{"id":1,"msg":"Line 1\nLine 2"}\n{"id":2,"msg":"test"}'
        >>> parse_jsonl_with_embedded_newlines(content)
        [{"id":1,"msg":"Line 1\nLine 2"}, {"id":2,"msg":"test"}]
     
zerror parsing final buffer: Nd   z..., error: )jsonloadsstripappendJSONDecodeErrorr   error)r
   json_objectsbuffercharjson_objecter   r   r   "parse_jsonl_with_embedded_newlines   s4   
r*   purposec                 C   s   | dkrdS dS )z
    Check if the model name should be replaced in the JSONL file for the deployment model name.

    Azure raises an error on create batch if the model name for deployment is not in the .jsonl.
    batchTFr   )r+   r   r   r   should_replace_model_in_jsonlB   s   r-   file_contentnew_model_namec              
   C   s   zjt | tr	| W S t| dr|  }nt | tr| d }n| }t |tr*|d}nt |tr2|}n| W S t|}t	|dkrB| W S g }|D ]}d|v rR||d d< |
t| qFd|d}t|dd	d
W S  tjttfyy   |  Y S w )Nread   zutf-8r   bodymodelr   zmodified_file.jsonlr	   )r   r   )
isinstancer   hasattrr0   tupler   decoder   r*   lenr"   r   dumpsjoinencoder   r#   UnicodeDecodeError	TypeError)r.   r/   Zfile_content_bytesZfile_content_strr%   Zmodified_linesr(   Zmodified_file_contentr   r   r   replace_model_in_jsonlO   s4   






r>   function_namec                    s.   t g d} rt fdd|D rdS dS )z
    Helper to return what the "metadata" field should be called in the request data

    For all /thread or /assistant endpoints we need to call this "litellm_metadata"

    For ALL other endpoints we call this "metadata
    )r,   Zgeneric_api_callZ_acreate_batchfileZ!_ageneric_api_call_with_fallbacksc                 3   s    | ]}| v V  qd S r   r   ).0methodr?   r   r   	<genexpr>   s    
z5_get_router_metadata_variable_name.<locals>.<genexpr>Zlitellm_metadatametadata)setany)r?   Z%ROUTER_METHODS_USING_LITELLM_METADATAr   rC   r   "_get_router_metadata_variable_name   s   	rH   )ior   osr   typingr   r   Zlitellm._loggingr   Zlitellm.types.llms.openair   r   BytesIOr   r   dictr*   boolr-   r>   rH   r   r   r   r   <module>   s    	/
2