DEV Community

Cover image for Atomic Agents Framework: Building a Simple AI Agent
Amulya Kumar for HyScaler

Posted on

14

Atomic Agents Framework: Building a Simple AI Agent

Atomic Agents Framework: Imagine building AI applications as easily as snapping together LEGO blocks. This is the revolutionary concept behind Atomic Agents, a new multi-agent framework inspired by Atomic Design principles. By breaking down AI systems into smaller, self-contained, reusable components, Atomic Agents promises a future where AI development is both modular and predictable.

Understanding Atomic Design

Before diving into Atomic Agents, let’s explore the foundation it’s built upon: Atomic Design. Created by Brad Frost, this methodology revolutionizes UI design by deconstructing interfaces into fundamental, reusable parts—like atoms, molecules, and organisms in chemistry. This approach ensures scalable, consistent user interfaces and simplifies the modification of individual components with minimal impact. Think of it as assembling intricate structures with simple LEGO pieces.

Chaining Agents and Tools

The true potential of the Atomic Agents Framework is unlocked when chaining the inputs and outputs of various tools and agents. By assigning an agent’s output schema as the input schema for a tool or another agent, developers can create complex yet comprehensible AI applications. Changing a tool or schema at any point won’t disrupt the system, ensuring flexibility and robustness.

Building a Simple AI Agent

To illustrate, let’s build a simple AI agent using Atomic Agents.

Install the Atomic Agents Package:
bash
Copy code
pip install atomic-agents openai

Alternatively, clone the repository and contribute to its improvement.

Import Necessary Components:
Python
Copy code
import os

from atomic_agents.lib.components.system_prompt_generator import SystemPromptGenerator, SystemPromptInfo

Define System Prompt Information:
Python
Copy code
system_prompt = SystemPromptInfo(

    background=['This assistant is a general-purpose AI designed to be helpful and friendly.'],

    steps=[

        'Understand the user\'s input and provide a relevant response.',

        'Respond to the user.'

    ],

    output_instructions=[

        'Provide helpful and relevant information to assist the user.',

        'Be friendly and respectful in all interactions.',

        'Always answer in rhyming verse.'

    ]

)

system_prompt_generator = SystemPromptGenerator(system_prompt)

Initialize Chat Memory:
Python
Copy code
from atomic_agents.lib.components.chat_memory import ChatMemory

memory = ChatMemory()

initial_memory = [{'role': 'assistant', 'content': 'How do you do? What can I do for you? Tell me, pray, what is your need today?'}]

memory.load(initial_memory)

Create Custom Chatbot:
Python
Copy code
from atomic_agents.agents.base_chat_agent import BaseChatAgent, BaseChatAgentConfig

import openai

import instructor

API_KEY = os.getenv('OPENAI_API_KEY', '')

client = instructor.from_openai(openai.OpenAI(api_key=API_KEY))

agent = BaseChatAgent(

    config=BaseChatAgentConfig(

        client=client,

        system_prompt_generator=system_prompt_generator,

        model='gpt-3.5-turbo',

        memory=memory,

    )

)

print(f'Agent: {initial_memory[0]["content"]}')

while True:

    user_input = input('You: ')

    if user_input.lower() in ['/exit', '/quit']:

        print('Exiting chat...')

        break

    response = agent.run(agent.input_schema(chat_message=user_input))

    print(f'Agent: {response.chat_message}')

Behind the Scenes

Underneath, each tool and agent has an input schema and an output schema, extending from the BaseAgentIO class. These schemas facilitate structured responses and documentation generation, enhancing clarity and precision in both agent performance and documentation.

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay