U
    h]F                     @  s   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mZ d dlmZ d dlmZ d dlmZmZ dd	d
ddZG dd deZG dd deZG dd deeZdS )    )annotationsN)
AnyAsyncIteratorCallableDictIteratorListMappingOptionalTupleUnion)AsyncCallbackManagerForLLMRunCallbackManagerForLLMRun)BaseLanguageModel)BaseLLM)GenerationChunk	LLMResultstrr   )stream_responsereturnc                 C  s4   t | }|ddkr|nd}t|dd|dS )z0Convert a stream response to a generation chunk.doneTNresponse )textgeneration_info)jsonloadsgetr   )r   Zparsed_responser    r   C/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/llms/ollama.py$_stream_response_to_generation_chunk   s    

 r    c                   @  s   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   r!   '   s   r!   c                   @  s  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&d'd(Zed)d&d*d+ZdCdddd,d-d.d/d0ZdDdddd,d1d.d2d3Z dEdd,dd,d-d4d5d6Z!dFdd,dd,d1d4d7d8Z"dGddd:d;d,d<d=d>d?Z#dHddd@d;d,d<d=dAdBZ$dS )I_OllamaCommonzhttp://localhost:11434r   base_urlZ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]authzDict[str, Any]r   c                 C  sZ   | 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.)r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r4   r5   r6   r7   )r(   r:   optionsr8   r9   r<   r=   )r(   r:   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r4   r5   r6   r7   r8   r9   r<   r=   selfr   r   r   _default_params   s,    z_OllamaCommon._default_paramszMapping[str, Any]c                 C  s   | j | jd| jS )zGet the identifying parameters.)r(   r:   )r(   r:   rD   rB   r   r   r   _identifying_params   s    z!_OllamaCommon._identifying_paramsr   zIterator[str])promptr4   imageskwargsr   c                 k  s4   ||d}| j f ||| j dd|E d H  d S NrF   rG   z/api/generate)payloadr4   api_url)_create_streamr'   )rC   rF   r4   rG   rH   rK   r   r   r   _create_generate_stream   s    

z%_OllamaCommon._create_generate_streamzAsyncIterator[str]c                 K sD   ||d}| j f ||| j dd|2 z3 d H W }|V  q*6 d S rI   )_acreate_streamr'   )rC   rF   r4   rG   rH   rK   itemr   r   r   _acreate_generate_stream   s    

z&_OllamaCommon._acreate_generate_stream)rL   rK   r4   rH   r   c           
        sZ   j d k	r|d k	rtdn j d k	r, j } j} jD ]}||kr8|| ||< q8d|krh|d |d< n(|d d|i fdd| D |d< |drd|dg i|}n|d|dg d	|}tj|d
dit jt	r jni  j
|d jd}d|_|jdkrN|jdkr2td j dn|j}	td|j d|	 |jddS )N2`stop` found in both the input and default params.rA   r4   c                   s    i | ]\}}| j kr||qS r   rD   .0kvrB   r   r   
<dictcomp>   s     
  z0_OllamaCommon._create_stream.<locals>.<dictcomp>messagesrF   rG   rJ   Content-Typeapplication/jsonT)urlr>   r?   r   streamr;   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)r4   
ValueErrorrD   itemsr   requestspost
isinstancer>   dictr?   r;   encodingstatus_coder!   r(   r   
iter_lines)
rC   rL   rK   r4   rH   paramskeyrequest_payloadr   optional_detailr   rB   r   rM      s\    


 

 z_OllamaCommon._create_streamc                  s   j d k	r|d k	rtdn j d k	r, j } j} jD ]}||kr8|| ||< q8d|krh|d |d< n(|d d|i fdd| D |d< |drd|dg i|}n|d|dg d	|}t 4 I d H }|j|d
