← Back to learning path

This chapter has not been reviewed in English; the original Simplified Chinese is shown.

Chapter · Published

应用内 RAG:检索、引用与权限过滤 (original language)

把检索结果变成可追溯上下文,避免把无权限文档送给模型。

Source: knowledge/p24-c/collection_0587a2bfc47ea3584d1b1c59/tutorials/ai-app-rag-grounding.mdAI application development
Chapter progress 0%Learning path progress 0% · 0/10 chapters completedContinue learning path →

RAG 的价值在于把回答绑定到可追溯资料,而不是让模型记住更多内容。检索前的权限过滤和回答后的引用检查同样重要。

核心概念

文档、版本、租户和访问标签应贯穿导入、索引、检索和答案。上下文拼装要保留来源 id,零结果时应明确说明没有找到资料,而不是补写事实。

可运行示例

from dataclasses import dataclass

@dataclass(frozen=True)
class Snippet:
    doc_id: str
    text: str
    roles: frozenset[str]

def authorized(snippet: Snippet, role: str) -> bool:
    return role in snippet.roles

snippets = [Snippet('policy-1', '退款需要订单号', frozenset({'support'}))]
context = [item.text for item in snippets if authorized(item, 'support')]

操作步骤

  1. 定义文档 id、版本、来源和访问标签。
  2. 先做租户和角色过滤,再执行向量或关键词检索。
  3. 为每个片段保留来源 id、标题和位置,输出时强制引用。
  4. 测试无结果、过期文档、越权角色和提示注入。

Use cases

适合产品手册、内部制度、客服知识库和有权限边界的资料问答。

常见问题

检索命中就代表答案正确吗? 不代表,还要检查来源是否支持结论。

能把所有文档都放进 prompt 吗? 应按相关性、权限和上下文预算裁剪。

官方参考

OpenAI Retrieval guide;OWASP LLM Top 10。

最后核对日期:2026-11-16。版本、接口和安全边界以链接的官方资料为准;本文不虚构性能或费用结论。

Related practical cases

Knowledge实战:带权限过滤的本地 RAG 服务 (original language)
Product Hub Demo · V0.3