forked from cleardry/MemOS-LLM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembedder.py
More file actions
48 lines (39 loc) · 1.36 KB
/
embedder.py
File metadata and controls
48 lines (39 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from memos.configs.embedder import EmbedderConfigFactory
from memos.embedders.factory import EmbedderFactory
# Scenario 1: Using EmbedderFactory
config = EmbedderConfigFactory.model_validate(
{
"backend": "ollama",
"config": {
"model_name_or_path": "nomic-embed-text:latest",
},
}
)
embedder = EmbedderFactory.from_config(config)
text = "This is a sample text for embedding generation."
embedding = embedder.embed([text])
print("Scenario 1 embedding shape:", len(embedding[0]))
print("==" * 20)
# Scenario 2: Batch embedding generation
texts = [
"First sample text for batch embedding.",
"Second sample text for batch embedding.",
"Third sample text for batch embedding.",
]
embeddings = embedder.embed(texts)
print("Scenario 2 batch embeddings count:", len(embeddings))
print("Scenario 2 first embedding shape:", len(embeddings[0]))
print("==" * 20)
# Scenario 3: Using SenTranEmbedder
config_hf = EmbedderConfigFactory.model_validate(
{
"backend": "sentence_transformer",
"config": {
"model_name_or_path": "nomic-ai/nomic-embed-text-v1.5",
},
}
)
embedder_hf = EmbedderFactory.from_config(config_hf)
text_hf = "This is a sample text for Hugging Face embedding generation."
embedding_hf = embedder_hf.embed([text_hf])
print("Scenario 3 HF embedding shape:", len(embedding_hf[0]))