LLM 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 LLM 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 LLM Agent
To create an agent using the BaseLlmAgent
class from the dandy.llm
module similar to how we created the other Dandy processors (bot, map and workflow).
from dandy.llm import BaseLlmAgent, LlmBot
class AssistantAgent(BaseLlmAgent):
# This attribute is required and determines which other processors you want this agent to have access to using.
processors = (
LlmBot,
)
intel = AssistantAgent.process('Can you give me an idea for a drawing?')
print(intel.text)
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!
Setup the Required BaseIntel
Objects.
from dandy.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.
from typing import Any
from dandy.bot.bot import BaseBot
from dandy.llm import DefaultLlmIntel
from tests.llm.agent.intel import EmailAddressIntel
class MuseumEmailFinderBot(BaseBot):
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'
)
Use a map to pull out the intent from the user request to discover a subject to talk about.
from dandy.llm import BaseLlmMap, Map
class MuseumSubjectLlmMap(BaseLlmMap):
map_keys_description = 'Colors of Museum Subjects'
description = 'Matches colors to figure out which subject to learn in a museum.'
map = Map({
'green': 'Dinosaurs',
'red': 'Birds',
'blue': 'Fishes',
'yellow': 'Reptiles',
'purple': 'Monkeys',
})
Have a workflow that recursively edits the email to make it more informative and creative.
from dandy.workflow.workflow import BaseWorkflow
from dandy.llm.bot.llm_bot import LlmBot
from tests.llm.agent.intel import EmailBodyIntel
class EmailProofReadingWorkflow(BaseWorkflow):
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 = LlmBot.process(
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
Put it all together in an Agent.
from dandy.llm.bot.llm_bot import LlmBot
from dandy.llm.agent.llm_agent import BaseLlmAgent
from dandy.llm.prompt.prompt import Prompt
from tests.llm.agent.intel import EmailIntel
from tests.llm.agent.llm_bots import MuseumEmailFinderBot
from tests.llm.agent.llm_maps import MuseumSubjectLlmMap
from tests.llm.agent.workflows import EmailProofReadingWorkflow
class MuseumEmailLlmAgent(BaseLlmAgent[EmailIntel]):
instructions_prompt = (
Prompt()
.text('Write an email to a museum based on the users input, the users email will always be the from address.')
.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.')
)
intel_class = EmailIntel
processors = (
LlmBot,
MuseumEmailFinderBot,
MuseumSubjectLlmMap,
EmailProofReadingWorkflow,
)
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"
}