Updating agent traits

This notebook demonstrates how to update an agent’s traits with new answers to questions using the add traits() and to_agent_list() methods.

Learn more about designing and using AI agents with surveys in the documentation.

Updating a single agent

We start by creating an agent and using it with a survey:

[1]:
from edsl import Agent, QuestionFreeText, QuestionMultipleChoice, Survey
[2]:
agent = Agent(
    name = "college student",
    traits = {
        "persona": "You are a sophomore at a community college in upstate New York.",
        "year": "sophomore",
        "school": "community college",
        "major": "biology",
        "state": "New York"
    }
)
[3]:
q1 = QuestionFreeText(
    question_name = "career_interests",
    question_text = "What are your career interests?"
)

q2 = QuestionMultipleChoice(
    question_name = "attend_grad_school",
    question_text = "Do you plan to attend grad school?",
    question_options = ["Yes", "No", "Undecided"]
)

survey = Survey([q1, q2])
[4]:
results = survey.by(agent).run()
Remote Job Log (2024-12-14 10:24:19)
Remote inference activated. Sending job to server...
Your survey is running at the Expected Parrot server...
Job sent to server. (Job uuid=8c8a74c1-59ef-4989-9abb-f82c5cc5fd18).
Job status: queued - last update: 2024-12-14 10:24:02 AM
Job status: running - last update: 2024-12-14 10:24:09 AM
Job status: running - last update: 2024-12-14 10:24:13 AM
[5]:
results.select("career_interests", "attend_grad_school")
[5]:
answer.career_interests answer.attend_grad_school
I'm majoring in biology, so I'm really interested in exploring careers related to that field. I'm considering options like research, environmental science, or possibly healthcare. I'm still figuring it out, but I'm excited about the possibilities and want to find something that combines my love for science with making a positive impact.Undecided

Creating a new agent with original and new traits

Here we use the to_agent_list() method to create a new agent that has both the original traits and new traits for the survey responses:

[6]:
new_agent = results.select("persona", "year", "school", "major", "state", "career_interests", "attend_grad_school").to_agent_list()[0]
[7]:
type(new_agent)
[7]:
edsl.agents.Agent.Agent
[8]:
new_agent
[8]:

Agent

key value
traits:persona You are a sophomore at a community college in upstate New York.
traits:year sophomore
traits:school community college
traits:major biology
traits:state New York
traits:career_interests I'm majoring in biology, so I'm really interested in exploring careers related to that field. I'm considering options like research, environmental science, or possibly healthcare. I'm still figuring it out, but I'm excited about the possibilities and want to find something that combines my love for science with making a positive impact.
traits:attend_grad_schoolUndecided

Updating an existing agent

Here we use the add_traits() method to add the agent’s answers to its existing traits:

[9]:
career_interests = results.select("career_interests").to_list()[0] # there is only 1 answer
career_interests
[9]:
"I'm majoring in biology, so I'm really interested in exploring careers related to that field. I'm considering options like research, environmental science, or possibly healthcare. I'm still figuring it out, but I'm excited about the possibilities and want to find something that combines my love for science with making a positive impact."
[10]:
attend_grad_school = results.select("attend_grad_school").to_list()[0]
attend_grad_school
[10]:
'Undecided'
[11]:
agent.add_trait({
    "career_interests": career_interests,
    "attend_grad_school": attend_grad_school
})
agent
[11]:

Agent

key value
name college student
traits:personaYou are a sophomore at a community college in upstate New York.
traits:year sophomore
traits:school community college
traits:major biology
traits:state New York

Updating an agent list

Here we update a list of agents all at once:

[12]:
from edsl import AgentList
[13]:
agents = AgentList(
    Agent(traits = {"persona":p}) for p in ["School principal", "Nurse"]
)
[14]:
q1 = QuestionFreeText(
    question_name = "ideal_vacation",
    question_text = "Describe your ideal vacation."
)

