topicGPT
v0.2.2 Removed redundant parameters
该存储库包含我们的论文“主题游戏:主题建模来提示大型语言模型”的提示(NAACL'24)。我们的topicgpt_python软件包由五个主要功能组成:
generate_topic_lvl1生成高级和可推广的主题。generate_topic_lvl2为每个高级主题生成低级和特定主题。refine_topics通过合并类似主题并删除无关的主题来完善生成的主题。assign_topics将生成的主题分配给输入文本,以及支持分配的报价。correct_topics通过重复模型来纠正生成的主题,以使最终主题分配扎根于主题列表中。 
topicgpt_python已发布!您可以通过pip install topicgpt_python安装它。我们支持OpenAI API,Vertexai,Azure API,Gemini API和VLLM(需要GPU进行推理)。见PYPI。 pip install topicgpt_python
# 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}
.jsonl数据文件: {
" id " : " IDs (optional) " ,
" text " : " Documents " ,
" label " : " Ground-truth labels (optional) "
}data/input中。还有一个示例数据文件data/input/sample.jsonl来调试代码。请查看demo.ipynb ,以获取完整的管道和更详细的说明。我们建议您先尝试使用更便宜(或开源)型号的子集运行,然后再扩展到整个数据集。
(可选)在config.yml中定义I/O路径,并使用以下方式加载:
import yaml
with open ( "config.yml" , "r" ) as f :
config = yaml . safe_load ( f )加载软件包:
from topicgpt_python import *生成高级主题:
generate_topic_lvl1 ( api , model , data , prompt_file , seed_file , out_file , topic_file , verbose )生成低级主题(可选)
generate_topic_lvl2 ( api , model , seed_file , data , prompt_file , out_file , topic_file , verbose )通过在重复项附近合并并删除低频(可选)的主题来完善产生的主题:
refine_topics ( api , model , prompt_file , generation_file , topic_file , out_file , updated_file , verbose , remove , mapping_file )分配和纠正主题,通常使用较弱的模型,如果使用付费API来节省成本:
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
)
查看示例输出的data/output文件夹。
我们还提供了topicgpt_python.metrics中的度量计算函数,以评估生成的主题与地面真相标签(调整后的兰德指数,谐波纯度和归一化互信息)之间的对齐。
@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}
}