dit j	t
r j	ni  j| jd4 I d H h}	|	jdkrX|	jdkr<tdn|	j}
td|	j d|
 |	j2 z3 d H W }|dV  q^6 W 5 Q I d H R X W 5 Q I d H R X d S )NrR   rA   r4   c                   s    i | ]\}}| j kr||qS r   rS   rT   rB   r   r   rX   -  s     
  z1_OllamaCommon._acreate_stream.<locals>.<dictcomp>rY   rF   rG   rJ   rZ   r[   )r\   r>   r?   r   r;   r_   r`   z(Ollama call failed with status code 404.ra   rb   r^   )r4   rd   rD   re   r   aiohttpZClientSessionrg   rh   r>   ri   r?   r;   statusr!   r   contentdecode)rC   rL   rK   r4   rH   rm   rn   ro   sessionr   rp   liner   rB   r   rO     s^    


 

 	z_OllamaCommon._acreate_streamF"Optional[CallbackManagerForLLMRun]boolr   )rF   r4   run_managerverboserH   r   c           	      K  sd   d }| j ||f|D ]:}|rt|}|d kr2|}n||7 }|r|j|j|d q|d kr`td|S Nrz   z$No data received from Ollama stream.)rN   r    on_llm_new_tokenr   rd   	rC   rF   r4   ry   rz   rH   final_chunkstream_respchunkr   r   r   _stream_with_aggregationR  s    z&_OllamaCommon._stream_with_aggregation'Optional[AsyncCallbackManagerForLLMRun]c           	        st   d }| j ||f|2 zH3 d H W }|rt|}|d kr:|}n||7 }|r|j|j|dI d H  q6 |d krptd|S r{   )rQ   r    r}   r   rd   r~   r   r   r   _astream_with_aggregationl  s    z'_OllamaCommon._astream_with_aggregation)NN)NN)N)N)NNF)NNF)%r"   r#   r$   r'   __annotations__r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   propertyrD   rE   rN   rQ   rM   rO   r   r   r   r   r   r   r&   +   s\   
     B @      r&   c                      s   e Zd ZdZG dd dZeddddZdd	d
d
dddd fddZdd	d
d
dddd fddZddd
ddddddZ	ddd
ddddddZ
  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")
    c                   @  s   e Zd ZdZdS )zOllama.ConfigZforbidN)r"   r#   r$   extrar   r   r   r   Config  s   r   r   r@   c                 C  s   dS )zReturn type of llm.z
ollama-llmr   rB   r   r   r   	_llm_type  s    zOllama._llm_typeNz	List[str]r3   rw   r   r   )promptsr4   rG   ry   rH   r   c           	        sD   g }|D ]0}t  j|f|||| jd|}||g qt|d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.")
        r4   rG   ry   rz   generations)superr   rz   appendr   	rC   r   r4   rG   ry   rH   r   rF   r   	__class__r   r   	_generate  s    zOllama._generatec           	        sJ   g }|D ]6}t  j|f|||| jd|I dH }||g qt|dS )r   r   Nr   )r   r   rz   r   r   r   r   r   r   
_agenerate  s    zOllama._ageneratezIterator[GenerationChunk])rF   r4   ry   rH   r   c                 k  sB   | j ||f|D ],}|rt|}|r6|j|j| jd |V  qd S Nr|   )rN   r    r}   r   rz   rC   rF   r4   ry   rH   r   r   r   r   r   _stream  s    zOllama._streamr   zAsyncIterator[GenerationChunk]c                 K sR   | j ||f|2 z:3 d H W }|rt|}|rD|j|j| jdI d H  |V  q6 d S r   )rQ   r    r}   r   rz   r   r   r   r   _astream  s    zOllama._astream)NNN)NNN)NN)NN)r"   r#   r$   r%   r   r   r   r   r   r   r   __classcell__r   r   r   r   r     s$      #   #    r   )
__future__r   r   typingr   r   r   r   r   r   r	   r
   r   r   rq   rf   Zlangchain_core.callbacksr   r   Zlangchain_core.language_modelsr   Z#langchain_core.language_models.llmsr   Zlangchain_core.outputsr   r   r    	Exceptionr!   r&   r   r   r   r   r   <module>   s   0  ^