I want to write Python code to evaluate clustering systems. based on similarity - both…
- User
I want to write Python code to evaluate clustering systems. based on similarity - both content and structure.
I want to use LLM as a judge with a "Leave one out" approach - i.e., iterate through all examples, each iteration the judge will review all clusters besides the current example and determine to which cluster to map the candidate example. Give a score based on this judgement.
Is the task clear?
- Assistant
Yes, your task is clear and quite interesting. Here's a breakdown of what you're describing:
🔍 Goal Evaluate the quality of a clustering system using a large language model (LLM) as a semantic judge, focusing on both content and structure similarity.
🧠 Method: Leave-One-Out LLM-Based Clustering Evaluation
Input:
A set of clusters C = [C1, C2, ..., Ck], each a list of examples.
A language model capable of comparing items and making assignment decisions (e.g., GPT-style model).
A similarity criterion (can be textual, structural, or both).
Procedure:
For each example e in the dataset:
Remove e from its original cluster.
Present the LLM with:
The remaining clusters (without e).
The candidate example e.
Ask the LLM: "Based on the examples in each cluster, which cluster does e best belong to?"
Record whether the LLM assigns e to the correct (original) cluster.
- User
I want you to implement a Python script that includes:
- the above pipeline
- separate prompts for content and for style --> so that the final score has a value for each aspect.
- Use the openai/togetherAI api
- Assistant
I’ve drafted a complete Python module that:
Reads a JSON dataset of examples with their true cluster IDs.
Implements leave-one-out evaluation, calling the LLM for both content and style judgments via separate prompts.
Supports both OpenAI and TogetherAI endpoints.
Computes accuracy and confusion matrices for each aspect.
You can install dependencies (openai, scikit-learn, etc.), set your OPENAI_API_KEY, and then run: bashCopyEditpython llm_clustering_evaluator.py --data_file path/to/data.json --api_type togetherai
Let me know if you’d like extensions—e.g. confidence scoring, batching, or richer prompt templates!