# Memory 與知識

> PentAGI 如何在 flow 中記住命令、結果與關聯性。

PentAGI 保留三種 [memory](https://pentagi.com/zh-tw/docs/glossary/#memory) 類型，讓 [agents](https://pentagi.com/zh-tw/docs/glossary/#agent) 能夠在先前工作的基礎上繼續執行，而不必從頭開始。

| Memory    | 儲存位置                             | 儲存內容                                          |
| --------- | -------------------------------- | --------------------------------------------- |
| 長期 memory | PostgreSQL with pgvector         | 結果、成功方法與 [tool](https://pentagi.com/zh-tw/docs/glossary/#tool) 知識的嵌入向量 |
| 工作 memory | [Flow](https://pentagi.com/zh-tw/docs/glossary/#flow) 上下文 | 當前目標、[task](https://pentagi.com/zh-tw/docs/glossary/#task) 狀態與可用資源     |
| 情節 memory | 命令歷史記錄                           | 過去的動作、其結果以及有效的模式                              |

每個命令及其輸出都會儲存在 PostgreSQL 中。Agents 在採取行動前會查詢 [vector store](https://pentagi.com/zh-tw/docs/glossary/#vector-store) 以尋找類似案例，並將 [findings](https://pentagi.com/zh-tw/docs/glossary/#finding) 寫回——`memorist` 和 `searcher` agents 負責大部分的查詢工作。

## [Knowledge graph（選用）](https://pentagi.com/zh-tw/docs/concepts/memory/#knowledge-graph-optional)

啟用 [knowledge graph](https://pentagi.com/zh-tw/docs/glossary/#knowledge-graph) 堆疊後，PentAGI 會將 agent 回應和 tool 執行傳送至 Neo4j 上的 [Graphiti](https://github.com/getzep/graphiti)。依據 `.env` template 的擷取規則，它會從終端機與檔案操作中提取實體與關係——包括主機、服務、漏洞、技術，以及它們隨時間演變的關聯性——並讓其他事件保持可搜尋狀態而不進行擷取。

```bash
GRAPHITI_ENABLED=true
GRAPHITI_URL=http://graphiti:8000
GRAPHITI_CONFIG_DIR=llm_configs
GRAPHITI_LLM_CLIENT_TYPE=openai
NEO4J_URI=bolt://neo4j:7687
NEO4J_PASSWORD=replace_with_a_strong_password
```

```bash
git clone --depth 1 https://github.com/vxcontrol/pentagi.git pentagi-src
cp -r pentagi-src/examples/graphiti ./graphiti
cp -r pentagi-src/examples/neo4j ./neo4j
rm -rf pentagi-src
curl -O https://raw.githubusercontent.com/vxcontrol/pentagi/main/docker-compose-graphiti.yml
docker compose -f docker-compose.yml -f docker-compose-graphiti.yml up -d
```

Graphiti 執行其專屬模型：`GRAPHITI_LLM_CLIENT_TYPE` 會選擇預設組態 — `openai`、`gemini`、`litellm` 或 `custom` — 其模型設定於 `./graphiti/<preset>.yaml`，而對應的金鑰則來自 `.env`（`openai` 使用 `OPEN_AI_KEY`）。2.2.0 之前的 `.env` 缺少這些行與 `GRAPHITI_INGEST_*` 規則：請從目前的 `.env.example` 複製它們。

> [!WARNING]
> Graphiti 為測試版整合功能，預設為關閉狀態。召回範圍僅限於當前 flow；內建的 Graphiti API 未具備驗證機制，因此請將其與 Neo4j 部署於私有網路中。提取與嵌入向量的費用獨立於 flow 本身的模型計費。

PentAGI 僅在啟動時檢查 Graphiti，持續數秒。若屆時 Graphiti 尚未就緒，PentAGI 會記錄警告訊息，並在下次重新啟動前於無 knowledge graph 的狀態下執行；因此，一旦 Graphiti 回報健康狀態，請執行 `docker restart pentagi`。