a
    dg'                     @   s   d dl Z d dlmZ d dlmZmZmZmZmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZmZmZ d dlmZ G d	d
 d
eZdS )    N)datetime)AnyDictListOptionalTuple)LLMChain)BaseLanguageModel)PromptTemplate)	BaseModel
ConfigDictField)GenerativeAgentMemoryc                   @   s  e Zd ZU dZeed< dZee ed< dZ	eed< eed< e
ed< eed	< d
Zeed< dZeed< dZeed< eejdZeed< eedZee ed< eddZeeee dddZeedddZeedddZeeeddd Zeedd!d"Z d5eeee ed#d$d%Z!eedd&d'Z"d6eee e#eef d(d)d*Z$d7eee e#eef d(d+d,Z%ed-d.d/Z&d8eee ed0d1d2Z'd9eee ed0d3d4Z(dS ):GenerativeAgentz<Agent as a character with memory and innate characteristics.nameNageN/AtraitsstatusmemoryllmFverbose summaryi  summary_refresh_seconds)default_factorylast_refresheddaily_summariesT)Zarbitrary_types_allowed)textreturnc                 C   s   t d|  }dd |D S )z8Parse a newline-separated string into a list of strings.z\nc                 S   s   g | ]}t d d| qS )z^\s*\d+\.\s*r   )resubstrip).0line r%   /var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_experimental/generative_agents/generative_agent.py
<listcomp>/       z/GenerativeAgent._parse_list.<locals>.<listcomp>)r    splitr"   )r   linesr%   r%   r&   _parse_list+   s    zGenerativeAgent._parse_list)promptr   c                 C   s   t | j|| j| jdS )z3Create a chain with the same settings as the agent.)r   r,   r   r   )r   r   r   r   selfr,   r%   r%   r&   chain1   s    zGenerativeAgent.chain)observationr   c                 C   s    t d}| |j|d S )NzOWhat is the observed entity in the following observation? {observation}
Entity=)r0   r
   from_templater/   runr"   )r.   r0   r,   r%   r%   r&   _get_entity_from_observation8   s    z,GenerativeAgent._get_entity_from_observation)r0   entity_namer   c                 C   s"   t d}| |j||d S )NzVWhat is the {entity} doing in the following observation? {observation}
The {entity} is)entityr0   r1   )r.   r0   r5   r,   r%   r%   r&   _get_entity_action?   s
    z"GenerativeAgent._get_entity_actionc                 C   s^   t d}| |}| ||}d| j d| }| d| }| j|dj|||gd S )z<Summarize memories that are most relevant to an observation.zC
{q1}?
Context from memory:
{relevant_memories}
Relevant context: 
z!What is the relationship between z and z is r,   )q1queries)r
   r2   r4   r7   r   r/   r3   r"   )r.   r0   r,   r5   Zentity_actionr9   q2r%   r%   r&   summarize_related_memoriesH   s    
z*GenerativeAgent.summarize_related_memories)r0   suffixnowr   c           
      C   s   t d| }| j|d}| |}|du r:t dn|d}t|||| j|| j	d}| j
|jf ddi|}	|	|| jj< | j|djf i | S )	z-React to a given observation or dialogue act.z{agent_summary_description}
It is {current_time}.
{agent_name}'s status: {agent_status}
Summary of relevant context from {agent_name}'s memory:
{relevant_memories}
Most recent observations: {most_recent_memories}
Observation: {observation}

r>   N%B %d, %Y, %I:%M %p)agent_summary_descriptioncurrent_timeZrelevant_memoriesZ
agent_namer0   Zagent_statusZmost_recent_memoriesr   r8   )r
   r2   get_summaryr<   r   r>   strftimedictr   r   r   Zget_num_tokensformatr   Zmost_recent_memories_token_keyr/   r3   r"   )
r.   r0   r=   r>   r,   rA   Zrelevant_memories_strcurrent_time_strkwargsZconsumed_tokensr%   r%   r&   _generate_reactionX   s0    
z"GenerativeAgent._generate_reactionc                 C   s    t d| j dd|  S )N^ r   )r    r!   r   r"   )r.   r   r%   r%   r&   _clean_response|   s    zGenerativeAgent._clean_response)r0   r>   r   c              	   C   s   d}| j |||d}| dd }| ji | jj| j d| d| | jj|i d|v r| |dd }d	| j d
| fS d|v r| |dd }d| j d| fS d	|fS dS )React to a given observation.a1  Should {agent_name} react to the observation, and if so, what would be an appropriate reaction? Respond in one line. If the action is to engage in dialogue, write:
SAY: "what to say"
otherwise, write:
REACT: {agent_name}'s reaction (if anything).
Either do nothing, react, or say something but not both.

r?   
r   
 observed z and reacted by zREACT:FrK   SAY:T said N)	rI   r"   r)   r   save_contextadd_memory_keyr   now_keyrL   )r.   r0   r>   call_to_action_templatefull_resultresultZreactionZ
said_valuer%   r%   r&   generate_reaction   s.    z!GenerativeAgent.generate_reactionc              	   C   s   d}| j |||d}| dd }d|v r| |dd }| ji | jj| j d| d| | jj|i d	| j d
| fS d|v r| |dd }| ji | jj| j d| d| | jj|i d| j d
| fS d	|fS dS )rM   zWhat would {agent_name} say? To end the conversation, write: GOODBYE: "what to say". Otherwise to continue the conversation, write: SAY: "what to say next"

r?   rN   r   zGOODBYE:rP   rO   z
 and said FrR   rQ   TN)	rI   r"   r)   rL   r   rS   rT   r   rU   )r.   r0   r>   rV   rW   rX   ZfarewellZresponse_textr%   r%   r&   generate_dialogue_response   sB    z*GenerativeAgent.generate_dialogue_response)r   c                 C   s.   t d}| |j| j| j dgd S )r   zHow would you summarize {name}'s core characteristics given the following statements:
{relevant_memories}Do not embellish.

Summary: z's core characteristics)r   r:   )r
   r2   r/   r3   r   r"   r-   r%   r%   r&   _compute_agent_summary   s    	
z&GenerativeAgent._compute_agent_summary)force_refreshr>   r   c                 C   s   |du rt  n|}|| j j}| jr4|| jks4|rD|  | _|| _| jdurT| jnd}d| j d| dd| j	  d| j  S )z*Return a descriptive summary of the agent.Nr   zName: z (age: )z
Innate traits: rN   )
r   r>   r   secondsr   r   r[   r   r   r   )r.   r\   r>   rB   Zsince_refreshr   r%   r%   r&   rC      s"    


zGenerativeAgent.get_summaryc                 C   sJ   |du rt  n|}| j||d}|d}| d| d| j d| j S )zFReturn a full header of the agent's status, summary, and current time.N)r\   r>   r@   z
It is z.
z's status: )r   r>   rC   rD   r   r   )r.   r\   r>   r   rG   r%   r%   r&   get_full_header   s
    
zGenerativeAgent.get_full_header)N)N)N)FN)FN))__name__
__module____qualname____doc__str__annotations__r   r   intr   r   r	   r   boolr   r   r   r   r>   r   listr   r   r   Zmodel_configstaticmethodr+   r
   r   r/   r4   r7   r<   rI   rL   r   rY   rZ   r[   rC   r_   r%   r%   r%   r&   r      s`   
	 
$ 
" 
,  r   )r    r   typingr   r   r   r   r   Zlangchain.chainsr   Zlangchain_core.language_modelsr	   Zlangchain_core.promptsr
   Zpydanticr   r   r   Z/langchain_experimental.generative_agents.memoryr   r   r%   r%   r%   r&   <module>   s   