Skip to content

Agent

What is an Agent?

After using bots, maps and workflows, you decide that you want something that can use all of them together and will use them each at the right time. That is where Agents come in to give you an easy way to combine everything together (including other agents) in a simple-to-use package.

Create a Basic Agent

To create an agent using the Agent class from the dandy module similar to how we created the other Dandy processors (bot, decoder and workflow).

from dandy import Agent, Bot


class AssistantAgent(Agent):
    # This attribute is required and determines which other processors you want this agent to have access to using.
    processors = (
        Bot,
    )


intel = AssistantAgent().process('Can you give me an idea for a drawing?')

print(intel.content)
Here are 15 creative drawing ideas spanning different artistic categories: 1. **Cybernetic Wildlife** - Draw a fusion of traditional animals with futuristic technology, like robotic birds with feathered wings or mechanical deer with glowing antlers 2. **Underwater Cityscape** - Create a detailed landscape showing a submerged metropolis with coral buildings and aquatic architecture 3. **Emotional Color Waves** - Design an abstract piece where different emotions are represented by flowing color gradients and geometric shapes 4. **Steampunk Forest** - Illustrate a magical woodland with Victorian-era machinery integrated into trees, steam-powered wildlife, and brass gears growing from the earth 5. **Floating Island Ecosystem** - Draw a fantasy landscape where islands drift through clouds, each with unique ecosystems and creatures adapted to aerial life 6. **Melted Architecture** - Create an abstract composition showing buildings and structures melting like ice cream, blending urban and organic forms 7. **Mythical Food Fusion** - Design a surreal scene featuring traditional dishes transformed into fantastical creatures or magical ingredients 8. **Time-Lapse Nature** - Illustrate a single landscape element (like a flower) that shows its transformation through different time periods or seasons 9. **Invisible World** - Draw microscopic scenes showing the hidden beauty of everyday objects, like dust particles forming intricate patterns or water droplets creating rainbow prisms 10. **Surreal Kitchen** - Create an abstract kitchen scene where appliances and ingredients behave in impossible ways, like floating pots or talking vegetables 11. **Crystal Cave Dwellers** - Design a fantasy scene showing underground creatures living in glowing crystal formations with bioluminescent plants 12. **Weather Personification** - Draw atmospheric elements as characters (rain as a dancing figure, wind as a flowing scarf, snow as a gentle cloud) 13. **Geometric Portrait** - Create a human face using only geometric shapes, lines, and mathematical patterns while maintaining recognizable features 14. **Floating Bookshelf** - Illustrate a whimsical scene where books float in mid-air, creating a library that defies gravity with floating pages and spinning volumes 15. **Seasonal Memory Palace** - Design an abstract composition representing different seasons through memory fragments, like childhood toys scattered across a landscape

Note

The above example only contains one processor so it will be assigned to process everything that the agent does. You can assign anything that is a sub class of the BaseProcessor which is everything in Dandy with a process method.

Example Implementation

Below is a quick demonstration on how you would build out an agent that would help you write emails to any of your favorite museums!

Set up the Required BaseIntel Objects.

intelligence/intel.py
from dandy.intel.intel import BaseIntel


class EmailIntel(BaseIntel):
    to_email_address: str
    from_email_address: str
    subject: str
    body: str


class EmailAddressIntel(BaseIntel):
    email_address: str


class EmailBodyIntel(BaseIntel):
    body: str

Create a mock bot to help us find the museum's email address and one that recursively edits the email to make it more informative and creative.

intelligence/bots.py
from dandy import Bot
from dandy.processor.bot.bot import Bot
from dandy.intel.intel import BaseIntel
from tests.agent.intelligence.intel import EmailAddressIntel, EmailBodyIntel


class MuseumEmailFinderBot(Bot):
    description = 'Finds the email address for a museum.'

    @classmethod
    def process(cls, museum_name: str) -> EmailAddressIntel:
        museum_words = museum_name.lower().split(" ")

        if 'tyrrell' in museum_words:
            return EmailAddressIntel(
                email_address='[email protected]'
            )
        else:
            return EmailAddressIntel(
                email_address=f'info@{"_".join(museum_name.lower().split(" "))}.com'
            )


class EmailProofReadingBot(Bot):
    description = 'Reads over the email body and makes sure it is a concise and informative as possible.'
    @classmethod
    def process(cls, email_body_intel: EmailBodyIntel, read_over_count: int = 2) -> EmailBodyIntel:
        for _ in range(read_over_count):
            email_body_intel = Bot().llm.prompt_to_intel(
                prompt=email_body_intel.body,
                intel_class=EmailBodyIntel,
                postfix_system_prompt='Update the user provided email body and make sure the email is a informative as possible and add some creative flair.'
            )

        return email_body_intel

Use a decoder to pull out the intent from the user request to discover a subject to talk about.

intelligence/decoders.py
from dandy.processor.decoder.decoder import Decoder


class MuseumSubjectDecoder(Decoder):
    mapping_keys_description = 'Colors of Museum Subjects'
    description = 'Matches colors to figure out which subject to learn in a museum.'
    mapping = {
        'green': 'Dinosaurs',
        'red': 'Birds',
        'blue': 'Fishes',
        'yellow': 'Reptiles',
        'purple': 'Monkeys',
    }

Put it all together in an Agent.

intelligence/agents.py
from dandy.processor.agent.agent import Agent
from dandy.processor.bot.bot import Bot
from dandy.llm.prompt.prompt import Prompt
from tests.agent.intelligence.intel import EmailIntel

from tests.agent.intelligence.bots import MuseumEmailFinderBot, EmailProofReadingBot
from tests.agent.intelligence.decoders import MuseumSubjectDecoder


class MuseumEmailAgent(Agent):
    llm_role = (
        Prompt()
        .text('Write an email to a museum based on the users request, the users provided email address will always be the from address for the final email.')
        .text('Figure out what subject the user would be most interested and make sure to note that in the email.')
        .text('Confirm the body of the email is well written.')
        .text('Make up an email address to send to based on the museum name.')
    )
    llm_intel_class = EmailIntel
    processors = (
        Bot,
        MuseumEmailFinderBot,
        MuseumSubjectDecoder,
        EmailProofReadingBot,
    )

We can now import the agent and use it to process an email.

museum.py
from intelligence.agents import MuseumEmailLlmAgent

email_intel = MuseumEmailLlmAgent().process(
    f'The Royal Tyrell Palaeontology Museum, green colors are awesome and my email is [email protected]'
)

print(email_intel)

The Agent will build a plan, user the appropriate processors and return the result as a EmailIntel object.

Ouput
{
    "to_email_address": "[email protected]",
    "from_email_address": "[email protected]",
    "subject": "Inquiry About Paleontology Exhibits and Green Colors",
    "body": "Dear Sir/Madam,\n\nI hope this message finds you well. My name is A. Person, and I am a passionate enthusiast of paleontology, particularly fascinated by the vibrant green hues found in fossilized remains. Your esteemed museum, The Royal Tyrell Palaeontology Museum, has always been a source of inspiration for my studies and curiosity.\n\nI would be immensely grateful if you could provide me with information on any upcoming exhibitions or events that focus on paleontology and the presence of green colors within fossils. Additionally, I am interested in learning more about your museum's research initiatives related to this topic.\n\nThank you very much for considering my request. I look forward to hearing from you soon.\n\nBest regards,\nA. Person"
}