Skip to content

feat: add superlinked retriever integration #32433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
140 changes: 140 additions & 0 deletions docs/docs/integrations/providers/superlinked.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
---
title: Superlinked
description: LangChain integration package for the Superlinked retrieval stack
---

import Link from '@docusaurus/Link';

### 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.

### Links

- <Link to="https://github.com/superlinked/langchain-superlinked">Integration repository</Link>
- <Link to="https://links.superlinked.com/langchain_repo_sl">Superlinked core repository</Link>
- <Link to="https://links.superlinked.com/langchain_article">Article: Build RAG using LangChain & Superlinked</Link>

### Install

```bash
pip install -U langchain-superlinked superlinked
```

### Quickstart

```python
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:

```python
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

```python
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

- <Link to="https://pypi.org/project/langchain-superlinked/">PyPI: langchain-superlinked</Link>
- <Link to="https://pypi.org/project/superlinked/">PyPI: superlinked</Link>
- <Link to="https://github.com/superlinked/langchain-superlinked">Source repository</Link>
- <Link to="https://links.superlinked.com/langchain_repo_sl">Superlinked core repository</Link>
- <Link to="https://links.superlinked.com/langchain_article">Build RAG using LangChain & Superlinked (article)</Link>


Loading