Views
No views yet
pip install mlx-embeddings1from mlx_embeddings import load, generate
2import mlx.core as mx
3
4model, tokenizer = load("mlx-community/embeddinggemma-300m-6bit")
5
6
7# For text embedding
8sentences = [
9 "task: sentence similarity | query: Nothing really matters.",
10 "task: sentence similarity | query: The dog is barking.",
11 "task: sentence similarity | query: The dog is barking.",
12]
13
14encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='mlx')
15
16# Compute token embeddings
17input_ids = encoded_input['input_ids']
18attention_mask = encoded_input['attention_mask']
19output = model(input_ids, attention_mask)
20
21embeddings = output.text_embeds # Normalized embeddings
22
23# Compute dot product between normalized embeddings
24similarity_matrix = mx.matmul(embeddings, embeddings.T)
25
26print("Similarity matrix between texts:")
27print(similarity_matrix)
28
29
30# You can use these task-specific prefixes for different tasks
31task_prefixes = {
32 "BitextMining": "task: search result | query: ",
33 "Clustering": "task: clustering | query: ",
34 "Classification": "task: classification | query: ",
35 "MultilabelClassification": "task: classification | query: ",
36 "PairClassification": "task: sentence similarity | query: ",
37 "InstructionRetrieval": "task: code retrieval | query: ",
38 "Reranking": "task: search result | query: ",
39 "Retrieval": "task: search result | query: ",
40 "Retrieval-query": "task: search result | query: ",
41 "Retrieval-document": "title: none | text: ",
42 "STS": "task: sentence similarity | query: ",
43 "Summarization": "task: summarization | query: ",
44 "document": "title: none | text: "
45}
46
47