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:

[2]:
from edsl import Agent, QuestionFreeText, QuestionMultipleChoice, Survey
[3]:
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"
    }
)
[4]:
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])
[5]:
results = survey.by(agent).run()
Job sent to server. (Job uuid=b9139966-653c-4526-ad42-f829d8ddb0d0).
Job completed and Results stored on Coop: https://www.expectedparrot.com/content/0039b65f-d25c-4b73-9e79-b160c7105a70.
[6]:
results.select("career_interests", "attend_grad_school").print(format="rich")
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━┓
┃ answer                                                                                     answer              ┃
┃ .career_interests                                                                          .attend_grad_school ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━┩
│ I'm majoring in biology, so I'm really interested in careers related to that field. Right  Undecided           │
│ now, I'm considering options like working in environmental science, conservation, or                           │
│ maybe even going into healthcare. I'm still exploring, but I definitely want to do                             │
│ something where I can make a positive impact on the environment or people's health.                            │
└───────────────────────────────────────────────────────────────────────────────────────────┴─────────────────────┘

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:

[7]:
new_agent = results.select("persona", "year", "school", "major", "state", "career_interests", "attend_grad_school").to_agent_list()[0]
[8]:
type(new_agent)
[8]:
edsl.agents.Agent.Agent
[9]:
new_agent
[9]:
{
    "traits": {
        "persona": "You are a sophomore at a community college in upstate New York.",
        "year": "sophomore",
        "school": "community college",
        "major": "biology",
        "state": "New York",
        "career_interests": "I'm majoring in biology, so I'm really interested in careers related to that field. Right now, I'm considering options like working in environmental science, conservation, or maybe even going into healthcare. I'm still exploring, but I definitely want to do something where I can make a positive impact on the environment or people's health.",
        "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:

[10]:
career_interests = results.select("career_interests").to_list()[0] # there is only 1 answer
career_interests
[10]:
"I'm majoring in biology, so I'm really interested in careers related to that field. Right now, I'm considering options like working in environmental science, conservation, or maybe even going into healthcare. I'm still exploring, but I definitely want to do something where I can make a positive impact on the environment or people's health."
[11]:
attend_grad_school = results.select("attend_grad_school").to_list()[0]
attend_grad_school
[11]:
'Undecided'
[12]:
agent.add_trait({
    "career_interests": career_interests,
    "attend_grad_school": attend_grad_school
})
agent
[12]:
{
    "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",
        "career_interests": "I'm majoring in biology, so I'm really interested in careers related to that field. Right now, I'm considering options like working in environmental science, conservation, or maybe even going into healthcare. I'm still exploring, but I definitely want to do something where I can make a positive impact on the environment or people's health.",
        "attend_grad_school": "Undecided"
    }
}

Updating an agent list

Here we update a list of agents all at once:

[13]:
from edsl import AgentList
[14]:
agents = AgentList(
    Agent(traits = {"persona":p}) for p in ["School principal", "Nurse"]
)
[15]:
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])
[16]:
results = survey.by(agents).run()
Job sent to server. (Job uuid=13db7cb7-2520-4e1d-b808-bdfdb7c3d2e7).
Job completed and Results stored on Coop: https://www.expectedparrot.com/content/df6b8426-24c5-46c5-8eea-512366e73356.
[17]:
results.select("persona", "ideal_vacation", "commute").print(format="rich")
┏━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━┓
┃ agent             answer                                                                             answer   ┃
┃ .persona          .ideal_vacation                                                                    .commute ┃
┡━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━┩
│ School principal  As a school principal, my ideal vacation would be a mix of relaxation and          Car      │
│                   inspiration. I'd love to visit a quiet coastal town where I can unwind and                  │
│                   recharge. Mornings would be spent walking along the beach, enjoying the fresh air           │
│                   and the sound of the waves. I'd also take some time to visit local schools or               │
│                   educational centers to see how they operate and perhaps gather new ideas to bring           │
│                   back to my own school. In the evenings, I'd enjoy reading a good book or having a           │
│                   nice dinner with fresh, local cuisine. It's important for me to return from                 │
│                   vacation feeling rejuvenated and ready to support my students and staff.                    │
├──────────────────┼───────────────────────────────────────────────────────────────────────────────────┼──────────┤
│ Nurse             As a nurse, my ideal vacation would be somewhere peaceful and rejuvenating, where  Car      │
│                   I can truly unwind and recharge. I'd love to visit a serene beachside location,             │
│                   perhaps in the Caribbean or the Maldives, where I can enjoy the sound of the                │
│                   waves and the warmth of the sun. I would spend my days reading a good book,                 │
│                   taking leisurely walks along the shore, and maybe indulging in some yoga or                 │
│                   meditation sessions to relax my mind and body. A spa day would definitely be on             │
│                   the agenda to pamper myself. I'd also love to try some local cuisine and maybe              │
│                   even take a cooking class to learn a new dish. Most importantly, it would be a              │
│                   time to disconnect from the hustle and bustle of everyday life and focus on                 │
│                   self-care and relaxation.                                                                   │
└──────────────────┴───────────────────────────────────────────────────────────────────────────────────┴──────────┘
[18]:
new_agents = results.select("persona", "ideal_vacation", "commute").to_agent_list()
[19]:
type(new_agents)
[19]:
edsl.agents.AgentList.AgentList
[20]:
new_agents
[20]:
[
    {
        "traits": {
            "persona": "School principal",
            "ideal_vacation": "As a school principal, my ideal vacation would be a mix of relaxation and inspiration. I'd love to visit a quiet coastal town where I can unwind and recharge. Mornings would be spent walking along the beach, enjoying the fresh air and the sound of the waves. I'd also take some time to visit local schools or educational centers to see how they operate and perhaps gather new ideas to bring back to my own school. In the evenings, I'd enjoy reading a good book or having a nice dinner with fresh, local cuisine. It's important for me to return from vacation feeling rejuvenated and ready to support my students and staff.",
            "commute": "Car"
        },
        "edsl_version": "0.1.36.dev1",
        "edsl_class_name": "Agent"
    },
    {
        "traits": {
            "persona": "Nurse",
            "ideal_vacation": "As a nurse, my ideal vacation would be somewhere peaceful and rejuvenating, where I can truly unwind and recharge. I'd love to visit a serene beachside location, perhaps in the Caribbean or the Maldives, where I can enjoy the sound of the waves and the warmth of the sun. I would spend my days reading a good book, taking leisurely walks along the shore, and maybe indulging in some yoga or meditation sessions to relax my mind and body. A spa day would definitely be on the agenda to pamper myself. I'd also love to try some local cuisine and maybe even take a cooking class to learn a new dish. Most importantly, it would be a time to disconnect from the hustle and bustle of everyday life and focus on self-care and relaxation.",
            "commute": "Car"
        },
        "edsl_version": "0.1.36.dev1",
        "edsl_class_name": "Agent"
    }
]

Posting to the Coop

[21]:
from edsl import Notebook
[22]:
n = Notebook(path = "updating_agents.ipynb")
[23]:
n.push(description = "Updating agent traits with new answers", visibility = "public")
[23]:
{'description': 'Updating agent traits with new answers',
 'object_type': 'notebook',
 'url': 'https://www.expectedparrot.com/content/af74189c-0f7a-4324-86f1-aeeeb34cfabe',
 'uuid': 'af74189c-0f7a-4324-86f1-aeeeb34cfabe',
 'version': '0.1.36.dev1',
 'visibility': 'public'}