
    hb                     v    d dl mZmZmZ d dlZd dlmZ d dlmZ  G d de      Z	d Z
d dlmZ  G d	 d
e      Zy)    )ListOptionalUnionN)verbose_logger)BaseLLMExceptionc                   J     e Zd Zdededeeej                  f   f fdZ	 xZ
S )OllamaErrorstatus_codemessageheadersc                 *    t         |   |||       y )N)r
   r   r   )super__init__)selfr
   r   r   	__class__s       \/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/llms/ollama/common_utils.pyr   zOllamaError.__init__
   s     	['7S    )__name__
__module____qualname__intstrr   dicthttpxHeadersr   __classcell__)r   s   @r   r	   r	   	   s8    TT),T7<T5===P7QT Tr   r	   c                 "   ddl }ddl}	 ddlm} | }| j                  d      r| j                  d      d   } 	 |j                  |j                  |j                  |                   }|j                  dv r| S 	 |j                         }|j                  d	      j                  |d
       |j                  d       |j                  |j                               j!                  d      S # t        $ r t	        d      w xY w# t        $ r |cY S w xY w)z
    Convert image to base64 encoded image if not already in base64 format

    If image is already in base64 format AND is a jpeg/png, return it

    If image is not JPEG/PNG, convert it to JPEG base64 format
    r   N)Imagez>ollama image conversion failed please run `pip install Pillow`zdata:,)JPEGPNGRGBr!   zutf-8)base64ioPILr   	Exception
startswithsplitopenBytesIO	b64decodeformatconvertsaveseek	b64encodegetvaluedecode)imager$   r%   r   orig
image_data
jpeg_images          r   _convert_imager8      s    
 D C $ZZ

6+;+;E+B CD
/L 0 Ju"":v6OOAJ//1299'BB#  
L
 	

  s   C( >D  (C= DD)BaseLLMModelInfoc                       e Zd ZdZeddd       Zeddee   defd       Zddee   de	e   fdZ
	 	 dded	ed
edededefdZed	edefd       Zy)OllamaModelInfoz
    Dynamic model listing for Ollama server.
    Fetches /api/models and /api/tags, then for each tag also /api/models?tag=...
    Returns the union of all model names.
    Nreturnc                      y N )api_keys    r   get_api_keyzOllamaModelInfo.get_api_key;   s    r   api_basec                 .    ddl m} | xs  |d      xs dS )Nr   )get_secret_strOLLAMA_API_BASEzhttp://localhost:11434)litellm.secret_managers.mainrD   )rB   rD   s     r   get_api_basezOllamaModelInfo.get_api_base?   s    ? X>*;<X@XXr   c                 6   | j                  |      }t               }	 t        j                  | d      }|j	                          |j                         }g }t        |t              rd|v rt        |d   t              r|d   }nt        |t              r|}|D ]Y  }t        |t              s|j                  d      xs |j                  d      }	t        |	t              sI|j                  |	       [ 	 t!        |      }|S # t        $ r}
t        j                  d|
        	 ddlm} |j                  dg       xs g }|D cg c]  }d	| 	 nc c}w c}cY d
}
~
S # t        $ r)}t        j                  d|        g cY d
}~cY d
}
~
S d
}~ww xY wd
}
~
ww xY w)zX
        List all models available on the Ollama server via /api/tags endpoint.
        z	/api/tagsmodelsnamemodelz&Error retrieving ollama tag endpoint: r   )models_by_providerollamazollama/Nz3Error retrieving static ollama models as fallback: )rG   setr   getraise_for_statusjson
isinstancer   listr   addr'   r   warninglitellmrL   sorted)r   r@   rB   basenamesrespdatamodels_listentrynmerL   staticme1results                   r   
get_modelszOllamaModelInfo.get_modelsF   sr   
   *%!	99vY/0D!!#99;DK4&$tH~t4"8nD$'"$!%.YYv&<%))G*<b#&IIbM %(   	""%KA3#OP	6+//"=C/56!'!666 &&I"N 				s`   CD !D 	FF$ EEEF	F'F FFFFFFr   rK   messagesoptional_paramslitellm_paramsc                     i S )z:
        No-op environment validation for Ollama.
        r?   )r   r   rK   re   rf   rg   r@   rB   s           r   validate_environmentz$OllamaModelInfo.validate_environments   s	     	r   c                     | S )z@
        Return the base model name for Ollama (no-op).
        r?   )rK   s    r   get_base_modelzOllamaModelInfo.get_base_model   s	    
 r   r>   )r<   N)NN)r   r   r   __doc__staticmethodrA   r   r   rG   r   rd   r   rS   ri   rk   r?   r   r   r;   r;   4   s       Yx} Y Y Y+# +$s) +h   	
   
 c c  r   r;   )typingr   r   r   r   rV   r   )litellm.llms.base_llm.chat.transformationr   r	   r8    litellm.llms.base_llm.base_utilsr9   r;   r?   r   r   <module>rq      s>    ( (  " FT" TCB >S& Sr   