smart_brain 0.1.0 → 0.1.2

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.
data/README.md CHANGED
@@ -1,173 +1,173 @@
1
- # SmartBrain
2
-
3
- SmartBrain 是一个面向 Agent 的记忆运行时(Memory Runtime)与上下文编排器(Context Composer)。
4
-
5
- 它的核心职责:
6
- - `commit_turn`:记录事件真相并沉淀结构化记忆
7
- - `compose_context`:在每轮请求前组装最小充分上下文
8
- - 联动 SmartRAG:对话记忆由 SmartBrain 管理,资源检索由 SmartRAG 提供
9
-
10
- ## 当前进展
11
-
12
- 当前仓库已实现并打通:
13
- - `commit_turn` / `compose_context` 主链路
14
- - Retention / Consolidation / Retrieval / Composition 策略
15
- - 检索器:exact + relational
16
- - 融合层:去重、规则重排、多样性、预算截断
17
- - SmartRAG 适配器:`NullClient` / `HttpClient` / `DirectClient`
18
- - 契约与可观测:`request_id` / `plan_id` / `context_id` 全链路追踪
19
- - RSpec 测试(单元 + 集成 + 回归)
20
-
21
- ## 项目结构
22
-
23
- - `lib/smart_brain.rb`:入口 API
24
- - `lib/smart_brain/runtime.rb`:主运行时编排
25
- - `lib/smart_brain/contracts/`:RetrievalPlan / EvidencePack / ContextPackage 校验
26
- - `lib/smart_brain/observability/`:日志与指标
27
- - `lib/smart_brain/event_store/`:事件存储(当前内存实现)
28
- - `lib/smart_brain/memory_store/`:记忆存储(当前内存实现)
29
- - `lib/smart_brain/retrievers/`:exact/relational 检索
30
- - `lib/smart_brain/fusion/`:多源融合
31
- - `lib/smart_brain/context_composer/`:上下文装配
32
- - `lib/smart_brain/adapters/smart_rag/`:SmartRAG 适配层
33
- - `config/brain.yml`:策略配置
34
- - `example.rb`:SmartBrain + SmartAgent + SmartPrompt + SmartRAG 联动示例
35
- - `docs/`:设计与协议文档
36
-
37
- ## 安装
38
-
39
- ```bash
40
- bundle install
41
- ```
42
-
43
- 如遇本地权限或 shared gem 污染,建议:
44
-
45
- ```bash
46
- bundle config set --local path 'vendor/bundle'
47
- bundle config set --local disable_shared_gems 'true'
48
- ```
49
-
50
- ## 快速开始(仅 SmartBrain)
51
-
52
- ```ruby
53
- require_relative 'lib/smart_brain'
54
-
55
- SmartBrain.configure
56
-
57
- SmartBrain.commit_turn(
58
- session_id: 'demo',
59
- turn_events: {
60
- messages: [
61
- { role: 'user', content: '请记住:默认数据库是 Postgres。' },
62
- { role: 'assistant', content: '已记录。' }
63
- ],
64
- decisions: [
65
- { key: 'decision:smartbrain:storage', decision: 'Use Postgres by default' }
66
- ]
67
- }
68
- )
69
-
70
- context = SmartBrain.compose_context(
71
- session_id: 'demo',
72
- user_message: '继续并总结关键结论'
73
- )
74
-
75
- puts context[:context_id]
76
- puts context.dig(:debug, :trace, :request_id)
77
- puts context.dig(:debug, :trace, :plan_id)
78
- ```
79
-
80
- ## SmartRAG 集成方式
81
-
82
- ### 1) NullClient(默认)
83
-
84
- 不配置 `smart_rag_client` 时,资源证据为空,仅使用记忆侧证据。
85
-
86
- ### 2) HttpClient
87
-
88
- ```ruby
89
- transport = lambda do |plan, timeout_seconds:|
90
- {
91
- plan_id: 'p1',
92
- supports_language_filter: true,
93
- evidences: []
94
- }
95
- end
96
-
97
- client = SmartBrain::Adapters::SmartRag::HttpClient.new(transport: transport, timeout_seconds: 2)
98
- SmartBrain.configure(smart_rag_client: client)
99
- ```
100
-
101
- ### 3) DirectClient(当前示例使用)
102
-
103
- ```ruby
104
- require '/home/mlf/smart_ai/smart_rag/lib/smart_rag'
105
- require_relative 'lib/smart_brain/adapters/smart_rag/direct_client'
106
-
107
- rag_config = SmartRAG::Config.load('/home/mlf/smart_ai/smart_rag/config/smart_rag.yml')
108
- rag = SmartRAG::SmartRAG.new(rag_config)
109
- client = SmartBrain::Adapters::SmartRag::DirectClient.new(rag: rag)
110
-
111
- SmartBrain.configure(smart_rag_client: client)
112
- ```
113
-
114
- ## example.rb 说明(已更新)
115
-
116
- `example.rb` 演示完整链路:
117
- 1. SmartBrain `compose_context`
118
- 2. SmartAgent 通过 `call_worker` 调用 SmartPrompt worker
119
- 3. SmartBrain `commit_turn`
120
- 4. 打印 `evidence(memory/resource)` 验证 SmartRAG 是否参与
121
-
122
- 示例依赖的本地文件:
123
- - `config/example_agent.yml`
124
- - `config/example_llm.yml`
125
- - `agents/brain_assistant.rb`
126
- - `workers/brain_assistant.rb`
127
- - `templates/brain_assistant.erb`
128
-
129
- 运行:
130
-
131
- ```bash
132
- bundle exec ruby example.rb
133
- ```
134
-
135
- ## 核心 API
136
-
137
- ### `SmartBrain.configure(config_path: nil, smart_rag_client: nil, clock: -> { Time.now.utc })`
138
- 初始化运行时并注入 SmartRAG 客户端(可选)。
139
-
140
- ### `SmartBrain.commit_turn(session_id:, turn_events:)`
141
- 写入事件、抽取记忆、冲突处理、摘要更新。
142
-
143
- ### `SmartBrain.compose_context(session_id:, user_message:, agent_state: {})`
144
- 生成 `ContextPackage`,内部包含检索计划与证据融合结果。
145
-
146
- ### `SmartBrain.diagnostics`
147
- 返回 compose/commit 观测日志与指标快照。
148
-
149
- ## 测试
150
-
151
- ```bash
152
- rspec
153
- ```
154
-
155
- ## 常见问题
156
-
157
- ### 1) `cannot load such file -- sequel/extensions/pgvector`
158
- 当前示例已在 `example.rb` 做兼容处理(`Sequel.extension 'pgvector'` + 去除 `database.extensions` 连接参数)。
159
-
160
- ### 2) `Config file not found: config/llm_config.yml`
161
- `example.rb` 已将 SmartRAG 里 EmbeddingService 的 `config_path` 注入为 `./config/example_llm.yml`。
162
-
163
- ### 3) `ruby-lsp: not found`
164
- ```bash
165
- gem install --user-install ruby-lsp debug
166
- ```
167
- 并将用户 gem bin 加入 `PATH`。
168
-
169
- ## 路线图
170
-
171
- - 将 EventStore/MemoryStore 从内存实现切换到 Postgres 实现
172
- - 接入真实 reranker / embedding 模型
173
- - 完善 SmartRAG ingest 与跨会话评估工具
1
+ # SmartBrain
2
+
3
+ SmartBrain 是一个面向 Agent 的记忆运行时(Memory Runtime)与上下文编排器(Context Composer)。
4
+
5
+ 它的核心职责:
6
+ - `commit_turn`:记录事件真相并沉淀结构化记忆
7
+ - `compose_context`:在每轮请求前组装最小充分上下文
8
+ - 联动 SmartRAG:对话记忆由 SmartBrain 管理,资源检索由 SmartRAG 提供
9
+
10
+ ## 当前进展
11
+
12
+ 当前仓库已实现并打通:
13
+ - `commit_turn` / `compose_context` 主链路
14
+ - Retention / Consolidation / Retrieval / Composition 策略
15
+ - 检索器:exact + relational
16
+ - 融合层:去重、规则重排、多样性、预算截断
17
+ - SmartRAG 适配器:`NullClient` / `HttpClient` / `DirectClient`
18
+ - 契约与可观测:`request_id` / `plan_id` / `context_id` 全链路追踪
19
+ - RSpec 测试(单元 + 集成 + 回归)
20
+
21
+ ## 项目结构
22
+
23
+ - `lib/smart_brain.rb`:入口 API
24
+ - `lib/smart_brain/runtime.rb`:主运行时编排
25
+ - `lib/smart_brain/contracts/`:RetrievalPlan / EvidencePack / ContextPackage 校验
26
+ - `lib/smart_brain/observability/`:日志与指标
27
+ - `lib/smart_brain/event_store/`:事件存储(当前内存实现)
28
+ - `lib/smart_brain/memory_store/`:记忆存储(当前内存实现)
29
+ - `lib/smart_brain/retrievers/`:exact/relational 检索
30
+ - `lib/smart_brain/fusion/`:多源融合
31
+ - `lib/smart_brain/context_composer/`:上下文装配
32
+ - `lib/smart_brain/adapters/smart_rag/`:SmartRAG 适配层
33
+ - `config/brain.yml`:策略配置
34
+ - `example.rb`:SmartBrain + SmartAgent + SmartPrompt + SmartRAG 联动示例
35
+ - `docs/`:设计与协议文档
36
+
37
+ ## 安装
38
+
39
+ ```bash
40
+ bundle install
41
+ ```
42
+
43
+ 如遇本地权限或 shared gem 污染,建议:
44
+
45
+ ```bash
46
+ bundle config set --local path 'vendor/bundle'
47
+ bundle config set --local disable_shared_gems 'true'
48
+ ```
49
+
50
+ ## 快速开始(仅 SmartBrain)
51
+
52
+ ```ruby
53
+ require_relative 'lib/smart_brain'
54
+
55
+ SmartBrain.configure
56
+
57
+ SmartBrain.commit_turn(
58
+ session_id: 'demo',
59
+ turn_events: {
60
+ messages: [
61
+ { role: 'user', content: '请记住:默认数据库是 Postgres。' },
62
+ { role: 'assistant', content: '已记录。' }
63
+ ],
64
+ decisions: [
65
+ { key: 'decision:smartbrain:storage', decision: 'Use Postgres by default' }
66
+ ]
67
+ }
68
+ )
69
+
70
+ context = SmartBrain.compose_context(
71
+ session_id: 'demo',
72
+ user_message: '继续并总结关键结论'
73
+ )
74
+
75
+ puts context[:context_id]
76
+ puts context.dig(:debug, :trace, :request_id)
77
+ puts context.dig(:debug, :trace, :plan_id)
78
+ ```
79
+
80
+ ## SmartRAG 集成方式
81
+
82
+ ### 1) NullClient(默认)
83
+
84
+ 不配置 `smart_rag_client` 时,资源证据为空,仅使用记忆侧证据。
85
+
86
+ ### 2) HttpClient
87
+
88
+ ```ruby
89
+ transport = lambda do |plan, timeout_seconds:|
90
+ {
91
+ plan_id: 'p1',
92
+ supports_language_filter: true,
93
+ evidences: []
94
+ }
95
+ end
96
+
97
+ client = SmartBrain::Adapters::SmartRag::HttpClient.new(transport: transport, timeout_seconds: 2)
98
+ SmartBrain.configure(smart_rag_client: client)
99
+ ```
100
+
101
+ ### 3) DirectClient(当前示例使用)
102
+
103
+ ```ruby
104
+ require 'smart_rag'
105
+ require_relative 'lib/smart_brain/adapters/smart_rag/direct_client'
106
+
107
+ rag_config = SmartRAG::Config.load(ENV.fetch('SMARTRAG_CONFIG_PATH'))
108
+ rag = SmartRAG::SmartRAG.new(rag_config)
109
+ client = SmartBrain::Adapters::SmartRag::DirectClient.new(rag: rag)
110
+
111
+ SmartBrain.configure(smart_rag_client: client)
112
+ ```
113
+
114
+ ## example.rb 说明(已更新)
115
+
116
+ `example.rb` 演示完整链路:
117
+ 1. SmartBrain `compose_context`
118
+ 2. SmartAgent 通过 `call_worker` 调用 SmartPrompt worker
119
+ 3. SmartBrain `commit_turn`
120
+ 4. 打印 `evidence(memory/resource)` 验证 SmartRAG 是否参与
121
+
122
+ 示例依赖的本地文件:
123
+ - `config/example_agent.yml`
124
+ - `config/example_llm.yml`
125
+ - `agents/brain_assistant.rb`
126
+ - `workers/brain_assistant.rb`
127
+ - `templates/brain_assistant.erb`
128
+
129
+ 运行:
130
+
131
+ ```bash
132
+ bundle exec ruby example.rb
133
+ ```
134
+
135
+ ## 核心 API
136
+
137
+ ### `SmartBrain.configure(config_path: nil, smart_rag_client: nil, clock: -> { Time.now.utc })`
138
+ 初始化运行时并注入 SmartRAG 客户端(可选)。
139
+
140
+ ### `SmartBrain.commit_turn(session_id:, turn_events:)`
141
+ 写入事件、抽取记忆、冲突处理、摘要更新。
142
+
143
+ ### `SmartBrain.compose_context(session_id:, user_message:, agent_state: {})`
144
+ 生成 `ContextPackage`,内部包含检索计划与证据融合结果。
145
+
146
+ ### `SmartBrain.diagnostics`
147
+ 返回 compose/commit 观测日志与指标快照。
148
+
149
+ ## 测试
150
+
151
+ ```bash
152
+ rspec
153
+ ```
154
+
155
+ ## 常见问题
156
+
157
+ ### 1) `cannot load such file -- sequel/extensions/pgvector`
158
+ 当前示例已在 `example.rb` 做兼容处理(`Sequel.extension 'pgvector'` + 去除 `database.extensions` 连接参数)。
159
+
160
+ ### 2) `Config file not found: config/llm_config.yml`
161
+ `example.rb` 已将 SmartRAG 里 EmbeddingService 的 `config_path` 注入为 `./config/example_llm.yml`。
162
+
163
+ ### 3) `ruby-lsp: not found`
164
+ ```bash
165
+ gem install --user-install ruby-lsp debug
166
+ ```
167
+ 并将用户 gem bin 加入 `PATH`。
168
+
169
+ ## 路线图
170
+
171
+ - 将 EventStore/MemoryStore 从内存实现切换到 Postgres 实现
172
+ - 接入真实 reranker / embedding 模型
173
+ - 完善 SmartRAG ingest 与跨会话评估工具