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)
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.
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.
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.
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.
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.
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.
{
"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"
}