q2 = QuestionMultipleChoice(
    question_name = "commute",
    question_text = "How do you typically commute to work?",
    question_options = ["Car", "Bus", "Train", "Bike", "Walk", "Other"]
)

survey = Survey([q1, q2])
[15]:
results = survey.by(agents).run()
Remote Job Log (2024-12-14 10:24:42)
Remote inference activated. Sending job to server...
Your survey is running at the Expected Parrot server...
Job sent to server. (Job uuid=b69a6d33-52aa-4d35-9668-5d9ddfe9a8eb).
Job status: queued - last update: 2024-12-14 10:24:21 AM
Job status: running - last update: 2024-12-14 10:24:26 AM
Job status: running - last update: 2024-12-14 10:24:31 AM
[16]:
results.select("persona", "ideal_vacation", "commute")
[16]:
agent.persona answer.ideal_vacation answer.commute
School principalAs a school principal, my ideal vacation would be a perfect blend of relaxation and inspiration. I would love to visit a serene location, perhaps a quiet coastal town or a picturesque mountain retreat, where I can unwind and recharge. Mornings would be spent enjoying a good book with a cup of coffee, while afternoons could involve exploring local culture or nature trails. I'd also appreciate the opportunity to visit educational landmarks or museums to gather new ideas and perspectives that I can bring back to the school. Ultimately, it would be a balance of rest, reflection, and learning.Car
Nurse As a nurse, my ideal vacation would be a place where I can completely unwind and recharge. I imagine a serene beach destination with soft sand and gentle waves. I'd love to have a cozy beachside bungalow where I can relax with a good book. It would be great to have access to a spa for some much-needed pampering and perhaps a yoga class by the ocean to help de-stress. I also enjoy trying new cuisines, so exploring local restaurants would be a must. Most importantly, it would be a place where I can disconnect from the hustle and bustle and focus on self-care and rejuvenation. Car
[17]:
new_agents = results.select("persona", "ideal_vacation", "commute").to_agent_list()
[18]:
type(new_agents)
[18]:
edsl.agents.AgentList.AgentList
[19]:
new_agents
[19]:

AgentList agents: 2;

commute persona ideal_vacation
Car School principalAs a school principal, my ideal vacation would be a perfect blend of relaxation and inspiration. I would love to visit a serene location, perhaps a quiet coastal town or a picturesque mountain retreat, where I can unwind and recharge. Mornings would be spent enjoying a good book with a cup of coffee, while afternoons could involve exploring local culture or nature trails. I'd also appreciate the opportunity to visit educational landmarks or museums to gather new ideas and perspectives that I can bring back to the school. Ultimately, it would be a balance of rest, reflection, and learning.
Car Nurse As a nurse, my ideal vacation would be a place where I can completely unwind and recharge. I imagine a serene beach destination with soft sand and gentle waves. I'd love to have a cozy beachside bungalow where I can relax with a good book. It would be great to have access to a spa for some much-needed pampering and perhaps a yoga class by the ocean to help de-stress. I also enjoy trying new cuisines, so exploring local restaurants would be a must. Most importantly, it would be a place where I can disconnect from the hustle and bustle and focus on self-care and rejuvenation.

Posting to the Coop

[20]:
from edsl import Notebook
[21]:
n = Notebook(path = "updating_agents.ipynb")
[22]:
info = n.push(description = "Updating agent traits with new answers", visibility = "public")
info
[22]:
{'description': 'Updating agent traits with new answers',
 'object_type': 'notebook',
 'url': 'https://www.expectedparrot.com/content/96979a08-f272-4fcf-86af-1b070104c535',
 'uuid': '96979a08-f272-4fcf-86af-1b070104c535',
 'version': '0.1.39.dev1',
 'visibility': 'public'}

Updating an object at Coop:

[23]:
n = Notebook(path = "updating_agents.ipynb") # resave
[24]:
n.patch(uuid = info["uuid"], value = n)
[24]:
{'status': 'success'}