ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

Hindsight Documents 文档管理指南:记忆溯源、批量更新与删除的完整实践

Hindsight Documents 文档管理指南:记忆溯源、批量更新与删除的完整实践 Hindsight Documents 文档管理指南记忆溯源、批量更新与删除的完整实践【免费下载链接】hindsightHindsight: Agent Memory That Learns项目地址: https://gitcode.com/GitHub_Trending/hindsight2/hindsight导读Documents 是 Hindsight 记忆库Memory Bank中负责溯源与组织的核心概念每次 retain 的内容都会归属到一个文档记忆从哪份 PDF、哪次对话、哪个文件而来都可以一路追踪。本文围绕 Hindsight 的 Documents API 展开完整覆盖 chunk 存储机制、带document_id的 retain、文档更新/删除/列表查询含tags_match四种匹配模式、响应格式与底层实现并给出 Python、Node.js、CLI 多语言可直接运行的示例。读完你能够为记忆建立可追溯的来源体系并安全地执行按文档批量的增、改、查、删。前置要求先完成 Quick Start并理解 retain 的工作方式。本文引用的 HTTP 路由与参数说明可在 api/http.py 中交叉验证。什么是 DocumentsDocuments 是保留内容retained content的容器。借助它你可以追踪来源——知道某条记忆来自哪份 PDF、哪次对话或哪个文件更新内容——对同一文档重新 retain刷新其中的事实批量删除——一次移除某个文档衍生出的全部记忆组织记忆——按来源对相关事实进行分组。在 Hindsight 的存储模型中文档是原始文本与提取事实之间的桥梁。从源码结构看documents表保存original_text、content_hash、memory_unit_count等字段相关表结构在 alembic/versions/5a366d414dce_initial_schema.py 中定义而每条记忆单元memory unit通过document_id回指其来源文档。这也解释了为什么 Hindsight 的统计响应里会出现total_documents与nodes_by_fact_type并列见 http.py 中的BankStatsResponse示例。Chunks事实背后的原始文本切片当内容被 retain 时Hindsight 会先将其切分为 chunks再从中提取事实。这些 chunk 与提取出的记忆一起存储保留了产生事实的原始文本片段对应chunks表见迁移 b7c4d8e9f1a2_add_chunks_table.py。Chunks 为什么重要上下文保留——chunk 中包含生成事实的原始文本当你需要逐字引用时可以直接取用更丰富的召回——在 recall 中包含 chunks可以为匹配到的事实提供上下文。在 Recall 中包含 Chunks在召回调用中设置include_chunksTrue即可在事实结果旁拿到原始文本块详见 Recall。服务端还提供了专门的 chunk 查询端点GET /v1/default/banks/{bank_id}/documents/{document_id}/chunksoperation_idlist_document_chunks按 chunk 索引顺序返回某文档的全部切片支持limit默认 100最大 1000与offset分页文档不存在时返回 404见 http.py。带 Document ID 的 Retain在 retain 时通过document_id参数把内容关联到某个文档。多条共享同一document_id的内容会被归并到同一文档省略时由系统自动生成该行为在 http.py 的MemoryInput.document_id字段说明中明确。Python# Retain with document ID client.retain( bank_idmy-bank, contentAlice presented the Q4 roadmap..., document_idmeeting-2024-03-15 ) # Batch retain for a document with different sections client.retain_batch( bank_idmy-bank, items[ {content: Item 1: Product launch delayed to Q2, document_id: meeting-2024-03-15-section-1}, {content: Item 2: New hiring targets announced, document_id: meeting-2024-03-15-section-2}, {content: Item 3: Budget approved for ML team, document_id: meeting-2024-03-15-section-3} ] )Node.js// Retain with document ID await client.retain(my-bank, Alice presented the Q4 roadmap..., { document_id: meeting-2024-03-15 }); // Batch retain for a document with different sections await client.retainBatch(my-bank, [ { content: Item 1: Product launch delayed to Q2, document_id: meeting-2024-03-15-section-1 }, { content: Item 2: New hiring targets announced, document_id: meeting-2024-03-15-section-2 }, { content: Item 3: Budget approved for ML team, document_id: meeting-2024-03-15-section-3 } ]);CLI# Retain content with document ID hindsight memory retain my-bank Meeting notes content... --doc-id notes-2024-03-15 # Batch retain from files hindsight memory retain-files my-bank docs/批量场景下每个文件还可以单独指定document_id、context、metadata、tagsFileRetainMetadata见 http.py实现每个文件一个文档的细粒度组织。更新 Documents重新 retain 实现替换使用相同的document_id重新 retain会替换旧内容——旧事实被删除、新事实被创建。其底层支持update_mode默认replace删除旧数据并从头重新处理可选append将新内容拼接到现有文档文本后再处理见 http.py。Python# Original client.retain( bank_idmy-bank, contentProject deadline: March 31, document_idproject-plan ) # Update (deletes old facts, creates new ones) client.retain( bank_idmy-bank, contentProject deadline: April 15 (extended), document_idproject-plan )Node.js// Original await client.retain(my-bank, Project deadline: March 31, { document_id: project-plan }); // Update await client.retain(my-bank, Project deadline: April 15 (extended), { document_id: project-plan });CLI# Original hindsight memory retain my-bank Project deadline: March 31 --doc-id project-plan # Update hindsight memory retain my-bank Project deadline: April 15 (extended) --doc-id project-plan获取 Document获取文档的原始文本与元数据。在召回操作返回带文档引用的记忆后用它来展开文档上下文非常有用服务端实现在 http.py 的get_document文档不存在返回 404。Pythonfrom hindsight_client_api import ApiClient, Configuration from hindsight_client_api.api import DocumentsApi async def get_document_example(): config Configuration(hosthttp://localhost:8888) api_client ApiClient(config) api DocumentsApi(api_client) # Get document to expand context from recall results doc await api.get_document( bank_idmy-bank, document_idmeeting-2024-03-15 ) print(fDocument: {doc.id}) print(fOriginal text: {doc.original_text}) print(fMemory count: {doc.memory_unit_count}) print(fCreated: {doc.created_at}) asyncio.run(get_document_example())Node.js// Get document to expand context from recall results const { data: doc, error } await sdk.getDocument({ client: apiClient, path: { bank_id: my-bank, document_id: meeting-2024-03-15-section-1 } }); if (error) { throw new Error(Failed to get document: ${JSON.stringify(error)}); } console.log(Document: ${doc.id}); console.log(Original text: ${doc.original_text}); console.log(Memory count: ${doc.memory_unit_count}); console.log(Created: ${doc.created_at});CLIhindsight document get my-bank meeting-2024-03-15更新 Document标签管理无需重新处理内容直接更新已有文档的可变字段。当前支持更新tags。HTTP 端点为PATCH /v1/default/banks/{bank_id}/documents/{document_id}operation_idupdate_document要求至少提供一个字段见 http.py。tags数组是整体替换不是合并请发送你希望文档最终拥有的完整集合——任何未包含的标签都会被移除空数组则清空全部。要删除单个标签需先读取文档当前标签去掉目标项后再发送其余部分。完全省略该字段不算一次更新会被拒绝并返回422。CLI# Replace tags with new values hindsight document update-tags my-bank meeting-2024-03-15 --tags team-a --tags team-b # Remove all tags hindsight document update-tags my-bank meeting-2024-03-15ℹ️ 观测会被重新整合re-consolidated标签变更时任何由该文档记忆衍生出的 consolidated observations 都会被失效并排队在新标签下重新整合。来自其他文档、但共享了这些观测的同源co-source记忆也会被重置。这是正确性要求而非附带效应consolidation 按记忆的标签集划定作用域旧标签下构建的 observation 不再有效若直接删除它会让所有从该 observation 整合出来的其他记忆失去来源除非这些记忆也一并重新入队。重排队列的规模等于与该文档同源记忆的数量——在密集同源的 bank 上可能是该文档自身记忆数的数倍。标签按集合与文档当前标签比较任何不改变集合的更新包括仅调整数组顺序都不会触发 retag也不会排队重新整合。因此可重复执行的标签规范化扫描只有在真正发生变更的那一轮才付出重新整合的代价。删除 Document删除一个文档及其全部关联记忆HTTP 端点为DELETE /v1/default/banks/{bank_id}/documents/{document_id}operation_iddelete_document见 http.py。Pythonfrom hindsight_client_api import ApiClient, Configuration from hindsight_client_api.api import DocumentsApi async def delete_document_example(): config Configuration(hosthttp://localhost:8888) api_client ApiClient(config) api DocumentsApi(api_client) # Delete document and all its memories result await api.delete_document( bank_idmy-bank, document_idmeeting-2024-03-15 ) print(fDeleted {result.memory_units_deleted} memories) asyncio.run(delete_document_example())Node.js// Delete document and all its memories const { data: deleteResult } await sdk.deleteDocument({ client: apiClient, path: { bank_id: my-bank, document_id: meeting-2024-03-15-section-1 } }); console.log(Deleted ${deleteResult.memory_units_deleted} memories);CLIhindsight document delete my-bank meeting-2024-03-15⚠️ 警告删除文档会永久移除从中提取的所有记忆。该操作无法撤销。删除成功时响应会携带memory_units_deleted计数DeleteDocumentResponse模型见 http.py可在执行前先get查看memory_unit_count预估影响面。列出 Documents列出 bank 中的文档支持按 ID 与标签过滤。HTTP 端点为GET /v1/default/banks/{bank_id}/documentsoperation_idlist_documents结果按updated_at降序最近写入优先返回见 http.py。Pythonfrom hindsight_client_api import ApiClient, Configuration from hindsight_client_api.api import DocumentsApi async def list_documents_example(): config Configuration(hosthttp://localhost:8888) api_client ApiClient(config) api DocumentsApi(api_client) # List all documents result await api.list_documents(bank_idmy-bank) print(fTotal documents: {result.total}) # Filter by document ID substring result await api.list_documents(bank_idmy-bank, qreport) # Filter by tags — only docs tagged with team-a (untagged excluded) result await api.list_documents( bank_idmy-bank, tags[team-a], tags_matchany_strict, ) # Combine ID search and tags result await api.list_documents( bank_idmy-bank, qmeeting, tags[team-a, team-b], tags_matchall_strict, # must have both tags ) # Paginate result await api.list_documents(bank_idmy-bank, limit20, offset40) print(fPage items: {len(result.items)}) import asyncio asyncio.run(list_documents_example())Node.jsconst apiClient createClient(createConfig({ baseUrl: http://localhost:8888 })); // List all documents const { data: allDocs } await sdk.listDocuments({ client: apiClient, path: { bank_id: my-bank } }); console.log(Total documents: ${allDocs.total}); // Filter by document ID substring const { data: reportDocs } await sdk.listDocuments({ client: apiClient, path: { bank_id: my-bank }, query: { q: report } }); // Filter by tags — only docs tagged with team-a (untagged excluded) const { data: taggedDocs } await sdk.listDocuments({ client: apiClient, path: { bank_id: my-bank }, query: { tags: [team-a], tags_match: any_strict } }); // Combine ID search and tags const { data: filtered } await sdk.listDocuments({ client: apiClient, path: { bank_id: my-bank }, query: { q: meeting, tags: [team-a, team-b], tags_match: all_strict } }); // Paginate const { data: page } await sdk.listDocuments({ client: apiClient, path: { bank_id: my-bank }, query: { limit: 20, offset: 40 } }); console.log(Page items: ${page.items.length});CLI# List all documents hindsight document list my-bank # Filter by ID substring hindsight document list my-bank --q report # Filter by tags hindsight document list my-bank --tags team-a --tags team-b过滤选项参数说明q对文档 ID 的大小写不敏感子串匹配。report可匹配report-2024、annual-report等tags按文档标签过滤可传多个值tags_match标签匹配方式默认any_strict见下表limit/offset分页。默认 limit 为 100tags_match模式模式行为any_strict(默认)文档必须拥有至少一个指定标签。未打标签的文档被排除any与any_strict相同但同时包含未打标签的文档all_strict文档必须拥有全部指定标签。未打标签的文档被排除all与all_strict相同但同时包含未打标签的文档limit与offset参数在服务端均受ge0约束见 http.pyGo 客户端中也提供了对应的ListDocuments、GetDocument、DeleteDocument等封装见 hindsight-clients/go/api_documents.go 与 model_document_list_item.go。Document 响应格式{ id: meeting-2024-03-15, bank_id: my-bank, original_text: Alice presented the Q4 roadmap..., content_hash: abc123def456, memory_unit_count: 12, nodes_by_fact_type: { world: 5, experience: 4, observation: 3 }, created_at: 2024-03-15T14:00:00Z, updated_at: 2024-03-15T14:00:00Z }nodes_by_fact_type按事实类型world/experience/observation统计该文档衍生的记忆数可用于快速评估文档在记忆图谱中的占比。此外响应还包含document_metadata、retain_params含 retain 时配置的observation_scopes如all_combinations、per_tag或显式标签集合见 http.py以及文档附件信息。若 bank 关闭了原文存储store_document_textFalse见 http.pyoriginal_text可能为空此时可从 chunk 文本回退获取。下一步Operations— 监控后台任务Memory Banks— 配置 bank 设置【免费下载链接】hindsightHindsight: Agent Memory That Learns项目地址: https://gitcode.com/GitHub_Trending/hindsight2/hindsight创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表