
    NgX                     j    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dS )    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dS )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                     dS )Nr
    selfs    ^/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/llms/bittensor.py	_llm_typezNIBittensorLLM._llm_type"   s        Npromptstoprun_managerkwargsc                    t          j                    }d|_        t          j                            d|          }t          | j        t                    rt          d| j                  }nd}d}| j
        |dz   }	n|t          | j
                  z   }	|                    d	d
           |                                }
|
                                                    d                              dd                              dd          }t#          j        |          }|d         d         }dd| dd}|                    d	d|           |                                }|                                                    d                              dd                              dd          }t#          j        |          }t          |t&                    r|r|s|D ]}	 t#          j        |gd|	dd|dgd          }|                    dd||           |                                }|                                                    d                              dd                              dd          }t#          j        |          }d|vr|d         d         d         d         }|                                 |c S # t,          $ r Y w xY w	 t#          j        |d|	dd|dgd          }|                    dd||           |                                }|                                                    d                              dd                              dd          }|r|                                 |S t#          j        |          }|d         d         d         d         }|                                 |S # t,          $ r)}|                    d	d | d!| |           Y d}~d"S 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&   so   : ,..!%{**+CW*UU d(#.. 	T/00EEE<%ST M
 +S1C-D-DDM 	U.///++--!!##**733;;D"EEMMdTVWW 	 
=11"9- /0w00 ,
 

 	U-w???))++!!((1199$CCKKDRTUU 	 z*%% dD!! 	d 	5 	  "j%(E)1m L L)/F C C)  G LL'7CCC$($4$4$6$6M%**,, r** r**	  !%
9 5 5I 	11 %i03I>yIEJJLLL LLL    H	Xj"!)mDD!'F;;!  G LL'7;;;''))H&&w//77bAAII$PRSS   "

!!
:..Ii(+I6yAEJJLLLL 	X 	X 	XLL > > >f > >LPPPWWWWWW	Xs9   %B3K/K
KKB1O AO 
PP  Pc                      | 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 @X@X tCy!@X 67	@X
 @X 
@X @X @X @XD 
WS#X%6 
 
 
 X
 
 
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 3 3 3 3 3d
 d
 d
 d
 d
S d
 d
 d
 d
 d
r   