嵌入缓存和回答缓存怎样用?
查看完整题解
参考回答
直接回答
入库时可用 IngestionPipeline 的缓存,复用相同节点与变换的结果,省去重复切分或嵌入。回答缓存通常由应用按需要加在查询边界,只有条件相同、允许复用的请求才共享结果。
讲清原理
嵌入相关的键要反映资料和变换版本。回答键还需区分模型、提示、会话或权限范围、索引版本,并有适当的失效时间。供应方的前缀缓存复用模型计算,和保存一份最终答案是不同机制。
例子与易错点
示例让同一个节点进入同一变换两次,核对第二次直接读到相同结果。应用自己的回答缓存仍要按租户和资料更新策略单独设计。
from llama_index.core.ingestion import IngestionPipeline
from llama_index.core.schema import TextNode, TransformComponent
from pydantic import PrivateAttr
class LabelFixture(TransformComponent):
_calls: int = PrivateAttr(default=0)
def __call__(self, nodes, **kwargs):
self._calls += 1
return [
node.model_copy(update={"text": "[checked] " + node.text})
for node in nodes
]
marker = LabelFixture()
pipeline = IngestionPipeline(transformations=[marker])
original = TextNode(id_="sample", text="Check the batteries.")
first = pipeline.run(nodes=[original])
second = pipeline.run(nodes=[original])
assert first[0].text == second[0].text == "[checked] Check the batteries."
assert original.text == "Check the batteries." and marker._calls == 1
print("two runs, one transformation")
这道题,你掌握得怎么样?
掌握程度由你主动标记,阅读和收藏不会自动改变状态。