A framework designed for controlled text generation in Large Language Models using dynamic attribute graphs.
Refer to our arXiv paper for detailed insights and methodology.
DATG (Dynamic Attribute Graphs-based Text Generation) is an innovative approach designed for controlled text generation, enabling precise control over text attributes during the decoding phase while maintaining the natural fluency of the text. This method leverages dynamic attribute graphs to evaluate and adjust key terms related to target attributes, thereby controlling the attributes of the generated text effectively without compromising text quality.
.
βββ .cache # Cache some results during evaluation to prevent losing all results
βββ .gitattributes # Git attributes
βββ .gitignore # Ignore files for git
βββ README.md # Project Description
βββ analyst.py # Generate the statistics
βββ config.py # Configuration file for experiment
βββ data # Data for experiment, training classifier and evaluation
βββ main.py # Core file for running the experiment
βββ method # Different CTG methods (including ours)
βββ requirements.txt # Required packages
βββ results # Results of the experiment
βββ stats # Statistics of the experiment generated by analyst.py using the results
βββ train # Scripts for training classifiers and other models
βββ utils # Utilities for the project
Install Python 3.8.18.
Clone the project repository.
Install required dependencies:
pip install -r requirements.txt
Complete Configuration in config.py
Before initiating experiments, configure config.py
to suit your experimental setup:
Model Paths: Specify the locations of your Large Language Models (LLMs) in MODEL_PATHS
. Ensure these paths are accurate to enable proper model loading.
Classifier Configuration: Assign paths for internal classifiers (used during generation) and external classifiers (used for evaluation) within TASK_CONFIGURATIONS
. Utilize the Jupyter notebooks in the train
directory for training these classifiers, and update their paths accordingly.
Data and Tasks: Define your specific datasets and tasks in TASK_CONFIGURATIONS
, including dataset paths and task-specific settings.
Perspective API: If required, insert your Perspective API keys into GOOGLE_API_KEYs
after obtaining them. Confirm your system's connectivity to https://commentanalyzer.googleapis.com for accessing API services.
Ensure all paths, APIs, and configurations are set correctly before running your experiments.
To run an experiment, use the following command:
python main.py --model_name <MODEL_NAME> --task_name <TASK_NAME>
Replace <MODEL_NAME>
with one of the available model names: [phi2_3B_Base
, llama2_13B_Base
, falcon_7B_Base
, opt_7B_Base
, alpaca_7B_Base
] , or more models you set in the config.py
.
Replace <TASK_NAME>
with one of the available task names: [toxicMitigation
, 2Positive
, 2Negative
], or more tasks you set in the config.py
.
Example:
python main.py --model_name phi2_3B_Base --task_name toxicMitigation
python analyst.py
This will analyze the results and generate statistical data based on the output from the experiments.
@inproceedings{liang-etal-2024-controlled,
title = "Controlled Text Generation for Large Language Model with Dynamic Attribute Graphs",
author = "Liang, Xun and Wang, Hanyu and Song, Shichao and Hu, Mengting and Wang, Xunzhi and Li, Zhiyu and Xiong, Feiyu and Tang, Bo",
editor = "Ku, Lun-Wei and Martins, Andre and Srikumar, Vivek",
booktitle = "Findings of the Association for Computational Linguistics ACL 2024",
month = aug,
year = "2024",
address = "Bangkok, Thailand and virtual meeting",
publisher = "Association for Computational Linguistics",
url = "https://aclanthology.org/2024.findings-acl.345",
doi = "10.18653/v1/2024.findings-acl.345",
pages = "5797--5814",
}