
    dhX                     b    S SK rS SKrS SKrS SKJrJrJrJr  S SK	J
r
  S SKJr   " S S\5      rg)    N)AnyListMappingOptional)CallbackManagerForLLMRun)LLMc                       \ rS rSr% Sr\\   \S'    Sr\\	   \S'    \
S\4S j5       r  SS	\S
\\\      S\\   S\S\4
S jjr\
S\\\4   4S j5       rSrg)NIBittensorLLM
   a  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                     g)Nr
    selfs    Z/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/llms/bittensor.py	_llm_typeNIBittensorLLM._llm_type"   s        Npromptstoprun_managerkwargsc                 D   [         R                  " 5       nSUl        [        R                  R                  SUS9n[        U R                  [        5      (       a  [        SU R                  5      nOSnSnU R                  c  US-   n	OU[        U R                  5      -   n	UR                  S	S
5        UR                  5       n
U
R                  5       R                  S5      R!                  SS5      R!                  SS5      n["        R$                  " U5      nUS   S   nSSU 3SS.nUR                  S	SUS9  UR                  5       nUR                  5       R                  S5      R!                  SS5      R!                  SS5      n["        R$                  " U5      n[        U[&        5      (       a  U(       a  U(       d  U H  n ["        R(                  " U/SU	S.SUS./S.5      nUR                  SSUU5        UR                  5       nUR                  5       R                  S5      R!                  SS5      R!                  SS5      n["        R$                  " U5      nSU;  a  M  US   S   S   S   nUR+                  5         Us  $     ["        R(                  " USU	S.SUS./S.5      nUR                  SSUU5        UR                  5       nUR                  5       R                  S5      R!                  SS5      R!                  SS5      nU(       a  UR+                  5         U$ ["        R$                  " U5      nUS   S   S   S   nUR+                  5         U$ ! [,         a     GM  f = f! [,         a!  nUR                  S	S U S!U 3US9   SnAg"SnAff = f)#aV  
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   _call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   B!M"*M"BM4 ,5M4 "
M10M14
N>NNc                 4    U R                   U R                  S.$ )zGet the identifying parameters.r   r   rX   r   s    r   _identifying_params"NIBittensorLLM._identifying_params   s      "//!//
 	
r   r   )NN)__name__
__module____qualname____firstlineno____doc__r   r8   __annotations__r   r6   propertyr   r   r   r   rU   r   rY   __static_attributes__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.clientr2   r>   r/   typingr   r   r   r   langchain_core.callbacksr   #langchain_core.language_models.llmsr   r
   r   r   r   <module>rg      s(      
 / / = 3d
S d
r   