-6 C
United States of America
Friday, December 13, 2024

What’s Atomic Brokers?


AI brokers are clever packages that carry out duties autonomously, reworking numerous industries. As AI brokers acquire reputation, numerous frameworks have emerged to simplify their growth and integration. Atomic Brokers is without doubt one of the newer entries on this area, designed to be light-weight, modular, and straightforward to make use of. Atomic Brokers supplies a hands-on, clear strategy, permitting builders to work straight with particular person parts. This makes it a sensible choice for constructing extremely customizable AI techniques that preserve readability and management at each step. On this article, we’ll discover how Atomic Brokers works and why its minimalist design can profit builders and AI lovers alike.

What’s Atomic Brokers?

How does Atomic Brokers Work?

Atomic means non-divisible. Within the Atomic Brokers framework, every agent is constructed from the bottom up utilizing fundamental, unbiased parts. In contrast to frameworks like AutoGen and Crew AI, which depend on high-level abstractions to handle inner parts, Atomic Brokers takes a low-level, modular strategy. This enables builders to straight management the person parts, akin to enter/output dealing with, device integration, and reminiscence administration, making every agent customizable and predictable.

Via a hands-on implementation with code, we’ll see how Atomic Brokers retains every half seen. This permits fine-tuned management over every step of the method, from enter processing to response technology.

What goes into the LLM

Constructing a Easy Agent on Atomic Brokers

Pre-requisites

Earlier than constructing Atomic brokers, guarantee you might have the required API keys for the required LLMs.

Load the .env file with the API keys wanted.

from dotenv import load_dotenv
load_dotenv(./env)

Key Libraries Required

  • atomic-agents – 1.0.9
  • teacher – 1.6.4 (The teacher library is used to get structured knowledge from the LLMs.)
  • wealthy – 13.9.4 (The wealthy library is used for textual content formatting.)

Constructing the Agent

Now let’s construct a easy agent utilizing Atomic Brokers.

Step 1: Import the required libraries.

import os
import teacher
import openai
from wealthy.console import Console
from wealthy.panel import Panel
from wealthy.textual content import Textual content
from wealthy.reside import Stay
from atomic_agents.brokers.base_agent import BaseAgent, BaseAgentConfig, BaseAgentInputSchema, BaseAgentOutputSchema

Now lets outline the shopper, LLM, and temperature parameters.

Step 2: Initialize the LLM.

shopper = teacher.from_openai(openai.OpenAI())

Step 3: Setup the agent.

agent = BaseAgent(

	config=BaseAgentConfig(

    	shopper=shopper,

    	mannequin="gpt-4o-mini",

    	temperature=0.2

	) )

We are able to run the agent now

outcome = agent.run(BaseAgentInputSchema(chat_message="why is mercury liquid at room temperature?"))
print(outcome.chat_message)

That’s it. We’ve constructed a easy agent with minimal code.

Allow us to initialize the agent and run it once more and see the results of the under code

agent = BaseAgent(
	config=BaseAgentConfig(
    	shopper=shopper,
    	mannequin="gpt-4o-mini",
    	temperature=0.2
	) )
agent.run(BaseAgentInputSchema(chat_message="what's its electron configuration?"))

>> BaseAgentOutputSchema(chat_message="To supply the electron configuration, I have to know which component you might be referring to. Might you please specify the component or its atomic quantity?")

Since now we have initialized the agent once more, it doesn’t know now we have requested the query about mercury.

So, let’s add reminiscence.

Including Reminiscence to the Agent

Step 1: Import the required Class and initialize the reminiscence.

from atomic_agents.lib.parts.agent_memory import AgentMemory
reminiscence = AgentMemory(max_messages=50)

Step 2: Construct the agent with reminiscence.

agent = BaseAgent(
	config=BaseAgentConfig(
    	shopper=shopper,
    	mannequin="gpt-4o-mini",
    	temperature=0.2,
    	reminiscence=reminiscence
	) )

