定制 tivins/llm-memory 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tivins/llm-memory

Composer 安装命令:

composer require tivins/llm-memory

包简介

Semantic memory store for LLM applications — embed, store, recall via pgvector.

README 文档

README

Semantic memory store for LLM applications — embed, store, and recall text fragments via pgvector.

Overview

A PHP library that provides a RAG-style retrieval pipeline:

  1. Store text fragments in PostgreSQL + pgvector with automatic embedding.
  2. Recall relevant fragments using a two-stage pipeline: vector ANN search → cross-encoder reranking.
  3. Deduplicate entries via SHA-256 content hashing per collection.

Requirements

  • PHP 8.3+
  • PostgreSQL with pgvector
  • llama.cpp embedding server (port 8081, e.g. bge-m3)
  • llama.cpp rerank server (port 8082, e.g. Qwen3-Reranker) — optional

Installation

composer require tivins/llm-memory

Quick start

use Tivins\LlmLib\LLM;
use Tivins\LlmMemory\MemoryStore;
use Tivins\Pdow\Database;

$db       = Database::connect('pgsql:host=127.0.0.1;dbname=memory_project', 'user', 'pass');
$embedder = new LLM('http://127.0.0.1:8081', defaultModel: 'bge-m3-Q8_0.gguf');
$reranker = new LLM('http://127.0.0.1:8082', defaultModel: 'Qwen3-Reranker-4B-Q4_K_M.gguf');

$memory = new MemoryStore($db, $embedder, $reranker);
$memory->migrate();
$memory->ensureCollection('user:42', 'bge-m3-Q8_0.gguf', 1024);

// Store
$memory->remember('user:42', 'Marie is allergic to peanuts.', kind: 'fact');

// Recall
$results = $memory->recall('user:42', 'What foods should Marie avoid?', topN: 3);

foreach ($results as $hit) {
    echo $hit->entry->content . ' (score: ' . $hit->rerankScore . ")\n";
}

// Forget
$memory->forget($entryId);
$memory->forgetCollection('user:42');

API

Method Description
ensureCollection(id, model, dim) Create a collection if it doesn't exist
remember(collection, content, kind?, metadata?) Store a text fragment (embeds automatically, deduplicates)
recall(collection, query, topN?, annTopK?) Retrieve relevant entries (ANN + optional rerank)
forget(id) Soft-delete a single entry
forgetCollection(id) Soft-delete all entries in a collection
count(collection) Count active entries in a collection
migrate() Run SQL migrations

Dependencies

Package Role
tivins/llm-lib Embedding & rerank HTTP clients
tivins/pdow PDO database wrapper

License

MIT.

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-06-21