Haystack#
pip install "needtoknow[haystack]"
NeedToKnowRetriever is a Haystack component that returns only the documents a subject may read,
from any needtoknow backend and authorizer. The extra requires haystack-ai 2.12 or later; CI tests
the current Haystack 3 release.
from haystack import Pipeline
from needtoknow import PermissionedRetriever
from needtoknow.haystack import NeedToKnowRetriever
pipeline = Pipeline()
pipeline.add_component("retriever", NeedToKnowRetriever(PermissionedRetriever(backend, authorizer), top_k=5))
# ... a prompt builder and a generator, connected to "retriever.documents"
result = pipeline.run({"retriever": {"query": question, "subject": subject}})
The subject is a run input#
One pipeline serves every user: the application passes the authenticated user as subject on
each run, and the component refuses to run without a needtoknow Subject. Build it from your
session, the user id and groups your identity provider vouches for, never from the request body or
from a model’s output (security model).
In an agent, keep subject out of the tool’s inputs that the model fills in.
Inputs and outputs#
Name |
Meaning |
|
|---|---|---|
Input |
|
The question to search for |
Input |
|
The authenticated user ( |
Input |
|
Optional; overrides the component’s |
Input |
|
Optional; a vector to search with instead of embedding |
Output |
|
Haystack |
Output |
|
False when needtoknow knows results may be missing (see concepts) |
run_async has the same inputs and outputs, for Pipeline.run_async. Any failure of the
permission service raises AuthorizationError, so the pipeline stops rather than generating from
unfiltered documents.
The component holds live connections to your vector store and permission service, so a pipeline that contains it cannot be serialized to YAML; build the pipeline in code.