Hindsight LlamaIndex 集成演进全解从 0.1.2 到 0.1.5 的版本历史与实现原理【免费下载链接】hindsightHindsight: Agent Memory That Learns项目地址: https://gitcode.com/GitHub_Trending/hindsight2/hindsight本指南以仓库内 LlamaIndex 集成 Changelog 为骨架系统梳理hindsight-llamaindex从 0.1.2 初版到 0.1.5 的每一个版本变更并结合 SDK 参考文档 与集成源码为你讲清每个 changelog 条目到底改了什么、为什么改、在代码里如何体现。读完你将掌握该集成的两种记忆模式工具驱动与自动记忆的完整用法、配置项的默认值与取值、0.1.4/0.1.5 两项关键改进统一 User-Agent 与云端默认连接的源码依据以及如何运行其门控端到端测试。一、版本时间线总览hindsight-llamaindex是 Hindsight 为 LlamaIndex 框架提供的官方集成包为 LlamaIndex Agent 提供持久化长期记忆。根据 pyproject.toml 中的版本号当前仓库承载的已是0.1.5版本Changelog 共记录了 4 个发布版本版本类型核心变更0.1.2首个版本新增 LlamaIndex 集成commit2d787c4f0.1.3Bug 修复修复文档 ID、记忆 API 与 ReAct 轨迹处理问题commitd93dfea80.1.4改进统一 User-Agent9372462e、PEP 561 类型标记d054b884、依赖安全升级ee4510a70.1.5改进 修复门控端到端测试套件ed34756c、默认连接 Hindsight Clouded34756c版本号与发行说明的对应关系可在 集成 Changelog 原文 中逐条核对该文档同时声明了每个集成独立发布节奏其 changelog 可在集成页找到——这与 主 Changelog 只覆盖核心API、CLI、控制平面的定位相互补充。二、0.1.2集成初版的功能底座0.1.2 是hindsight-llamaindex的起点changelog 仅一条Added LlamaIndex integration for Hindsight.。这一版本所确立的架构至今未变从 集成包源码结构 可以还原初版就具备的两大互补模式2.1 工具驱动模式HindsightToolSpec基于 LlamaIndex 的BaseToolSpec抽象将 Hindsight 的记忆操作暴露为 Agent 可自主调用的工具。tools.py 中spec_functions定义了三个工具且同步/异步实现成对提供异步供 ReActAgent 等异步 Agent 使用同步作为回退spec_functions [ (retain_memory, aretain_memory), (recall_memory, arecall_memory), (reflect_on_memory, areflect_on_memory), ]三个工具分别对应 Hindsight 的三类核心操作retain_memory存储信息到长期记忆用于保存重要事实、用户偏好、决策等跨会话需要记住的内容recall_memory检索相关记忆返回编号列表实现见 tools.py无结果时返回No relevant memories found.reflect_on_memory基于记忆进行综合推理产出连贯总结而非原始事实tools.py 中直接返回response.text。2.2 自动记忆模式HindsightMemory基于 LlamaIndex 的BaseMemory接口实现每轮自动存储、召回注入上下文。关键生命周期见 memory.py事件发生什么Agent 收到输入aget(input)从 Hindsight 召回相关记忆以 SYSTEM 消息前缀注入Agent 产生输出aput(message)将消息 retain 到 Hindsight 供将来召回新会话开始本地聊天缓冲清空但历史记忆仍可通过 recall 获得其中值得注意的细节是_recall_query()memory.py当aget()未显式传入input时会自动回退到本地历史中最近一条 USER 消息作为召回查询——这是为 workflow 型 LlamaIndex Agent如llama_index.core.agent.workflow.ReActAgent在主路径上不带input调用aget()的情况专门设计的。2.3 初版即可用的 Quick Start两个模式的完整最小示例均收录在 集成 README 与 SDK 参考 中工具驱动模式如下import asyncio from hindsight_client import Hindsight from hindsight_llamaindex import HindsightToolSpec from llama_index.llms.openai import OpenAI from llama_index.core.agent import ReActAgent async def main(): client Hindsight(base_urlhttp://localhost:8888) spec HindsightToolSpec( clientclient, bank_iduser-123, missionTrack user preferences, ) tools spec.to_tool_list() agent ReActAgent(toolstools, llmOpenAI(modelgpt-4o)) response await agent.run(Remember that I prefer dark mode) print(response) asyncio.run(main())自动记忆模式则通过HindsightMemory.from_client(client..., bank_iduser-123, mission...)创建然后以await agent.run(..., memorymemory)的形式传入注意是传给run()而不是构造函数。三、0.1.3针对可靠性三个层面的 Bug 修复0.1.3 的 changelog 条目写道Fixed LlamaIndex integration issues with document IDs, the memory API, and ReAct trace handling to improve reliability and correctness.三个关键词对应源码中的三处实现3.1 document IDs自动生成唯一文档 IDmemory.py 与 tools.py 中均有_generate_document_id()生成格式为{session_id}-{uuid4().hex[:12]}其中session_id在实例构造时以str(uuid.uuid4())[:8]随机生成。若用户未显式指定retain_document_id每次 retain 调用都会自动生成独立文档 IDtools.py 中的_retain_kwargs避免文档 ID 冲突导致覆盖。3.2 memory API完善 BaseMemory 接口实现HindsightMemory完整实现了BaseMemory的全部接口方法get/aget、put/aput、get_all、set/aset、reset。其中set/asetmemory.py只对超出原历史长度的新增消息执行 retain避免重复存储。reset()仅清空本地缓冲不会删除 Hindsight 中的长期记忆memory.py。3.3 ReAct 轨迹处理只保留最终答案当助手消息带有 ReAct 推理轨迹Thought:/Action:/Action Input:/Observation:前缀时memory.py 中的_extract_clean_content()会用正则_REACT_PATTERN检测是否为 ReAct 推理消息若是则用_ANSWER_PATTERN提取最后一个Answer:块之后的文本作为干净内容若只有推理轨迹而没有Answer:返回空字符串并跳过 retain防止把中间推理过程污染长期记忆。这一设计保证了存入 Hindsight 的是最终答案而非冗长的推理过程。四、0.1.4工程化与安全加固0.1.4 是纯改进版本三条变更体现了集成包从能用走向工程化4.1 统一 User-Agent 请求头commit9372462e所有 HTTP 请求现在携带一致的识别性 User-Agent 头。实现位于 _client.pytry: _VERSION metadata.version(hindsight-llamaindex) except metadata.PackageNotFoundError: _VERSION 0.0.0 _USER_AGENT fhindsight-llamaindex/{_VERSION}该 UA 通过resolve_client()中Hindsight(**{base_url: url, timeout: TIMEOUT_DEFAULT, user_agent: _USER_AGENT})注入客户端_client.py。同时 _client.py 还定义了按操作区分的超时retain 15s、recall 10s、reflect 30s、bank 15s、默认 30s。这让服务端能够识别流量来源也是排查问题时定位哪个集成版本在调用的关键线索。4.2 PEP 561 类型标记文件commitd054b884包内新增py.typed标记文件见 hindsight_llamaindex 包目录。PEP 561 约定包内存在py.typed文件即声明该包是内联类型的mypy、pyright等类型检查器会直接使用包内的类型注解而不再回退到.pyi桩文件或types-*第三方桩。对于使用 LlamaIndex 的强类型代码库这意味着HindsightToolSpec、HindsightMemory、configure()等的类型信息开箱即用。4.3 依赖安全升级commitee4510a7更新依赖以修复关键级和高危级安全漏洞。从 pyproject.toml 可以看到当前依赖面收敛在两个核心包上llama-index-core0.11.0 hindsight-client0.4.0这提示安全升级主要作用于传递依赖链。同时 pyproject.toml 也声明了 Python 3.10 的要求与 MIT 许可证。对使用方而言升级到 0.1.4 及以上版本即可获得修复后的依赖锁定仓库内提供 uv.lock。五、0.1.5开箱即用的连接与门控测试0.1.5 是本 changelog 的最新版本包含一项改进和一项 Bug 修复两者都由DK09876在 commited34756c中完成5.1 改进门控端到端测试套件Replaced the deprecated manual test with a gated end-to-end test suite, improving integration reliability without requiring a real LLM for every run.这句 changelog 需要拆解两个关键词gated门控从 pyproject.toml 可以看到测试标记机制markers [ requires_real_llm: end-to-end test that needs live external services (a running Hindsight server and/or real LLM provider keys). Excluded from the deterministic PR-CI bucket via -m not requires_real_llm; run on its own via -m requires_real_llm., ]即端到端测试被标记为requires_real_llm常规 PR-CI 用-m not requires_real_llm排除需要真实服务时单独用-m requires_real_llm运行。without requiring a real LLM for every run测试注释test_e2e.py解释了原因——该集成直接与 Hindsight 服务通信事实抽取由服务端的 LLM 完成因此端到端测试只需要一个运行中的 Hindsight 实例无需在测试侧配置任何 LLM 提供方密钥。test_e2e.py 展示了门控机制的具体实现HINDSIGHT_API_URL os.getenv(HINDSIGHT_API_URL, http://localhost:8888) def _hindsight_available() - bool: try: with urllib.request.urlopen(f{HINDSIGHT_API_URL}/health, timeout3) as r: return r.status 200 except Exception: return False requires_hindsight pytest.mark.skipif( not _hindsight_available(), reasonfHindsight not reachable at {HINDSIGHT_API_URL}, ) pytestmark [requires_hindsight, pytest.mark.requires_real_llm]测试通过探测{HINDSIGHT_API_URL}/health判断服务是否可达不可达则整体跳过。文件中还提供了_recall_until_nonempty()辅助函数test_e2e.py由于 retain 后事实抽取是异步的它会以 1 秒间隔最多轮询 12 次等待记忆从已提交变为可召回。5.2 Bug 修复默认连接 Hindsight CloudLlamaIndex integration now defaults to using Hindsight Cloud, improving out-of-the-box connectivity and reducing setup issues.这一修复让集成零配置可用。默认 URL 常量定义在 config.pyDEFAULT_HINDSIGHT_API_URL https://api.hindsight.vectorize.io HINDSIGHT_API_KEY_ENV HINDSIGHT_API_KEY客户端解析逻辑resolve_client()_client.py按如下优先级决定连接参数显式传入的client实例优先否则使用hindsight_api_url参数或configure()配置的 URL都没有则回退到DEFAULT_HINDSIGHT_API_URL即 Hindsight CloudAPI Key 依次从api_key参数 →configure()→HINDSIGHT_API_KEY环境变量解析。关键点在于 API Key在构造时是可选的——注释_client.py明确写道a missing key only fails when a call is actually made即只有真正发起调用时才需要 Key。这意味着设置好HINDSIGHT_API_KEY环境变量后create_hindsight_tools(bank_iduser-123)或HindsightMemory.from_defaults(bank_iduser-123)即可直接连通云端完全无需手动拼接客户端。这一云端默认行为同样覆盖了HindsightMemory。测试 test_memory.py 中的test_from_defaults_uses_cloud_default_when_nothing_supplied用例专门钉住了该行为注释提到这是2026-06-02 audit要求的行为对齐此前HindsightMemory要求显式传 client 或 URL而工具工厂已具备云端默认能力两者行为不一致。自托管用户通过hindsight_api_urlhttp://localhost:8888覆盖默认即可。六、配置系统configure() 与参数优先级贯穿 0.1.4/0.1.5 两版改进的基础设施是全局配置系统。configure()config.py以进程级全局单例保存默认值from hindsight_llamaindex import configure configure( hindsight_api_urlhttp://localhost:8888, api_keyyour-api-key, # 或设置 HINDSIGHT_API_KEY 环境变量 budgetmid, tags[source:llamaindex], contextmy-app, missionTrack user preferences, )config.py 中HindsightLlamaIndexConfig的完整默认值如下参数默认值说明hindsight_api_urlhttps://api.hindsight.vectorize.ioHindsight API 地址api_keyNone回退HINDSIGHT_API_KEY认证密钥调用时才校验budgetmid召回预算级别low/mid/highmax_tokens4096召回结果最大 token 数tagsNone存储记忆时的默认标签recall_tagsNone召回时过滤的标签recall_tags_matchany标签匹配模式any/all/any_strict/all_strictcontextllamaindexretain 操作的来源标签missionNone银行bank使命用于事实抽取上下文verboseFalse是否输出详细日志参数解析优先级tools.py遵循None 哨兵回退模式构造HindsightToolSpec/HindsightMemory时传入的参数最优先未传入时回退到configure()全局配置全局配置也未设置时再落到代码内默认值。reset_config()可清空全局配置。更多参数细节如retain_metadata、recall_types、reflect_response_schema等可查 SDK 参考的参数表。七、从 Changelog 到实战三条可直接落地的建议结合上述版本历史可以总结出三个与升级决策直接相关的实践要点1. 升级到 0.1.4 以获得安全与可观测性保障。0.1.4 修复了关键/高危安全漏洞并让所有请求携带hindsight-llamaindex/{version}User-Agent便于在服务端日志中识别流量来源py.typed让类型检查器原生生效。2. 使用 0.1.5 的云端默认连接简化引导。只需设置HINDSIGHT_API_KEY环境变量即可零配置连接 Hindsight Cloud自托管场景用configure(hindsight_api_urlhttp://localhost:8888)一行覆盖。注意 API Key 在构造时不校验、调用时才校验的行为因此配置错误会在首次操作时以友好错误信息暴露。3. 本地验证利用门控测试而非手动脚本。运行HINDSIGHT_API_URL... pytest -m requires_real_llm tests/test_e2e.py即可对运行中的 Hindsight 实例做完整 retain → recall 轮询 → reflect 验证测试文件见 test_e2e.py日常 CI 用-m not requires_real_llm只跑确定性的单元测试如 test_memory.py 与 test_tools.py无需真实 LLM。八、结论从 0.1.2 的功能底座到 0.1.3 对文档 ID、记忆 API 与 ReAct 轨迹处理的可靠性修复再到 0.1.4 的工程化User-Agent、PEP 561、安全升级与 0.1.5 的体验优化云端默认连接、门控 e2e 测试hindsight-llamaindex的四次发布勾勒出一条清晰的成熟路径。每个 changelog 条目都能在 hindsight-integrations/llamaindex 源码与测试中找到对应实现——这正是理解版本变更如何落地的最佳方式。如需查看更多集成包的独立发布记录可对照 主 Changelog 索引 中各集成页入口继续深入。【免费下载链接】hindsightHindsight: Agent Memory That Learns项目地址: https://gitcode.com/GitHub_Trending/hindsight2/hindsight创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考