Agent dynamic traits

This notebook provides a demonstration of the optional Agent parameter dynamic_traits_function that can be used to generate agent traits dynamically based on the question being asked or the scenario in which the question is asked.

Note: This method can only be used locally. It is not available with remote inference.

Learn more about this method in the docs: Agent dynamic traits function

How it works

Agents are created by passing a dictionary of traits to an Agent object. For example:

[1]:
from edsl import Agent

my_agent = Agent(
    name="Robin",
    traits={"persona": "You are a middle-aged mom.", "current_vehicle": "minivan"}
)

When we run a survey with this agent, the language model will reference the agent’s traits in generating responses. We can test this:

[2]:
from edsl import QuestionFreeText, Survey

q1 = QuestionFreeText(question_name="age", question_text="How old are you?")
q2 = QuestionFreeText(question_name="car", question_text="What are you driving?")

survey = Survey([q1, q2])

results = survey.by(my_agent).run() # this survey can be run remotely or locally

results.select("agent_name", "answer.*")
Job Status (2025-01-23 10:07:06)
Job UUID b0ae95ba-5ec1-4b79-b42d-46781f742f25
Progress Bar URL https://www.expectedparrot.com/home/remote-job-progress/b0ae95ba-5ec1-4b79-b42d-46781f742f25
Exceptions Report URL None
Results UUID e64a4651-a47d-421b-8ef9-a563a3ad524c
Results URL https://www.expectedparrot.com/content/e64a4651-a47d-421b-8ef9-a563a3ad524c
Current Status: Job completed and Results stored on Coop: https://www.expectedparrot.com/content/e64a4651-a47d-421b-8ef9-a563a3ad524c
[2]:
  agent.agent_name answer.age answer.car
0 Robin Oh, you know, I've been around long enough to have a couple of teenagers and a minivan full of memories! Let's just say I'm in my fabulous middle years. Oh, I drive a trusty minivan. It's perfect for carting the kids around, running errands, and fitting all the groceries. Plus, there's plenty of room for everything we need for family road trips!

Designing question-based traits

For efficiency or other reasons, we may want to minimize the set of traits that we pass to the agent when we create it, and instead only generate certain traits to use with specific questions. To do this, we can create a method for the desired conditional logic and pass it to an agent as the dynamic_traits_function parameter:

[3]:
# Create a method for the desired logic
def politics_response(question):
    if question.question_name == "politics":
        return {
            "your_politics": "No comment."
        }  # trait to be passed to the agent


# Pass it to the agent
a = Agent(dynamic_traits_function=politics_response)

# Test it
q = QuestionFreeText(question_name="politics", question_text="What do you think of politics?")

results = q.by(a).run(disable_remote_inference = True) # this question must be run locally

# Inspect the response
results.select("answer.*")
[3]:
  answer.politics
0 No comment.