a
    bgX                     @   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   p/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/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u 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|vrW q|d d d d }|  |W   S  ty   Y qY n0 qzt|d|	dd|dgd}|dd|| | }| ddddd}|r@|  |W S t|}|d d d d }|  |W S  ty } z*|jd	d | d!| |d W Y d}~d"S d}~0 0 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)Z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
   rB   r   r   r   r   _identifying_params   s    z"NIBittensorLLM._identifying_params)NN)__name__
__module____qualname____doc__r   r0   __annotations__r
   r.   propertyr   r   r   r   rA   r   rC   r   r   r   r   r   
   s"   
  
 r   )http.clientr*   r6   r'   typingr   r   r   r   Zlangchain_core.callbacksr   Z#langchain_core.language_models.llmsr   r   r   r   r   r   <module>   s   