Now, we are able to ask the above-mentioned questions once more in an identical manner. However on this case, it’ll reply the electron configuration.

We are able to additionally entry all of the messages with reminiscence.get_history()

Now, let’s change the system immediate.

Altering the System Immediate

Step 1: Import the required Class and take a look at the prevailing system immediate.

from atomic_agents.lib.parts.system_prompt_generator import SystemPromptGenerator

print(agent.system_prompt_generator.generate_prompt())
agent.system_prompt_generator.background

Step 2: Outline the customized system immediate.

system_prompt_generator = SystemPromptGenerator(
	background=[
    	"This assistant is a specialized Physics expert 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.",
	],
)

We are able to additionally add a message to the reminiscence individually.

Step 3: Add a message to the reminiscence.

reminiscence = AgentMemory(max_messages=50)

initial_message = BaseAgentOutputSchema(chat_message="Hi there! How can I help you right now?")

reminiscence.add_message("assistant", initial_message)

Step 4: Now, we are able to construct the agent with reminiscence and a customized system immediate.

agent = BaseAgent(
	config=BaseAgentConfig(
    	shopper=shopper,
    	mannequin="gpt-4o-mini",
    	temperature=0.2,
    	system_prompt_generator=system_prompt_generator,
    	reminiscence=reminiscence
	) )

outcome = agent.run(BaseAgentInputSchema(chat_message="why is mercury liquid at room temperature?"))
print(outcome.chat_message)

Right here’s the output in rhyming verse:

AI agent on Atomic Agents - output

Up thus far, we’ve been having a dialog, one message at a time. Now, let’s discover learn how to have interaction in a steady chat with the agent.

Constructing a Steady Agent Chat in Atomic Brokers

In Atomic Brokers, including chat performance is so simple as utilizing some time loop.

# outline console for formatting the chat textual content.
console is used to print and format the dialog.
console = Console()

# Initialize the reminiscence and agent
reminiscence = AgentMemory(max_messages=50)
agent = BaseAgent(
	config=BaseAgentConfig(
    	shopper=shopper,
    	mannequin="gpt-4o-mini",
    	temperature=0.2,
    	reminiscence=reminiscence
	)
)

We’ll use “exit” and “stop” key phrases to exit the chat.

whereas True:
    # Immediate the consumer for enter with a styled immediate
    user_input = console.enter("[bold blue]You:[/bold blue] ")
    # Verify if the consumer needs to exit the chat
    if user_input.decrease() in ["exit", "quit"]:
        console.print("Exiting chat...")
        break

    # Course of the consumer's enter by means of the agent and get the response
    input_schema = BaseAgentInputSchema(chat_message=user_input)
    response = agent.run(input_schema)

    agent_message = Textual content(response.chat_message, fashion="daring inexperienced")
    console.print(Textual content("Agent:", fashion="daring inexperienced"), finish=" ")
    console.print(agent_message)

With the above code, the whole output of the mannequin is displayed without delay. We are able to additionally stream the output message like we do with ChatGPT.

Constructing a Chat Stream in Atomic Brokers

Within the Chat as outlined above, LLM output is displayed solely after the entire content material is generated. If the output is lengthy, it’s higher to stream the output in order that we are able to take a look at the output as it’s being generated. Let’s see how to try this.

Step 1: To stream the output, we have to use the asynchronous shopper of the LLM.

shopper = teacher.from_openai(openai.AsyncOpenAI())

Step 2: Outline the agent.

reminiscence = AgentMemory(max_messages=50)
agent = BaseAgent(
	config=BaseAgentConfig(
    	shopper=shopper,
    	mannequin="gpt-4o-mini",
    	temperature=0.2,
    	reminiscence=reminiscence
	) )

Now let’s see learn how to stream the chat.

Step 3: Add the perform to stream the chat.

