Embeddings (LLMEmbed)¶
LLMEmbed generates vector embeddings from text, useful for semantic search, clustering, and RAG applications.
Basic Usage¶
from microdc import Client, LLMEmbed
client = Client(api_key="mDC_...")
job = LLMEmbed(model="text-embedding-ada-002")
job.add_texts(["Hello world", "Goodbye world"])
job_id = client.send_job(job)
client.wait_for_all()
result = client.get_job_details(job_id)
embeddings = result.result['embeddings']
for i, embedding in enumerate(embeddings):
print(f"Text {i}: {len(embedding)} dimensions")
Adding Texts¶
job = LLMEmbed(model="text-embedding-ada-002")
# Add a single text
job.add_text("Hello world")
# Add multiple texts at once
job.add_texts([
"The quick brown fox",
"A journey of a thousand miles",
"To be or not to be"
])
Configuration Options¶
job = LLMEmbed(
model="text-embedding-ada-002", # Required: model name
dimensions=None, # Embedding dimensions
normalize=True, # Normalize embeddings
encoding_format="float" # Output format: "float" or "base64"
)
| Parameter | Type | Default | Description |
|---|---|---|---|
model |
str |
(required) | Embedding model name |
dimensions |
int |
None |
Output embedding dimensions |
normalize |
bool |
True |
Normalize output vectors |
encoding_format |
str |
"float" |
"float" or "base64" |
Batch Embeddings¶
For large batches of texts:
from microdc import Client, LLMEmbed
client = Client(api_key="mDC_...")
# Large batch of texts
texts = [f"Document {i}" for i in range(100)]
# Split into chunks for parallel processing
chunk_size = 20
job_ids = []
for i in range(0, len(texts), chunk_size):
chunk = texts[i:i + chunk_size]
job = LLMEmbed(model="text-embedding-ada-002")
job.add_texts(chunk)
job.metadata = {"chunk_index": i // chunk_size}
job_ids.append(client.send_job(job))
client.wait_for_all()
# Collect all embeddings
all_embeddings = []
for job_id in job_ids:
details = client.get_job_details(job_id)
if details.is_successful():
all_embeddings.extend(details.result['embeddings'])
client.acknowledge_job(job_id)
print(f"Generated {len(all_embeddings)} embeddings")
Use Case: Semantic Search¶
import numpy as np
from microdc import Client, LLMEmbed
client = Client(api_key="mDC_...")
# Embed documents
documents = [
"Python is a programming language",
"Machine learning uses statistical methods",
"Neural networks are inspired by the brain"
]
doc_job = LLMEmbed(model="text-embedding-ada-002")
doc_job.add_texts(documents)
doc_id = client.send_job(doc_job)
# Embed query
query_job = LLMEmbed(model="text-embedding-ada-002")
query_job.add_texts(["What is ML?"])
query_id = client.send_job(query_job)
client.wait_for_all()
# Calculate similarities
doc_result = client.get_job_details(doc_id)
query_result = client.get_job_details(query_id)
doc_embeddings = np.array(doc_result.result['embeddings'])
query_embedding = np.array(query_result.result['embeddings'][0])
similarities = np.dot(doc_embeddings, query_embedding)
best_match = documents[np.argmax(similarities)]
print(f"Best match: {best_match}")
See the RAG notebook
For a complete RAG implementation, see the Simple RAG notebook.