> ## Content Index
> Fetch the complete content index at: https://www.hubwiz.com/blog/llms.txt
> Use this file to discover other available public pages before exploring further.

# Memori：基于SQL的AI记忆引擎
- URL: https://www.hubwiz.com/blog/memori-sql-based-ai-memory-engine/
- Published: 2025-12-17T02:37:12.000Z
- Updated: 2025-12-17T02:37:12.000Z
- Description: Memori 是一款开源的 SQL 原生记忆引擎，它是首个旨在为AI系统提供连续、结构化且可查询记忆的产品。
- Author: admin
- Tags: AGENT, MEMORY, MEMORI, #Import 2026-10-14 14:41

自然语言处理、推理和问题解决领域取得了巨大的进步。然而，人工智能最关键、最基本的组成部分——记忆——却仍然没有得到解决。大多数人工智能应用倾向于以“无状态”模式运行，并将每一次通信和交换都视为独立实体。用户被迫一遍又一遍地重复相同的操作。人工智能应用的这种僵化导致生产力低下、个性化不足，并且无法将学习视为一个随时间累积的过程。

Memori 是一款开源的 SQL 原生记忆引擎，它是首个旨在为人工智能系统提供连续、结构化且可查询记忆的同类产品。传统的记忆解决方案基于向量数据库和专有系统，由于必须与专有解决方案交互，因此无法为用户提供简洁性、数据控制和成本节约。而 Memori 则可以直接与 SQL 数据库（SQLite、PostgreSQL、MySQL）交互。

## 1、什么是 Memori？

Memori 是一个用于 AI 应用的记忆层，它使模型能够：

- 记住过去的交互（对话、偏好、项目）
- 无需手动提示即可智能检索上下文
- 将记忆存储在标准 SQL 数据库中（无厂商锁定）
- 高效扩展（比向量数据库便宜 80-90%）

![](https://www.hubwiz.com/blog/content/images/2025/12/image-266.png)

## 2、Memori与传统解决方案有何不同

![](https://www.hubwiz.com/blog/content/images/2025/12/image-267.png)

## 3、为什么选择 Memori？

### 3.1 极致简洁

Memori 只需一行代码即可为任何 LLM 启用记忆：

```
memori.enable()  # That's it!
```

### 3.2 真正的数据所有权

与闭源记忆系统不同，Memori 将所有数据存储在标准 SQL 数据库中（默认使用 SQLite，企业级使用 PostgreSQL/MySQL）。

- 可以将整个记忆导出为 .db 文件。
- 直接使用 SQL 查询记忆。
- 不受厂商锁定——您可以将数据迁移到任何地方。

### 3.3 成本效益

向量数据库（例如 Pinecone、Chroma）在大规模部署时成本可能非常高昂。Memori：

- 使用 SQL 索引实现快速检索。
- 避免使用高维嵌入（这会消耗大量计算资源）。
- 与基于向量的解决方案相比，成本降低 80-90%。

### 3.4 合规性和可审计性

由于记忆存储在 SQL 中，您可以获得：

- 完整的审计跟踪（谁在何时访问了什么）
- 数据驻留控制（记忆托管在本地或首选云平台）
- 符合 GDPR/CCPA 标准（轻松删除/导出数据）

## 4、使用Memori构建 AI

让我们逐步了解如何使用 OpenAI 的 Memori 进行实现。

步骤一：安装

```
pip install memorisdk openai
```

步骤二：基本设置（意识模式）

```
from memori import Memori
from openai import OpenAI

# Initialize OpenAI client
openai_client = OpenAI()

# Enable Memori with conscious memory (short-term injection)
memori = Memori(conscious_ingest=True)
memori.enable()  # Starts recording all LLM interactions

print("=== First Conversation - Establishing Context ===")
response1 = openai_client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "I'm working on a Python FastAPI project."}]
)
print("Assistant:", response1.choices[0].message.content)

## Output
Assistant: That sounds great! FastAPI is a fantastic choice for building APIs in Python.
Would you like help with routing, database integration, or authentication?
```

步骤 3：测试记忆保留

```
print("\n=== Second Conversation - Memory Provides Context ===")
response2 = openai_client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Help me add user authentication."}]
)
print("Assistant:", response2.choices[0].message.content)

## Output

Assistant: Since you're working on a **FastAPI project**, I recommend using **OAuth2 with JWT** for authentication.
Here's a basic structure:

```python
from fastapi import Depends, FastAPI, HTTPException
from fastapi.security import OAuth2PasswordBearer

app = FastAPI()
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")

@app.get("/protected-route")
async def protected_route(token: str = Depends(oauth2_scheme)):
    return {"message": "Authenticated!"}
```

步骤 4：直接查询记忆（SQL）

由于记忆存储在 SQLite 中，您可以手动检查它们：

```
sqlite3 my_ai_memory.db

-- View all stored memories
SELECT * FROM memories;

-- Search for FastAPI-related memories
SELECT * FROM memories
WHERE content LIKE '%FastAPI%';
```

## 5、结束语

Memori 代表了 AI 系统处理记忆方式的范式转变。它利用 SQL 原生存储、双内存模式和极致的简洁性，解决了以下关键挑战：

- 不再有无状态交互——AI 像人类一样记忆。
- 避免厂商锁定——您的数据始终属于您。
- 避免成本飙升——SQL 比向量数据库便宜得多。
- 避免合规性难题——完全可审计和可控。

---

原文链接：[Memori: The Open-Source SQL-Native Memory Engine for AI](https://medium.com/@andysingal/memori-the-open-source-sql-native-memory-engine-for-ai-f8902369225d?ref=hubwiz.com)

汇智网翻译整理，转载请标明出处