
    Ng2G                    0   d dl mZ d dl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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 d dlmZmZ d d	lmZ ddZ G d de          Z  G d de          Z! eddd           G d dee!                      Z"dS )    )annotationsN)
AnyAsyncIteratorCallableDictIteratorListMappingOptionalTupleUnion)
deprecated)AsyncCallbackManagerForLLMRunCallbackManagerForLLMRun)BaseLanguageModel)BaseLLM)GenerationChunk	LLMResult)
ConfigDictstream_responsestrreturnr   c                    t          j        |           }|                    d          du r|nd}t          |                    dd          |          S )z0Convert a stream response to a generation chunk.doneTNresponse )textgeneration_info)jsonloadsgetr   )r   parsed_responser   s      [/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/llms/ollama.py$_stream_response_to_generation_chunkr$      sa     j11O)8)<)<V)D)D)L)LooRVO  R00/       c                      e Zd ZdZdS )OllamaEndpointNotFoundErrorz-Raised when the Ollama endpoint is not found.N)__name__
__module____qualname____doc__ r%   r#   r'   r'   )   s        7777r%   r'   c                     e Zd ZU dZded<   	 dZded<   	 dZded<   	 dZd	ed
<   	 dZd	ed<   	 dZ	ded<   	 dZ
ded<   	 dZded<   	 dZded<   	 dZded<   	 dZd	ed<   	 dZd	ed<   	 dZded<   	 dZd	ed<   	 dZded<   	 dZd	ed<   	 dZded<   	 dZded<   	 dZded<   	 dZded<   	 dZded<   	 dZded <   	 dZd!ed"<   	 dZd#ed$<   	 ed?d'            Zed@d)            Z	 	 dAdBd/Z	 	 dAdCd1Z 	 dDdEd4Z!	 dDdFd5Z"	 	 	 dGdHd<Z#	 	 	 dGdId>Z$dS )J_OllamaCommonzhttp://localhost:11434r   base_urlllama2modelNzOptional[int]mirostatzOptional[float]mirostat_etamirostat_taunum_ctxnum_gpu
num_threadnum_predictrepeat_last_nrepeat_penaltytemperatureOptional[List[str]]stoptfs_ztop_ktop_pzOptional[str]systemtemplateformattimeoutzOptional[Union[int, str]]
keep_alivezOptional[bool]rawzOptional[dict]headerszUnion[Callable, Tuple, None]authr   Dict[str, Any]c                    | j         | j        | j        | j        | j        | j        | j        | j        | j        | j	        | j
        | j        | j        | j        | j        | j        d| j        | j        | j        | j        dS )z.Get the default parameters for calling Ollama.)r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r=   r>   r?   r@   )r1   rC   optionsrA   rB   rE   rF   )r1   rC   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r=   r>   r?   r@   rA   rB   rE   rF   selfs    r#   _default_paramsz_OllamaCommon._default_params   s     Zk M $ 1 $ 1<<"o#/!%!3"&"5#/	   k/8-
 
 	
r%   Mapping[str, Any]c                2    i | j         | j        d| j        S )zGet the identifying parameters.)r1   rC   )r1   rC   rN   rL   s    r#   _identifying_paramsz!_OllamaCommon._identifying_params   s$     XDJ$+>>W$BVWWr%   promptimageskwargsr   Iterator[str]c              +  R   K   ||d} | j         d||| j         dd|E d {V  d S NrR   rS   z/api/generate)payloadr=   api_urlr,   )_create_streamr/   )rM   rR   r=   rS   rT   rY   s         r#   _create_generate_streamz%_OllamaCommon._create_generate_stream   s|       $v66&4& 
}333
 
 	
 
 	
 	
 	
 	
 	
 	
 	
 	
 	
r%   AsyncIterator[str]c               b   K   ||d} | j         d||| j         dd|2 3 d {V }|W V  6 d S rW   )_acreate_streamr/   )rM   rR   r=   rS   rT   rY   items          r#   _acreate_generate_streamz&_OllamaCommon._acreate_generate_stream   s       $v66.$. 
}333
 
 	
 
 	 	 	 	 	 	 	$ JJJJJ
 
 
s   .rZ   rY   c                8     j         |t          d           j          j         } j        } j        D ]}||v r||         ||<   d|v r|d         |d<   n1i |d         d|i fd|                                D             |d<   |                    d          rd|                    dg           i|}n.|                    d          |                    dg           d|}t          j        |d	d
it           j        t                    r j        ni  j
        |d j                  }d|_        |j        dk    rD|j        dk    rt          d j         d          |j        }	t          d|j         d|	           |                    d          S )N2`stop` found in both the input and default params.rK   r=   c                .    i | ]\  }}|j         v||S r,   rN   .0kvrM   s      r#   