async def principal():
    # Begin an infinite loop to deal with consumer inputs and agent responses
    whereas True:
        # Immediate the consumer for enter with a styled immediate
        user_input = console.enter("n[bold blue]You:[/bold blue] ")
        # Verify if the consumer needs to exit the chat
        if user_input.decrease() in ["exit", "quit"]:
            console.print("Exiting chat...")
            break

        # Course of the consumer's enter by means of the agent and get the streaming response
        input_schema = BaseAgentInputSchema(chat_message=user_input)
        console.print()  # Add newline earlier than response

        # Use Stay show to indicate streaming response
        with Stay("", refresh_per_second=4, auto_refresh=True) as reside:
            current_response = ""
            async for partial_response in agent.stream_response_async(input_schema):
                if hasattr(partial_response, "chat_message") and partial_response.chat_message:
                    # Solely replace if now we have new content material
                    if partial_response.chat_message != current_response:
                        current_response = partial_response.chat_message
                        # Mix the label and response within the reside show
                        display_text = Textual content.assemble(("Agent: ", "daring inexperienced"), (current_response, "inexperienced"))
                        reside.replace(display_text)

In case you are utilizing jupyter lab or jupyter pocket book, ensure you run the under code, operating the async perform outlined above.

import nest_asyncio
nest_asyncio.apply()

Step 4: Now we are able to run the async perform principal.

import asyncio
asyncio.run(principal())

Including Customized Output Schema in Atomic Brokers

Let’s see learn how to add customized output schema which is helpful for getting structured output for the agent.

Step 1: Outline the Class as proven right here.

from typing import Listing
from pydantic import Area

from atomic_agents.lib.base.base_io_schema import BaseIOSchema

class CustomOutputSchema(BaseIOSchema):
    """This schema represents the response generated by the chat agent, together with steered follow-up questions."""

    chat_message: str = Area(
        ...,
        description="The chat message exchanged between the consumer and the chat agent.",
    )
    suggested_user_questions: Listing[str] = Area(
        ...,
        description="An inventory of steered follow-up questions the consumer might ask the agent.",
    )
custom_system_prompt = SystemPromptGenerator(
    background=[
        "This assistant is a knowledgeable AI designed to be helpful, friendly, and informative.",
        "It has a wide range of knowledge on various topics and can engage in diverse conversations.",
    ],
    steps=[
        "Analyze the user's input to understand the context and intent.",
        "Formulate a relevant and informative response based on the assistant's knowledge.",
        "Generate 3 suggested follow-up questions for the user to explore the topic further.",
    ],
    output_instructions=[
        "Provide clear, concise, and accurate information in response to user queries.",
        "Maintain a friendly and professional tone throughout the conversation.",
        "Conclude each response with 3 relevant suggested questions for the user.",
    ],
)

Step 2: Outline the customized system immediate.

custom_system_prompt = SystemPromptGenerator(
    background=[
        "This assistant is a knowledgeable AI designed to be helpful, friendly, and informative.",
        "It has a wide range of knowledge on various topics and can engage in diverse conversations.",
    ],
    steps=[
        "Analyze the user's input to understand the context and intent.",
        "Formulate a relevant and informative response based on the assistant's knowledge.",
        "Generate 3 suggested follow-up questions for the user to explore the topic further.",
    ],
    output_instructions=[
        "Provide clear, concise, and accurate information in response to user queries.",
        "Maintain a friendly and professional tone throughout the conversation.",
        "Conclude each response with 3 relevant suggested questions for the user.",
    ],
)

Now we are able to outline the shopper, agent, and loop for the stream as now we have finished earlier than.

Step 3: Outline the shopper, agent, and loop.

shopper = teacher.from_openai(openai.AsyncOpenAI())
reminiscence = AgentMemory(max_messages=50)
agent = BaseAgent(
	config=BaseAgentConfig(
    	shopper=shopper,
    	mannequin="gpt-4o-mini",
    	temperature=0.2,
    	system_prompt_generator=custom_system_prompt,
    	reminiscence=reminiscence,
    	output_schema=CustomOutputSchema
	)
)

