Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
ntindle committed May 10, 2024
2 parents b35b89f + c562b3b commit 6181fb1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
6 changes: 4 additions & 2 deletions autogpts/autogpt/autogpt/agents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

from .base import BaseAgent, BaseAgentConfiguration, BaseAgentSettings
from .features.agent_file_manager import FileManagerComponent
from .features.context import ContextComponent
from .features.context import AgentContext, ContextComponent
from .features.watchdog import WatchdogComponent
from .prompt_strategies.one_shot import (
OneShotAgentActionProposal,
Expand Down Expand Up @@ -82,6 +82,8 @@ class AgentSettings(BaseAgentSettings):
)
"""(STATE) The action history of the agent."""

context: AgentContext = Field(default_factory=AgentContext)


class Agent(BaseAgent, Configurable[AgentSettings]):
default_settings: AgentSettings = AgentSettings(
Expand Down Expand Up @@ -132,7 +134,7 @@ def __init__(
)
self.web_search = WebSearchComponent(legacy_config)
self.web_selenium = WebSeleniumComponent(legacy_config, llm_provider, self.llm)
self.context = ContextComponent(self.file_manager.workspace)
self.context = ContextComponent(self.file_manager.workspace, settings.context)
self.watchdog = WatchdogComponent(settings.config, settings.history)

self.created_at = datetime.now().strftime("%Y%m%d_%H%M%S")
Expand Down
18 changes: 10 additions & 8 deletions autogpts/autogpt/autogpt/agents/features/context.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import contextlib
from pathlib import Path
from typing import Iterator, Optional
from typing import Iterator

from pydantic import BaseModel, Field
from typing_extensions import Annotated

from autogpt.agents.protocols import CommandProvider, MessageProvider
from autogpt.command_decorator import command
Expand All @@ -12,11 +15,10 @@
from autogpt.utils.exceptions import InvalidArgumentError


class AgentContext:
items: list[ContextItem]

def __init__(self, items: Optional[list[ContextItem]] = None):
self.items = items or []
class AgentContext(BaseModel):
items: list[Annotated[ContextItem, Field(discriminator="type")]] = Field(
default_factory=list
)

def __bool__(self) -> bool:
return len(self.items) > 0
Expand All @@ -42,8 +44,8 @@ def format_numbered(self, workspace: FileStorage) -> str:
class ContextComponent(MessageProvider, CommandProvider):
"""Adds ability to keep files and folders open in the context (prompt)."""

def __init__(self, workspace: FileStorage):
self.context = AgentContext()
def __init__(self, workspace: FileStorage, context: AgentContext):
self.context = context
self.workspace = workspace

def get_messages(self) -> Iterator[ChatMessage]:
Expand Down
16 changes: 11 additions & 5 deletions autogpts/autogpt/autogpt/models/context_item.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
from abc import ABC, abstractmethod
from pathlib import Path
from typing import Optional
from typing import Literal, Optional

from pydantic import BaseModel, Field

Expand All @@ -11,7 +11,7 @@
logger = logging.getLogger(__name__)


class ContextItem(ABC):
class BaseContextItem(ABC):
@property
@abstractmethod
def description(self) -> str:
Expand All @@ -38,8 +38,9 @@ def fmt(self, workspace: FileStorage) -> str:
)


class FileContextItem(BaseModel, ContextItem):
class FileContextItem(BaseModel, BaseContextItem):
path: Path
type: Literal["file"] = "file"

@property
def description(self) -> str:
Expand All @@ -54,8 +55,9 @@ def get_content(self, workspace: FileStorage) -> str:
return decode_textual_file(file, self.path.suffix, logger)


class FolderContextItem(BaseModel, ContextItem):
class FolderContextItem(BaseModel, BaseContextItem):
path: Path
type: Literal["folder"] = "folder"

@property
def description(self) -> str:
Expand All @@ -73,7 +75,11 @@ def get_content(self, workspace: FileStorage) -> str:
return "\n".join(items)


class StaticContextItem(BaseModel, ContextItem):
class StaticContextItem(BaseModel, BaseContextItem):
item_description: str = Field(alias="description")
item_source: Optional[str] = Field(alias="source")
item_content: str = Field(alias="content")
type: Literal["static"] = "static"


ContextItem = FileContextItem | FolderContextItem | StaticContextItem
7 changes: 1 addition & 6 deletions cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,7 @@ def create(agent_name):
new_agent_dir = f"./autogpts/{agent_name}"
new_agent_name = f"{agent_name.lower()}.json"

existing_arena_files = [name.lower() for name in os.listdir("./arena/")]

if (
not os.path.exists(new_agent_dir)
and not new_agent_name in existing_arena_files
):
if not os.path.exists(new_agent_dir):
shutil.copytree("./autogpts/forge", new_agent_dir)
click.echo(
click.style(
Expand Down

0 comments on commit 6181fb1

Please sign in to comment.