Add Context segment to prompt

This commit is contained in:
Reinier van der Leer
2023-08-23 00:59:17 +02:00
parent 380e92596e
commit b0405ac8ee
4 changed files with 18 additions and 5 deletions

View File

@@ -45,7 +45,7 @@ from .utils.exceptions import (
logger = logging.getLogger(__name__)
class Agent(BaseAgent, ContextMixin, WorkspaceMixin):
class Agent(ContextMixin, WorkspaceMixin, BaseAgent):
"""Agent class for interacting with Auto-GPT."""
def __init__(

View File

@@ -5,6 +5,7 @@ from typing import TYPE_CHECKING
if TYPE_CHECKING:
from ..base import BaseAgent
from autogpt.llm.base import Message
from autogpt.models.context_item import ContextItem
@@ -34,7 +35,7 @@ class AgentContext:
class ContextMixin:
"""Mixin that adds context support to a class"""
"""Mixin that adds context support to a BaseAgent subclass"""
context: AgentContext
@@ -43,6 +44,17 @@ class ContextMixin:
super(ContextMixin, self).__init__(**kwargs)
def construct_base_prompt(self, *args, **kwargs):
if kwargs.get("append_messages") is None:
kwargs["append_messages"] = []
if self.context:
kwargs["append_messages"].insert(
0, Message("system", "# Context\n" + self.context.format_numbered())
)
return super(ContextMixin, self).construct_base_prompt(*args, **kwargs)
def get_agent_context(agent: BaseAgent) -> AgentContext | None:
if isinstance(agent, ContextMixin):

View File

@@ -16,6 +16,9 @@ class WorkspaceMixin:
"""Workspace that the agent has access to, e.g. for reading/writing files."""
def __init__(self, **kwargs):
# Initialize other bases first, because we need the config from BaseAgent
super(WorkspaceMixin, self).__init__(**kwargs)
config: Config = getattr(self, "config")
if not isinstance(config, Config):
raise ValueError(f"Cannot initialize Workspace for Agent without Config")
@@ -24,8 +27,6 @@ class WorkspaceMixin:
self.workspace = Workspace(config.workspace_path, config.restrict_to_workspace)
super(WorkspaceMixin, self).__init__(**kwargs)
def get_agent_workspace(agent: BaseAgent) -> Workspace | None:
if isinstance(agent, WorkspaceMixin):

View File

@@ -38,7 +38,7 @@ from .features.workspace import WorkspaceMixin
logger = logging.getLogger(__name__)
class PlanningAgent(BaseAgent, ContextMixin, WorkspaceMixin):
class PlanningAgent(ContextMixin, WorkspaceMixin, BaseAgent):
"""Agent class for interacting with Auto-GPT."""
ThoughtProcessID = Literal["plan", "action", "evaluate"]