smart_brain 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/README.en.md +33 -0
- data/README.md +127 -1
- data/docs/installation.md +198 -0
- data/docs/media_memory_schema.md +271 -0
- data/docs/retrieval_plan.md +3 -1
- data/docs/user_guide.md +1 -0
- data/examples/07_media_memory.rb +53 -0
- data/lib/smart_brain/adapters/smart_rag/direct_client.rb +41 -0
- data/lib/smart_brain/adapters/smart_rag/http_client.rb +104 -2
- data/lib/smart_brain/adapters/smart_rag/http_transport.rb +138 -0
- data/lib/smart_brain/adapters/smart_rag/media_metadata_extractor.rb +255 -0
- data/lib/smart_brain/adapters/smart_rag/null_client.rb +37 -0
- data/lib/smart_brain/configuration.rb +4 -1
- data/lib/smart_brain/runtime.rb +19 -1
- data/lib/smart_brain/version.rb +1 -1
- data/lib/smart_brain.rb +15 -2
- metadata +36 -42
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 79a975252d1cd55bc21928851ba6c2e250ad7d6a87d26d659c025da5e18836d6
|
|
4
|
+
data.tar.gz: 4a5cff31efd5d411dd14e8100561a2ab6fd04678801eeb283f493261329a73c6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: '01534922911c9446056dc33859bec7cb6818593a08ab59d5194beb4fb1877a104cf59535309ccf66015a4d8ac199c9bfd47d6480331a12872642dbe53d39b6cf'
|
|
7
|
+
data.tar.gz: 711502b6b7435e057669580b174c03425a03ccfd5f38c4d57f79307b6ec7a37e95ec6ee6090a3da36a25b435b9ed3d13ee89e9d4008206030a1cf0addb90820c
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.3.0 - 2026-08-14
|
|
4
|
+
|
|
5
|
+
- Add document, image, audio, and video memory ingestion through the public runtime facade.
|
|
6
|
+
- Add asynchronous media job enqueue, status, listing, cancellation, retry, and queue statistics APIs.
|
|
7
|
+
- Add media metadata extraction with graceful partial-result fallbacks.
|
|
8
|
+
- Add multipart-capable SmartRAG HTTP transport with scoped retrieval and media operations.
|
|
9
|
+
- Propagate media-type resource filters into SmartRAG retrieval plans.
|
|
10
|
+
- Keep null, direct, and HTTP SmartRAG adapters behaviorally aligned for optional deployments.
|
|
11
|
+
- Require SmartRAG 0.2 for the media, principal-isolation, and durable-job contracts.
|
|
12
|
+
|
|
3
13
|
## 0.2.0 - 2026-08-03
|
|
4
14
|
|
|
5
15
|
- Add domain-isolated global, project, expert, task, and legacy session memory scopes.
|
data/README.en.md
CHANGED
|
@@ -33,6 +33,7 @@ The repository now includes a runnable v0.1 flow with:
|
|
|
33
33
|
- `config/brain.yml`: policy config
|
|
34
34
|
- `example.rb`: SmartBrain + SmartAgent + SmartPrompt + SmartRAG demo
|
|
35
35
|
- `docs/`: design and protocol documents
|
|
36
|
+
- `docs/media_memory_schema.md`: media-memory contract, production controls, and SmartRAG migration requirements (Chinese)
|
|
36
37
|
|
|
37
38
|
## Installation
|
|
38
39
|
|
|
@@ -98,6 +99,17 @@ client = SmartBrain::Adapters::SmartRag::HttpClient.new(transport: transport, ti
|
|
|
98
99
|
SmartBrain.configure(smart_rag_client: client)
|
|
99
100
|
```
|
|
100
101
|
|
|
102
|
+
For the built-in SmartRAG HTTP API, configure the remote endpoint and Bearer token directly:
|
|
103
|
+
|
|
104
|
+
```ruby
|
|
105
|
+
client = SmartBrain::Adapters::SmartRag::HttpClient.for_url(
|
|
106
|
+
base_url: 'http://127.0.0.1:9393',
|
|
107
|
+
timeout_seconds: 30,
|
|
108
|
+
headers: { 'Authorization' => "Bearer #{ENV.fetch('SMARTRAG_TOKEN')}" }
|
|
109
|
+
)
|
|
110
|
+
SmartBrain.configure(smart_rag_client: client)
|
|
111
|
+
```
|
|
112
|
+
|
|
101
113
|
### 3) DirectClient (used in `example.rb`)
|
|
102
114
|
|
|
103
115
|
```ruby
|
|
@@ -111,6 +123,27 @@ client = SmartBrain::Adapters::SmartRag::DirectClient.new(rag: rag)
|
|
|
111
123
|
SmartBrain.configure(smart_rag_client: client)
|
|
112
124
|
```
|
|
113
125
|
|
|
126
|
+
## Media Queue Production Contract
|
|
127
|
+
|
|
128
|
+
SmartBrain delegates image, audio, and video persistence to SmartRAG. Before deploying the current queue code, migrate the **SmartRAG database** through `017_add_media_job_request_fingerprint`; running `smart_brain migrate` does not apply SmartRAG migrations. Migrations 015 and 016 add leases, shared S3/MinIO object lifecycle, staging protection, and principal-owned documents. Migration 017 backfills and requires a canonical SHA-256 request fingerprint.
|
|
129
|
+
|
|
130
|
+
Use a stable idempotency key for asynchronous retries:
|
|
131
|
+
|
|
132
|
+
```ruby
|
|
133
|
+
job = SmartBrain.enqueue_media(
|
|
134
|
+
source: '/data/product-demo.mp4',
|
|
135
|
+
options: { media_type: 'video', idempotency_key: 'product-demo-v1' }
|
|
136
|
+
)
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
For the same authenticated principal, repeating the same operation, source, and canonicalized options returns the original job with `deduplicated: true`. Reusing the key with a different payload returns SmartRAG HTTP `409` and `code: "idempotency_conflict"`. Keep both the original key and payload for network retries; generate a new key when the business payload changes.
|
|
140
|
+
|
|
141
|
+
`HttpClient` converts a non-2xx response into a SmartBrain result with `status: "failed"`; an idempotency conflict is visible in `warnings` as `SmartRAG HTTP 409`. `DirectClient` propagates `SmartRAG::Core::MediaJobQueue::IdempotencyConflict` to the caller.
|
|
142
|
+
|
|
143
|
+
Authenticated SmartRAG retrieval uses defense in depth: it pushes the principal's PostgreSQL-owned document IDs into search and rechecks returned candidates before emitting evidence. Invalid Bearer tokens return 401. For multi-instance workers, configure a shared S3 or MinIO endpoint, bucket, and prefix; retained failed jobs protect their staging objects until pruning.
|
|
144
|
+
|
|
145
|
+
SmartRAG's opt-in real integration suites verify MinIO cross-instance storage and deletion, asynchronous worker materialization, failed-job GC protection, Bearer-token tenant isolation, HTTP 409 behavior, and concurrent idempotency races. See the SmartRAG README section "Real storage and isolation verification" for environment variables and commands.
|
|
146
|
+
|
|
114
147
|
## `example.rb` (Updated)
|
|
115
148
|
|
|
116
149
|
The example demonstrates the real loop:
|
data/README.md
CHANGED
|
@@ -35,6 +35,23 @@ SmartBrain 把 Agent 的可用信息分为两类,分工明确:
|
|
|
35
35
|
|
|
36
36
|
## 安装
|
|
37
37
|
|
|
38
|
+
### 从 RubyGems 安装(推荐给最终用户)
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
gem install smart_brain
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
装完即可用,默认零外部依赖(memory 后端 + stub LLM):
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
smart_brain --version
|
|
48
|
+
smart_brain status
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
完整安装/配置/可选能力(PostgreSQL 持久化、Ollama/OpenAI LLM、SmartRAG 资源检索)见 **[`docs/installation.md`](docs/installation.md)**。
|
|
52
|
+
|
|
53
|
+
### 从源码开发
|
|
54
|
+
|
|
38
55
|
```bash
|
|
39
56
|
bundle install
|
|
40
57
|
```
|
|
@@ -256,6 +273,7 @@ docs/
|
|
|
256
273
|
context_package.md # ContextPackage 协议
|
|
257
274
|
retrieval_plan.md # RetrievalPlan 协议
|
|
258
275
|
evidence_pack.md # EvidencePack 协议
|
|
276
|
+
media_memory_schema.md # 媒体记忆契约、生产能力和迁移约束
|
|
259
277
|
spec/
|
|
260
278
|
spec_helper.rb
|
|
261
279
|
commit_turn_spec.rb # commit_turn 单元测试
|
|
@@ -367,7 +385,22 @@ SmartBrain.configure # 无需额外配置
|
|
|
367
385
|
|
|
368
386
|
### 2) HttpClient(HTTP 远程)
|
|
369
387
|
|
|
370
|
-
|
|
388
|
+
通过 SmartRAG HTTP 服务调用远端资源库,支持 JSON URL 导入、multipart 文件上传和超时降级:
|
|
389
|
+
|
|
390
|
+
```ruby
|
|
391
|
+
client = SmartBrain::Adapters::SmartRag::HttpClient.for_url(
|
|
392
|
+
base_url: 'http://127.0.0.1:9393',
|
|
393
|
+
timeout_seconds: 30,
|
|
394
|
+
headers: { 'Authorization' => "Bearer #{ENV.fetch('SMARTRAG_TOKEN')}" }
|
|
395
|
+
)
|
|
396
|
+
SmartBrain.configure(smart_rag_client: client)
|
|
397
|
+
|
|
398
|
+
SmartBrain.add_video(source: '/data/demo.mp4', options: { tags: ['demo'] })
|
|
399
|
+
```
|
|
400
|
+
|
|
401
|
+
本地路径自动使用 multipart 上传,HTTP/HTTPS source 使用 JSON URL 导入。OCR、视觉描述和转写 callable 不能跨进程序列化,必须配置在远端 SmartRAG `HttpApp` 的 `extractors:` 中。
|
|
402
|
+
|
|
403
|
+
也可以继续通过自定义 transport lambda 集成其他协议:
|
|
371
404
|
|
|
372
405
|
```ruby
|
|
373
406
|
transport = lambda do |plan, timeout_seconds:|
|
|
@@ -407,6 +440,99 @@ SmartBrain.configure(smart_rag_client: client)
|
|
|
407
440
|
|
|
408
441
|
Mapper 返回的 `filters` 会作为 `scope_filters` 发送给 SmartRAG。业务 scope 存在时,后端必须返回 `scope_filter_applied: true`;mapper 缺失、映射失败或后端未确认时,adapter 默认 fail closed,丢弃资源证据并在 `warnings` 和 `explain.ignored_fields` 中说明原因。
|
|
409
442
|
|
|
443
|
+
## 多媒体记忆(MVP)
|
|
444
|
+
|
|
445
|
+
SmartBrain 通过 SmartRAG 保存图片、音频和视频的技术元数据,并把描述、OCR 或转写文本作为普通 section 建立全文及向量索引:
|
|
446
|
+
|
|
447
|
+
```ruby
|
|
448
|
+
SmartBrain.add_image(
|
|
449
|
+
source: '/data/whiteboard.jpg',
|
|
450
|
+
options: {
|
|
451
|
+
title: 'Q3 路线图白板',
|
|
452
|
+
tags: ['roadmap'],
|
|
453
|
+
image_describer: ->(path) { vision_client.describe(path) },
|
|
454
|
+
ocr_extractor: ->(path) { ocr_client.extract(path) }
|
|
455
|
+
}
|
|
456
|
+
)
|
|
457
|
+
|
|
458
|
+
SmartBrain.add_audio(
|
|
459
|
+
source: '/data/weekly-meeting.wav',
|
|
460
|
+
options: {
|
|
461
|
+
tags: ['meeting'],
|
|
462
|
+
audio_transcriber: ->(path) { whisper_client.transcribe(path) }
|
|
463
|
+
}
|
|
464
|
+
)
|
|
465
|
+
|
|
466
|
+
SmartBrain.add_video(
|
|
467
|
+
source: '/data/product-demo.mp4',
|
|
468
|
+
options: {
|
|
469
|
+
tags: ['demo'],
|
|
470
|
+
# start/end 使用秒;也可以返回 start_ms/end_ms。
|
|
471
|
+
video_transcriber: ->(audio_path) {
|
|
472
|
+
whisper_client.transcribe(audio_path, timestamps: true)
|
|
473
|
+
# => { segments: [{ text: '打开设置', start: 5.0, end: 9.0 }] }
|
|
474
|
+
},
|
|
475
|
+
frame_describer: ->(frame_path, timestamp_ms) {
|
|
476
|
+
vision_client.describe(frame_path, timestamp_ms: timestamp_ms)
|
|
477
|
+
},
|
|
478
|
+
frame_interval_seconds: 30,
|
|
479
|
+
max_frames: 12
|
|
480
|
+
}
|
|
481
|
+
)
|
|
482
|
+
```
|
|
483
|
+
|
|
484
|
+
`add_media` 会根据扩展名和 MIME 类型自动判断类型;`add_image`、`add_audio`、`add_video` 固定类型。Pillow 或 ffprobe 缺失、语义提取器未配置或调用失败时,导入仍会保存文件名、标签和可获得的技术元数据,返回 `status: "partial"` 及 `warnings`。
|
|
485
|
+
|
|
486
|
+
按媒体类型限制资源检索:
|
|
487
|
+
|
|
488
|
+
```ruby
|
|
489
|
+
SmartBrain.compose_context(
|
|
490
|
+
session_id: 'demo',
|
|
491
|
+
user_message: '查资料:路线图白板上写了什么?',
|
|
492
|
+
resource_filters: { media_type: ['image'] }
|
|
493
|
+
)
|
|
494
|
+
```
|
|
495
|
+
|
|
496
|
+
SmartRAG 的 P1 配置可启用本地内容寻址存储(SHA-256 去重),并内置 OpenAI-compatible 图片描述/音频转写与可选 Tesseract OCR。未启用内容存储时仍保存原文件路径或 URL。
|
|
497
|
+
|
|
498
|
+
异步导入和状态查询:
|
|
499
|
+
|
|
500
|
+
```ruby
|
|
501
|
+
job = SmartBrain.enqueue_media(
|
|
502
|
+
source: '/data/long-demo.mp4',
|
|
503
|
+
options: { media_type: 'video', tags: ['demo'] }
|
|
504
|
+
)
|
|
505
|
+
status = SmartBrain.media_job(job_id: job[:job_id])
|
|
506
|
+
|
|
507
|
+
failed = SmartBrain.media_jobs(status: 'failed', limit: 20)
|
|
508
|
+
SmartBrain.retry_media_job(job_id: failed[:jobs].first[:id])
|
|
509
|
+
SmartBrain.cancel_media_job(job_id: job[:job_id]) # 仅 queued 状态可取消
|
|
510
|
+
queue = SmartBrain.media_job_statistics
|
|
511
|
+
```
|
|
512
|
+
|
|
513
|
+
HTTP 客户端会发送 `options.async=true`,服务端返回 202;SmartRAG worker 负责消费 PostgreSQL `media_jobs` 队列。P2 支持任务分页/状态过滤、取消、人工重试、卡死任务恢复、保留期清理和健康指标。默认 `NullClient` 对写入和任务查询返回 `unsupported`。
|
|
514
|
+
|
|
515
|
+
P3 支持按调用方隔离的幂等入队。HTTP 模式可把幂等键放在 options 中;服务端也接受标准请求头 `Idempotency-Key`:
|
|
516
|
+
|
|
517
|
+
```ruby
|
|
518
|
+
job = SmartBrain.enqueue_media(
|
|
519
|
+
source: '/data/long-demo.mp4',
|
|
520
|
+
options: { media_type: 'video', idempotency_key: 'demo-video-v1' }
|
|
521
|
+
)
|
|
522
|
+
```
|
|
523
|
+
|
|
524
|
+
部署当前版本前,SmartRAG 数据库必须执行到迁移 `017_add_media_job_request_fingerprint`。该迁移为历史任务回填规范化 SHA-256 请求指纹,并将指纹列设为必填。同一 principal 下,用相同幂等键重复提交相同 operation、source 和 options,会返回原任务并标记 `deduplicated: true`;相同键对应不同 source 或 options 时,SmartRAG 返回 HTTP `409` 和 `code: "idempotency_conflict"`。嵌套 Hash 的键顺序以及 symbol/string 键不影响指纹,数组顺序仍有意义。
|
|
525
|
+
|
|
526
|
+
`HttpClient` 会将该 409 转成 SmartBrain 的失败结果:`status: "failed"`,并在 `warnings` 中包含 `SmartRAG HTTP 409`。调用方应保持原载荷和原幂等键进行网络重试;业务载荷发生变化时必须生成新键。`DirectClient` 进程内调用时,SmartRAG 的 `MediaJobQueue::IdempotencyConflict` 会直接向上传播。
|
|
527
|
+
|
|
528
|
+
多实例部署应把 SmartRAG `media.content_store.provider` 配置成 `s3`,可连接 AWS S3 或 MinIO。P3 worker 使用 heartbeat 租约避免长任务被错误恢复,并维护媒体对象引用计数和零引用垃圾回收。HTTP Bearer token 可继续通过 `HttpClient.for_url(headers: ...)` 设置。
|
|
529
|
+
|
|
530
|
+
SmartRAG 迁移 016 将认证 principal 写入文档 owner,并把它应用到检索、文档读取/列表/删除和统计。HTTP 检索会把 principal 对应的文档 ID 下推给搜索后端,并在生成 EvidencePack 前根据 PostgreSQL owner 再次过滤候选;即使后端忽略 `document_ids`,也不能返回其他租户证据。failed 任务保留期间,其 staging 对象不会被 GC 删除,人工重试仍可读取原始媒体;同步导入在后续处理失败时留下的零引用对象可被 GC 回收。
|
|
531
|
+
|
|
532
|
+
SmartRAG 提供显式启用的真实集成测试。`SMARTRAG_MINIO_E2E=1` 的 MinIO 用例覆盖跨实例上传/下载、异步 worker 读取 `s3://` staging URI、对象引用和真实 GC 删除;`media_tenant_isolation_spec.rb` 与 `media_p3_spec.rb` 覆盖 Bearer 多租户检索、非法 token、幂等 409 和并发唯一键竞争。具体环境变量和命令见 SmartRAG README 的 “Real storage and isolation verification”。
|
|
533
|
+
|
|
534
|
+
视频的转写片段和关键帧描述分别保存为 section。检索命中后,EvidencePack 的 `metadata` 会包含 `extraction_kind`、`start_ms`、`end_ms` 或 `frame_timestamp_ms`,调用方可据此跳转到视频位置。视频处理依赖 `ffmpeg`;缺失时导入降级为 `partial`。
|
|
535
|
+
|
|
410
536
|
## 运行示例
|
|
411
537
|
|
|
412
538
|
### example.rb
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
# SmartBrain 安装与配置指南(面向最终用户)
|
|
2
|
+
|
|
3
|
+
本指南面向通过 RubyGems 安装 SmartBrain 的用户。读完并照做后即可直接使用,
|
|
4
|
+
无需克隆源码或维护开发环境。
|
|
5
|
+
|
|
6
|
+
## 1. 安装
|
|
7
|
+
|
|
8
|
+
要求 Ruby >= 3.0,已安装 RubyGems:
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
gem install smart_brain
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
验证安装:
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
smart_brain --version # 打印版本号
|
|
18
|
+
smart_brain status # 打印运行时诊断(默认 memory 后端)
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
如果 `smart_brain` 命令不在 PATH 里,检查 RubyGems 的 bin 目录(`gem env` 里的
|
|
22
|
+
`EXECUTABLE DIRECTORY`)并把它加入 `PATH`。
|
|
23
|
+
|
|
24
|
+
## 2. 开箱即用(零外部依赖)
|
|
25
|
+
|
|
26
|
+
默认配置(打包在 gem 内的 `config/brain.yml`):
|
|
27
|
+
|
|
28
|
+
- 存储后端:`memory`(进程内,重启即丢,适合开发/试用)
|
|
29
|
+
- LLM:`stub`(确定性、零网络,摘要走模板、重排走词法)
|
|
30
|
+
- 资源 RAG:未挂载(`NullClient`,资源证据为空)
|
|
31
|
+
|
|
32
|
+
因此装完什么都不用配就能跑:
|
|
33
|
+
|
|
34
|
+
```ruby
|
|
35
|
+
require 'smart_brain'
|
|
36
|
+
|
|
37
|
+
SmartBrain.configure
|
|
38
|
+
|
|
39
|
+
SmartBrain.commit_turn(
|
|
40
|
+
session_id: 'demo',
|
|
41
|
+
turn_events: {
|
|
42
|
+
messages: [
|
|
43
|
+
{ role: 'user', content: '记住:默认数据库是 Postgres。' },
|
|
44
|
+
{ role: 'assistant', content: '已记录。' }
|
|
45
|
+
],
|
|
46
|
+
decisions: [{ key: 'decision:storage', decision: '默认用 Postgres' }]
|
|
47
|
+
}
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
context = SmartBrain.compose_context(session_id: 'demo', user_message: '继续并总结关键结论')
|
|
51
|
+
puts context[:working_summary]
|
|
52
|
+
puts context[:evidence].size
|
|
53
|
+
puts context.dig(:debug, :trace) # => { context_id, request_id, plan_id }
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## 3. 配置方式
|
|
57
|
+
|
|
58
|
+
SmartBrain 的配置优先级(从高到低):
|
|
59
|
+
|
|
60
|
+
1. **环境变量**(最推荐,尤其面向进程/容器/agent 工具)
|
|
61
|
+
2. **自定义配置文件**:`SmartBrain.configure(config_path: '/path/to/brain.yml')`,
|
|
62
|
+
CLI 用 `--config PATH` 或环境变量 `SMARTBRAIN_CONFIG`
|
|
63
|
+
3. **gem 内置默认值**:`config/brain.yml`
|
|
64
|
+
|
|
65
|
+
> 内置 `config/brain.yml` 位于 gem 安装目录内,**不要直接修改它**(升级会被覆盖)。
|
|
66
|
+
> 需要自定义时,复制一份到自己的目录,改完再用 `config_path` / `--config` 指向它。
|
|
67
|
+
|
|
68
|
+
### 3.1 环境变量速查
|
|
69
|
+
|
|
70
|
+
| 变量 | 作用 | 默认 |
|
|
71
|
+
|---|---|---|
|
|
72
|
+
| `SMARTBRAIN_BACKEND` | `memory` / `postgres` | `memory` |
|
|
73
|
+
| `SMARTBRAIN_DB_HOST` | Postgres 主机 | `127.0.0.1` |
|
|
74
|
+
| `SMARTBRAIN_DB_PORT` | Postgres 端口 | `5432` |
|
|
75
|
+
| `SMARTBRAIN_DB_NAME` | Postgres 数据库 | `smart_brain_development` |
|
|
76
|
+
| `SMARTBRAIN_DB_USER` | Postgres 用户 | `smart_brain` |
|
|
77
|
+
| `SMARTBRAIN_DB_PASSWORD` | Postgres 密码 | `smart_brain` |
|
|
78
|
+
| `SMARTBRAIN_LLM_PROVIDER` | `stub` / `ollama` / `openai` | `stub` |
|
|
79
|
+
| `SMARTBRAIN_LLM_MODEL` | 生成式模型名 | 配置文件里的 `model` |
|
|
80
|
+
| `SMARTBRAIN_LLM_BASE_URL` | LLM 端点 | `http://localhost:11434` |
|
|
81
|
+
| `SMARTBRAIN_LLM_API_KEY` | OpenAI 兼容 API key | 空 |
|
|
82
|
+
| `SMARTBRAIN_CONFIG` | 配置文件路径(CLI 用) | 无 |
|
|
83
|
+
|
|
84
|
+
## 4. 可选能力(按需开启)
|
|
85
|
+
|
|
86
|
+
### 4.1 持久化(PostgreSQL)
|
|
87
|
+
|
|
88
|
+
跨进程/重启保留记忆,并启用 `memory_chunks` 全文检索:
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
# 一次性:建角色与库(以 postgres 超级用户执行)
|
|
92
|
+
sudo -u postgres createuser -d smart_brain
|
|
93
|
+
sudo -u postgres psql -c "ALTER USER smart_brain PASSWORD 'smart_brain';"
|
|
94
|
+
sudo -u postgres createdb -O smart_brain smart_brain_development
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
然后:
|
|
98
|
+
|
|
99
|
+
```bash
|
|
100
|
+
export SMARTBRAIN_BACKEND=postgres
|
|
101
|
+
export SMARTBRAIN_DB_NAME=smart_brain_development
|
|
102
|
+
smart_brain migrate # 幂等建表(也可省略:postgres 后端启动时会自动 migrate)
|
|
103
|
+
smart_brain status # 应显示 backend=postgres
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### 4.2 真 LLM 摘要与重排(Ollama)
|
|
107
|
+
|
|
108
|
+
默认 `stub` 不联网。切到本地 Ollama 后,`working_summary` 走真摘要、Fusion 走
|
|
109
|
+
LLM-as-judge 重排:
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
ollama serve & # 起 ollama
|
|
113
|
+
ollama pull qwen3 # 注意:需要「生成式」模型(qwen3/llama2),不是 qwen3-embedding
|
|
114
|
+
export SMARTBRAIN_LLM_PROVIDER=ollama
|
|
115
|
+
export SMARTBRAIN_LLM_MODEL=qwen3
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
OpenAI 兼容端点(silicon_flow 等):
|
|
119
|
+
|
|
120
|
+
```bash
|
|
121
|
+
export SMARTBRAIN_LLM_PROVIDER=openai
|
|
122
|
+
export SMARTBRAIN_LLM_BASE_URL=https://api.siliconflow.cn/v1
|
|
123
|
+
export SMARTBRAIN_LLM_API_KEY=sk-...
|
|
124
|
+
export SMARTBRAIN_LLM_MODEL=Qwen/Qwen3-8B
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
> 说明:`qwen3-embedding` 只能算向量,不能做文本生成;摘要/重排必须用生成式聊天模型。
|
|
128
|
+
|
|
129
|
+
### 4.3 资源 RAG(SmartRAG)
|
|
130
|
+
|
|
131
|
+
SmartBrain 默认不依赖 SmartRAG(资源证据为空)。需要文档/网页/代码库检索时:
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
gem install smart_rag
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
再按需挂载进程内直连或 HTTP 适配器(见
|
|
138
|
+
[`user_guide.md §12`](user_guide.md) 与 README):
|
|
139
|
+
|
|
140
|
+
```ruby
|
|
141
|
+
require 'smart_rag'
|
|
142
|
+
require 'smart_brain/adapters/smart_rag/direct_client'
|
|
143
|
+
|
|
144
|
+
rag = SmartRAG::SmartRAG.new(...) # 需配置 PostgreSQL + LLM + embedding
|
|
145
|
+
client = SmartBrain::Adapters::SmartRag::DirectClient.new(rag: rag)
|
|
146
|
+
SmartBrain.configure(smart_rag_client: client)
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## 5. 使用方式
|
|
150
|
+
|
|
151
|
+
安装后同一套能力可通过四种方式调用(共享同一个 `Server::Service` 门面):
|
|
152
|
+
|
|
153
|
+
```bash
|
|
154
|
+
# 1) CLI 一次性命令
|
|
155
|
+
smart_brain commit --data '{"session_id":"demo","turn_events":{"messages":[{"role":"user","content":"用 Postgres"}],"decisions":[{"key":"decision:db","decision":"用 Postgres"}]}}'
|
|
156
|
+
smart_brain compose --data '{"session_id":"demo","user_message":"持久化方案?"}'
|
|
157
|
+
smart_brain search --data '{"session_id":"demo","query":"Postgres"}'
|
|
158
|
+
|
|
159
|
+
# 2) HTTP API(Puma)
|
|
160
|
+
smart_brain serve --host 0.0.0.0 --port 9292
|
|
161
|
+
curl -s localhost:9292/status
|
|
162
|
+
curl -XPOST localhost:9292/commit -H 'Content-Type: application/json' -d '{...}'
|
|
163
|
+
|
|
164
|
+
# 3) MCP(接 Claude Code / Cursor,19 个工具)
|
|
165
|
+
smart_brain mcp
|
|
166
|
+
|
|
167
|
+
# 4) Ruby 库(嵌入自己的 Agent 进程)
|
|
168
|
+
# require 'smart_brain'; SmartBrain.configure; SmartBrain.commit_turn(...)
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
> **注意 CLI 一次性命令与后端的关系**:`smart_brain commit/search/compose` 每次都是独立进程。
|
|
172
|
+
> 默认 `memory` 后端只存在进程内,所以「先 commit 再 search」在**同一条命令里看不到**(第二次是全新进程)。
|
|
173
|
+
> 要用 CLI 跨命令持久化,请先切 `SMARTBRAIN_BACKEND=postgres` 并 `smart_brain migrate`。
|
|
174
|
+
> MCP 和 HTTP serve 是长驻进程,进程存续期内 memory 后端也能跨调用记住;跨重启仍需 postgres。
|
|
175
|
+
|
|
176
|
+
MCP 接入配置示例见 [`mcp.md`](mcp.md);完整 API 与治理能力见
|
|
177
|
+
[`user_guide.md`](user_guide.md)。
|
|
178
|
+
|
|
179
|
+
## 6. 常见问题
|
|
180
|
+
|
|
181
|
+
- **`smart_brain: command not found`**:把 `gem env` 显示的 EXECUTABLE DIRECTORY 加入 `PATH`。
|
|
182
|
+
- **`cannot load such file -- sequel/extensions/pgvector`**:见 README「常见问题」;不影响核心使用。
|
|
183
|
+
- **Postgres 连接失败**:检查 `SMARTBRAIN_DB_*` 是否与建库时一致,先 `smart_brain migrate`。
|
|
184
|
+
- **切 ollama 后摘要还是模板**:确认 `SMARTBRAIN_LLM_PROVIDER=ollama` 且模型是生成式(非 embedding)。
|
|
185
|
+
- **要不要装 `smart_rag`**:只有需要资源 RAG 时才装;纯对话记忆场景完全不需要。
|
|
186
|
+
|
|
187
|
+
## 7. 从源码开发/运行测试(可选)
|
|
188
|
+
|
|
189
|
+
仅在需要改代码或跑测试时:
|
|
190
|
+
|
|
191
|
+
```bash
|
|
192
|
+
git clone https://github.com/zhuangbiaowei/smart_brain
|
|
193
|
+
cd smart_brain
|
|
194
|
+
bundle install
|
|
195
|
+
bundle exec rspec # memory 后端
|
|
196
|
+
SMARTBRAIN_PG=1 bundle exec rspec # 含 PostgreSQL 集成
|
|
197
|
+
SMARTBRAIN_LLM=1 bundle exec rspec spec/smoke_ollama_spec.rb # 真实 Ollama 冒烟
|
|
198
|
+
```
|