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-02-07 20:30:17)
Job UUID 3540bcaf-b0c0-4803-8946-684f3a27a84f
Progress Bar URL https://www.expectedparrot.com/home/remote-job-progress/3540bcaf-b0c0-4803-8946-684f3a27a84f
Exceptions Report URL None
Results UUID 6e54fca7-e924-4f28-8042-6226f1b502fe
Results URL https://www.expectedparrot.com/content/6e54fca7-e924-4f28-8042-6226f1b502fe
Current Status: Job completed and Results stored on Coop: https://www.expectedparrot.com/content/6e54fca7-e924-4f28-8042-6226f1b502fe
[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 medical field, possibly as a physician assistant or maybe even going to medical school to become a doctor. I love studying biology and understanding how the human body works, so I'm excited to explore different paths in healthcare. For now, I'm focusing on doing well in my classes and gaining some experience through internships or volunteer work. 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 medical field, possibly as a physician assistant or maybe even going to medical school to become a doctor. I love studying biology and understanding how the human body works, so I'm excited to explore different paths in healthcare. For now, I'm focusing on doing well in my classes and gaining some experience through internships or volunteer work.
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 medical field, possibly as a physician assistant or maybe even going to medical school to become a doctor. I love studying biology and understanding how the human body works, so I'm excited to explore different paths in healthcare. For now, I'm focusing on doing well in my classes and gaining some experience through internships or volunteer work."
[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-02-07 20:30:35)
Job UUID ea12fa17-1c20-4eb8-a9b0-bcabfdd31d8f
Progress Bar URL https://www.expectedparrot.com/home/remote-job-progress/ea12fa17-1c20-4eb8-a9b0-bcabfdd31d8f
Exceptions Report URL None
Results UUID 9d78611f-aecd-4de8-9c55-3639219056b0
Results URL https://www.expectedparrot.com/content/9d78611f-aecd-4de8-9c55-3639219056b0
Current Status: Job completed and Results stored on Coop: https://www.expectedparrot.com/content/9d78611f-aecd-4de8-9c55-3639219056b0
[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 one that allows me to unwind and recharge while also gaining new perspectives that I can bring back to my school. I would love to visit a place with a rich cultural history, perhaps somewhere in Europe like Italy or Greece, where I can explore ancient sites and learn about different educational philosophies throughout history. Car
1 Nurse As a nurse, my ideal vacation would be somewhere peaceful and rejuvenating, where I can unwind and recharge from the demands of my job. I imagine a serene beach with crystal-clear waters and soft, white sand. I’d love to spend my days lounging under a sun umbrella, reading a good book, and listening to the gentle waves. A spa nearby for massages and relaxation treatments would be perfect. In the evenings, I’d enjoy fresh seafood dinners by the ocean, watching the sunset. It’s all about relaxation and taking a break from the hustle and bustle of the hospital. 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 one that allows me to unwind and recharge while also gaining new perspectives that I can bring back to my school. I would love to visit a place with a rich cultural history, perhaps somewhere in Europe like Italy or Greece, where I can explore ancient sites and learn about different educational philosophies throughout history. Car
1 Nurse As a nurse, my ideal vacation would be somewhere peaceful and rejuvenating, where I can unwind and recharge from the demands of my job. I imagine a serene beach with crystal-clear waters and soft, white sand. I’d love to spend my days lounging under a sun umbrella, reading a good book, and listening to the gentle waves. A spa nearby for massages and relaxation treatments would be perfect. In the evenings, I’d enjoy fresh seafood dinners by the ocean, watching the sunset. It’s all about relaxation and taking a break from the hustle and bustle of the hospital. Car

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/d5705447-8907-4578-a43a-6e2822e92c66',
 'uuid': 'd5705447-8907-4578-a43a-6e2822e92c66',
 'version': '0.1.43.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'}