Repository navigation
Expand file tree
/
Copy pathpersona_manager.py
More file actions
179 lines (146 loc) · 6.48 KB
/
Copy pathpersona_manager.py
File metadata and controls
179 lines (146 loc) · 6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python3
"""
Persona Manager for Nash Equilibrium Simulation
Loads and manages personas from the Nemotron dataset
"""
import random
from typing import List, Dict, Optional
from datasets import load_dataset
import logging
class PersonaManager:
"""Manages loading and selection of personas from Nemotron dataset"""
def __init__(self, num_personas: int = 100, seed: Optional[int] = None):
"""
Initialize the persona manager
Args:
num_personas: Number of personas to select (default 100)
seed: Random seed for reproducible selection
"""
self.num_personas = num_personas
self.seed = seed
self.personas = []
self.logger = logging.getLogger('PersonaManager')
# Set random seed if provided
if seed is not None:
random.seed(seed)
# Load the personas
self._load_personas()
def _load_personas(self):
"""Load personas from the Nemotron dataset"""
try:
self.logger.info("Loading Nemotron personas dataset...")
dataset = load_dataset("nvidia/Nemotron-Personas")
# Get total number of available personas
total_personas = len(dataset['train'])
self.logger.info(f"Found {total_personas} total personas in dataset")
# Randomly select the requested number of personas
indices = random.sample(range(total_personas), min(self.num_personas, total_personas))
# Extract selected personas
for i, idx in enumerate(indices):
persona_data = dataset['train'][idx]
# Add an internal ID for tracking
persona_data['participant_id'] = i
self.personas.append(persona_data)
self.logger.info(f"Selected {len(self.personas)} personas for simulation")
except Exception as e:
self.logger.error(f"Error loading personas: {e}")
raise
def get_persona(self, participant_id: int) -> Dict:
"""
Get a specific persona by participant ID
Args:
participant_id: The participant's ID
Returns:
The persona dictionary for that participant
"""
if 0 <= participant_id < len(self.personas):
return self.personas[participant_id]
else:
raise ValueError(f"Invalid participant_id: {participant_id}")
def get_persona_description(self, participant_id: int) -> str:
"""
Get a formatted description of a participant's persona
Args:
participant_id: The participant's ID
Returns:
A formatted string describing the persona
"""
persona = self.get_persona(participant_id)
# Create a rich description combining different aspects
description = f"""
Persona Profile (Participant #{participant_id}):
- Core: {persona.get('persona', 'No description available')}
- Professional: {persona.get('professional_persona', 'Not specified')}
- Age: {persona.get('age', 'Unknown')}, {persona.get('sex', 'Unknown')}, {persona.get('marital_status', 'Unknown').replace('_', ' ')}
- Location: {persona.get('city', 'Unknown')}, {persona.get('state', 'Unknown')}
- Education: {persona.get('education_level', 'Unknown').replace('_', ' ')}
- Occupation: {persona.get('occupation', 'Unknown').replace('_', ' ')}
"""
# Add skills if available
skills_list = persona.get('skills_and_expertise_list', '')
if skills_list and skills_list != "None":
# Parse the string representation of the list
try:
import ast
skills = ast.literal_eval(skills_list)
if skills:
description += f"- Key Skills: {', '.join(skills[:3])}\n"
except:
pass
# Add hobbies if available
hobbies_list = persona.get('hobbies_and_interests_list', '')
if hobbies_list and hobbies_list != "None":
try:
import ast
hobbies = ast.literal_eval(hobbies_list)
if hobbies:
description += f"- Interests: {', '.join(hobbies[:3])}\n"
except:
pass
return description.strip()
def get_persona_trait(self, participant_id: int) -> str:
"""
Extract a concise personality trait description for prompts
Args:
participant_id: The participant's ID
Returns:
A concise trait description
"""
persona = self.get_persona(participant_id)
# Try to extract key traits from the persona description
main_persona = persona.get('persona', '')
# Extract adjectives and key characteristics
if main_persona:
# Look for descriptive phrases
import re
# Common patterns for personality traits
patterns = [
r'(disciplined|organized|practical|curious|ambitious|cautious|competitive|creative|resilient|meticulous)',
r'(risk-averse|profit-driven|analytically calculating|opportunistically aggressive|conservatively patient)',
r'(sociable|reserved|outgoing|introverted|balanced)',
r'balances (\w+) with (\w+)'
]
traits = []
for pattern in patterns:
matches = re.findall(pattern, main_persona, re.IGNORECASE)
if matches:
if isinstance(matches[0], tuple):
traits.extend(matches[0])
else:
traits.extend(matches)
if traits:
# Combine up to 3 traits
trait_description = " and ".join(traits[:3])
return trait_description
# Fallback to a generic description based on demographics
age = persona.get('age', 40)
occupation = persona.get('occupation', 'worker').replace('_', ' ')
if age < 30:
return "young and ambitious"
elif age < 50:
return "experienced and balanced"
else:
return "seasoned and thoughtful"
def get_all_personas(self) -> List[Dict]:
"""Get all loaded personas"""
return self.personas