<dictcomp>z0_OllamaCommon._create_stream.<locals>.<dictcomp>   ,    TTTDAqat?S6S6S1a6S6S6Sr%   messagesrR   rS   rX   Content-Typeapplication/jsonT)urlrG   rH   r   streamrD   utf-8     zwOllama call failed with status code 404. Maybe your model is not found and you should pull the model with `ollama pull z`.$Ollama call failed with status code . Details: )decode_unicode)r=   
ValueErrorrN   itemsr!   requestspost
isinstancerG   dictrH   rD   encodingstatus_coder'   r1   r   
iter_lines)
rM   rZ   rY   r=   rT   paramskeyrequest_payloadr   optional_details
   `         r#   r[   z_OllamaCommon._create_stream   s;    9 T%5QRRRY"9D%' 	* 	*Cf}}$Sks &y 1F9!#!! ! UTTTFLLNNTTT!F9 ;;z"" 	)7;;z2+F+FQ&QOO "++h//!++h33  O
 = 2#-dlD#A#AI4<<r  L

 

 

 $3&&#s**1VGKzV V V   #+- 38;O 3 3!03 3   ""$"777r%   c           	       K    j         |t          d           j          j         } j        } j        D ]}||v r||         ||<   d|v r|d         |d<   n1i |d         d|i fd|                                D             |d<   |                    d          rd|                    dg           i|}n.|                    d          |                    dg           d|}t          j                    4 d {V }|                    |d	d
it           j	        t                    r j	        ni  j        | j                  4 d {V 	 }	|	j        dk    r;|	j        dk    rt          d          |	j        }
t          d|	j         d|
           |	j        2 3 d {V }|                    d          W V   6 	 d d d           d {V  n# 1 d {V swxY w Y   d d d           d {V  d S # 1 d {V swxY w Y   d S )Nrc   rK   r=   c                .    i | ]\  }}|j         v||S r,   re   rf   s      r#   rj   z1_OllamaCommon._acreate_stream.<locals>.<dictcomp>/  rk   r%   rl   rR   rS   rX   rm   rn   )ro   rG   rH   r   rD   rr   rs   z(Ollama call failed with status code 404.rt   ru   rq   )r=   rw   rN   rx   r!   aiohttpClientSessionrz   r{   rG   r|   rH   rD   statusr'   r   contentdecode)rM   rZ   rY   r=   rT   r   r   r   sessionr   r   lines   `           r#   r_   z_OllamaCommon._acreate_stream  s      9 T%5QRRRY"9D%' 	* 	*Cf}}$Sks &y 1F9!#!! ! UTTTFLLNNTTT!F9 ;;z"" 	)7;;z2+F+FQ&QOO "++h//!++h33  O (** 	/ 	/ 	/ 	/ 	/ 	/ 	/g||"$6'1$,'E'EMt||2 Y$ $ 	 	 / / / / / / / / ?c))#--9F   +3-(;8? ; ;)8; ;   #+"2 / / / / / / /$++g....... #3"2+/ / / / / / / / / / / / / / / / / / / / / / / / / / /	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/sD   AG9AG!G'GG9
G	G9"G	#G99
HHFrun_manager"Optional[CallbackManagerForLLMRun]verboseboolr   c                    d } | j         ||fi |D ];}|r7t          |          }||}n||z  }|r|                    |j        |           <|t	          d          |S Nr   z$No data received from Ollama stream.)r\   r$   on_llm_new_tokenr   rw   	rM   rR   r=   r   r   rT   final_chunkstream_respchunks	            r#   _stream_with_aggregationz&_OllamaCommon._stream_with_aggregationT  s     26747OOOO 	 	K 
<[II&"'KK5(K 00
 ' 1    CDDDr%   'Optional[AsyncCallbackManagerForLLMRun]c                   K   d } | j         ||fi |2 3 d {V }|r=t          |          }||}n||z  }|r"|                    |j        |           d {V  G6 |t	          d          |S r   )ra   r$   r   r   rw   r   s	            r#   _astream_with_aggregationz'_OllamaCommon._astream_with_aggregationn  s       26!>!>vt!V!Vv!V!V 	 	 	 	 	 	 	+ 
