U
    hX                     @   s\   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 deZdS )    N)AnyListMappingOptional)CallbackManagerForLLMRun)LLMc                   @   s~   e Zd ZU dZee ed< dZee ed< e	edddZ
deeee  ee eed	d
dZe	eeef dd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                 C   s   dS )Nr    selfr   r   F/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/llms/bittensor.py	_llm_type"   s    zNIBittensorLLM._llm_typeN)promptstoprun_managerkwargsr   c              
   K   s  t  }d|_tjjd|d}t| jtr8t	d| j}nd}d}| j
dkrT|d }	n|t| j
 }	|d	d
 | }
|
 ddddd}t|}|d d }dd| dd}|jd	d|d | }| ddddd}t|}t|tr|r|s|D ]}zt|gd|	dd|dgd}|dd|| | }| ddddd}t|}d|krW q|d d d d }|  |W   S  tk
r   Y qY nX qzt|d|	dd|dgd}|dd|| | }| ddddd}|rB|  |W S t|}|d d d d }|  |W S  tk
r } z&|jd	d | d!| |d W Y d"S d}~X Y nX dS )#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'   Zdefault_promptr	   Zapi_key_responseZapi_keys_dataZapi_keys_jsonr   r   Zminer_responseZ
miner_datar"   uidpayloadZinit_responseZ	init_dataZ	init_jsonZreplyresponseZ
utf_stringZ	json_resper   r   r   _call&   s    



  




zNIBittensorLLM._callc                 C   s   | j | jdS )zGet the identifying parameters.r	   r
   rC   r   r   r   r   _identifying_params   s    z"NIBittensorLLM._identifying_params)NN)__name__
__module____qualname____doc__r   r1   __annotations__r
   r/   propertyr   r   r   r   rB   r   rD   r   r   r   r   r   
   s"   
  
 r   )http.clientr+   r7   r(   typingr   r   r   r   Zlangchain_core.callbacksr   Z#langchain_core.language_models.llmsr   r   r   r   r   r   <module>   s   