async def principal():
    # Begin an infinite loop to deal with consumer inputs and agent responses
    whereas True:
        # Immediate the consumer for enter with a styled immediate
        user_input = console.enter("[bold blue]You:[/bold blue] ")
        # Verify if the consumer needs to exit the chat
        if user_input.decrease() in ["/exit", "/quit"]:
            console.print("Exiting chat...")
            break

        # Course of the consumer's enter by means of the agent and get the streaming response
        input_schema = BaseAgentInputSchema(chat_message=user_input)
        console.print()  # Add newline earlier than response

        # Use Stay show to indicate streaming response
        with Stay("", refresh_per_second=4, auto_refresh=True) as reside:
            current_response = ""
            current_questions: Listing[str] = []

            async for partial_response in agent.stream_response_async(input_schema):
                if hasattr(partial_response, "chat_message") and partial_response.chat_message:
                    # Replace the message half
                    if partial_response.chat_message != current_response:
                        current_response = partial_response.chat_message

                    # Replace questions if obtainable
                    if hasattr(partial_response, "suggested_user_questions"):
                        current_questions = partial_response.suggested_user_questions

                    # Mix all parts for show
                    display_text = Textual content.assemble(("Agent: ", "daring inexperienced"), (current_response, "inexperienced"))

                    # Add questions if now we have them
                    if current_questions:
                        display_text.append("nn")
                        display_text.append("Advised questions you might ask:n", fashion="daring cyan")
                        for i, query in enumerate(current_questions, 1):
                            display_text.append(f"{i}. {query}n", fashion="cyan")

                    reside.replace(display_text)

        console.print()
asyncio.run(principal())

The output is as follows:

atomic agents

Conclusion

On this article, now we have seen how we are able to construct brokers utilizing particular person parts. Atomic Brokers supplies a streamlined, modular framework that empowers builders with full management over every element of their AI brokers. By emphasizing simplicity and transparency, it permits for extremely customizable agent options with out the complexity of high-level abstractions. This makes Atomic Brokers a wonderful selection for these in search of hands-on, adaptable AI growth. As AI agent growth evolves, we are going to see extra options arising in Atomic Brokers, providing a minimalist strategy for constructing clear, tailor-made options.

Do you want to be taught extra about AI brokers and learn how to construct them? Our Agentic AI Pioneer Program could make you an AI agent professional, no matter your expertise and background. Do test it out right now!

Incessantly Requested Questions

Q1. What makes Atomic Brokers totally different from different AI agent frameworks?

A. Atomic Brokers emphasizes a modular, low-level strategy, permitting builders to straight handle every element. In contrast to high-level frameworks, it provides extra management and transparency, making it very best for constructing extremely personalized brokers.

Q2. Can I exploit Atomic Brokers with common LLMs like GPT-4o?

A. Sure, Atomic Brokers is suitable with numerous LLMs, together with GPT-4o. By integrating with APIs like OpenAI’s, you possibly can leverage these fashions throughout the framework to construct responsive and clever brokers.

Q3. How does Atomic Brokers deal with reminiscence for contextual conversations?

A. Atomic Brokers contains reminiscence administration parts that permit brokers to retain previous interactions. This permits context-aware conversations, the place the agent can keep in mind earlier messages and construct on them for a cohesive consumer expertise.

This fall. Is it doable to customise the agent’s persona and response fashion?

A. Sure, Atomic Brokers helps customized system prompts, permitting you to outline particular response kinds and behaviors on your agent, making it adaptable to varied conversational contexts or skilled tones.

Q5. Is Atomic Brokers appropriate for production-level functions?

A. Whereas Atomic Brokers is light-weight and developer-friendly, it’s nonetheless a brand new framework that wants additional exploring to check for manufacturing use. Its modular construction helps scaling and permits builders to construct, take a look at, and deploy dependable AI brokers effectively.

I’m working as an Affiliate Knowledge Scientist at Analytics Vidhya, a platform devoted to constructing the Knowledge Science ecosystem. My pursuits lie within the fields of Pure Language Processing (NLP), Deep Studying, and AI Brokers.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles