Last active
February 12, 2025 14:21
-
-
Save rjtavares/a8b14ed2bc64121572825712c9363a54 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import llm | |
import sqlite_utils | |
import numpy as np | |
from scipy.cluster.hierarchy import linkage, cut_tree | |
from collections import defaultdict | |
collection_path = "your path here" | |
db = sqlite_utils.Database(collection_path) | |
collection = llm.Collection("pocket", db, create=False) | |
embeddings_array = np.vstack([llm.decode(emb['embedding']) for emb in collection.db['embeddings'].rows]) | |
complete_clustering = linkage(embeddings_array, method="complete", metric="cosine") | |
cluster_labels = cut_tree(complete_clustering, n_clusters=7).reshape(-1, ) | |
groups = defaultdict(list) | |
for id, label in zip(article_ids, cluster_labels): | |
groups[label].append(id) | |
print(groups) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment