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()
Job Status (2025-03-03 12:55:17)
Job UUID ab1f81dd-0b74-4a78-b025-c78214895382
Progress Bar URL https://www.expectedparrot.com/home/remote-job-progress/ab1f81dd-0b74-4a78-b025-c78214895382
Exceptions Report URL None
Results UUID b6e298b0-ff19-46c5-b8d7-c25186dd3f4d
Results URL https://www.expectedparrot.com/content/b6e298b0-ff19-46c5-b8d7-c25186dd3f4d
Current Status: Job completed and Results stored on Coop: https://www.expectedparrot.com/content/b6e298b0-ff19-46c5-b8d7-c25186dd3f4d
[5]:
results.select("career_interests", "attend_grad_school")
[5]:
  answer.career_interests answer.attend_grad_school
0 I'm really interested in pursuing a career in the biological sciences. Since I'm majoring in biology, I'm exploring options like working in environmental conservation or possibly going into research. I'm also considering the healthcare field, as I've always been fascinated by how the human body works and how we can improve health outcomes. I still have some time to decide, so I'm keeping my options open while I learn more about each path. 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
0 traits:persona You are a sophomore at a community college in upstate New York.
1 traits:year sophomore
2 traits:school community college
3 traits:major biology
4 traits:state New York
5 traits:career_interests I'm really interested in pursuing a career in the biological sciences. Since I'm majoring in biology, I'm exploring options like working in environmental conservation or possibly going into research. I'm also considering the healthcare field, as I've always been fascinated by how the human body works and how we can improve health outcomes. I still have some time to decide, so I'm keeping my options open while I learn more about each path.
6 traits:attend_grad_school Undecided

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 really interested in pursuing a career in the biological sciences. Since I'm majoring in biology, I'm exploring options like working in environmental conservation or possibly going into research. I'm also considering the healthcare field, as I've always been fascinated by how the human body works and how we can improve health outcomes. I still have some time to decide, so I'm keeping my options open while I learn more about each path."
[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
0 traits:persona You are a sophomore at a community college in upstate New York.
1 traits:year sophomore
2 traits:school community college
3 traits:major biology
4 traits:state New York
5 name college student

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()
Job Status (2025-03-03 12:55:28)
Job UUID c868d503-c00e-4b9d-a907-c1ec4e91fa91
Progress Bar URL https://www.expectedparrot.com/home/remote-job-progress/c868d503-c00e-4b9d-a907-c1ec4e91fa91
Exceptions Report URL None
Results UUID a5b46fcb-048a-4e8c-9691-8049e886798e
Results URL https://www.expectedparrot.com/content/a5b46fcb-048a-4e8c-9691-8049e886798e
Current Status: Job completed and Results stored on Coop: https://www.expectedparrot.com/content/a5b46fcb-048a-4e8c-9691-8049e886798e
[16]:
results.select("persona", "ideal_vacation", "commute")
[16]:
  agent.persona answer.ideal_vacation answer.commute
0 School principal As a school principal, my ideal vacation would be a blend of relaxation and enrichment. I'd love to escape to a quiet coastal town where I can unwind with a good book, ideally something educational or related to leadership. Mornings would be for long walks on the beach, afternoons for exploring local museums or historical sites, and evenings for enjoying local cuisine. It would be a chance to recharge and gather new ideas to bring back to the school. A little time for reflection and planning for the upcoming school year would also be part of the itinerary. Car
1 Nurse As a nurse, my ideal vacation would be a mix of relaxation and adventure. I'd love to spend some time at a serene beach resort where I can unwind and soak up the sun, perhaps with a good book in hand. It's important to recharge and escape the hustle and bustle of a hospital environment. After a few days of relaxation, I'd enjoy exploring local culture and cuisine, perhaps visiting markets or taking a cooking class. A bit of hiking or a nature excursion would also be perfect to stay active and enjoy the beauty of nature. Overall, a balance of rest and exploration would make for a perfect getaway. 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;

  persona ideal_vacation commute
0 School principal As a school principal, my ideal vacation would be a blend of relaxation and enrichment. I'd love to escape to a quiet coastal town where I can unwind with a good book, ideally something educational or related to leadership. Mornings would be for long walks on the beach, afternoons for exploring local museums or historical sites, and evenings for enjoying local cuisine. It would be a chance to recharge and gather new ideas to bring back to the school. A little time for reflection and planning for the upcoming school year would also be part of the itinerary. Car
1 Nurse As a nurse, my ideal vacation would be a mix of relaxation and adventure. I'd love to spend some time at a serene beach resort where I can unwind and soak up the sun, perhaps with a good book in hand. It's important to recharge and escape the hustle and bustle of a hospital environment. After a few days of relaxation, I'd enjoy exploring local culture and cuisine, perhaps visiting markets or taking a cooking class. A bit of hiking or a nature excursion would also be perfect to stay active and enjoy the beauty of nature. Overall, a balance of rest and exploration would make for a perfect getaway. Car

Posting to the Coop

[21]:
from edsl import Notebook

nb = Notebook(path = "updating_agents.ipynb")

if refresh := False:
    nb.push(
        description = "Updating agent traits with new answers",
        alias = "updating-agents-notebook",
        visibility = "public"
    )
else:
    nb.patch('b1a2c6d2-24cd-4708-ac6c-dbcd7381bfba', value = nb)