o
    Ag2G                     @  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 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dZG dd deZ G dd deZ!eddddG d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                 C  s4   t | }|ddu 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   parsed_responser    r!   f/var/www/html/development/chatbot/venv/lib/python3.10/site-packages/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dGd'd(ZedHd*d+Z		dIdJd1d2Z		dIdKd4d5Z 	dLdMd8d9Z!	dLdNd:d;Z"			<dOdPdBdCZ#			<dOdQdEdFZ$dS )R_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                 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/   r0   r1   r2   r3   r4   r5   r6   r8   r9   r:   r;   )r,   r>   optionsr<   r=   r@   rA   )r,   r>   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r8   r9   r:   r;   r<   r=   r@   rA   selfr!   r!   r"   _default_params   s,   z_OllamaCommon._default_paramsMapping[str, Any]c                 C  s   i | j | jd| jS )zGet the identifying parameters.)r,   r>   )r,   r>   rH   rF   r!   r!   r"   _identifying_params   s   z!_OllamaCommon._identifying_paramspromptimageskwargsr   Iterator[str]c                 k  s6    ||d}| j d||| j dd|E d H  d S NrK   rL   z/api/generate)payloadr8   api_urlr!   )_create_streamr*   )rG   rK   r8   rL   rM   rQ   r!   r!   r"   _create_generate_stream   s   

z%_OllamaCommon._create_generate_streamAsyncIterator[str]c                 K sF   ||d}| j d||| j dd|2 z	3 d H W }|V  q6 d S rO   )_acreate_streamr*   )rG   rK   r8   rL   rM   rQ   itemr!   r!   r"   _acreate_generate_stream   s   

z&_OllamaCommon._acreate_generate_streamrR   rQ   c           
        sX   j d ur|d urtd j d ur j } j} jD ]}||v r'|| ||< qd|v r3|d |d< ni |d d|i fdd| D |d< |drZd|dg i|}n|d|dg d	|}tj|d
dit jt	rw jni  j
|d jd}d|_|jdkr|jdkrtd j d|j}	td|j d|	 |jddS )N2`stop` found in both the input and default params.rE   r8   c                       i | ]\}}| j vr||qS r!   rH   .0kvrF   r!   r"   
<dictcomp>        z0_OllamaCommon._create_stream.<locals>.<dictcomp>messagesrK   rL   rP   Content-Typeapplication/jsonT)urlrB   rC   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)r8   
ValueErrorrH   itemsr   requestspost
isinstancerB   dictrC   r?   encodingstatus_coder$   r,   r   
iter_lines)
rG   rR   rQ   r8   rM   paramskeyrequest_payloadr   optional_detailr!   rF   r"   rS      sf   






z_OllamaCommon._create_streamc              
    s   j d ur|d urtd j d ur j } j} jD ]}||v r(|| ||< qd|v r4|d |d< ni |d d|i fdd| D |d< |dr[d|dg i|}n|d|dg d	|}t 4 I d H l}|j|d
dit j	t
r j	ni  j| jd4 I d H 8}	|	jdkr|	jdkrtd|	j}
td|	j d|
 |	j2 z3 d H W }|dV  q6 W d   I d H  n1 I d H sw   Y  W d   I d H  d S 1 I d H sw   Y  d S )NrY   rE   r8   c                   rZ   r!   r[   r\   rF   r!   r"   r`   /  ra   z1_OllamaCommon._acreate_stream.<locals>.<dictcomp>rb   rK   rL   rP   rc   rd   )re   rB   rC   r   r?   rh   ri   z(Ollama call failed with status code 404.rj   rk   rg   )r8   rm   rH   rn   r   aiohttpClientSessionrp   rq   rB   rr   rC   r?   statusr$   r   contentdecode)rG   rR   rQ   r8   rM   rv   rw   rx   sessionr   ry   liner!   rF   r"   rV     sl   



	


*.z_OllamaCommon._acreate_streamFrun_manager"Optional[CallbackManagerForLLMRun]verboseboolr   c           	      K  sh   d }| j ||fi |D ]}|r)t|}|d u r|}n||7 }|r)|j|j|d q|d u r2td|S Nr   z$No data received from Ollama stream.)rT   r#   on_llm_new_tokenr   rm   	rG   rK   r8   r   r   rM   final_chunkstream_respchunkr!   r!   r"   _stream_with_aggregationT  s    z&_OllamaCommon._stream_with_aggregation'Optional[AsyncCallbackManagerForLLMRun]c           	        sz   d }| j ||fi |2 z$3 d H W }|r1t|}|d u r |}n||7 }|r1|j|j|dI d H  q6 |d u r;td|S r   )rX   r#   r   r   rm   r   r!   r!   r"   _astream_with_aggregationn  s$    z'_OllamaCommon._astream_with_aggregation)r   rD   )r   rI   NN)
rK   r   r8   r7   rL   r7   rM   r   r   rN   )
rK   r   r8   r7   rL   r7   rM   r   r   rU   )N)
rR   r   rQ   r   r8   r7   rM   r   r   rN   )
rR   r   rQ   r   r8   r7   rM   r   r   rU   )NNF)rK   r   r8   r7   r   r   r   r   rM   r   r   r   )rK   r   r8   r7   r   r   r   r   rM   r   r   r   )%r%   r&   r'   r*   __annotations__r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   propertyrH   rJ   rT   rX   rS   rV   r   r   r!   r!   r!   r"   r)   -   s   
 B@r)   z0.3.1z1.0.0zlangchain_ollama.OllamaLLM)sinceremovalalternative_importc                      sx   e Zd ZdZeddZedddZ			d d! fddZ			d d! fddZ			d"d#ddZ
		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")
    forbid)extrar   r   c                 C  s   dS )zReturn type of llm.z
ollama-llmr!   rF   r!   r!   r"   	_llm_type  s   zOllama._llm_typeNprompts	List[str]r8   r7   rL   r   r   rM   r   r   c           	        sD   g }|D ]}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.")
        r8   rL   r   r   generations)superr   r   appendr   	rG   r   r8   rL   r   rM   r   rK   r   	__class__r!   r"   	_generate  s   
zOllama._generatec           	        sL   g }|D ]}t  j|f|||| jd|I dH }||g qt|dS )r   r   Nr   )r   r   r   r   r   r   r   r!   r"   
_agenerate  s   
zOllama._ageneraterK   Iterator[GenerationChunk]c                 k  sH    | j ||fi |D ]}|r!t|}|r|j|j| jd |V  qd S Nr   )rT   r#   r   r   r   rG   rK   r8   r   rM   r   r   r!   r!   r"   _stream  s   zOllama._streamr   AsyncIterator[GenerationChunk]c                 K sX   | j ||fi |2 z3 d H W }|r(t|}|r%|j|j| jdI d H  |V  q6 d S r   )rX   r#   r   r   r   r   r!   r!   r"   _astream  s    zOllama._astream)r   r   )NNN)r   r   r8   r7   rL   r7   r   r   rM   r   r   r   r   )
rK   r   r8   r7   r   r   rM   r   r   r   )
rK   r   r8   r7   r   r   rM   r   r   r   )r%   r&   r'   r(   r   model_configr   r   r   r   r   r   __classcell__r!   r!   r   r"   r     s*    ##r   )r   r   r   r   )#
__future__r   r   typingr   r   r   r   r   r   r	   r
   r   r   rz   ro   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!   r"   <module>   s,    0
  ^