TopicGPT
This repository contains scripts and prompts for our paper "TopicGPT: Topic Modeling by Prompting Large Language Models" (NAACL'24). Our topicgpt_python package consists of five main functions:
generate_topic_lvl1generates high-level and generalizable topics.generate_topic_lvl2generates low-level and specific topics to each high-level topic.refine_topicsrefines the generated topics by merging similar topics and removing irrelevant topics.assign_topicsassigns the generated topics to the input text, along with a quote that supports the assignment.correct_topicscorrects the generated topics by reprompting the model so that the final topic assignment is grounded in the topic list.
📣 Updates
- [11/09/24] Python package
topicgpt_pythonis released! You can install it viapip install topicgpt_python. We support OpenAI API, VertexAI, Azure API, Gemini API, and vLLM (requires GPUs for inference). See PyPI. - [11/18/23] Second-level topic generation code and refinement code are uploaded.
- [11/11/23] Basic pipeline is uploaded. Refinement and second-level topic generation code are coming soon.
📦 Using TopicGPT
Getting Started
- Make a new Python 3.9+ environment using virtualenv or conda.
- Install the required packages:
pip install topicgpt_python
- Set your API key:
# Run in shell # Needed only for the OpenAI API deployment export OPENAI_API_KEY={your_openai_api_key} # Needed only for the Vertex AI deployment export VERTEX_PROJECT={your_vertex_project} # e.g. my-project export VERTEX_LOCATION={your_vertex_location} # e.g. us-central1 # Needed only for Gemini deployment export GEMINI_API_KEY={your_gemini_api_key} # Needed only for the Azure API deployment export AZURE_OPENAI_API_KEY={your_azure_api_key} export AZURE_OPENAI_ENDPOINT={your_azure_endpoint} - Refer to https://openai.com/pricing/ for OpenAI API pricing or to https://cloud.google.com/vertex-ai/pricing for Vertex API pricing.
Data
- Prepare your
.jsonldata file in the following format:{ "id": "IDs (optional)", "text": "Documents", "label": "Ground-truth labels (optional)" }
- Put your data file in
data/input. There is also a sample data filedata/input/sample.jsonlto debug the code. - Raw dataset used in the paper (Bills and Wiki): [link].
Pipeline
Check out demo.ipynb for a complete pipeline and more detailed instructions. We advise you to try running on a subset with cheaper (or open-source) models first before scaling up to the entire dataset.
-
(Optional) Define I/O paths in
config.ymland load using:import yaml with open("config.yml", "r") as f: config = yaml.safe_load(f)
-
Load the package:
from topicgpt_python import *
-
Generate high-level topics:
generate_topic_lvl1(api, model, data, prompt_file, seed_file, out_file, topic_file, verbose)
-
Generate low-level topics (optional)
generate_topic_lvl2(api, model, seed_file, data, prompt_file, out_file, topic_file, verbose)
-
Refine the generated topics by merging near duplicates and removing topics with low frequency (optional):
refine_topics(api, model, prompt_file, generation_file, topic_file, out_file, updated_file, verbose, remove, mapping_file)
-
Assign and correct the topics, usually with a weaker model if using paid APIs to save cost:
assign_topics( api, model, data, prompt_file, out_file, topic_file, verbose )
correct_topics( api, model, data_path, prompt_path, topic_path, output_path, verbose ) -
Check out the
data/outputfolder for sample outputs. -
We also offer metric calculation functions in
topicgpt_python.metricsto evaluate the alignment between the generated topics and the ground-truth labels (Adjusted Rand Index, Harmonic Purity, and Normalized Mutual Information).
📜 Citation
@misc{pham2023topicgpt,
title={TopicGPT: A Prompt-based Topic Modeling Framework},
author={Chau Minh Pham and Alexander Hoyle and Simeng Sun and Mohit Iyyer},
year={2023},
eprint={2311.01449},
archivePrefix={arXiv},
primaryClass={cs.CL}
}
Metadata
Release files for topicgpt-python 0.2.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| topicgpt_python-0.2.7.tar.gz | 21.3 kB | Without provenance | Details for topicgpt_python-0.2.7.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| topicgpt_python-0.2.7-py3-none-any.whl | Python 3 | none | any | Details for topicgpt_python-0.2.7-py3-none-any.whl |
Total release size: 46.7 kB
Release files / topicgpt_python-0.2.7.tar.gz
| Download URL | topicgpt_python-0.2.7.tar.gz |
|---|---|
| Size | 21.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1c7b63780d7cdf4386f832b103c8aab727f88f61f04add4e6951085bce7df0cf
|
|
BLAKE2b-256 checksum How to use checksums |
d77a4eaba34e155780cd89ed2c365ae6003e7c5a630eb85af25fd818decd4af3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.1
|
Release files / topicgpt_python-0.2.7-py3-none-any.whl
| Download URL | topicgpt_python-0.2.7-py3-none-any.whl |
|---|---|
| Size | 25.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e56c4d1f4932d47a275464f61c2d2f422be494427dbb97793853ce0a38137047
|
|
BLAKE2b-256 checksum How to use checksums |
03ad9b8949f39ea2ef5a7ded19970f87e6e808c965d53321ea44d461acde3006
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.1
|
Security for topicgpt-python 0.2.7
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.