
    Fdo                    h    d Z ddlmZ ddlmZ ddlmZ ddlmZm	Z	 ddl
mZ  G d de	          Zd
S )%Agent manager for managing GPT agents    )annotations)Config)ChatSequence)Messagecreate_chat_completion)	Singletonc                  :    e Zd ZdZddZddZddZddZddZdS )AgentManagerr   configr   c                0    d| _         i | _        || _        d S )Nr   )next_keyagentsr   )selfr   s     OC:\Users\Administrator\Downloads\Auto-GPT-master\autogpt\agent\agent_manager.py__init__zAgentManager.__init__   s"      	     taskstrcreation_promptmodelreturntuple[int, str]c                f   t          j        |t          d|          g          }| j        j        D ]_}|                                s|                    |                                          x}r|                    d |D                        `t          || j                  j
        }|                    d|           d}t          | j        j                  D ]I\  }	}|                                s|                    d |D                       x}
r|	rdnd}| | |
 }J|r|dk    r|                    d|           | j        }| xj        dz  c_        |t!          |          |f| j        |<   | j        j        D ],}|                                s|                    |          }-||fS )	a  Create a new agent and return its key

        Args:
            task: The task to perform
            creation_prompt: Prompt passed to the LLM at creation
            model: The model to use to run this agent

        Returns:
            The key of the new agent
        userc                &    g | ]}t          d i |S  r   .0raw_msgs     r   
<listcomp>z-AgentManager.create_agent.<locals>.<listcomp>*   &     S S S!3!37!3!3 S S Sr   promptr   	assistant c                6    g | ]}|                                 S r   rawr!   ms     r   r#   z-AgentManager.create_agent.<locals>.<listcomp>6        6Q6Q6Q1quuww6Q6Q6Qr   
   )r   	for_modelr   r   pluginscan_handle_pre_instructionpre_instructionr+   extendr   contentadd	enumeratecan_handle_on_instructionon_instructionr   listr   can_handle_post_instructionpost_instruction)r   r   r   r   messagespluginplugin_messagesagent_replyplugins_replyiplugin_resultsepkeys                r   create_agentzAgentManager.create_agent   s     )%'&/2R2R1STTk) 	U 	UF4466 "("8"8"H"HH U S S? S S STTT,DK
 
 

 	 	[+..."4;#677 	G 	GIAv3355  & 5 56Q6Q6Q6Q6Q R RR} G'ddR#0 F# F} F F 	5]b00LLm444m 	 $x..%8Ck) 	? 	?F5577  11+>>KKKr   rF   	str | intmessagec                :   | j         t          |                   \  }}}t          j        ||          }|                    d|           | j        j        D ]W}|                                s|                    d |D                       x}r|	                    d |D                        Xt          || j                  j        }|                    d|           |}	t          | j        j                  D ]I\  }
}|                                s|                    d |D                       x}r|
rdnd}|	 | | }	J|	r|	dk    r|                    d|	           | j        j        D ],}|                                s|                    |          }-|S )	zSend a message to an agent and return its response

        Args:
            key: The key of the agent to message
            message: The message to send to the agent

        Returns:
            The agent's response
        r   c                6    g | ]}|                                 S r   r*   r,   s     r   r#   z.AgentManager.message_agent.<locals>.<listcomp>]   s     9T9T9Ta!%%''9T9T9Tr   c                &    g | ]}t          d i |S r   r   r    s     r   r#   z.AgentManager.message_agent.<locals>.<listcomp>^   r$   r   r%   r'   c                6    g | ]}|                                 S r   r*   r,   s     r   r#   z.AgentManager.message_agent.<locals>.<listcomp>k   r.   r   r/   r(   )r   intr   r1   r7   r   r2   r3   r4   r5   r   r6   r8   r9   r:   r<   r=   )r   rF   rI   r   r>   r   r?   r@   rA   rB   rC   rD   rE   s                r   message_agentzAgentManager.message_agentJ   s    !%CHH 5h  )%::VW%%%k) 	U 	UF4466 "("8"89T9T89T9T9T"U"UU U S S? S S STTT -DK
 
 

 	 	[+...#"4;#677 	G 	GIAv3355  & 5 56Q6Q6Q6Q6Q R RR} G'ddR#0 F# F} F F 	5]b00LLm444k) 	? 	?F5577  11+>>KKr   list[tuple[str | int, str]]c                H    d | j                                         D             S )zkReturn a list of all agents

        Returns:
            A list of tuples of the form (key, task)
        c                $    g | ]\  }\  }}}||fS r   r   )r!   rF   r   _s       r   r#   z,AgentManager.list_agents.<locals>.<listcomp>   s'    III 1\dAqdIIIr   )r   items)r   s    r   list_agentszAgentManager.list_agentsy   s'     JIT[5F5F5H5HIIIIr   boolc                T    	 | j         t          |          = dS # t          $ r Y dS w xY w)zDelete an agent from the agent manager

        Args:
            key: The key of the agent to delete

        Returns:
            True if successful, False otherwise
        TF)r   rN   KeyError)r   rF   s     r   delete_agentzAgentManager.delete_agent   s>    	CHH%4 	 	 	55	s    
''N)r   r   )r   r   r   r   r   r   r   r   )rF   rH   rI   r   r   r   )r   rP   )rF   rH   r   rV   )	__name__
__module____qualname____doc__r   rG   rO   rU   rY   r   r   r   r   r   
   s        //   1  1  1  1 f- - - -^J J J J     r   r   )	metaclassN)r]   
__future__r   autogpt.configr   autogpt.llm.baser   autogpt.llm.chatr   r   autogpt.singletonr	   r   r   r   r   <module>rd      s    + + " " " " " " ! ! ! ! ! ! ) ) ) ) ) ) < < < < < < < < ' ' ' ' ' 'G G G G GY G G G G G Gr   