A toolkit for simulating social media dynamics using Large Language Models (LLMs) as agent opinion models. The framework supports multiple opinion models and can run distributed simulations on HPC clusters.
LSS-KIT simulates social networks where agents:
- Generate posts based on their political views and social context
- Read and evaluate posts from their neighbors
- Update their preferences based on social interactions
- Evolve their behavior over time through reinforcement learning
The system supports both traditional opinion models (BCM) and modern LLM-based agents that generate realistic social media content.
LSS-KIT/
├── src/
│ ├── simulation.py # Main simulation engine
│ ├── interfaces.py # Model dispatch interface
│ ├── save.py # Data persistence (with secret filtering)
│ ├── executors/
│ │ └── hf.py # Async LLM execution
│ └── models/
│ └── llm/ # LLM opinion model
│ ├── agent.py # Agent behaviors
│ └── utlities.py # Prompt composition
├── Scripts/
│ └── Experiment_1/ # Example experiment
│ ├── RUNME.py # Main experiment script
│ ├── PARAMS.json # Experiment parameters
│ ├── prompts.yaml # LLM prompts
│ ├── run_job.sh # SLURM batch script
│ └── .env # API credentials (create this)
└── notebooks/ # Analysis notebooks
- Create conda environment with Python 3.11:
conda create -n lss python=3.11 pip
conda activate lss- Install dependencies:
pip install torch torchvision torchaudio
pip install igraph numpy aiohttp pyyaml python-dotenv requests transformers huggingface_hub vllm- Clone the repository:
git clone https://github.com/Fabio-FS/a_KIT_for_LSS.git
cd a_KIT_for_LSS- Create conda environment in your workspace:
conda create -p $WORK/conda_envs/lss python=3.11 pip
conda activate $WORK/conda_envs/lss
pip install vllm transformers huggingface_hub igraph numpy aiohttp pyyaml python-dotenv requests
pip install accelerate- Cache the model (run once on login node):
export HF_HOME=$WORK/hf_cache
python -c "from transformers import AutoTokenizer; AutoTokenizer.from_pretrained('meta-llama/Llama-3.1-8B-Instruct')"Create Scripts/Experiment_1/.env:
HF_TOKEN=your_huggingface_token_here
API_URL=http://127.0.0.1:8000/v1/chat/completions
MODEL=meta-llama/Llama-3.1-8B-InstructEdit Scripts/Experiment_1/PARAMS.json:
{
"replicas": 2,
"num_agents": 16,
"neighbors": 4,
"timesteps": 10,
"fill_history": 3,
"opinion_model": "LLM",
"rewiring_p": 0.05,
"post_read_per_round": 2
}cd Scripts/Experiment_1
python RUNME.pySubmit to SLURM scheduler:
cd Scripts/Experiment_1
sbatch run_job.shThe cluster script will:
- Allocate a GPU node
- Start vLLM server locally
- Run the simulation using the local LLM
- Save results to
$WORK/lss_runs
- Initialization: Create network topology (Watts-Strogatz) and initialize agents
- Thermalization: Pre-generate initial posts for system warm-up
- Main Loop: For each timestep:
- Calculate post weights based on success, recency, and personal preferences
- Agents read top-k posts from neighbors
- Agents evaluate posts (like/dislike decisions)
- Update agent success and neighbor preferences
- Generate new posts based on reading history and agent identity
Agents have political identities (far left, left, center, right, far right) and:
- Generate posts reflecting their political views
- Evaluate posts from neighbors based on alignment with their beliefs
- Maintain memory of recent interactions
- Adapt their neighbor preferences based on successful interactions
Results are saved with automatic secret filtering:
- Arrays: Agent weights, reading matrices, like counts (
.npzformat) - Posts: All generated text content (
.csvformat) - Metadata: Experiment parameters and run information (
.jsonformat) - Manifest: Overview of all files and experiment metadata
- Modular Architecture: Easy to add new opinion models
- Async LLM Execution: Efficient parallel processing of language model calls
- HPC Ready: SLURM integration with local GPU inference
- Secret Protection: Automatic filtering of API tokens from saved data
- Reproducible: Full parameter tracking and random seed control
Results are organized as:
runs/
└── experiment_id/
├── manifest.json # Experiment overview
├── r000/ # Replica 0
│ ├── arrays.npz # Numerical data
│ ├── posts.csv # Generated content
│ └── meta.json # Replica metadata
└── r001/ # Replica 1
└── ...
Use the provided loader functions to analyze results:
from src.save import load_manifest, load_replica
# Load experiment metadata
manifest = load_manifest("runs/experiment_id")
# Load specific replica
data = load_replica("runs/experiment_id/r000")
weights = data["WEIGHTS"]
posts = data["POSTS_long"] # [(agent_id, timestep, text), ...]- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Ensure all experiments reproduce correctly
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
If you use this toolkit in your research, please cite:
@software{lss_kit,
title={LSS-KIT: Large-Scale Social Simulation Toolkit},
author={Your Name},
year={2025},
url={https://github.com/Fabio-FS/a_KIT_for_LSS}
}