来源:互联网 更新时间:2026-08-23 14:02

知识库缺了必要的上下文,这事儿再常见不过。RAG系统在找不到确切答案时,不是老老实实说不知道,反而提供一些模棱两可的信息,用户被带到沟里,体验自然好不了。那么,怎么破?主要有两条路。
“垃圾进,垃圾出”这个道理大家都懂。源数据如果质量堪忧——比如存在矛盾信息、乱码、重复——再完美的RAG流程也白搭。高质量的数据是顺畅运行的基石。下面这些策略不仅解决这个痛点,还能顺带应付后续提到的其他问题。
知识库信息不足,系统容易“硬编”答案。这时候提示词设计很关键。比如说,明确告诉大模型:“如果你确定不了答案,就说不知道。”这能引导模型承认局限、表达得更清晰。当然,这不能保证绝对正确,但数据优化之后配上恰当的提示词,算得上是提高系统透明度的有效手段。
—2
初步检索阶段,关键文档可能因为排名不够高被漏掉,大模型也就没法给出精确响应。有研究直接点破:“问题的答案其实就藏在文档里,只是因为它排名不够高,所以没有被呈现给用户。” 怎么解决?两个方向。
chunk_size 和 similarity_top_k 是控制数据检索效率和准确性的两个核心参数。调一调它们,就能在计算效率和检索质量之间找到更好的平衡点。
把检索结果交给大模型之前,先重新排个序,效果提升相当明显。LlamaIndex 的笔记就展示了这一点:
参考代码:
import os
from llama_index.postprocessor.cohere_rerank import CohereRerank
api_key = os.environ["COHERE_API_KEY"]
cohere_rerank = CohereRerank(api_key=api_key, top_n=2)
query_engine = index.as_query_engine(
similarity_top_k=10,
node_postprocessors=[cohere_rerank]
)
response = query_engine.query("What did Sam Altman do in this essay?")
—3
大模型没能按照要求的格式输出,比如表格、列表之类。四种解法供参考。
输出解析可以这样用:
以下是用 LangChain 输出解析模块的示例代码(可在 LlamaIndex 中应用):
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.core.output_parsers import LangchainOutputParser
from llama_index.llms.openai import OpenAI
from langchain.output_parsers import StructuredOutputParser, ResponseSchema
documents = SimpleDirectoryReader("../paul_graham_essay/data").load_data()
index = VectorStoreIndex.from_documents(documents)
response_schemas = [
ResponseSchema(name="Education", description="Describes the author's educational experience/background."),
ResponseSchema(name="Work", description="Describes the author's work experience/background.")
]
lc_output_parser = StructuredOutputParser.from_response_schemas(response_schemas)
output_parser = LangchainOutputParser(lc_output_parser)
llm = OpenAI(output_parser=output_parser)
query_engine = index.as_query_engine(llm=llm)
response = query_engine.query("What are a few things the author did growing up?")
print(str(response))
Pydantic 程序能把输入字符串转换成结构化的 Pydantic 对象。LlamaIndex 提供几种类型:
一个 OpenAI Pydantic 程序示例:
from pydantic import BaseModel
from typing import List
from llama_index.program.openai import OpenAIPydanticProgram
class Song(BaseModel):
title: str
length_seconds: int
class Album(BaseModel):
name: str
artist: str
songs: List[Song]
prompt_template_str = """
Generate an example album, with an artist and a list of songs.
Using the movie {movie_name} as inspiration.
"""
program = OpenAIPydanticProgram.from_defaults(
output_cls=Album, prompt_template_str=prompt_template_str, verbose=True
)
output = program(movie_name="The Shining", description="Data model for an album.")
通过 OpenAI 的 JSON 模式,将 `response_format` 设为 `{ "type": "json_object" }`,大模型就只能生成可解析为有效 JSON 的字符串。这样输出格式就强制统一了,但注意它并不支持根据特定 schema 做验证。
—4
回答没把话说完。虽然部分答复没有错,但需要的信息明明在上下文中,却没能全部给出来。比如问“文档A、B和C中讨论的主要议题是什么?”,如果单独对每份文档查一遍,反而更靠谱。
比较类问题在最初的 RAG 方法里表现尤其差。提升推理能力的一个有效方式是引入查询理解层——在向量化查询之前先变换一下。四种常见变换方法:
以下是用 HyDE(假设文档嵌入)进行查询重写的示例。给定自然语言查询,首先生成一个假设文档/答案,然后用这个假设文档做嵌入搜索,而不是用原始查询。
documents = SimpleDirectoryReader("../paul_graham_essay/data").load_data()
index = VectorStoreIndex(documents)
query_str = "what did paul graham do after going to RISD"
hyde = HyDEQueryTransform(include_original=True)
query_engine = index.as_query_engine()
query_engine = TransformQueryEngine(query_engine, query_transform=hyde)
response = query_engine.query(query_str)
print(response)
腾讯ima怎么把微信内容一键导入知识库?
黄金价格不断创新高!黄金稳定币XAU、PAXG市值达11亿美元
腾讯ima怎么创建共享知识库?
Celestia价格预测2026-2032:TIA币能否引领山寨币上涨行情?历史价格回顾
新浪互联网热点小时报丨2026年07月26日16时_今日实时互联网热点速递
比特币(BTC)核心周期指标复刻历史走势 价格或跌破5.8万美元关键支撑位
新浪机器学习热点小时报丨2026年07月25日18时_今日实时机器学习热点速递
WorkBuddy微信版怎么获得积分?
新浪人工智能热点小时报丨2026年07月30日18时_今日实时人工智能热点速递
比特币 2025 年价格预测:BTC 的未来走势
车载冰箱重置到出厂设置几步?
5000元起的鼠标哪个最值得入手?
管线机怎么接云米净水器
短剧《史上最强洪荒修为》剧情介绍
Aptos(APT)2026-2032年价格预测与历史走势梳理
笔记本移动电源推荐哪款?
结婚家电首选:Leader懒人三筒Ultra热泵洗烘一体
kimi提示词专家使用方法新手指南
短剧《仙人跳获透视,古玩玉器我全拿捏》剧情介绍
腾讯ima知识库怎么分类管理?
手机号码测吉凶
本站所有软件,都由网友上传,如有侵犯你的版权,请发邮件haolingcc@hotmail.com 联系删除。 版权所有 Copyright@2012-2013 haoling.cc