mcs-core 0.1.0__tar.gz
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.
- mcs_core-0.1.0/LICENSE +21 -0
- mcs_core-0.1.0/PKG-INFO +231 -0
- mcs_core-0.1.0/README.md +200 -0
- mcs_core-0.1.0/mcs/__init__.py +52 -0
- mcs_core-0.1.0/mcs/core/__init__.py +18 -0
- mcs_core-0.1.0/mcs/core/builder.py +433 -0
- mcs_core-0.1.0/mcs/core/calibrated_estimator.py +61 -0
- mcs_core-0.1.0/mcs/core/content_merge.py +81 -0
- mcs_core-0.1.0/mcs/core/context_renderer.py +245 -0
- mcs_core-0.1.0/mcs/core/errors.py +63 -0
- mcs_core-0.1.0/mcs/core/mcs.py +251 -0
- mcs_core-0.1.0/mcs/core/plugin.py +109 -0
- mcs_core-0.1.0/mcs/core/plugin_manager.py +113 -0
- mcs_core-0.1.0/mcs/core/query_engine.py +864 -0
- mcs_core-0.1.0/mcs/core/store.py +364 -0
- mcs_core-0.1.0/mcs/core/token_budget.py +180 -0
- mcs_core-0.1.0/mcs/core/write_pipeline.py +1044 -0
- mcs_core-0.1.0/mcs/diagnostics/__init__.py +5 -0
- mcs_core-0.1.0/mcs/diagnostics/graph_quality.py +278 -0
- mcs_core-0.1.0/mcs/entities/__init__.py +54 -0
- mcs_core-0.1.0/mcs/entities/config.py +325 -0
- mcs_core-0.1.0/mcs/entities/decisions.py +217 -0
- mcs_core-0.1.0/mcs/entities/graph.py +170 -0
- mcs_core-0.1.0/mcs/interfaces/__init__.py +23 -0
- mcs_core-0.1.0/mcs/interfaces/arbitration_plugin.py +50 -0
- mcs_core-0.1.0/mcs/interfaces/compaction_plugin.py +64 -0
- mcs_core-0.1.0/mcs/interfaces/edge_extension.py +63 -0
- mcs_core-0.1.0/mcs/interfaces/entry_plugin.py +54 -0
- mcs_core-0.1.0/mcs/interfaces/index.py +52 -0
- mcs_core-0.1.0/mcs/interfaces/llm.py +304 -0
- mcs_core-0.1.0/mcs/interfaces/maintenance.py +35 -0
- mcs_core-0.1.0/mcs/interfaces/node_extension.py +57 -0
- mcs_core-0.1.0/mcs/interfaces/postprocess_plugin.py +43 -0
- mcs_core-0.1.0/mcs/interfaces/preprocess_plugin.py +25 -0
- mcs_core-0.1.0/mcs/interfaces/priority_scorer.py +44 -0
- mcs_core-0.1.0/mcs/interfaces/query_preprocess_plugin.py +43 -0
- mcs_core-0.1.0/mcs/interfaces/storage_schema_ext.py +35 -0
- mcs_core-0.1.0/mcs/interfaces/trim_plugin.py +61 -0
- mcs_core-0.1.0/mcs/interfaces/write_preprocess_plugin.py +43 -0
- mcs_core-0.1.0/mcs/plugins/__init__.py +7 -0
- mcs_core-0.1.0/mcs/plugins/entry/__init__.py +5 -0
- mcs_core-0.1.0/mcs/plugins/entry/hub_fallback.py +142 -0
- mcs_core-0.1.0/mcs/plugins/index/__init__.py +8 -0
- mcs_core-0.1.0/mcs/plugins/index/alias_index.py +179 -0
- mcs_core-0.1.0/mcs/plugins/llm/__init__.py +7 -0
- mcs_core-0.1.0/mcs/plugins/llm/claude_llm.py +191 -0
- mcs_core-0.1.0/mcs/plugins/llm/deepseek_llm.py +163 -0
- mcs_core-0.1.0/mcs/plugins/llm/ollama_llm.py +259 -0
- mcs_core-0.1.0/mcs/plugins/maintenance/__init__.py +6 -0
- mcs_core-0.1.0/mcs/plugins/maintenance/dedup_maintenance.py +186 -0
- mcs_core-0.1.0/mcs/plugins/maintenance/fanout_reducer.py +905 -0
- mcs_core-0.1.0/mcs/plugins/maintenance/graph_summary.py +96 -0
- mcs_core-0.1.0/mcs/plugins/maintenance/summary_regen.py +92 -0
- mcs_core-0.1.0/mcs/plugins/postprocess/__init__.py +6 -0
- mcs_core-0.1.0/mcs/plugins/postprocess/rerank.py +197 -0
- mcs_core-0.1.0/mcs/plugins/postprocess/summary.py +70 -0
- mcs_core-0.1.0/mcs/plugins/preprocess/__init__.py +9 -0
- mcs_core-0.1.0/mcs/plugins/preprocess/cross_doc_linker.py +387 -0
- mcs_core-0.1.0/mcs/plugins/preprocess/source_tracking.py +372 -0
- mcs_core-0.1.0/mcs/plugins/trim/__init__.py +5 -0
- mcs_core-0.1.0/mcs/plugins/trim/llm_seed_selector.py +108 -0
- mcs_core-0.1.0/mcs/plugins/trim/priority_trim.py +71 -0
- mcs_core-0.1.0/mcs/presets/__init__.py +21 -0
- mcs_core-0.1.0/mcs/presets/phase1.py +226 -0
- mcs_core-0.1.0/mcs/prompts/__init__.py +141 -0
- mcs_core-0.1.0/mcs/prompts/adjudicate.py +61 -0
- mcs_core-0.1.0/mcs/prompts/arbitrate.py +35 -0
- mcs_core-0.1.0/mcs/prompts/decide_directions.py +41 -0
- mcs_core-0.1.0/mcs/prompts/decide_hub.py +141 -0
- mcs_core-0.1.0/mcs/prompts/extract_concepts.py +131 -0
- mcs_core-0.1.0/mcs/prompts/extract_work_events.py +111 -0
- mcs_core-0.1.0/mcs/prompts/gen_aliases.py +36 -0
- mcs_core-0.1.0/mcs/prompts/gen_graph_summary.py +30 -0
- mcs_core-0.1.0/mcs/prompts/gen_summary.py +24 -0
- mcs_core-0.1.0/mcs/prompts/generalize.py +40 -0
- mcs_core-0.1.0/mcs/prompts/judge_relations.py +207 -0
- mcs_core-0.1.0/mcs/prompts/merge.py +101 -0
- mcs_core-0.1.0/mcs/prompts/merge_content.py +66 -0
- mcs_core-0.1.0/mcs/prompts/navigate_hub.py +89 -0
- mcs_core-0.1.0/mcs/prompts/select_facts.py +144 -0
- mcs_core-0.1.0/mcs/prompts/select_nodes.py +60 -0
- mcs_core-0.1.0/mcs/prompts/split.py +128 -0
- mcs_core-0.1.0/mcs/prompts/synthesize.py +27 -0
- mcs_core-0.1.0/mcs/rendering.py +64 -0
- mcs_core-0.1.0/mcs/stores/__init__.py +11 -0
- mcs_core-0.1.0/mcs/stores/in_memory.py +450 -0
- mcs_core-0.1.0/mcs/stores/sqlite_store.py +920 -0
- mcs_core-0.1.0/mcs/utils/__init__.py +6 -0
- mcs_core-0.1.0/mcs/utils/env_expand.py +57 -0
- mcs_core-0.1.0/mcs/utils/imports.py +62 -0
- mcs_core-0.1.0/mcs/utils/text_utils.py +196 -0
- mcs_core-0.1.0/mcs/utils/timestamps.py +62 -0
- mcs_core-0.1.0/mcs/utils/tokenizer.py +51 -0
- mcs_core-0.1.0/mcs_agent/__init__.py +50 -0
- mcs_core-0.1.0/mcs_agent/__main__.py +10 -0
- mcs_core-0.1.0/mcs_agent/app.py +165 -0
- mcs_core-0.1.0/mcs_agent/builder.py +290 -0
- mcs_core-0.1.0/mcs_agent/context.py +374 -0
- mcs_core-0.1.0/mcs_agent/llm.py +41 -0
- mcs_core-0.1.0/mcs_agent/llms/__init__.py +17 -0
- mcs_core-0.1.0/mcs_agent/llms/anthropic.py +219 -0
- mcs_core-0.1.0/mcs_agent/llms/base.py +42 -0
- mcs_core-0.1.0/mcs_agent/llms/callable.py +36 -0
- mcs_core-0.1.0/mcs_agent/llms/openai.py +101 -0
- mcs_core-0.1.0/mcs_agent/llms/registry.py +30 -0
- mcs_core-0.1.0/mcs_agent/loop.py +359 -0
- mcs_core-0.1.0/mcs_agent/memory.py +1102 -0
- mcs_core-0.1.0/mcs_agent/tools.py +537 -0
- mcs_core-0.1.0/mcs_agent/trace.py +125 -0
- mcs_core-0.1.0/mcs_core.egg-info/PKG-INFO +231 -0
- mcs_core-0.1.0/mcs_core.egg-info/SOURCES.txt +200 -0
- mcs_core-0.1.0/mcs_core.egg-info/dependency_links.txt +1 -0
- mcs_core-0.1.0/mcs_core.egg-info/entry_points.txt +3 -0
- mcs_core-0.1.0/mcs_core.egg-info/requires.txt +25 -0
- mcs_core-0.1.0/mcs_core.egg-info/top_level.txt +3 -0
- mcs_core-0.1.0/mcs_mcp/__init__.py +17 -0
- mcs_core-0.1.0/mcs_mcp/__main__.py +8 -0
- mcs_core-0.1.0/mcs_mcp/server.py +292 -0
- mcs_core-0.1.0/pyproject.toml +73 -0
- mcs_core-0.1.0/setup.cfg +4 -0
- mcs_core-0.1.0/tests/test_agent_app.py +148 -0
- mcs_core-0.1.0/tests/test_agent_builder.py +227 -0
- mcs_core-0.1.0/tests/test_agent_context.py +479 -0
- mcs_core-0.1.0/tests/test_agent_llms.py +254 -0
- mcs_core-0.1.0/tests/test_agent_loop.py +300 -0
- mcs_core-0.1.0/tests/test_agent_memory.py +1254 -0
- mcs_core-0.1.0/tests/test_agent_split_merge.py +764 -0
- mcs_core-0.1.0/tests/test_agent_tools.py +238 -0
- mcs_core-0.1.0/tests/test_agent_trace.py +537 -0
- mcs_core-0.1.0/tests/test_alias_poisoning.py +116 -0
- mcs_core-0.1.0/tests/test_anti_regression.py +194 -0
- mcs_core-0.1.0/tests/test_bench_doc_rerank.py +164 -0
- mcs_core-0.1.0/tests/test_bench_env.py +98 -0
- mcs_core-0.1.0/tests/test_bench_multihop.py +225 -0
- mcs_core-0.1.0/tests/test_builder_token_counter.py +64 -0
- mcs_core-0.1.0/tests/test_calibrated_estimator.py +139 -0
- mcs_core-0.1.0/tests/test_capture_api.py +177 -0
- mcs_core-0.1.0/tests/test_claude_count_tokens.py +59 -0
- mcs_core-0.1.0/tests/test_claude_llm.py +156 -0
- mcs_core-0.1.0/tests/test_config_from_file.py +235 -0
- mcs_core-0.1.0/tests/test_config_integration.py +129 -0
- mcs_core-0.1.0/tests/test_config_token_budget.py +42 -0
- mcs_core-0.1.0/tests/test_consolidate_api.py +209 -0
- mcs_core-0.1.0/tests/test_consolidation.py +467 -0
- mcs_core-0.1.0/tests/test_content_merge.py +96 -0
- mcs_core-0.1.0/tests/test_context_renderer.py +162 -0
- mcs_core-0.1.0/tests/test_context_window_size.py +61 -0
- mcs_core-0.1.0/tests/test_cross_doc_linker.py +169 -0
- mcs_core-0.1.0/tests/test_decision_apply.py +461 -0
- mcs_core-0.1.0/tests/test_dedup_maintenance.py +188 -0
- mcs_core-0.1.0/tests/test_deepseek_count_tokens.py +59 -0
- mcs_core-0.1.0/tests/test_deepseek_llm.py +77 -0
- mcs_core-0.1.0/tests/test_diary.py +163 -0
- mcs_core-0.1.0/tests/test_diary_api.py +173 -0
- mcs_core-0.1.0/tests/test_directed_hierarchy.py +219 -0
- mcs_core-0.1.0/tests/test_directed_navigation.py +173 -0
- mcs_core-0.1.0/tests/test_edge_extension_model.py +476 -0
- mcs_core-0.1.0/tests/test_env_expand.py +71 -0
- mcs_core-0.1.0/tests/test_fanout_reducer.py +569 -0
- mcs_core-0.1.0/tests/test_fragments.py +399 -0
- mcs_core-0.1.0/tests/test_golden_cage_agent_bench.py +198 -0
- mcs_core-0.1.0/tests/test_golden_cage_dataset.py +174 -0
- mcs_core-0.1.0/tests/test_graph_direction.py +141 -0
- mcs_core-0.1.0/tests/test_graph_meta.py +79 -0
- mcs_core-0.1.0/tests/test_graph_quality.py +184 -0
- mcs_core-0.1.0/tests/test_graph_summary_plugin.py +126 -0
- mcs_core-0.1.0/tests/test_graph_view.py +331 -0
- mcs_core-0.1.0/tests/test_hub_fallback.py +117 -0
- mcs_core-0.1.0/tests/test_import_from_path.py +67 -0
- mcs_core-0.1.0/tests/test_llm_count_tokens.py +102 -0
- mcs_core-0.1.0/tests/test_llm_json_mode.py +140 -0
- mcs_core-0.1.0/tests/test_locomo_builder.py +250 -0
- mcs_core-0.1.0/tests/test_locomo_data.py +392 -0
- mcs_core-0.1.0/tests/test_locomo_eval.py +261 -0
- mcs_core-0.1.0/tests/test_locomo_metrics.py +238 -0
- mcs_core-0.1.0/tests/test_manage_ui.py +219 -0
- mcs_core-0.1.0/tests/test_mcp_server.py +540 -0
- mcs_core-0.1.0/tests/test_mcs_api.py +338 -0
- mcs_core-0.1.0/tests/test_mcs_maintenance.py +72 -0
- mcs_core-0.1.0/tests/test_multi_universe.py +239 -0
- mcs_core-0.1.0/tests/test_multi_universe_ingest.py +201 -0
- mcs_core-0.1.0/tests/test_ollama_count_tokens.py +53 -0
- mcs_core-0.1.0/tests/test_ollama_llm.py +281 -0
- mcs_core-0.1.0/tests/test_persistence.py +512 -0
- mcs_core-0.1.0/tests/test_pipeline_query.py +858 -0
- mcs_core-0.1.0/tests/test_pipeline_write.py +830 -0
- mcs_core-0.1.0/tests/test_plugin_chains.py +288 -0
- mcs_core-0.1.0/tests/test_plugin_import_path.py +50 -0
- mcs_core-0.1.0/tests/test_prompt_parse_lenient.py +232 -0
- mcs_core-0.1.0/tests/test_rendering.py +80 -0
- mcs_core-0.1.0/tests/test_rerank.py +182 -0
- mcs_core-0.1.0/tests/test_rw_select_prompt_split.py +288 -0
- mcs_core-0.1.0/tests/test_seed_graph.py +180 -0
- mcs_core-0.1.0/tests/test_separate_accumulate_frontier.py +253 -0
- mcs_core-0.1.0/tests/test_skeleton.py +411 -0
- mcs_core-0.1.0/tests/test_text_utils.py +58 -0
- mcs_core-0.1.0/tests/test_timestamps.py +107 -0
- mcs_core-0.1.0/tests/test_token_budget.py +198 -0
- mcs_core-0.1.0/tests/test_unified_graph_schema.py +358 -0
- mcs_core-0.1.0/tests/test_unified_ingest.py +405 -0
- mcs_core-0.1.0/tests/test_work_event_anchor_resolution.py +125 -0
- mcs_core-0.1.0/tests/test_work_narrative_events.py +313 -0
mcs_core-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 MCS Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
mcs_core-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: mcs-core
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: MCS (Maximum-Context Subgraph) — LLM-native knowledge graph engine
|
|
5
|
+
Author: MCS Team
|
|
6
|
+
License: MIT
|
|
7
|
+
Requires-Python: >=3.10
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Requires-Dist: openai<2.0,>=1.0
|
|
11
|
+
Requires-Dist: jieba>=0.42
|
|
12
|
+
Requires-Dist: ujson>=5.0
|
|
13
|
+
Requires-Dist: tiktoken>=0.7
|
|
14
|
+
Provides-Extra: dev
|
|
15
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
16
|
+
Requires-Dist: pytest-asyncio>=0.21; extra == "dev"
|
|
17
|
+
Requires-Dist: ruff>=0.1; extra == "dev"
|
|
18
|
+
Requires-Dist: httpx>=0.27; extra == "dev"
|
|
19
|
+
Provides-Extra: claude
|
|
20
|
+
Requires-Dist: anthropic<1.0,>=0.40; extra == "claude"
|
|
21
|
+
Provides-Extra: yaml
|
|
22
|
+
Requires-Dist: pyyaml>=6; extra == "yaml"
|
|
23
|
+
Provides-Extra: mcp
|
|
24
|
+
Requires-Dist: mcp>=1.0; extra == "mcp"
|
|
25
|
+
Requires-Dist: pyyaml>=6; extra == "mcp"
|
|
26
|
+
Provides-Extra: agent
|
|
27
|
+
Requires-Dist: fastapi>=0.110; extra == "agent"
|
|
28
|
+
Requires-Dist: uvicorn>=0.27; extra == "agent"
|
|
29
|
+
Requires-Dist: apscheduler>=3.10; extra == "agent"
|
|
30
|
+
Dynamic: license-file
|
|
31
|
+
|
|
32
|
+
# MCS - Maximum-Context Subgraph
|
|
33
|
+
|
|
34
|
+
一个**完整的通用记忆引擎**——由大模型语义驱动,把零散文本组织成图结构的语义记忆,并维持一条硬不变量,使任意节点的活跃视图永远放得进一个 LLM 上下文窗口。不依赖 embedding / 向量检索,靠大模型直接阅读"装得下的局部子图"完成关系发现、聚类与召回。
|
|
35
|
+
|
|
36
|
+
MCS 默认返回相关节点集合(`List[Node]`),不是自然语言答案。它专注于"记忆本身",把合成答案、多轮对话、追问加深等留给上层(RAG / Agent / Chatbot)。
|
|
37
|
+
|
|
38
|
+
## 核心定位
|
|
39
|
+
|
|
40
|
+
MCS 不假设外部知识天然就近——它靠**核心不变量 + 守门 + 聚类裂变主动维持语义局部性**:无论图如何增长,任意节点的活跃双向视图(关系边 + 层级邻居)都被收敛到语义内聚、且渲染 token ≤ 一个上下文窗口 T。因此沿关联边的语义游走总能在有界视野内到达相关知识,大模型可以一次"读完"局部视野再做判断。导航、归纳、查询都建立在这之上。
|
|
41
|
+
|
|
42
|
+
## 快速开始
|
|
43
|
+
|
|
44
|
+
### 安装
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
python -m venv .venv
|
|
48
|
+
.venv\Scripts\activate # Windows
|
|
49
|
+
# source .venv/bin/activate # macOS / Linux
|
|
50
|
+
pip install -e ".[dev]"
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### 基本用法
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
from mcs.presets import create_mcs
|
|
57
|
+
|
|
58
|
+
# 知识图谱模式(默认配置含 Phase 1 插件)
|
|
59
|
+
mcs = create_mcs(llm="deepseek", db_path="mcs.db")
|
|
60
|
+
# 需先设置环境变量 DEEPSEEK_API_KEY 或传入 plugin_configs
|
|
61
|
+
|
|
62
|
+
# 摄入文本 → 自动抽概念、定位、入图
|
|
63
|
+
mcs.ingest("深度学习是机器学习的一个子领域,它使用多层神经网络来学习数据的表示。")
|
|
64
|
+
mcs.ingest("卷积神经网络是一种专门处理网格状数据的深度学习模型。")
|
|
65
|
+
|
|
66
|
+
# 查询 → 默认返回相关节点集合
|
|
67
|
+
nodes = mcs.query("什么是深度学习?")
|
|
68
|
+
for n in nodes:
|
|
69
|
+
print(n.name, "—", n.content[:80])
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
或使用 Builder 完整自定义:
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
from mcs.presets import Phase1Builder
|
|
76
|
+
from mcs.entities.config import MCSConfig
|
|
77
|
+
|
|
78
|
+
config = MCSConfig.knowledge_graph(write_llm="deepseek", read_llm="deepseek")
|
|
79
|
+
config.plugin_configs["deepseek_llm"]["api_key"] = "your-api-key"
|
|
80
|
+
|
|
81
|
+
builder = Phase1Builder(config)
|
|
82
|
+
mcs = builder.build() # 返回即用的 MCS 实例
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### 配置文件(YAML)
|
|
86
|
+
|
|
87
|
+
也支持从 **YAML 文件**配置(preset 铺底 + 字段叠加 + `${VAR}` 环境变量插值 + import-path 第三方插件):
|
|
88
|
+
|
|
89
|
+
```bash
|
|
90
|
+
pip install -e ".[yaml]" # 可选依赖 PyYAML
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
```yaml
|
|
94
|
+
# mcs.yaml
|
|
95
|
+
preset: knowledge_graph # 复用默认 Phase1 插件集,只覆盖要改的字段
|
|
96
|
+
write_llm: deepseek
|
|
97
|
+
plugin_configs:
|
|
98
|
+
deepseek_llm:
|
|
99
|
+
api_key: ${DEEPSEEK_API_KEY} # 秘密走环境、不进文件
|
|
100
|
+
sqlite_storage:
|
|
101
|
+
path: mcs.db
|
|
102
|
+
shared_plugins:
|
|
103
|
+
- my_pkg.exts:MyEdgeExtension # import-path 第三方插件
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
```python
|
|
107
|
+
from mcs.entities.config import MCSConfig
|
|
108
|
+
from mcs.presets import Phase1Builder
|
|
109
|
+
|
|
110
|
+
config = MCSConfig.from_file("mcs.yaml")
|
|
111
|
+
mcs = Phase1Builder(config).build()
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
> ⚠️ 配置文件是**受信输入**:YAML 经 import-path 可加载任意代码,**勿接受陌生来源**。
|
|
115
|
+
> 自定义 LLM 必须走**无 preset** 路径(`knowledge_graph()` 只认 deepseek/claude/ollama)。
|
|
116
|
+
> 详见 [配置文件文档](docs/configuration.md)。
|
|
117
|
+
|
|
118
|
+
### 切换后端
|
|
119
|
+
|
|
120
|
+
<details>
|
|
121
|
+
<summary>Claude / Anthropic 后端</summary>
|
|
122
|
+
|
|
123
|
+
```python
|
|
124
|
+
mcs = create_mcs(
|
|
125
|
+
llm="claude",
|
|
126
|
+
db_path="mcs.db",
|
|
127
|
+
plugin_configs={
|
|
128
|
+
"claude_llm": {
|
|
129
|
+
"auth_token": "your-anthropic-token",
|
|
130
|
+
"model": "claude-3-5-sonnet-latest",
|
|
131
|
+
"base_url": "https://api.anthropic.com",
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
)
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
需先安装:`pip install -e ".[claude]"`
|
|
138
|
+
|
|
139
|
+
</details>
|
|
140
|
+
|
|
141
|
+
<details>
|
|
142
|
+
<summary>Ollama 本地后端(零 token 成本)</summary>
|
|
143
|
+
|
|
144
|
+
```python
|
|
145
|
+
mcs = create_mcs(
|
|
146
|
+
llm="ollama",
|
|
147
|
+
db_path="mcs.db",
|
|
148
|
+
plugin_configs={
|
|
149
|
+
"ollama_llm": {
|
|
150
|
+
"base_url": "http://localhost:11434/v1",
|
|
151
|
+
"model": "qwen3.5:9b",
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
)
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
前置条件:安装 Ollama → `ollama serve` → `ollama pull qwen3.5:9b`
|
|
158
|
+
|
|
159
|
+
适用场景:大量实验迭代、离线/隐私场景。小模型结构化 JSON 可靠性弱于云端,但 lenient parser 已缓解。
|
|
160
|
+
|
|
161
|
+
</details>
|
|
162
|
+
|
|
163
|
+
<details>
|
|
164
|
+
<summary>不带 API key 跑通</summary>
|
|
165
|
+
|
|
166
|
+
`examples/basic_usage.py` 和 `examples/wiki_example.py` 默认走 mock 模式(不需要 API key、不联网)。
|
|
167
|
+
|
|
168
|
+
```bash
|
|
169
|
+
python examples/basic_usage.py
|
|
170
|
+
# 或加真实 LLM:
|
|
171
|
+
# MCS_LLM_MODE=real DEEPSEEK_API_KEY=sk-... python examples/basic_usage.py
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
</details>
|
|
175
|
+
|
|
176
|
+
## 作为 MCP Server
|
|
177
|
+
|
|
178
|
+
MCS 可作为 **MCP(stdio)server** 暴露 `query` / `ingest` 工具,供 Claude Desktop 等客户端把知识图谱当工具用:
|
|
179
|
+
|
|
180
|
+
```bash
|
|
181
|
+
pip install -e ".[mcp]" # 可选依赖 mcp + pyyaml
|
|
182
|
+
export MCS_CONFIG=/path/to/mcs.yaml # YAML 配置(见「配置文件」)
|
|
183
|
+
mcs-mcp # 或 python -m mcs_mcp
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
```json
|
|
187
|
+
// Claude Desktop: claude_desktop_config.json
|
|
188
|
+
{
|
|
189
|
+
"mcpServers": {
|
|
190
|
+
"mcs": { "command": "mcs-mcp", "args": ["--config", "/abs/path/mcs.yaml"],
|
|
191
|
+
"env": { "DEEPSEEK_API_KEY": "sk-..." } }
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
> `query` 经 agent 多步探索(多轮 LLM、耗时较长)、调用经 `MemoryStore` 单 worker 串行保证 SQLite 线程亲和。详见 [MCP Server 文档](docs/mcp-server.md)。
|
|
197
|
+
|
|
198
|
+
## 文档
|
|
199
|
+
|
|
200
|
+
| 文档 | 说明 |
|
|
201
|
+
|------|------|
|
|
202
|
+
| [文档索引](docs/INDEX.md) | 所有文档的统一导航入口 |
|
|
203
|
+
| [上手指南](docs/getting-started.md) | 5 分钟跑通:安装 → 写入 → 查询 → MCP → Agent |
|
|
204
|
+
| [架构总览](docs/architecture.md) | 系统定位、双层结构、核心不变量、插件体系 |
|
|
205
|
+
| [图模型设计](docs/graph-model-design.md) | 完整、权威的图模型与核心算法设计 |
|
|
206
|
+
| [常见问题](docs/faq.md) | FAQ |
|
|
207
|
+
|
|
208
|
+
## 评测
|
|
209
|
+
|
|
210
|
+
MCS 使用 MultiHop-RAG 做文档级多跳检索评测,指标为 Hit@k / MAP@k / MRR@k。
|
|
211
|
+
|
|
212
|
+
**最新结果**(609 篇 whole-doc 建图、200 query、DeepSeek 后端,`T=16K`):**hit@10 ≈ 0.70 / recall@10 ≈ 0.39**(分类型 inference 0.74 / temporal 0.69 / comparison 0.67)。
|
|
213
|
+
|
|
214
|
+
> ⚠️ 当前 hit@10 的最大制约是**跨语言**:建图摘要由 LLM 生成为中文,而 query 为英文,词法交集近零。反事实对照(body 换英文原文、零 LLM)可达 ~0.84——说明瓶颈在语言对齐与下游重排,而非图模型 / 召回链路本身(全节点 gold 召回 0.89+)。详见 [评测报告](bench/multihop_rag/REPORT.md)。
|
|
215
|
+
|
|
216
|
+
下方命令为 200 篇子集的**快速上手**(配置与上述权威数字不同,不直接对应该数字):
|
|
217
|
+
|
|
218
|
+
```bash
|
|
219
|
+
# 200 篇子集,DeepSeek 后端(相关性重排默认开)
|
|
220
|
+
python -m bench.multihop_rag --llm deepseek --corpus-subset 200 --output ./mh_out
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
详见 [bench/README.md](bench/README.md)。
|
|
224
|
+
|
|
225
|
+
## 贡献
|
|
226
|
+
|
|
227
|
+
欢迎贡献!请阅读 [CONTRIBUTING.md](CONTRIBUTING.md) 了解环境搭建、开发流程和提交规范。
|
|
228
|
+
|
|
229
|
+
## 许可证
|
|
230
|
+
|
|
231
|
+
本项目基于 [MIT 许可证](LICENSE) 开源。
|
mcs_core-0.1.0/README.md
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
# MCS - Maximum-Context Subgraph
|
|
2
|
+
|
|
3
|
+
一个**完整的通用记忆引擎**——由大模型语义驱动,把零散文本组织成图结构的语义记忆,并维持一条硬不变量,使任意节点的活跃视图永远放得进一个 LLM 上下文窗口。不依赖 embedding / 向量检索,靠大模型直接阅读"装得下的局部子图"完成关系发现、聚类与召回。
|
|
4
|
+
|
|
5
|
+
MCS 默认返回相关节点集合(`List[Node]`),不是自然语言答案。它专注于"记忆本身",把合成答案、多轮对话、追问加深等留给上层(RAG / Agent / Chatbot)。
|
|
6
|
+
|
|
7
|
+
## 核心定位
|
|
8
|
+
|
|
9
|
+
MCS 不假设外部知识天然就近——它靠**核心不变量 + 守门 + 聚类裂变主动维持语义局部性**:无论图如何增长,任意节点的活跃双向视图(关系边 + 层级邻居)都被收敛到语义内聚、且渲染 token ≤ 一个上下文窗口 T。因此沿关联边的语义游走总能在有界视野内到达相关知识,大模型可以一次"读完"局部视野再做判断。导航、归纳、查询都建立在这之上。
|
|
10
|
+
|
|
11
|
+
## 快速开始
|
|
12
|
+
|
|
13
|
+
### 安装
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
python -m venv .venv
|
|
17
|
+
.venv\Scripts\activate # Windows
|
|
18
|
+
# source .venv/bin/activate # macOS / Linux
|
|
19
|
+
pip install -e ".[dev]"
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
### 基本用法
|
|
23
|
+
|
|
24
|
+
```python
|
|
25
|
+
from mcs.presets import create_mcs
|
|
26
|
+
|
|
27
|
+
# 知识图谱模式(默认配置含 Phase 1 插件)
|
|
28
|
+
mcs = create_mcs(llm="deepseek", db_path="mcs.db")
|
|
29
|
+
# 需先设置环境变量 DEEPSEEK_API_KEY 或传入 plugin_configs
|
|
30
|
+
|
|
31
|
+
# 摄入文本 → 自动抽概念、定位、入图
|
|
32
|
+
mcs.ingest("深度学习是机器学习的一个子领域,它使用多层神经网络来学习数据的表示。")
|
|
33
|
+
mcs.ingest("卷积神经网络是一种专门处理网格状数据的深度学习模型。")
|
|
34
|
+
|
|
35
|
+
# 查询 → 默认返回相关节点集合
|
|
36
|
+
nodes = mcs.query("什么是深度学习?")
|
|
37
|
+
for n in nodes:
|
|
38
|
+
print(n.name, "—", n.content[:80])
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
或使用 Builder 完整自定义:
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from mcs.presets import Phase1Builder
|
|
45
|
+
from mcs.entities.config import MCSConfig
|
|
46
|
+
|
|
47
|
+
config = MCSConfig.knowledge_graph(write_llm="deepseek", read_llm="deepseek")
|
|
48
|
+
config.plugin_configs["deepseek_llm"]["api_key"] = "your-api-key"
|
|
49
|
+
|
|
50
|
+
builder = Phase1Builder(config)
|
|
51
|
+
mcs = builder.build() # 返回即用的 MCS 实例
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### 配置文件(YAML)
|
|
55
|
+
|
|
56
|
+
也支持从 **YAML 文件**配置(preset 铺底 + 字段叠加 + `${VAR}` 环境变量插值 + import-path 第三方插件):
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
pip install -e ".[yaml]" # 可选依赖 PyYAML
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
```yaml
|
|
63
|
+
# mcs.yaml
|
|
64
|
+
preset: knowledge_graph # 复用默认 Phase1 插件集,只覆盖要改的字段
|
|
65
|
+
write_llm: deepseek
|
|
66
|
+
plugin_configs:
|
|
67
|
+
deepseek_llm:
|
|
68
|
+
api_key: ${DEEPSEEK_API_KEY} # 秘密走环境、不进文件
|
|
69
|
+
sqlite_storage:
|
|
70
|
+
path: mcs.db
|
|
71
|
+
shared_plugins:
|
|
72
|
+
- my_pkg.exts:MyEdgeExtension # import-path 第三方插件
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
from mcs.entities.config import MCSConfig
|
|
77
|
+
from mcs.presets import Phase1Builder
|
|
78
|
+
|
|
79
|
+
config = MCSConfig.from_file("mcs.yaml")
|
|
80
|
+
mcs = Phase1Builder(config).build()
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
> ⚠️ 配置文件是**受信输入**:YAML 经 import-path 可加载任意代码,**勿接受陌生来源**。
|
|
84
|
+
> 自定义 LLM 必须走**无 preset** 路径(`knowledge_graph()` 只认 deepseek/claude/ollama)。
|
|
85
|
+
> 详见 [配置文件文档](docs/configuration.md)。
|
|
86
|
+
|
|
87
|
+
### 切换后端
|
|
88
|
+
|
|
89
|
+
<details>
|
|
90
|
+
<summary>Claude / Anthropic 后端</summary>
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
mcs = create_mcs(
|
|
94
|
+
llm="claude",
|
|
95
|
+
db_path="mcs.db",
|
|
96
|
+
plugin_configs={
|
|
97
|
+
"claude_llm": {
|
|
98
|
+
"auth_token": "your-anthropic-token",
|
|
99
|
+
"model": "claude-3-5-sonnet-latest",
|
|
100
|
+
"base_url": "https://api.anthropic.com",
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
)
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
需先安装:`pip install -e ".[claude]"`
|
|
107
|
+
|
|
108
|
+
</details>
|
|
109
|
+
|
|
110
|
+
<details>
|
|
111
|
+
<summary>Ollama 本地后端(零 token 成本)</summary>
|
|
112
|
+
|
|
113
|
+
```python
|
|
114
|
+
mcs = create_mcs(
|
|
115
|
+
llm="ollama",
|
|
116
|
+
db_path="mcs.db",
|
|
117
|
+
plugin_configs={
|
|
118
|
+
"ollama_llm": {
|
|
119
|
+
"base_url": "http://localhost:11434/v1",
|
|
120
|
+
"model": "qwen3.5:9b",
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
)
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
前置条件:安装 Ollama → `ollama serve` → `ollama pull qwen3.5:9b`
|
|
127
|
+
|
|
128
|
+
适用场景:大量实验迭代、离线/隐私场景。小模型结构化 JSON 可靠性弱于云端,但 lenient parser 已缓解。
|
|
129
|
+
|
|
130
|
+
</details>
|
|
131
|
+
|
|
132
|
+
<details>
|
|
133
|
+
<summary>不带 API key 跑通</summary>
|
|
134
|
+
|
|
135
|
+
`examples/basic_usage.py` 和 `examples/wiki_example.py` 默认走 mock 模式(不需要 API key、不联网)。
|
|
136
|
+
|
|
137
|
+
```bash
|
|
138
|
+
python examples/basic_usage.py
|
|
139
|
+
# 或加真实 LLM:
|
|
140
|
+
# MCS_LLM_MODE=real DEEPSEEK_API_KEY=sk-... python examples/basic_usage.py
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
</details>
|
|
144
|
+
|
|
145
|
+
## 作为 MCP Server
|
|
146
|
+
|
|
147
|
+
MCS 可作为 **MCP(stdio)server** 暴露 `query` / `ingest` 工具,供 Claude Desktop 等客户端把知识图谱当工具用:
|
|
148
|
+
|
|
149
|
+
```bash
|
|
150
|
+
pip install -e ".[mcp]" # 可选依赖 mcp + pyyaml
|
|
151
|
+
export MCS_CONFIG=/path/to/mcs.yaml # YAML 配置(见「配置文件」)
|
|
152
|
+
mcs-mcp # 或 python -m mcs_mcp
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
```json
|
|
156
|
+
// Claude Desktop: claude_desktop_config.json
|
|
157
|
+
{
|
|
158
|
+
"mcpServers": {
|
|
159
|
+
"mcs": { "command": "mcs-mcp", "args": ["--config", "/abs/path/mcs.yaml"],
|
|
160
|
+
"env": { "DEEPSEEK_API_KEY": "sk-..." } }
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
> `query` 经 agent 多步探索(多轮 LLM、耗时较长)、调用经 `MemoryStore` 单 worker 串行保证 SQLite 线程亲和。详见 [MCP Server 文档](docs/mcp-server.md)。
|
|
166
|
+
|
|
167
|
+
## 文档
|
|
168
|
+
|
|
169
|
+
| 文档 | 说明 |
|
|
170
|
+
|------|------|
|
|
171
|
+
| [文档索引](docs/INDEX.md) | 所有文档的统一导航入口 |
|
|
172
|
+
| [上手指南](docs/getting-started.md) | 5 分钟跑通:安装 → 写入 → 查询 → MCP → Agent |
|
|
173
|
+
| [架构总览](docs/architecture.md) | 系统定位、双层结构、核心不变量、插件体系 |
|
|
174
|
+
| [图模型设计](docs/graph-model-design.md) | 完整、权威的图模型与核心算法设计 |
|
|
175
|
+
| [常见问题](docs/faq.md) | FAQ |
|
|
176
|
+
|
|
177
|
+
## 评测
|
|
178
|
+
|
|
179
|
+
MCS 使用 MultiHop-RAG 做文档级多跳检索评测,指标为 Hit@k / MAP@k / MRR@k。
|
|
180
|
+
|
|
181
|
+
**最新结果**(609 篇 whole-doc 建图、200 query、DeepSeek 后端,`T=16K`):**hit@10 ≈ 0.70 / recall@10 ≈ 0.39**(分类型 inference 0.74 / temporal 0.69 / comparison 0.67)。
|
|
182
|
+
|
|
183
|
+
> ⚠️ 当前 hit@10 的最大制约是**跨语言**:建图摘要由 LLM 生成为中文,而 query 为英文,词法交集近零。反事实对照(body 换英文原文、零 LLM)可达 ~0.84——说明瓶颈在语言对齐与下游重排,而非图模型 / 召回链路本身(全节点 gold 召回 0.89+)。详见 [评测报告](bench/multihop_rag/REPORT.md)。
|
|
184
|
+
|
|
185
|
+
下方命令为 200 篇子集的**快速上手**(配置与上述权威数字不同,不直接对应该数字):
|
|
186
|
+
|
|
187
|
+
```bash
|
|
188
|
+
# 200 篇子集,DeepSeek 后端(相关性重排默认开)
|
|
189
|
+
python -m bench.multihop_rag --llm deepseek --corpus-subset 200 --output ./mh_out
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
详见 [bench/README.md](bench/README.md)。
|
|
193
|
+
|
|
194
|
+
## 贡献
|
|
195
|
+
|
|
196
|
+
欢迎贡献!请阅读 [CONTRIBUTING.md](CONTRIBUTING.md) 了解环境搭建、开发流程和提交规范。
|
|
197
|
+
|
|
198
|
+
## 许可证
|
|
199
|
+
|
|
200
|
+
本项目基于 [MIT 许可证](LICENSE) 开源。
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"""MCS - Maximum-Context Subgraph:可扩展的知识图谱与检索引擎。
|
|
2
|
+
|
|
3
|
+
顶层 ``MCS`` 类将图存储、LLM 后端、插件链、读写管线组装在一起。
|
|
4
|
+
推荐使用 Builder 构建 MCS 实例:
|
|
5
|
+
|
|
6
|
+
# 快捷方式(推荐)
|
|
7
|
+
from mcs.presets import create_mcs
|
|
8
|
+
mcs = create_mcs(llm="deepseek", db_path="mcs.db")
|
|
9
|
+
|
|
10
|
+
# 或完整自定义
|
|
11
|
+
from mcs.presets import Phase1Builder
|
|
12
|
+
from mcs.entities.config import MCSConfig
|
|
13
|
+
|
|
14
|
+
config = MCSConfig.knowledge_graph(write_llm="deepseek", read_llm="deepseek")
|
|
15
|
+
config.plugin_configs["deepseek_llm"]["api_key"] = "..."
|
|
16
|
+
builder = Phase1Builder(config)
|
|
17
|
+
mcs = builder.build() # 返回即用的 MCS 实例
|
|
18
|
+
|
|
19
|
+
# 使用
|
|
20
|
+
mcs.ingest("深度学习是机器学习的一个子领域...")
|
|
21
|
+
nodes = mcs.query("什么是深度学习?")
|
|
22
|
+
mcs.shutdown()
|
|
23
|
+
|
|
24
|
+
MCS 实例由 Builder 一次性构建完成,无需调用 initialize()。
|
|
25
|
+
|
|
26
|
+
参见 ``openspec/specs/`` 获取各能力的契约定义。
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
from __future__ import annotations
|
|
30
|
+
|
|
31
|
+
__version__ = "0.1.0"
|
|
32
|
+
|
|
33
|
+
from mcs.core.builder import MCSBuilder
|
|
34
|
+
from mcs.core.mcs import MCS
|
|
35
|
+
from mcs.core.plugin import Plugin, PluginType
|
|
36
|
+
from mcs.core.store import StoreInterface
|
|
37
|
+
from mcs.entities.config import MCSConfig
|
|
38
|
+
from mcs.presets import Phase1Builder, create_mcs, get_phase1_plugin_registry
|
|
39
|
+
from mcs.stores.in_memory import InMemoryStore
|
|
40
|
+
from mcs.stores.sqlite_store import SQLiteStore
|
|
41
|
+
|
|
42
|
+
__all__ = [
|
|
43
|
+
"MCS",
|
|
44
|
+
"MCSConfig",
|
|
45
|
+
"MCSBuilder",
|
|
46
|
+
"Phase1Builder",
|
|
47
|
+
"StoreInterface",
|
|
48
|
+
"InMemoryStore",
|
|
49
|
+
"SQLiteStore",
|
|
50
|
+
"create_mcs",
|
|
51
|
+
"get_phase1_plugin_registry",
|
|
52
|
+
]
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"""Core engine - stable graph operations and pipeline state machines.
|
|
2
|
+
|
|
3
|
+
Contains:
|
|
4
|
+
- ``mcs``: MCS top-level orchestrator with dual PluginManager architecture
|
|
5
|
+
- ``builder``: MCSBuilder abstract base class for building MCS instances
|
|
6
|
+
- ``errors``: Exception hierarchy
|
|
7
|
+
- ``plugin``: Plugin base class and PluginType enum
|
|
8
|
+
- ``plugin_manager``: PluginManager and PluginContext
|
|
9
|
+
- ``query_engine``: QueryEngine for read pipeline
|
|
10
|
+
- ``store``: StoreInterface ABC
|
|
11
|
+
- ``token_budget``: TokenBudget
|
|
12
|
+
- ``write_pipeline``: WritePipeline for ingest pipeline
|
|
13
|
+
- ``context_renderer``: ContextRenderer for LLM input rendering
|
|
14
|
+
|
|
15
|
+
纯数据模型(Node/Edge/Subgraph、Decision 系列、MCSConfig)已迁至 ``mcs.entities``。
|
|
16
|
+
|
|
17
|
+
See architecture.md §2.
|
|
18
|
+
"""
|