mirror of
https://github.com/aljazceru/Auto-GPT.git
synced 2025-12-18 14:34:23 +01:00
Add pre_instruction and on_instruction hooks.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
"""Agent manager for managing GPT agents"""
|
||||
from typing import List, Tuple, Union
|
||||
from autogpt.llm_utils import create_chat_completion
|
||||
from autogpt.config.config import Singleton
|
||||
from autogpt.config.config import Singleton, Config
|
||||
|
||||
|
||||
class AgentManager(metaclass=Singleton):
|
||||
@@ -10,6 +10,7 @@ class AgentManager(metaclass=Singleton):
|
||||
def __init__(self):
|
||||
self.next_key = 0
|
||||
self.agents = {} # key, (task, full_message_history, model)
|
||||
self.cfg = Config()
|
||||
|
||||
# Create new GPT agent
|
||||
# TODO: Centralise use of create_chat_completion() to globally enforce token limit
|
||||
@@ -28,6 +29,10 @@ class AgentManager(metaclass=Singleton):
|
||||
messages = [
|
||||
{"role": "user", "content": prompt},
|
||||
]
|
||||
for plugin in self.cfg.plugins:
|
||||
plugin_messages = plugin.pre_instruction(messages)
|
||||
if plugin_messages:
|
||||
messages.extend(plugin_messages)
|
||||
|
||||
# Start GPT instance
|
||||
agent_reply = create_chat_completion(
|
||||
@@ -35,9 +40,13 @@ class AgentManager(metaclass=Singleton):
|
||||
messages=messages,
|
||||
)
|
||||
|
||||
# Update full message history
|
||||
messages.append({"role": "assistant", "content": agent_reply})
|
||||
plugins_reply = agent_reply
|
||||
for plugin in self.cfg.plugins:
|
||||
plugin_result = plugin.on_instruction(messages)
|
||||
if plugin_result:
|
||||
plugins_reply = f"{plugins_reply}\n{plugin_result}"
|
||||
|
||||
messages.append({"role": "assistant", "content": plugins_reply})
|
||||
key = self.next_key
|
||||
# This is done instead of len(agents) to make keys unique even if agents
|
||||
# are deleted
|
||||
@@ -45,7 +54,7 @@ class AgentManager(metaclass=Singleton):
|
||||
|
||||
self.agents[key] = (task, messages, model)
|
||||
|
||||
return key, agent_reply
|
||||
return key, plugins_reply
|
||||
|
||||
def message_agent(self, key: Union[str, int], message: str) -> str:
|
||||
"""Send a message to an agent and return its response
|
||||
@@ -62,16 +71,26 @@ class AgentManager(metaclass=Singleton):
|
||||
# Add user message to message history before sending to agent
|
||||
messages.append({"role": "user", "content": message})
|
||||
|
||||
for plugin in self.cfg.plugins:
|
||||
plugin_messages = plugin.pre_instruction(messages)
|
||||
if plugin_messages:
|
||||
messages.extend(plugin_messages)
|
||||
|
||||
# Start GPT instance
|
||||
agent_reply = create_chat_completion(
|
||||
model=model,
|
||||
messages=messages,
|
||||
)
|
||||
|
||||
plugins_reply = agent_reply
|
||||
for plugin in self.cfg.plugins:
|
||||
plugin_result = plugin.on_instruction(messages)
|
||||
if plugin_result:
|
||||
plugins_reply = f"{plugins_reply}\n{plugin_result}"
|
||||
# Update full message history
|
||||
messages.append({"role": "assistant", "content": agent_reply})
|
||||
messages.append({"role": "assistant", "content": plugins_reply})
|
||||
|
||||
return agent_reply
|
||||
return plugins_reply
|
||||
|
||||
def list_agents(self) -> List[Tuple[Union[str, int], str]]:
|
||||
"""Return a list of all agents
|
||||
|
||||
Reference in New Issue
Block a user