ARTICLE DETAIL

资讯详情

深耕网站视觉设计与运营推广的一线实战洞察。

Memori 异步场景下什么时候需要调用 augmentation.wait()?

Memori 异步场景下什么时候需要调用 augmentation.wait()? Memori 异步场景下什么时候需要调用 augmentation.wait()【免费下载链接】MemoriMemori is agent-native memory infrastructure. A LLM-agnostic layer that turns agent execution and conversation into structured, persistent state for production systems. Built for enterprise, Memori works with the data infrastructure you already run, no rip-and-replace, and deploys across managed cloud, single-tenant cloud, VPC, and on-premises.项目地址: https://gitcode.com/GitHub_Trending/me/Memori在 Memori BYODBbring your own database的 Python 和 TypeScript 项目中把 LLM 客户端注册进 Memori 之后Advanced Augmentation 引擎会在后台处理对话读取完整会话、识别事实与偏好、抽取语义三元组、生成向量嵌入最后写入你自己的数据库。整个处理是异步的——LLM 响应立即返回会话在后台排队处理见 Advanced Augmentation。于是异步场景的核心判断就变成什么时候必须调用augmentation.wait()什么时候应该故意不调用在短生命周期脚本里漏掉它进程可能在后台处理完成前退出记忆不会落库而在长驻服务里调用它则会把本不该阻塞的响应路径拖慢。下面的判断规则和可运行示例全部来自官方文档 Async Patterns、FAQ 与 Troubleshooting。判断规则四类运行上下文augmentation.wait()的语义是阻塞直到后台 augmentation 完成FAQ 原文It blocks until background augmentation finishes. Only needed in short-lived scripts that might exit before processing completes.。Async Patterns 文档给出了完整判断表运行上下文PythonTypeScript短生命周期脚本Short-lived scriptmem.augmentation.wait()await mem.augmentation.wait()Web 服务器Web server不需要不需要测试套件Test suitemem.augmentation.wait()await mem.augmentation.wait()无服务器函数Serverless functionmem.augmentation.wait()await mem.augmentation.wait()判断标准一句话进程/实例会在后台处理完成前退出或冻结就必须等。测试套件和无服务器函数属于这一类而 Web 服务器是长驻进程文档在 Express 示例中明确说明In a long-running server, omitaugmentation.wait()— augmentation continues in the background without blocking the response并且建议每个请求创建新的 Memori 实例使每个请求拥有独立的 attribution 和 session。注意两种语言的调用形式不同Python 是同步调用的mem.augmentation.wait()即使在async def内也是如此TypeScript 一律写作await mem.augmentation.wait()。必调 wait 的主路径异步短脚本准备条件以文档 quickstart 为准PythonPython 3.10一个 OpenAI API key执行pip install memori openai并设置环境变量export OPENAI_API_KEYyour-openai-api-keyyour-openai-api-key替换为你自己的 keyTypeScriptNode.js 20执行npm install memorilabs/memori openai better-sqlite3同样设置OPENAI_API_KEY。Python 异步脚本Async Patterns 文档给出的完整示例SQLite 演示build()会创建 Memori 的 schema 表import os import asyncio from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from memori import Memori from openai import AsyncOpenAI engine create_engine(sqlite:///memori.db) SessionLocal sessionmaker(bindengine) async def main(): client AsyncOpenAI(api_keyos.getenv(OPENAI_API_KEY)) mem Memori(connSessionLocal).llm.register(client) mem.attribution(entity_iduser_123, process_idasync_agent) mem.config.storage.build() response await client.chat.completions.create( modelgpt-4.1-mini, messages[{role: user, content: I prefer async Python.}] ) print(response.choices[0].message.content) mem.augmentation.wait() asyncio.run(main())connSessionLocal传的是 session 工厂而非单个 session——文档的线程安全表格里标注connSessionLocal(factory) 是安全模式而connlambda: existing_session共享单个 session 是不安全模式。TypeScript 短脚本import dotenv/config; import Database from better-sqlite3; import { OpenAI } from openai; import { Memori } from memorilabs/memori; const db new Database(memori.db); const client new OpenAI(); const mem new Memori({ conn: () db }).llm.register(client); mem.attribution(user_123, my-script); if (!mem.config.storage) { throw new Error(Storage not initialized); } await mem.config.storage.build(); const response await client.chat.completions.create({ model: gpt-4.1-mini, messages: [{ role: user, content: My favorite color is blue. }], }); console.log(response.choices[0]?.message?.content); // Required in short-lived scripts — augmentation runs in the background await mem.augmentation.wait(); db.close();TypeScript 的conn接收的是工厂函数而不是连接本身。文档的连接工厂表格里conn: () db传入已打开的better-sqlite3Database 实例是安全模式conn: () sharedClient这种共享单个客户端的模式则不安全。长驻服务里不调用 waitFastAPI 示例启动时构建一次 storage每个请求内注册客户端、设置 attribution然后直接返回 LLM 响应全程不调用 wait示例来自 Async Patternsimport os from fastapi import FastAPI from pydantic import BaseModel from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from memori import Memori from openai import AsyncOpenAI app FastAPI() engine create_engine(sqlite:///memori.db, connect_args{check_same_thread: False}) SessionLocal sessionmaker(bindengine) Memori(connSessionLocal).config.storage.build() class ChatRequest(BaseModel): message: str app.post(/chat/{user_id}) async def chat(user_id: str, req: ChatRequest): client AsyncOpenAI(api_keyos.getenv(OPENAI_API_KEY)) mem Memori(connSessionLocal).llm.register(client) mem.attribution(entity_iduser_id, process_idfastapi_async) response await client.chat.completions.create( modelgpt-4.1-mini, messages[{role: user, content: req.message}] ) return {response: response.choices[0].message.content}TypeScript 的 Express / Fastify 示例是同一模式启动阶段await mem.config.storage.build()请求处理内新建new Memori({ conn: () pool })并注册客户端文档注释明确写着 Dont await augmentation — let it run in the background。生产 Python 服务建议使用 PostgreSQL 并放大连接池pool_pre_pingTrue, pool_size20, max_overflow40, pool_recycle300这也是 Async Patterns 文档给出的生产建议。如何验证记忆是否落库运行短脚本后直接查询自己的数据库确认对话和抽取结果已写入Quickstart 提供的检查命令sqlite3 memori.db SELECT * FROM memori_conversation_message; sqlite3 memori.db SELECT * FROM memori_entity_fact;用 Python Quickstart / TypeScript Quickstart 的跨会话验证创建完全新的 client 和 Memori 实例不带任何先前上下文再问 Whats my favorite color?。文档预期第二次响应应能召回 blue以此证明记忆已跨会话持久化。如果记忆没写入或召回为空按 Troubleshooting 文档处理No Memories Being Created① 确认 LLM 调用前已设置 attribution未设置则不存记忆② 短生命周期脚本中调用mem.augmentation.wait()③ 确认 LLM 客户端已通过llm.register()注册——未注册时对话不会被捕获。Recall Returns Empty核对召回使用的entity_id与写入时一致调用mem.augmentation.wait()TypeScriptawait mem.augmentation.wait()Python 可增大召回条数mem.recall(query, limit10)或降低相关性阈值mem.config.recall_relevance_threshold 0.05TypeScript 为mem.config.recallRelevanceThreshold 0.05;。限制与边界attribution是硬前提Advanced Augmentation 文档的 important 提示明确说明没有 attributionMemori 既不能创建也不能召回记忆。首次运行时 Memori 会下载嵌入模型首次运行偏慢可用python -m memori setup预下载。未注册 API key 的开源版本按 IP 有 Advanced Augmentation 配额遇到QuotaExceededError时按错误提示注册免费 API key文档标注免费 key 为 5,000/月并通过export MEMORI_API_KEYyour-key设置。文档示例统一使用 SQLite OpenAIgpt-4.1-mini演示更换数据库或 provider 时wait()的判断规则不变仍按上文四类上下文对照执行。进一步阅读Async Patterns线程安全与连接工厂模式、Advanced Augmentation抽取类型与数据库表结构、Troubleshooting无记忆/召回为空的排查清单。【免费下载链接】MemoriMemori is agent-native memory infrastructure. A LLM-agnostic layer that turns agent execution and conversation into structured, persistent state for production systems. Built for enterprise, Memori works with the data infrastructure you already run, no rip-and-replace, and deploys across managed cloud, single-tenant cloud, VPC, and on-premises.项目地址: https://gitcode.com/GitHub_Trending/me/Memori创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表