o
    `žhk  ã                   @   sÌ   d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ d dlm	Z	 d dl
mZ d dlmZ e je jd� e  e¡Zedd�d	d
„ ƒZdefdd„Zddd„Z	ddedededededefdd„ZdS )é    N)Ú	lru_cache)ÚAsyncClient)Úavailable_functions)ÚSYSTEM_PROMPTS)Úlevelé   )Úmaxsizec                   C   s   t ƒ S )N)Úlist_models© r
   r
   ú>C:\Users\shubh\Videos\agent\agent\modules\llm\ollama_client.pyÚlist_models_cached   s   r   Úreturnc               
   C   st   zdd l } |  d¡}| ¡  dd„ | ¡  dg ¡D ƒW S  ty9 } zt dt|ƒ› �¡ g W  Y d }~S d }~ww )Nr   zhttp://localhost:11434/api/tagsc                 S   s   g | ]}|d  ‘qS )Únamer
   )Ú.0Úmodelr
   r
   r   Ú
<listcomp>   s    zlist_models.<locals>.<listcomp>ÚmodelszModel listing failed: )ÚrequestsÚgetÚraise_for_statusÚjsonÚ	ExceptionÚloggerÚerrorÚstr)r   ÚresponseÚer
   r
   r   r	      s   
€þr	   c               
   C   sØ   dd„ } dd„ }zdd l }| d¡}|jdkrt d¡ W d S W n ty;   t d	¡ | d
ƒr9t d¡ |d
ƒ Y nw ztjddgtjtjd� t d¡ W d S  tyk } zt 	d|› �¡ W Y d }~d S d }~ww )Nc                 S   sF   t   t jt j¡�}| d| f¡dkW  d   ƒ S 1 sw   Y  d S )NÚ	localhostr   )ÚsocketÚAF_INETÚSOCK_STREAMÚ
connect_ex)ÚportÚsr
   r
   r   Úis_port_in_use   s   $ÿz-ensure_ollama_running.<locals>.is_port_in_usec                 S   s„   t  ddg¡D ]8}|jdd�D ]/}|jj| kr>z	| ¡  W   d S  ty= } zt d| › d|› �¡ W Y d }~qd }~ww qqd S )NÚpidr   Zinet)ÚkindzFailed to kill process on port z: )	ÚpsutilZprocess_iterZnet_connectionsÚladdrr"   Úkillr   r   r   )r"   ÚprocÚconnr   r
   r
   r   Ú	free_port"   s   
"€ÿüÿÿz(ensure_ollama_running.<locals>.free_portr   zhttp://localhost:11434éÈ   zOllama is already running.z-Ollama not running. Attempting to start it...iª,  z#Port 11434 is in use, freeing it...ÚollamaZserve)ÚstdoutÚstderrzOllama server started.zFailed to start Ollama: )
r   r   Ústatus_coder   Úinfor   Ú
subprocessÚPopenÚDEVNULLr   )r$   r,   r   r   r   r
   r
   r   Úensure_ollama_running   s.   



þ

€ü€ÿr6   Úcore_llmÚpromptr   ÚtoolsÚcontextÚ
agent_typec              
   Ã   sf  �t  | ¡ t d ¡}d|dœg| ¢dt|ƒdœ‘}zztƒ }|j|||ddid�I d H }|j}	|	j}
t 	d|	j
¡ |	j
r’|	j
d	 }|jj}|jj}t|tƒrP|nt |¡}d
d„ | ¡ D ƒ}|tv r’tjt| fi |¤ŽI d H }dd|› d| d d › �dœ}|j||gddid�I d H }|jjW S |
W S  ty² } zt d¡ dt|ƒ› �W  Y d }~S d }~ww )NZCORE_LLMÚsystem)ÚroleÚcontentr:   Ztemperaturegš™™™™™Ù?)r   Úmessagesr9   ÚoptionszAssistant: %sr   c                 S   s8   i | ]\}}||d krt |tƒr| ¡ rt|ƒn|“qS )Úmax_results)Ú
isinstancer   ÚisdigitÚint)r   ÚkÚvr
   r
   r   Ú
<dictcomp>^   s   8 z query_ollama.<locals>.<dictcomp>zBased on this result:
 z/, try to answer the original question by user: éÿÿÿÿr>   g        )r   r?   r@   zError in query_ollama:zOllama Tool Call Error: )r   r   Úupperr   r   ÚchatÚmessager>   r   r2   Z
tool_callsÚfunctionr   Ú	argumentsrB   Údictr   ÚloadsÚitemsr   ÚasyncioÚ	to_threadr   Ú	exception)r8   r   r9   r:   r;   Zsystem_promptZformatted_messagesÚclientr   ÚmsgÚassistant_textZ	tool_callÚfn_nameZraw_argsÚargsÚresultZmini_sysZfollowupr   r
   r
   r   Úquery_ollama>   sP   €ÿþýü
ÿ
ÿ

€þrZ   )r   N)r7   )Úloggingr   r3   r'   r   rQ   Ú	functoolsr   r.   r   Úmodules.toolsr   Ú%modules.system_prompts.system_promptsr   ÚbasicConfigÚINFOÚ	getLoggerÚ__name__r   r   Úlistr	   r6   r   rN   rZ   r
   r
   r
   r   Ú<module>   s<    



&ûÿþýüûú