
    7|h2G                       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"y)    )annotationsN)
AnyAsyncIteratorCallableDictIteratorListMappingOptionalTupleUnion)
deprecated)AsyncCallbackManagerForLLMRunCallbackManagerForLLMRun)BaseLanguageModel)BaseLLM)GenerationChunk	LLMResult)
ConfigDictc                    t        j                  |       }|j                  d      du r|nd}t        |j                  dd      |      S )z0Convert a stream response to a generation chunk.doneTNresponse )textgeneration_info)jsonloadsgetr   )stream_responseparsed_responser   s      ^/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/llms/ollama.py$_stream_response_to_generation_chunkr"      sK     jj1O)8)<)<V)D)LoRVO  R0/     c                      e Zd ZdZy)OllamaEndpointNotFoundErrorz-Raised when the Ollama endpoint is not found.N)__name__
__module____qualname____doc__ r#   r!   r%   r%   )   s    7r#   r%   c                  z   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	 	 d/	 	 	 	 	 	 	 	 	 d0d'Z	 	 d/	 	 	 	 	 	 	 	 	 d1d(Z 	 d2	 	 	 	 	 	 	 	 	 d3d)Z!	 d2	 	 	 	 	 	 	 	 	 d4d*Z"	 	 	 d5	 	 	 	 	 	 	 	 	 	 	 d6d+Z#	 	 	 d5	 	 	 	 	 	 	 	 	 	 	 d7d,Z$y)8_OllamaCommonzhttp://localhost:11434str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]authc                   | 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.)r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r<   r=   r>   r?   )r0   rB   optionsr@   rA   rD   rE   )r0   rB   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r<   r=   r>   r?   r@   rA   rD   rE   selfs    r!   _default_paramsz_OllamaCommon._default_params   s     ZZkk MM $ 1 1 $ 1 1<<<<"oo#//!%!3!3"&"5"5#//		  kk//88-
 	
r#   c                P    i | j                   | j                  d| j                  S )zGet the identifying parameters.)r0   rB   )r0   rB   rL   rJ   s    r!   _identifying_paramsz!_OllamaCommon._identifying_params   s(     XDJJ$++>W$BVBVWWr#   c              +  p   K   ||d} | j                   d||| j                   dd|E d {    y 7 wNpromptimagesz/api/generate)payloadr<   api_urlr*   )_create_streamr.   )rK   rR   r<   rS   kwargsrT   s         r!   _create_generate_streamz%_OllamaCommon._create_generate_stream   sO      $v6&4&& 
}}o]3
 	
 	
 	
s   ,646c                  K   ||d} | j                   d||| j                   dd|2 3 d {   }| 7 
6 y wrP   )_acreate_streamr.   )rK   rR   r<   rS   rW   rT   items          r!   _acreate_generate_streamz&_OllamaCommon._acreate_generate_stream   sc      $v6.$.. 
}}o]3
 	
 	 	$ J	 
s   )><:<><>c                   | j                   |t        d      | j                   | j                   }| j                  }| j                  D ]  }||v s||   ||<    d|v r	|d   |d<   nAi |d   d|i|j                         D ci c]  \  }}|| j                  vs|| c}}|d<   |j	                  d      rd|j	                  dg       i|}	n&|j	                  d      |j	                  dg       d|}	t        j                  |dd	it        | j                  t              r| j                  ni | j                  |	d
| j                        }
d|
_        |
j                  dk7  rO|
j                  dk(  rt        d| j                   d      |
j                   }t        d|
j                   d|       |
j#                  d
      S c c}}w )N2`stop` found in both the input and default params.rI   r<   messagesrR   rS   rQ   Content-Typeapplication/jsonT)urlrF   rG   r   streamrC   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<   
ValueErrorrL   itemsr   requestspost
isinstancerF   dictrG   rC   encodingstatus_coder%   r0   r   
iter_lines)rK   rU   rT   r<   rW   paramskeykvrequest_payloadr   optional_details               r!   rV   z_OllamaCommon._create_stream   s    99 T%5QRRYY"99D%%'' 	*Cf}$Sks	*  &y 1F9!#!! %+LLNTDAqat?S?S6S1a4T!F9 ;;z")7;;z2+FQ&QO "++h/!++h3 O
 == 2#-dllD#A4<<r  LL

 $3&##s*1GGKzzlRTV  #+-- :8;O;O:P Q!!0 13  ""$"77I Us   GGc           	      K   | j                   |t        d      | j                   | j                   }| j                  }| j                  D ]  }||v s||   ||<    d|v r	|d   |d<   nAi |d   d|i|j                         D ci c]  \  }}|| j                  vs|| c}}|d<   |j	                  d      rd|j	                  dg       i|}	n&|j	                  d      |j	                  dg       d|}	t        j                         4 d {   }
|
j                  |dd	it        | j                  t              r| j                  ni | j                  |	| j                  
      4 d {   }|j                  dk7  rA|j                  dk(  rt        d      |j                  }t        d|j                   d|       |j                   2 3 d {   }|j#                  d       c c}}w 7 7 7 #6 d d d       d {  7   n# 1 d {  7  sw Y   nxY wd d d       d {  7   y # 1 d {  7  sw Y   y xY ww)Nr^   rI   r<   r_   rR   rS   rQ   r`   ra   )rb   rF   rG   r   rC   re   rf   z(Ollama call failed with status code 404.rg   rh   rd   )r<   rj   rL   rk   r   aiohttpClientSessionrm   rn   rF   ro   rG   rC   statusr%   r   contentdecode)rK   rU   rT   r<   rW   rs   rt   ru   rv   rw   sessionr   rx   lines                 r!   rZ   z_OllamaCommon._acreate_stream  ss     99 T%5QRRYY"99D%%'' 	*Cf}$Sks	*  &y 1F9!#!! %+LLNTDAqat?S?S6S1a4T!F9 ;;z")7;;z2+FQ&QO "++h/!++h3 O ((* 	/ 	/g||"$6'1$,,'Et||2 YY$ $ 	 / / ??c)#-9F  +3--(B8??BS T))8(9;  #+"2"2 / /$++g..G U	//*/"2+/ / / / /	/ 	/ 	/ 	/ 	/s   AI	4I	G.G.$A.I	G4I	AH4/G60H43AHG:G8
