topicGPT

chtmp223MIT更新放缓

用大模型做主题建模的提示词框架,自动从文本语料中生成可读的主题描述并分配文档标签,替代传统 LDA 类方法。

TopicGPT: A Prompt-Based Framework for Topic Modeling [NAACL'24]

v0.2.7发布于 2025-03-15 13:55 UTC422star65fork同步于 2026-11-07 07:28 UTC

怎么上手

由 README 提炼,执行前请确认
pip install topicgpt_python
export OPENAI_API_KEY={your_openai_api_key}
硬件要求按 README 说明

README 未说明显存要求;默认走云端 API(OpenAI、VertexAI、Azure、Gemini),只有用 vLLM 后端时才需要 GPU 推理

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

用大模型提示词做主题建模的 Python 框架,替代传统 LDA 式主题模型

用大模型做主题建模的提示词框架,自动从文本语料中生成可读的主题描述并分配文档标签,替代传统 LDA 类方法。

核心能力

  • 分两级生成高层与细粒度主题
  • 合并近重复主题并剔除低频主题
  • 为每篇文本分配主题并附支撑引文
  • 重提示校正主题使结果锚定主题表
  • 内置 ARI、调和纯度、NMI 评估指标

容易踩的坑

需自行准备 jsonl 格式数据并放到 data/input 目录;不同后端要分别设置对应的 API key 环境变量;建议先用便宜或开源模型在小样本上调通再全量跑

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

4 个版本v0.2.7
  1. v0.2.7v0.2.7 Added Ollama support最新正式版2025-03-15 · 2025-03-15 13:55 UTC

    Added Ollama support in utils.py

  2. v0.2.2v0.2.2 Removed redundant parameters2024-11-10 · 2024-11-10 19:23 UTC
    查看更新说明

    Update to v0.2.2: https://pypi.org/project/topicgpt-python/

    Major Changes

    • Removed refined_again from refine_topics(), which is a redundant parameter.
    • Updated documentation.
  3. v0.2.0v0.2.0 Python Package `topicgpt_python`2024-11-10 · 2024-11-10 18:13 UTC
    查看更新说明

    Released topicgpt_python package here: https://pypi.org/project/topicgpt-python/

    Major changes

    • Compatible with OpenAI, VertexAI and vLLM models.
    • Refactored and packaged code for ease of use.
  4. v1.0.0Experiment Code2024-11-08 · 2024-11-08 14:28 UTC
    查看更新说明

    The code in this release was used to generate the experimental results presented in the TopicGPT paper.

    Full Changelog: https://github.com/chtmp223/topicGPT/commits/v1.0.0

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    TopicGPT

    arXiV Website

    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_lvl1 generates high-level and generalizable topics.
    • generate_topic_lvl2 generates low-level and specific topics to each high-level topic.
    • refine_topics refines the generated topics by merging similar topics and removing irrelevant topics.
    • assign_topics assigns the generated topics to the input text, along with a quote that supports the assignment.
    • correct_topics corrects the generated topics by reprompting the model so that the final topic assignment is grounded in the topic list.

    TopicGPT Pipeline Overview

    📣 Updates

    • [11/09/24] Python package topicgpt_python is released! You can install it via pip 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

    1. Make a new Python 3.9+ environment using virtualenv or conda.
    2. 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 .jsonl data 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 file data/input/sample.jsonl to 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.

    1. (Optional) Define I/O paths in config.yml and load using:

      import yaml
      
      with open("config.yml", "r") as f:
          config = yaml.safe_load(f)
      
    2. Load the package:

      from topicgpt_python import *
      
    3. Generate high-level topics:

      generate_topic_lvl1(api, model, data, prompt_file, seed_file, out_file, topic_file, verbose)
      
    4. Generate low-level topics (optional)

      generate_topic_lvl2(api, model, seed_file, data, prompt_file, out_file, topic_file, verbose)
      
    5. 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)
      
    6. 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
      ) 
      
    7. Check out the data/output folder for sample outputs.

    8. We also offer metric calculation functions in topicgpt_python.metrics to 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}
    }