0Pricing
LLM Apps in Production (RAG + Vector DB + Caching) · 课时

自查询与引用

通过能够将自然语言转换为元数据过滤条件的自查询检索器进一步提升 RAG,并让答案标注来源,使用户能够信任并验证结果。

自查询与引用 是 CoddyKit 上的免费 LLM Apps in Production (RAG + Vector DB + Caching) 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 LLM Apps in Production (RAG + Vector DB + Caching) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 LLM Apps in Production (RAG + Vector DB + Caching) 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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.

常见问题解答

「自查询与引用」课时是免费的吗?

是的 — 「自查询与引用」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 LLM Apps in Production (RAG + Vector DB + Caching) 课程的其余内容,请升级到 CoddyKit PRO。 LLM Apps in Production (RAG + Vector DB + Caching) 课程共包含 4 节课。

「自查询与引用」这节课中我会学到什么?

通过能够将自然语言转换为元数据过滤条件的自查询检索器进一步提升 RAG,并让答案标注来源,使用户能够信任并验证结果。 你通过在浏览器中直接运行的动手代码来练习 LLM Apps in Production (RAG + Vector DB + Caching),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 LLM Apps in Production (RAG + Vector DB + Caching) 需要有经验吗?

无需任何先前经验。CoddyKit 上的 LLM Apps in Production (RAG + Vector DB + Caching) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「自查询与引用」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 LLM Apps in Production (RAG + Vector DB + Caching) 课中编写并运行代码吗?

能。每节 LLM Apps in Production (RAG + Vector DB + Caching) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 查询改写与重新排序
  2. 多阶段与智能体式 RAG 模式
  3. 处理复杂文档结构
  4. 自查询与引用
← 返回 LLM Apps in Production (RAG + Vector DB + Caching)