G:H.I	6H48G::H;H4H	H4H	HH	H4"I	-H0.I	4I:H=;II	c                    d } | j                   ||fi |D ]:  }|st        |      }||}n||z  }|s|j                  |j                  |       < |t	        d      |S Nverbosez$No data received from Ollama stream.)rX   r"   on_llm_new_tokenr   rj   	rK   rR   r<   run_managerr   rW   final_chunkstream_respchunks	            r!   _stream_with_aggregationz&_OllamaCommon._stream_with_aggregationT  s     267477OO 	K<[I&"'K5(K00

 ' 1 	 CDDr#   c                   K   d } | j                   ||fi |2 3 d {   }|st        |      }||}n||z  }|s$|j                  |j                  |       d {    J7 E7 6 |t	        d      |S wr   )r\   r"   r   r   rj   r   s	            r!   _astream_with_aggregationz'_OllamaCommon._astream_with_aggregationn  s      26!>!>!>vt!Vv!V 	 	+<[I&"'K5(K%66

 ' 7   	 "W CDDsA   A7A'A#A'A7A7 A7A%A7#A'%A7'A7)returnzDict[str, Any])r   zMapping[str, Any]NN)
rR   r-   r<   r;   rS   r;   rW   r   r   Iterator[str])
rR   r-   r<   r;   rS   r;   rW   r   r   AsyncIterator[str])N)
rU   r-   rT   r   r<   r;   rW   r   r   r   )
rU   r-   rT   r   r<   r;   rW   r   r   r   )NNF)rR   r-   r<   r;   r   "Optional[CallbackManagerForLLMRun]r   boolrW   r   r   r   )rR   r-   r<   r;   r   'Optional[AsyncCallbackManagerForLLMRun]r   r   rW   r   r   r   )%r&   r'   r(   r.   __annotations__r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   propertyrL   rN   rX   r\   rV   rZ   r   r   r*   r#   r!   r,   r,   -   s0   ,Hc,-E3"Hm"B %)L/(5
 %)L/(% "G]!$ "G]!+ !%J$N
 "&K%C $(M='= '+NO*, $(K'= !%D
$&!E?!M  E=0 "E?!C !FM D"Hm"U FM 7!G]!(,0J)0	_ C"G^"
 *.D
&-T 
 
6 X X %)&*	

 "
 $	

 
 

$ %)&*	 " $	
  
( %)	<8<8 <8 "	<8
 <8 
<8D %)	;/;/ ;/ "	;/
 ;/ 
;/@ %):> " 8	
   
: %)?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)extrac                     y)zReturn type of llm.z
ollama-llmr*   rJ   s    r!   	_llm_typezOllama._llm_type  s     r#   c           	         g }|D ]3  }t        	|   |f|||| j                  d|}|j                  |g       5 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   
rK   promptsr<   rS   r   rW   r   rR   r   	__class__s
            r!   	_generatezOllama._generate  si    &  		.F':' K }-		. [11r#   c           	        K   g }|D ];  }t        	|   |f|||| j                  d| d{   }|j                  |g       = t	        |      S 7 $w)r   r   Nr   )r   r   r   r   r   r   s
            r!   
_ageneratezOllama._agenerate  sw     &  		.F % A!'! ! K }-		. [11s   *AA%Ac              +     K    | j                   ||fi |D ]=  }|st        |      }|r'|j                  |j                  | j                         | ? y wNr   )rX   r"   r   r   r   rK   rR   r<   r   rW   r   r   s          r!   _streamzOllama._stream  sc      8477OO 	K<[I00

 $ 1  	s
   A;Ac                  K    | j                   ||fi |2 3 d {   }|st        |      }|r/|j                  |j                  | j                         d {    | N7 I7 6 y wr   )r\   r"   r   r   r   r   s          r!   _astreamzOllama._astream  s      "?!>!>vt!Vv!V 	 	+<[I%66

 $ 7    		 "Ws<   A+A)A%A)A+7A+A'
A+%A)'A+)A+)r   r-   )NNN)r   z	List[str]r<   r;   rS   r;   r   r   rW   r   r   r   r   )
rR   r-   r<   r;   r   r   rW   r   r   zIterator[GenerationChunk])
rR   r-   r<   r;   r   r   rW   r   r   zAsyncIterator[GenerationChunk])r&   r'   r(   r)   r   model_configr   r   r   r   r   r   __classcell__)r   s   @r!   r   r     s;    L   %)&*:>22 "2 $	2
 82 2 
2F %)&*:>22 "2 $	2
 82 2 
2F %):>	 " 8	
  
#( %)?C	 " =	
  
(r#   r   )r   r-   r   r   )#
__future__r   r   typingr   r   r   r   r   r	   r
   r   r   r   rz   rl   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 = 7 = 8) 8Y% Yx
 
3
rWm r
rr#   