DSPy
The sie-dspy package provides DSPy-compatible modules for SIE: dense and sparse embedders, a reranker, and an extractor supporting entities, relations, classifications, and object detection.
Installation
Section titled “Installation”pip install sie-dspyThis installs sie-sdk and numpy as dependencies.
Start the Server
Section titled “Start the Server”# Docker (recommended)docker run -p 8080:8080 ghcr.io/superlinked/sie-server:default
# Or with GPUdocker run --gpus all -p 8080:8080 ghcr.io/superlinked/sie-server:defaultEmbedder
Section titled “Embedder”SIEEmbedder is callable and works directly with DSPy’s retrieval components.
from sie_dspy import SIEEmbedder
embedder = SIEEmbedder( base_url="http://localhost:8080", model="BAAI/bge-m3",)
# Embed a single textvector = embedder("What is machine learning?")print(vector.shape) # (1024,)
# Embed multiple textsvectors = embedder(["First document", "Second document"])print(vectors.shape) # (2, 1024)Configuration Options
Section titled “Configuration Options”| Parameter | Type | Default | Description |
|---|---|---|---|
base_url | str | http://localhost:8080 | SIE server URL |
model | str | BAAI/bge-m3 | Model to use |
gpu | str | None | Target GPU type for routing |
options | dict | None | Model-specific options |
timeout_s | float | 180.0 | Request timeout in seconds |
Full Example
Section titled “Full Example”Build a complete DSPy RAG program using SIE embeddings:
import dspyfrom sie_dspy import SIEEmbedder
# 1. Configure DSPy with your LLMlm = dspy.LM("openai/gpt-4o-mini")dspy.configure(lm=lm)
# 2. Create SIE embedderembedder = SIEEmbedder( base_url="http://localhost:8080", model="BAAI/bge-m3",)
# 3. Build retriever with your corpuscorpus = [ "Machine learning is a branch of artificial intelligence.", "Neural networks are inspired by biological neurons.", "Deep learning uses multiple layers of neural networks.", "Python is popular for machine learning development.",]
retriever = dspy.retrievers.Embeddings( corpus=corpus, embedder=embedder, k=2,)
# 4. Define a RAG signatureclass RAG(dspy.Signature): """Answer questions using retrieved context.""" context: list[str] = dspy.InputField(desc="retrieved passages") question: str = dspy.InputField() answer: str = dspy.OutputField()
# 5. Build the RAG programclass RAGProgram(dspy.Module): def __init__(self): self.retriever = retriever self.generate = dspy.ChainOfThought(RAG)
def forward(self, question: str): passages = self.retriever(question) return self.generate(context=passages, question=question)
# 6. Run the programrag = RAGProgram()result = rag("What is deep learning?")print(result.answer)Sparse Embeddings
Section titled “Sparse Embeddings”SIESparseEmbedder provides sparse vectors for hybrid search workflows.
from sie_dspy import SIEEmbedder, SIESparseEmbedder
dense_embedder = SIEEmbedder(model="BAAI/bge-m3")sparse_embedder = SIESparseEmbedder(model="BAAI/bge-m3")
# Index corpus - store both in your vector DB (Qdrant, Weaviate, etc.)dense_vecs = dense_embedder(corpus)sparse_vecs = sparse_embedder.embed_documents(corpus)
# Querydense_query = dense_embedder(query)sparse_query = sparse_embedder.embed_query(query)Configuration Options
Section titled “Configuration Options”| Parameter | Type | Default | Description |
|---|---|---|---|
base_url | str | http://localhost:8080 | SIE server URL |
model | str | BAAI/bge-m3 | Model to use |
gpu | str | None | Target GPU type for routing |
options | dict | None | Model-specific options |
timeout_s | float | 180.0 | Request timeout in seconds |
Reranking
Section titled “Reranking”SIEReranker is a dspy.Module that reranks passages by relevance to a query using a cross-encoder model.
from sie_dspy import SIEReranker
reranker = SIEReranker( base_url="http://localhost:8080", model="jinaai/jina-reranker-v2-base-multilingual",)
result = reranker( query="What is machine learning?", passages=["ML learns from data.", "Weather is sunny.", "Deep learning uses neural nets."], k=2,)
print(result.passages) # Top 2 most relevant passagesprint(result.scores) # Corresponding relevance scoresConfiguration Options
Section titled “Configuration Options”| Parameter | Type | Default | Description |
|---|---|---|---|
base_url | str | http://localhost:8080 | SIE server URL |
model | str | jinaai/jina-reranker-v2-base-multilingual | Reranker model |
gpu | str | None | Target GPU type for routing |
options | dict | None | Model-specific options |
timeout_s | float | 180.0 | Request timeout in seconds |
Extraction
Section titled “Extraction”SIEExtractor is a dspy.Module that extracts structured data from text. It supports all extraction types: entities (GLiNER), relations (GLiREL), classifications (GLiClass), and object detection (GroundingDINO/OWL-v2). The forward() method returns a dspy.Prediction with entities, relations, classifications, and objects fields (plus _dict variants for JSON serialization).
Entity Extraction
Section titled “Entity Extraction”from sie_dspy import SIEExtractor
extractor = SIEExtractor( base_url="http://localhost:8080", model="urchade/gliner_multi-v2.1", labels=["person", "organization", "location"],)
result = extractor(text="Tim Cook announced new products at Apple Park in Cupertino.")
for entity in result.entities: print(f"{entity.label}: {entity.text} ({entity.score:.2f})")# person: Tim Cook (0.96)# organization: Apple (0.91)# location: Cupertino (0.88)
# Also available as dicts for JSON serializationprint(result.entities_dict)Relation Extraction
Section titled “Relation Extraction”Extract relationships between entities using GLiREL:
from sie_dspy import SIEExtractor
extractor = SIEExtractor( base_url="http://localhost:8080", model="jackboyla/glirel-large-v0", labels=["works_for", "ceo_of", "founded"],)
result = extractor(text="Tim Cook is the CEO of Apple Inc.")
for relation in result.relations: print(f"{relation.head} --{relation.relation}--> {relation.tail}")# Tim Cook --ceo_of--> Apple Inc.
# Also available as dictsprint(result.relations_dict)Text Classification
Section titled “Text Classification”Classify text into categories using GLiClass:
from sie_dspy import SIEExtractor
extractor = SIEExtractor( base_url="http://localhost:8080", model="knowledgator/gliclass-base-v1.0", labels=["positive", "negative", "neutral"],)
result = extractor(text="I absolutely loved this movie! The acting was superb.")
for classification in result.classifications: print(f"{classification.label}: {classification.score:.2f}")# positive: 0.94# neutral: 0.04# negative: 0.02
# Also available as dictsprint(result.classifications_dict)Configuration Options
Section titled “Configuration Options”The extraction model determines which result fields are populated.
| Parameter | Type | Default | Description |
|---|---|---|---|
base_url | str | http://localhost:8080 | SIE server URL |
model | str | urchade/gliner_multi-v2.1 | Extraction model (GLiNER, GLiREL, GLiClass, GroundingDINO, OWL-v2) |
labels | list[str] | ["person", "organization", "location"] | Labels for extraction (entity types, relation types, or classification categories) |
gpu | str | None | Target GPU type for routing |
options | dict | None | Model-specific options |
timeout_s | float | 180.0 | Request timeout in seconds |
What’s Next
Section titled “What’s Next”- Encode Text - embedding API details
- Score / Rerank - reranking details
- Extract - extraction details (NER, relations, classification, vision)
- Model Catalog - all supported models
- Troubleshooting - common errors and solutions