Self-querying e citazioni
Portate RAG a un livello superiore con retriever self-querying che trasformano il linguaggio naturale in filtri sui metadati e con risposte che citano le fonti, così che gli utenti possano fidarsi e verificarle.
Self-querying e citazioni è una lezione LLM Apps in Production (RAG + Vector DB + Caching) gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento LLM Apps in Production (RAG + Vector DB + Caching), e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso LLM Apps in Production (RAG + Vector DB + Caching) include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
When Questions Carry Filters
Users ask things like give me 2023 reports about pricing. That sentence contains a filter (year 2023) and a semantic query (pricing).
A self-querying retriever automatically separates the two.
How Self-Querying Works
An LLM reads the question and emits a structured query: the semantic search string plus a metadata filter. The retriever then applies both to the vector store.
Describing Your Metadata
You tell the retriever what fields exist so it knows what it can filter on.
from langchain.chains.query_constructor.schema import AttributeInfo
fields = [
AttributeInfo(name='year', description='Publication year', type='integer'),
AttributeInfo(name='topic', description='Document topic', type='string')
]Building the Retriever
Combine the LLM, the store, a content description, and the field info into a self-query retriever.
from langchain.retrievers.self_query.base import SelfQueryRetriever
retriever = SelfQueryRetriever.from_llm(
llm, vectorstore,
'Company reports', fields
)Seeing It in Action
Now a natural-language question is split into a filter and a search automatically — no manual filter code.
docs = retriever.invoke(
'pricing reports from 2023'
)Why Citations Matter
In production, users must be able to verify answers. Unsourced answers are hard to trust and hide hallucinations. Citations link each claim back to its document.
Carrying Source Metadata
Citations rely on each chunk storing where it came from — file name, page, or URL — in its metadata. Set this at load time.
doc.metadata['source'] = 'policy.pdf#p3'Prompting for Citations
Number the context chunks and ask the model to cite the numbers it used. This is simple and reliable.
ctx = '\n'.join(
f'[{i}] {d.page_content}'
for i, d in enumerate(docs)
)
# 'Cite sources like [1] after each claim.'Mapping Numbers to Sources
After generation, map the cited numbers back to real source metadata so the UI can show clickable references.
sources = {i: d.metadata['source']
for i, d in enumerate(docs)}Verifying Citations
Models sometimes cite wrong or nonexistent sources. A safety check confirms each cited chunk actually supports the claim, flagging unsupported statements.
Putting It Together
Self-querying gets the right documents using filters in the question; citations make the resulting answer transparent. Together they raise both precision and trust in advanced RAG.
Quick Check
Test your advanced RAG knowledge.
Recap
You learned two advanced RAG techniques:
- Self-querying turns natural language into metadata filters plus a semantic query
- Describe your fields so the LLM knows what to filter
- Citations link claims to sources for trust
- Carry source metadata, prompt for citations, and verify them
Filtering and citing together make RAG both precise and trustworthy.
Domande Frequenti
La lezione «Self-querying e citazioni» è gratuita?
Sì — il testo completo di «Self-querying e citazioni» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso LLM Apps in Production (RAG + Vector DB + Caching), passa a CoddyKit PRO. Il corso LLM Apps in Production (RAG + Vector DB + Caching) include 4 lezioni in totale.
Cosa imparerò in «Self-querying e citazioni»?
Portate RAG a un livello superiore con retriever self-querying che trasformano il linguaggio naturale in filtri sui metadati e con risposte che citano le fonti, così che gli utenti possano fidarsi e… Eserciti LLM Apps in Production (RAG + Vector DB + Caching) con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare LLM Apps in Production (RAG + Vector DB + Caching)?
Non è richiesta alcuna esperienza precedente. LLM Apps in Production (RAG + Vector DB + Caching) su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Self-querying e citazioni»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione LLM Apps in Production (RAG + Vector DB + Caching)?
Sì. Ogni lezione LLM Apps in Production (RAG + Vector DB + Caching) include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Riscrittura delle query e reranking
- Pattern RAG multi-stage e agentici
- Gestire strutture documentali complesse
- Self-querying e citazioni