
    7|hX                     b    d dl Zd dlZd dlZd dlmZmZmZmZ d dl	m
Z
 d dlmZ  G d de      Zy)    N)AnyListMappingOptional)CallbackManagerForLLMRun)LLMc                       e Zd ZU dZee   ed<   	 dZee   ed<   	 e	defd       Z
	 	 dded	eee      d
ee   dedef
dZe	deeef   fd       Zy)NIBittensorLLMa  NIBittensor LLMs

    NIBittensorLLM is created by Neural Internet (https://neuralinternet.ai/),
    powered by Bittensor, a decentralized network full of different AI models.

    To analyze API_KEYS and logs of your usage visit
        https://api.neuralinternet.ai/api-keys
        https://api.neuralinternet.ai/logs

    Example:
        .. code-block:: python

            from langchain_community.llms import NIBittensorLLM
            llm = NIBittensorLLM()
    system_promptr   top_responsesreturnc                      y)Nr
    selfs    a/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/llms/bittensor.py	_llm_typezNIBittensorLLM._llm_type"   s        Npromptstoprun_managerkwargsc                    t        j                         }d|_        t        j                  j                  d|      }t        | j                  t              rt        d| j                        }nd}d}| j                  |dz   }	n|t        | j                        z   }	|j                  d	d
       |j                         }
|
j                         j                  d      j!                  dd      j!                  dd      }t#        j$                  |      }|d   d   }dd| dd}|j                  d	d|       |j                         }|j                         j                  d      j!                  dd      j!                  dd      }t#        j$                  |      }t        |t&              r|r|s|D ]  }	 t#        j(                  |gd|	dd|dgd      }|j                  dd||       |j                         }|j                         j                  d      j!                  dd      j!                  dd      }t#        j$                  |      }d|vr|d   d   d   d   }|j+                          |c S  	 t#        j(                  |d|	dd|dgd      }|j                  dd||       |j                         }|j                         j                  d      j!                  dd      j!                  dd      }|r|j+                          |S t#        j$                  |      }|d   d   d   d   }|j+                          |S # t,        $ r Y w xY w# t,        $ r$}|j                  d	d | d!| |       Y d}~y"d}~ww xY w)#a  
        Wrapper around the bittensor top miner models. Its built by Neural Internet.

        Call the Neural Internet's BTVEP Server and return the output.

        Parameters (optional):
            system_prompt(str): A system prompt defining how your model should respond.
            top_responses(int): Total top miner responses to retrieve from Bittensor
                protocol.

        Return:
            The generated response(s).

        Example:
            .. code-block:: python

                from langchain_community.llms import NIBittensorLLM
                llm = NIBittensorLLM(system_prompt="Act like you are programmer with                 5+ years of experience.")
        Tztest.neuralinternet.ai)contextd   r   zwYou are an assistant which is created by Neural Internet(NI)             in decentralized network named as a Bittensor.Nz? Your task is to provide accurate response based on user promptGETz/admin/api-keys/zutf-8
 	api_keyzapplication/jsonzBearer z
2023-05-19)zContent-TypeAuthorizationzEndpoint-Versionz/top_miner_uids)headerssystem)rolecontentuser)uidsmessagesPOSTz/chatchoicesmessager%   )top_nr(   z/error_msg?e=z&p=zBSorry I am unable to provide response now, Please try again later.)sslcreate_default_contextcheck_hostnamehttpclientHTTPSConnection
isinstancer   intminr   strrequestgetresponsereaddecodereplacejsonloadslistdumpsclose	Exception)r   r   r   r   r   r   connr,   default_promptr   api_key_responseapi_keys_dataapi_keys_jsonr    r"   miner_response
miner_datar'   uidpayloadinit_response	init_data	init_jsonreplyresponse
utf_string	json_respes                               r   _callzNIBittensorLLM._call&   s   : ,,.!%{{**+CW*U d((#.T//0EE<%ST 
 +S1C1C-DDM 	U./++-!!#**73;;D"EMMdTVW 	 

=1"9- /&wi0 ,

 	U-w?))+!((199$CKKDRTU 	 zz*% dD!d5 "jj%(E)1m L)/F C)G LL'7C$($4$4$6M%**, r* r*	  !%

9 5I 	1 %i03I>yIEJJL L3<	Xjj"!)mD!'F;!G LL'7;'')H&&w/77bAII$PRS  

!!

:.Ii(+I6yAEJJLL5 ! 6  	XLL-s#fX >LPW	Xs8   5BMM7BM 4M 	MM	N M;;N c                 4    | j                   | j                  dS )zGet the identifying parameters.r   r   rU   r   s    r   _identifying_paramsz"NIBittensorLLM._identifying_params   s      "//!//
 	
r   )NN)__name__
__module____qualname____doc__r   r6   __annotations__r   r4   propertyr   r   r   r   rS   r   rV   r   r   r   r
   r
   
   s      C= W#$M8C=$#  3     %):>	@X@X tCy!@X 67	@X
 @X 
@XD 
WS#X%6 
 
r   r
   )http.clientr0   r<   r-   typingr   r   r   r   langchain_core.callbacksr   #langchain_core.language_models.llmsr   r
   r   r   r   <module>ra      s(      
 / / = 3d
S d
r   