Skip to main content
Open on GitHub

Superlinked

Overview

Superlinked enables context‑aware retrieval using multiple space types (text similarity, categorical, numerical, recency, and more). The langchain-superlinked package provides a LangChain‑native SuperlinkedRetriever that plugs directly into your RAG chains.

Install

pip install -U langchain-superlinked superlinked

Quickstart

import superlinked.framework as sl
from langchain_superlinked import SuperlinkedRetriever

# 1) Define schema
class DocumentSchema(sl.Schema):
id: sl.IdField
content: sl.String

doc_schema = DocumentSchema()

# 2) Define space and index
text_space = sl.TextSimilaritySpace(
text=doc_schema.content, model="sentence-transformers/all-MiniLM-L6-v2"
)
doc_index = sl.Index([text_space])

# 3) Define query
query = (
sl.Query(doc_index)
.find(doc_schema)
.similar(text_space.text, sl.Param("query_text"))
.select([doc_schema.content])
.limit(sl.Param("limit"))
)

# 4) Minimal app setup
source = sl.InMemorySource(schema=doc_schema)
executor = sl.InMemoryExecutor(sources=[source], indices=[doc_index])
app = executor.run()
source.put([
{"id": "1", "content": "Machine learning algorithms process data efficiently."},
{"id": "2", "content": "Natural language processing understands human language."},
])

# 5) LangChain retriever
retriever = SuperlinkedRetriever(
sl_client=app, sl_query=query, page_content_field="content"
)

# Search
docs = retriever.invoke("artificial intelligence", limit=2)
for d in docs:
print(d.page_content)

What the retriever expects (App and Query)

The retriever takes two core inputs:

  • sl_client: a Superlinked App created by running an executor (e.g., InMemoryExecutor(...).run())
  • sl_query: a QueryDescriptor returned by chaining sl.Query(...).find(...).similar(...).select(...).limit(...)

Minimal setup:

import superlinked.framework as sl
from langchain_superlinked import SuperlinkedRetriever

class Doc(sl.Schema):
id: sl.IdField
content: sl.String

doc = Doc()
space = sl.TextSimilaritySpace(text=doc.content, model="sentence-transformers/all-MiniLM-L6-v2")
index = sl.Index([space])

query = (
sl.Query(index)
.find(doc)
.similar(space.text, sl.Param("query_text"))
.select([doc.content])
.limit(sl.Param("limit"))
)

source = sl.InMemorySource(schema=doc)
app = sl.InMemoryExecutor(sources=[source], indices=[index]).run()

retriever = SuperlinkedRetriever(sl_client=app, sl_query=query, page_content_field="content")

Note: For a persistent vector DB, pass vector_database=... to the executor (e.g., Qdrant) before .run().

Use within a chain

from langchain_core.runnables import RunnablePassthrough
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI

def format_docs(docs):
return "\n\n".join(doc.page_content for doc in docs)

prompt = ChatPromptTemplate.from_template(
"""
Answer based on context:\n\nContext: {context}\nQuestion: {question}
"""
)

chain = ({"context": retriever | format_docs, "question": RunnablePassthrough()}
| prompt
| ChatOpenAI())

answer = chain.invoke("How does machine learning work?")

Resources