o
    Zh                     @  s   d dl mZ d dlmZmZmZmZmZmZ d dl	m
Z
mZ d dlmZmZmZ d dlmZmZmZmZmZmZ d dlmZmZmZ d dlmZ dddZdddZG dd deeZdS )    )annotations)AnyAsyncIteratorDictIteratorListOptional)AsyncCallbackManagerForLLMRunCallbackManagerForLLMRun)BaseChatModelagenerate_from_streamgenerate_from_stream)	AIMessageAIMessageChunkBaseMessageChatMessageHumanMessageSystemMessage)ChatGenerationChatGenerationChunk
ChatResult)BaseFriendlimessager   returnstrc                 C  sB   t | ts
t | trdS t | trdS t | trdS td|  )zGet role of the message.

    Args:
        message (BaseMessage): The message object.

    Raises:
        ValueError: Raised when the message is of an unknown type.

    Returns:
        str: The role of the message.
    userZ	assistantsystemzGot unknown type )
isinstancer   r   r   r   
ValueErrorr    r    _/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/chat_models/friendli.pyget_role   s   

r"   messagesList[BaseMessage]Dict[str, Any]c                 C  s   ddd | D iS )zGet a request of the Friendli chat API.

    Args:
        messages (List[BaseMessage]): Messages comprising the conversation so far.

    Returns:
        Dict[str, Any]: The request for the Friendli chat API.
    r#   c                 S     g | ]
}t ||jd qS )Zrolecontentr"   r(   .0r   r    r    r!   
<listcomp>:   s    z$get_chat_request.<locals>.<listcomp>r    )r#   r    r    r!   get_chat_request0   s   
r-   c                   @  s   e Zd ZU dZdZded< ed'ddZed(d
dZed(ddZ	ed)ddZ
	d*d+ddZ		d,d-ddZ		d,d.d d!Z		d,d/d#d$Z		d,d0d%d&ZdS )1ChatFriendlia3  Friendli LLM for chat.

    ``friendli-client`` package should be installed with `pip install friendli-client`.
    You must set ``FRIENDLI_TOKEN`` environment variable or provide the value of your
    personal access token for the ``friendli_token`` argument.

    Example:
        .. code-block:: python

            from langchain_community.chat_models import FriendliChat

            chat = Friendli(
                model="meta-llama-3.1-8b-instruct", friendli_token="YOUR FRIENDLI TOKEN"
            )
            chat.invoke("What is generative AI?")
    zmeta-llama-3.1-8b-instructr   modelr   Dict[str, str]c                 C  s   ddiS )NZfriendli_tokenZFRIENDLI_TOKENr    selfr    r    r!   
lc_secretsU   s   zChatFriendli.lc_secretsr%   c                 C  s   | j | j| j| j| j| jdS )z@Get the default parameters for calling Friendli completions API.Zfrequency_penaltyZpresence_penaltyZ
max_tokensstopZtemperatureZtop_pr4   r1   r    r    r!   _default_paramsY   s   zChatFriendli._default_paramsc                 C  s   d| j i| jS )zGet the identifying parameters.r/   )r/   r6   r1   r    r    r!   _identifying_paramse   s   z ChatFriendli._identifying_paramsc                 C  s   dS )Nzfriendli-chatr    r1   r    r    r!   	_llm_typej   s   zChatFriendli._llm_typeNr5   Optional[List[str]]kwargsr   c                 K  sJ   | j }| jdur|durtd| jdur| j|d< n||d< i ||S )z,Get the parameters used to invoke the model.Nz2`stop` found in both the input and default params.r5   )r6   r5   r   )r2   r5   r:   paramsr    r    r!   _get_invocation_paramsn   s   
z#ChatFriendli._get_invocation_paramsr#   r$   run_manager"Optional[CallbackManagerForLLMRun]Iterator[ChatGenerationChunk]c           	      k  s    | j dd|i|}| jjjjdi t|d| jd|}|D ]}|jd jj	}|r=|r4|
| tt|ddV  q"d S Nr5   T)streamr/   r   r(   r   r    )r<   clientchatcompletionscreater-   r/   choicesdeltar(   on_llm_new_tokenr   r   	r2   r#   r5   r=   r:   r;   rA   chunkrH   r    r    r!   _stream{   s"   
zChatFriendli._stream'Optional[AsyncCallbackManagerForLLMRun]"AsyncIterator[ChatGenerationChunk]c           	      K s   | j dd|i|}| jjjjdi t|d| jd|I d H }|2 z"3 d H W }|jd jj	}|rG|r>|
|I d H  tt|ddV  q%6 d S r@   )r<   async_clientrD   rE   rF   r-   r/   rG   rH   r(   rI   r   r   rJ   r    r    r!   _astream   s"   zChatFriendli._astreamr   c           	      K  s   | j r| j|f||d|}t|S | jdd|i|}| jjjjddd |D d| jd|}t	|j
d jjd}tt|d	gd
S )Nr5   r=   r5   c                 S  r&   r'   r)   r*   r    r    r!   r,          z*ChatFriendli._generate.<locals>.<listcomp>Fr#   rA   r/   r   rB   r   Zgenerationsr    )	streamingrL   r   r<   rC   rD   rE   rF   r/   r   rG   r   r(   r   r   	r2   r#   r5   r=   r:   Zstream_iterr;   responser   r    r    r!   	_generate   s*   
zChatFriendli._generatec           	        s   | j r| j|f||d|}t|I d H S | jdd|i|}| jjjjddd |D d| jd|I d H }t	|j
d jjd}tt|d	gd
S )NrQ   r5   c                 S  r&   r'   r)   r*   r    r    r!   r,      rR   z+ChatFriendli._agenerate.<locals>.<listcomp>FrS   r   rB   r   rT   r    )rU   rP   r   r<   rO   rD   rE   rF   r/   r   rG   r   r(   r   r   rV   r    r    r!   
_agenerate   s,   
zChatFriendli._agenerate)r   r0   )r   r%   )r   r   )N)r5   r9   r:   r   r   r%   )NN)
r#   r$   r5   r9   r=   r>   r:   r   r   r?   )
r#   r$   r5   r9   r=   rM   r:   r   r   rN   )
r#   r$   r5   r9   r=   r>   r:   r   r   r   )
r#   r$   r5   r9   r=   rM   r:   r   r   r   )__name__
__module____qualname____doc__r/   __annotations__propertyr3   r6   r7   r8   r<   rL   rP   rX   rY   r    r    r    r!   r.   A   s2   
 !r.   N)r   r   r   r   )r#   r$   r   r%   ) 
__future__r   typingr   r   r   r   r   r   Zlangchain_core.callbacksr	   r
   Z*langchain_core.language_models.chat_modelsr   r   r   Zlangchain_core.messagesr   r   r   r   r   r   Zlangchain_core.outputsr   r   r   Z!langchain_community.llms.friendlir   r"   r-   r.   r    r    r    r!   <module>   s      

