Creating Components

The Minimal Component

Components can be used to implement various functionalities like providing messages to the prompt, executing code, or interacting with external services.

Component is a class that inherits from AgentComponent OR implements one or more protocols. Every protocol inherits AgentComponent, so your class automatically becomes a component once you inherit any protocol.

class MyComponent(AgentComponent):
    pass

This is already a valid component, but it doesn't do anything yet. To add some functionality to it, you need to implement one or more protocols.

Let's create a simple component that adds "Hello World!" message to the agent's prompt. To do this we need to implement MessageProvider protocol in our component. MessageProvider is an interface with get_messages method:

# No longer need to inherit AgentComponent, because MessageProvider already does it
class HelloComponent(MessageProvider):
    def get_messages(self) -> Iterator[ChatMessage]:
        yield ChatMessage.user("Hello World!")

Now we can add our component to an existing agent or create a new Agent class and add it there:

class MyAgent(Agent):
    self.hello_component = HelloComponent()

get_messages will called by the agent each time it needs to build a new prompt and the yielded messages will be added accordingly.

Passing Data to and Between Components

Since components are regular classes you can pass data (including other components) to them via the __init__ method. For example we can pass a config object and then retrieve an API key from it when needed:

circle-info

Note

Component-specific configuration handling isn't implemented yet.

Configuring Components

Components can be configured using a pydantic model. To make component configurable, it must inherit from ConfigurableComponent[BM] where BM is the configuration class inheriting from pydantic's BaseModel. You should pass the configuration instance to the ConfigurableComponent's __init__ or set its config property directly. Using configuration allows you to load confugration from a file, and also serialize and deserialize it easily for any agent. To learn more about configuration, including storing sensitive information and serialization see Component Configurationarrow-up-right.

Providing Commands

To extend what an agent can do, you need to provide commands using CommandProvider protocol. For example to allow agent to multiply two numbers, you can create a component like this:

To learn more about commands see 🛠️ Commandsarrow-up-right.

Prompt Structure

After components provided all necessary data, the agent needs to build the final prompt that will be send to a llm. Currently, PromptStrategy (not a protocol) is responsible for building the final prompt.

If you want to change the way the prompt is built, you need to create a new PromptStrategy class, and then call relevant methods in your agent class. You can have a look at the default strategy used by the AutoGPT Agent: OneShotAgentPromptStrategyarrow-up-right, and how it's used in the Agentarrow-up-right (search for self.prompt_strategy).

Example UserInteractionComponent

Let's create a slightly simplified version of the component that is used by the built-in agent. It gives an ability for the agent to ask user for input in the terminal.

  1. Create a class for the component that inherits from CommandProvider.

  2. Implement command method that will ask user for input and return it.

  3. The command needs to be decorated with @command.

  4. We need to implement CommandProvider's get_commands method to yield the command.

  5. Since agent isn't always running in the terminal or interactive mode, we need to disable this component by setting self._enabled=False when it's not possible to ask for user input.

The final component should look like this:

Now if we want to use our user interaction instead of the default one we need to somehow remove the default one (if our agent inherits from Agent the default one is inherited) and add our own. We can simply override the user_interaction in __init__ method:

Alternatively we can disable the default component by setting it to None:

Learn more

The best place to see more examples is to look at the built-in components in the classic/original_autogpt/componentsarrow-up-right and classic/original_autogpt/commandsarrow-up-right directories.

Guide on how to extend the built-in agent and build your own: 🤖 Agentsarrow-up-right Order of some components matters, see 🧩 Componentsarrow-up-right to learn more about components and how they can be customized. To see built-in protocols with accompanying examples visit ⚙️ Protocolsarrow-up-right.

Last updated

Was this helpful?