
    +#h_                     T    d dl Z d dlZd dlmZmZ d dlZd dlmZ d Z G d de      Z	y)    N)AnyLiteral)LMc                 J   t         j                   j                         j                         }t        |      | z   |z   }t	        j
                         j                  |j                  d             t	        j
                         j                         }d| dt        |      | dS )z7Creates a serialized request object for the Ollama API.zutf-8z	chatcmpl-zchat.completion)idobjectcreatedmodel)
datetimenow	timestampstrhashlibsha1updateencode	hexdigestint)
model_namepromptr   	id_stringid_hashs        O/var/www/html/sandstorm/venv/lib/python3.12/site-packages/dsp/modules/ollama.pypost_request_metadatar   
   s    !!%%'113II+f4ILLN)**734lln&&(GgY'3DQTU^Q_jtuu    c                        e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 ddeded   dedededed	ed
ededededef fdZdefdZ	defdZ
deeef   defdZ	 	 ddedededeeeef      fdZ xZS )OllamaLocalan  Wrapper around a locally hosted Ollama model (API: https://github.com/jmorganca/ollama/blob/main/docs/modelfile.md#valid-parameters-and-values and https://github.com/jmorganca/ollama/blob/main/docs/api.md#generate-a-completion).
    Returns dictionary info in the OpenAI API style (https://platform.openai.com/docs/api-reference/chat/object).

    Args:
        model (str, optional): Name of Ollama model. Defaults to "llama2".
        model_type (Literal["chat", "text"], optional): The type of model that was specified. Mainly to decide the optimal prompting strategy. Defaults to "text".
        base_url (str):  Protocol, host name, and port to the served ollama model. Defaults to "http://localhost:11434" as in ollama docs.
        timeout_s (float): Timeout period (in seconds) for the post request to llm.
        **kwargs: Additional arguments to pass to the API.
    r
   
model_type)chattextbase_url	timeout_stemperature
max_tokenstop_ptop_kfrequency_penaltypresence_penaltynnum_ctxc           	         t         |   |       d| _        || _        || _        || _        || _        |||||	|
||d|| _        | j                  d   | j                  d<   g | _        d|v r|d   nd| _	        d| _
        y )Nollama)r#   r$   r%   r&   r'   r(   r)   r*   r$   num_predictversion r   )super__init__providerr   r!   r   r"   kwargshistoryr.   _prev_prompt_eval_count)selfr
   r   r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r3   	__class__s                 r   r1   zOllamaLocal.__init__   s      	 $ " '$!2 0

 

 &*[[%>M"-/,5,?vi(R ()$r   r   c                    |}i | j                   |}t        | j                  |      }g |d<   | j                  |j                         D ci c]  \  }}|dvs|| c}}dd}| j                  dk(  r
d|dg|d<   n||d	<   | j                  dk(  r| j
                   d
n| j
                   d}d}	t        |d         D ]  }
t        j                  ||| j                        }|j                  dk7  r%t        d|j                   d|j                          |j                         }| j                  dk(  r |j                  d      j                  d      n|j                  d      }|d   j                  |
ddj!                  |      ddd       |	|j                  d      z  }	 j                         D ci c]  \  }}|dvs|| c}}|d<   |j                  d| j"                        |	|j                  d| j"                        |	z   d|d<   ||||d}| j$                  j                  |       |S c c}}w c c}}w ) Nchoices)r)   r$   F)r
   optionsstreamr   user)rolecontentmessagesr   z	/api/chatz/api/generater   r)   )jsontimeout   zError: CODE z - messager>   response	assistantr/   stop)indexrC   finish_reason
eval_count)rD   additional_kwargsprompt_eval_count)prompt_tokenscompletion_tokenstotal_tokensusage)r   rD   r3   
raw_kwargs)r3   r   r   itemsr   r!   rangerequestspostr"   status_codeprintr    r@   getappendjoinr5   r4   )r6   r   r3   rP   request_infokvsettings_dicturlstrtot_eval_tokensirD   response_jsonr    r4   s                  r   basic_requestzOllamaLocal.basic_requestM   sk   
*DKK*6*,T__fE"$Y__)/XA1DW;W1X

 ??f$28V)L(MM*%&,M(#0460IDMM?),RVR_R_Q``mOnvc{# 	?A}}V-XH ##s*X%9%9$:#hmm_MN$MMOM ??f, !!),00;"&&z2 
 #** +#%774=  &,	 }00>>O3	?4 ?L>Q>Q>S,mdaWX`lWlQT,m() +../BDD`D`a!0)--.A4C_C_`crr!
W $$	
 	G$i YH -ns   H9H9<H?	H?c                 6    d|v r|d=  | j                   |fi |S )z9Wrapper for requesting completions from the Ollama model.r   )rb   )r6   r   r3   s      r   requestzOllamaLocal.request   s*    6!|$!t!!&3F33r   choicereturnc                     |d   d   S )NrC   r>    )r6   re   s     r   _get_choice_textzOllamaLocal._get_choice_text   s    i ++r   only_completedreturn_sortedc                     |sJ d       |du sJ d        | j                   |fi |}|d   }|D cg c]  }|d   dk7  s| }}|rt        |      r|}|D cg c]  }| j                  |       }	}|	S c c}w c c}w )a  Retrieves completions from Ollama.

        Args:
            prompt (str): prompt to send to Ollama
            only_completed (bool, optional): return only completed responses and ignores completion due to length. Defaults to True.
            return_sorted (bool, optional): sort the completion choices using the returned probabilities. Defaults to False.

        Returns:
            list[dict[str, Any]]: list of completion choices
        zfor nowFr9   rH   length)rd   lenri   )
r6   r   rj   rk   r3   rD   r9   ccompleted_choicescompletionss
             r   __call__zOllamaLocal.__call__   s    $ (y(~%0y0%4<<1&19%(/R11_3E3QQRRc"34'G9@AAt,,Q/AA S
 Bs   A4A4A9)llama2r    zhttp://localhost:11434x   g                 r   r   rv   i   )TF)__name__
__module____qualname____doc__r   r   floatr   r1   rb   rd   dictr   ri   boollistrr   __classcell__)r7   s   @r   r   r      s#   	 .40 #$"#,),) N+,) 	,)
 ,) ,) ,) ,) ,) !,)  ,) ,) ,)\=C =~4c 4,tCH~ ,# ,  $#	     	  
d38n	 r   r   )
r   r   typingr   r   rS   dsp.modules.lmr   r   r   rh   r   r   <module>r      s(        vc" cr   