AgentSkillsCN

edsl-agent-list-operations

AgentList的操作——命名、采样、随机打乱、拆分、合并、差值计算,以及格式转换。

SKILL.md
--- frontmatter
name: edsl-agent-list-operations
description: AgentList operations - naming, sampling, shuffling, splitting, combining, deltas, and conversion
allowed-tools: Read, Glob, Bash(python:*)

AgentList Operations

Operations for manipulating collections of agents.

Naming Agents

Set Names from Traits

python
# Name agents using trait values (removes used traits by default)
agents = agents.give_names("respondent_id")

# Keep the traits used for naming
agents = agents.give_names("first_name", "last_name", remove_traits=False)

# Custom separator for multiple traits
agents = agents.give_names("city", "id", separator="_")
# Names: "NYC_001", "LA_002", etc.

Assign UUID Names

python
agents = agents.give_uuid_names()

Sampling and Shuffling

python
# Random sample (returns new AgentList)
sample = agents.sample(n=10, seed=42)

# Shuffle order (returns new AgentList)
shuffled = agents.shuffle(seed=42)

# Split into two groups
train, test = agents.split(frac_left=0.8, seed=42)

Combining AgentLists

python
# Concatenate (must have same traits and codebook)
combined = agents1 + agents2

# Collapse agents with same name (merges their traits)
collapsed = agents.collapse()

Applying Deltas (Batch Updates)

python
from edsl import AgentDelta, AgentListDeltas

# Create deltas for named agents
deltas = AgentListDeltas({
    "Alice": AgentDelta({"age": 31, "status": "promoted"}),
    "Bob": AgentDelta({"age": 26})
})

# Apply to agent list (returns new AgentList)
updated_agents = agents.apply_deltas(deltas)

Conversion

python
# To Dataset
dataset = agents.to_dataset()

# To ScenarioList
scenarios = agents.to_scenario_list()

# To pandas DataFrame
df = agents.to_pandas()

Accessing Agents

python
# By index
agent = agents[0]
agent = agents[-1]

# Slicing
subset = agents[0:5]

# By position
first = agents.first()
last = agents.last()
agent = agents.at(3)

# Iteration
for agent in agents:
    print(agent.traits)

Running Surveys with Agents

python
from edsl import Survey, QuestionFreeText

survey = Survey([
    QuestionFreeText(question_name="opinion", question_text="What do you think?")
])

# Run with agents
results = survey.by(agents).run()

# Or using agent.to()
results = agents.to(survey).run()

Quick Reference

OperationMethod
Name from traitsagents.give_names("trait")
UUID namesagents.give_uuid_names()
Sampleagents.sample(n=10, seed=42)
Shuffleagents.shuffle(seed=42)
Splitagents.split(frac_left=0.8)
Combineagents1 + agents2
Collapseagents.collapse()
Apply deltasagents.apply_deltas(deltas)
To Datasetagents.to_dataset()
To ScenarioListagents.to_scenario_list()
To DataFrameagents.to_pandas()
First/Lastagents.first(), agents.last()
Lengthlen(agents)