Team Ai
Apppublic

openenv/coding_env

sourceHugging Faceupdated 3mo agoView on Hugging Face
21likes
interfaces.cpython-311.pyc60 linesDownload Raw Back to __pycache__
1�

2���h����ddlmZmZddlmZmZmZddlmZm	Z	m3Z4Gd�de��ZGd�de��ZGd	�d5e��Z
Gd�de��Zd
S)�)�ABC�abstractmethod)�Any�Protocol�	TypedDict�)�Action�Observation�Statec�(�eZdZUdZeed<eed<dS)�MessagezXA message in a conversation.6 7    Compatible with Huggingface chat template format.8    �role�contentN)�__name__�9__module__�__qualname__�__doc__�str�__annotations__���&/app/src/core/env_server/interfaces.pyr
r

s.���������10�I�I�I�
�L�L�L�L�Lrr
c�b�eZdZdZ		ddeedededzdedef11d	�Z		ddedededefd
�Z12dS)�ModelTokenizerz�Protocol for tokenizers that support chat templates.13 14    This protocol defines the interface that tokenizers must implement15    to work with chat-based environments. It's compatible with16    Huggingface transformers tokenizers.17    TN�conversation�tokenize�return_tensors�kwargs�returnc��dS)a�Apply a chat template to format and optionally tokenize a conversation.18 19        Args:20            conversation: List of message dictionaries with 'role' and 'content'21            tokenize: Whether to tokenize the output22            return_tensors: Format for returned tensors ('pt' for PyTorch)23            **kwargs: Additional arguments24 25        Returns:26            Formatted and optionally tokenized conversation27        Nr)�selfrrrrs     r�apply_chat_templatez"ModelTokenizer.apply_chat_templates	��$	�rF�	token_ids�skip_special_tokensc��dS)aDecode token IDs back to text.28 29        Args:30            token_ids: Token IDs to decode31            skip_special_tokens: Whether to skip special tokens in output32            **kwargs: Additional arguments33 34        Returns:35            Decoded text string36        Nr)r!r#r$rs    r�decodezModelTokenizer.decode3s	��	�r)TN)F)rrrr�listr
�boolrrr"r&rrrrrs����������%)�	���7�m�����d�37�	�38��39
�
����*;@�
�
��
�37�
�KN�
�	�
�
�
�
�
�
rrc�2�eZdZdZededefd���ZdS)�	TransformaTransform observations to add rewards, metrics, or other modifications.40 41    Transforms follow the TorchRL pattern where they take an observation42    and return a (potentially modified) observation. This allows for43    flexible reward computation and observation augmentation.44    �observationrc��dS)z�Transform an observation.45 46        Args:47            observation: The input observation48 49        Returns:50            The transformed observation51        Nr�r!r+s  r�__call__zTransform.__call__Ks	��	
�rN)rrrrrr52r.rrrr*r*CsL���������	
�K�	
�K�	
�	
�	
��^�	
�	
�	
rr*c��eZdZdZddedzfd�Zedefd���Zede	defd���Z53eedefd	�����Z
d54edefd�ZdS)
�Environmentz�Base class for all environment servers following Gym/Gymnasium API.55 56    Args:57        transform: Optional transform to apply to observations58    N�	transformc��||_dS�N�r1)r!r1s  r�__init__zEnvironment.__init___s
��"����rrc��dS)z5Reset the environment and return initial observation.Nr�r!s r�resetzEnvironment.resetb�	��	
�r�actionc��dS)zTake a step in the environment.Nr)r!r:s  r�stepzEnvironment.stepgr9rc��dS)z"Get the current environment state.Nrr7s r�statezEnvironment.statels	��	
�rr+c�>�|j�|�|��S|S)z#Apply transform if one is provided.r4r-s  r�_apply_transformzEnvironment._apply_transformrs"���>�%��>�>�+�.�.�.��rr3)rrrrr*r5rr59r8r	r<�propertyrr>r@rrrr0r0Xs���������#�#�)�d�"2�#�#�#�#��
�{�
�
�
��^�
��
�6�
�k�
�
�
��^�
���
�u�
�
�
��^��X�
��K��K������rr0N)�abcrr�typingrrr�typesr	r60rr
rr*r0rrr�<module>rEs��$�#�#�#�#�#�#�#�+�+�+�+�+�+�+�+�+�+�-�-�-�-�-�-�-�-�-�-������i����)�)�)�)�)�X�)�)�)�X
�
�
�
�
��
�
�
�*�����#�����r