a
    dg                      @  s   d dl mZ d dlmZmZmZmZmZ G dd dZddddd	Z	d
ddddZ
d#dd
dddddZdd
ddddZd$dd
dddddZd%dd
ddd d!d"ZdS )&    )annotations)AnyDictListOptionalUnionc                   @  s0   e Zd ZddddddZddd	d
ZeZdS )_EmbedFr   boolvaluekeepc                 C  s   || _ || _d S Nr
   )selfr   r    r   u/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_experimental/rl_chain/helpers.py__init__   s    z_Embed.__init__str)returnc                 C  s
   t | jS r   )r   r   )r   r   r   r   __str__   s    z_Embed.__str__N)F)__name__
__module____qualname__r   r   __repr__r   r   r   r   r      s   r   r   r   )	embeddingr   c                 C  s   d dd t| D S )z!Convert an embedding to a string. c                 S  s   g | ]\}}| d | qS ):r   ).0ier   r   r   
<listcomp>       z'stringify_embedding.<locals>.<listcomp>)join	enumerate)r   r   r   r   stringify_embedding   s    r#   r   r	   )itemr   c                 C  s    t | tpt | tot | jtS )zCheck if an item is a string.)
isinstancer   r   r   )r$   r   r   r   is_stringtype_instance   s    
r&   NzUnion[str, _Embed]zOptional[str]z Dict[str, Union[str, List[str]]])r$   model	namespacer   c                 C  s   d}t | tr8t|| j}| jrd| jddd }n,t | trP| dd}ntdt	|  d|du rttd||| iS )z#Embed a string or an _Embed object. r   _zUnsupported type z for embeddingNzPThe default namespace must be provided when embedding a string or _Embed object.)
r%   r   r#   encoder   r   replacer   
ValueErrortype)r$   r'   r(   Zkeep_strencodedr   r   r   embed_string_type   s    

r0   r   zDict[str, Any])r$   r'   r   c                 C  sh   i }|   D ]V\}}t|trPg ||< |D ]"}t|||}|| ||  q*q|t||| q|S )zEmbed a dictionary item.)itemsr%   listr0   appendupdate)r$   r'   Z
inner_dictns
embed_itemZembed_list_itemZembeddedr   r   r   embed_dict_type6   s    
r7   r2   z&List[Dict[str, Union[str, List[str]]]]c                   s   g }| D ]z}t |tr(|t|| qt |trpt|||}tt|d    fdd|D i}|| q|t||| q|S )zEmbed a list item.r   c                   s   g | ]}|  qS r   r   )r   r$   Z	first_keyr   r   r   R   r    z#embed_list_type.<locals>.<listcomp>)	r%   dictr3   r7   r2   embed_list_typenextiterr0   )r$   r'   r(   Zret_listr6   Zitem_embeddinggroupingr   r8   r   r:   D   s    

r:   zEUnion[Union[str, _Embed], Dict, List[Union[str, _Embed]], List[Dict]])to_embedr'   r(   r   c                 C  sf   t | trt | jts t | tr.t| ||gS t | trDt| |gS t | trZt| ||S t	ddS )a  
    Embed the actions or context using the SentenceTransformer model
    (or a model that has an `encode` function).

    Attributes:
        to_embed: (Union[Union(str, _Embed(str)), Dict, List[Union(str, _Embed(str))], List[Dict]], required) The text to be embedded, either a string, a list of strings or a dictionary or a list of dictionaries.
        namespace: (str, optional) The default namespace to use when dictionary or list of dictionaries not provided.
        model: (Any, required) The model to use for embedding
    Returns:
        List[Dict[str, str]]: A list of dictionaries where each dictionary has the namespace as the key and the embedded string as the value
    z"Invalid input format for embeddingN)
r%   r   r   r   r0   r9   r7   r2   r:   r-   )r>   r'   r(   r   r   r   embedY   s    

r?   )N)N)N)
__future__r   typingr   r   r   r   r   r   r#   r&   r0   r7   r:   r?   r   r   r   r   <module>   s   	   