<[II&"'KK5(K %66
 ' 7          "W CDDDs   A)r   rI   )r   rO   NN)
rR   r   r=   r<   rS   r<   rT   r   r   rU   )
rR   r   r=   r<   rS   r<   rT   r   r   r]   )N)
rZ   r   rY   r   r=   r<   rT   r   r   rU   )
rZ   r   rY   r   r=   r<   rT   r   r   r]   )NNF)rR   r   r=   r<   r   r   r   r   rT   r   r   r   )rR   r   r=   r<   r   r   r   r   rT   r   r   r   )%r(   r)   r*   r/   __annotations__r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   propertyrN   rQ   r\   ra   r[   r_   r   r   r,   r%   r#   r.   r.   -   sG        ,H,,,,-E"H""""B %)L((((5
 %)L((((% "G!!!!$ "G!!!!+ !%J$$$$N
 "&K%%%%C $(M''''= '+N****, $(K''''= !%D$$$$&!E!!!!M  E0 "E!!!!C !F    D"H""""U F    7!G!!!!(,0J0000	_ C"G""""
 *.D----T 
 
 
 X
6 X X X XX %)&*	
 
 
 
 
$ %)&*	    ( %)	<8 <8 <8 <8 <8D %)	;/ ;/ ;/ ;/ ;/@ %):>    : %)?C      r%   r.   z0.3.1z1.0.0zlangchain_ollama.OllamaLLM)sinceremovalalternative_importc                       e Zd ZdZ ed          Zedd            Z	 	 	 dd fdZ	 	 	 dd fdZ		 	 dddZ
	 	 dddZ xZS ) OllamazOllama locally runs large language models.
    To use, follow the instructions at https://ollama.ai/.
    Example:
        .. code-block:: python
            from langchain_community.llms import Ollama
            ollama = Ollama(model="llama2")
    forbid)extrar   r   c                    dS )zReturn type of llm.z
ollama-llmr,   rL   s    r#   	_llm_typezOllama._llm_type  s	     |r%   Nprompts	List[str]r=   r<   rS   r   r   rT   r   r   c           	         g }|D ]<} t                      j        |f|||| j        d|}|                    |g           =t	          |          S )d  Call out to Ollama's generate endpoint.
        Args:
            prompt: The prompt to pass into the model.
            stop: Optional list of stop words to use when generating.
        Returns:
            The string generated by the model.
        Example:
            .. code-block:: python
                response = ollama("Tell me a joke.")
        r=   rS   r   r   generations)superr   r   appendr   
rM   r   r=   rS   r   rT   r   rR   r   	__class__s
            r#   	_generatezOllama._generate  s    &  		. 		.F:%'':'   K }----[1111r%   c           	        K   g }|D ]B} t                      j        |f|||| j        d| d{V }|                    |g           Ct	          |          S )r   r   Nr   )r   r   r   r   r   r   s
            r#   
_ageneratezOllama._agenerate  s      &  		. 		.F A A!'! ! ! !      K }----[1111r%   rR   Iterator[GenerationChunk]c              +     K    | j         ||fi |D ]:}|r6t          |          }|r!|                    |j        | j                   |V  ;d S Nr   )r\   r$   r   r   r   rM   rR   r=   r   rT   r   r   s          r#   _streamzOllama._stream  s       847OOOO 	 	K <[II 00
 $ 1    	 	r%   r   AsyncIterator[GenerationChunk]c                  K    | j         ||fi |2 3 d {V }|r=t          |          }|r'|                    |j        | j                   d {V  |W V  G6 d S r   )ra   r$   r   r   r   r   s          r#   _astreamzOllama._astream  s       "?!>vt!V!Vv!V!V 	 	 	 	 	 	 	+ <[II %66
 $ 7           "W!V!Vs   A)r   r   )NNN)r   r   r=   r<   rS   r<   r   r   rT   r   r   r   r   )
rR   r   r=   r<   r   r   rT   r   r   r   )
rR   r   r=   r<   r   r   rT   r   r   r   )r(   r)   r*   r+   r   model_configr   r   r   r   r   r   __classcell__)r   s   @r#   r   r     s         :  L    X %)&*:>2 2 2 2 2 2 2F %)&*:>2 2 2 2 2 2 2F %):>	    ( %)?C	        r%   r   )r   r   r   r   )#
__future__r   r   typingr   r   r   r   r   r	   r
   r   r   r   r   ry   langchain_core._api.deprecationr   langchain_core.callbacksr   r   langchain_core.language_modelsr   #langchain_core.language_models.llmsr   langchain_core.outputsr   r   pydanticr   r$   	Exceptionr'   r.   r   r,   r%   r#   <module>r      s   " " " " " "                           6 6 6 6 6 6        = < < < < < 7 7 7 7 7 7 = = = = = = = =         8 8 8 8 8) 8 8 8Y Y Y Y Y% Y Y Yx
 
3  
r r r r rWm r r 
r r rr%   