deeprefine-cli 0.1.5__tar.gz → 0.1.7__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.
Files changed (26) hide show
  1. {deeprefine_cli-0.1.5/deeprefine_cli.egg-info → deeprefine_cli-0.1.7}/PKG-INFO +21 -32
  2. {deeprefine_cli-0.1.5 → deeprefine_cli-0.1.7}/README.md +20 -29
  3. deeprefine_cli-0.1.7/SKILL.md +311 -0
  4. {deeprefine_cli-0.1.5 → deeprefine_cli-0.1.7/deeprefine_cli.egg-info}/PKG-INFO +21 -32
  5. {deeprefine_cli-0.1.5 → deeprefine_cli-0.1.7}/deeprefine_cli.egg-info/SOURCES.txt +3 -0
  6. deeprefine_cli-0.1.7/deeprefine_skill/SKILL.md +311 -0
  7. {deeprefine_cli-0.1.5 → deeprefine_cli-0.1.7}/deeprefine_skill/__init__.py +1 -1
  8. deeprefine_cli-0.1.7/deeprefine_skill/agent_graph.py +194 -0
  9. deeprefine_cli-0.1.7/deeprefine_skill/agent_loop.py +212 -0
  10. deeprefine_cli-0.1.7/deeprefine_skill/agent_prompts.py +56 -0
  11. {deeprefine_cli-0.1.5 → deeprefine_cli-0.1.7}/deeprefine_skill/cli.py +163 -0
  12. {deeprefine_cli-0.1.5 → deeprefine_cli-0.1.7}/deeprefine_skill/refine_runner.py +4 -1
  13. {deeprefine_cli-0.1.5 → deeprefine_cli-0.1.7}/pyproject.toml +2 -1
  14. deeprefine_cli-0.1.5/SKILL.md +0 -62
  15. deeprefine_cli-0.1.5/deeprefine_skill/SKILL.md +0 -62
  16. {deeprefine_cli-0.1.5 → deeprefine_cli-0.1.7}/LICENSE +0 -0
  17. {deeprefine_cli-0.1.5 → deeprefine_cli-0.1.7}/MANIFEST.in +0 -0
  18. {deeprefine_cli-0.1.5 → deeprefine_cli-0.1.7}/deeprefine_cli.egg-info/dependency_links.txt +0 -0
  19. {deeprefine_cli-0.1.5 → deeprefine_cli-0.1.7}/deeprefine_cli.egg-info/entry_points.txt +0 -0
  20. {deeprefine_cli-0.1.5 → deeprefine_cli-0.1.7}/deeprefine_cli.egg-info/requires.txt +0 -0
  21. {deeprefine_cli-0.1.5 → deeprefine_cli-0.1.7}/deeprefine_cli.egg-info/top_level.txt +0 -0
  22. {deeprefine_cli-0.1.5 → deeprefine_cli-0.1.7}/deeprefine_skill/adapter_graphify.py +0 -0
  23. {deeprefine_cli-0.1.5 → deeprefine_cli-0.1.7}/deeprefine_skill/history.py +0 -0
  24. {deeprefine_cli-0.1.5 → deeprefine_cli-0.1.7}/deeprefine_skill/installers.py +0 -0
  25. {deeprefine_cli-0.1.5 → deeprefine_cli-0.1.7}/deeprefine_skill/paths.py +0 -0
  26. {deeprefine_cli-0.1.5 → deeprefine_cli-0.1.7}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: deeprefine-cli
3
- Version: 0.1.5
3
+ Version: 0.1.7
4
4
  Summary: CLI and Cursor skill to refine graphify knowledge graphs with DeepRefine
5
5
  Author: HKUST-KnowComp
6
6
  License-Expression: MIT
@@ -20,17 +20,15 @@ Classifier: Programming Language :: Python :: 3.12
20
20
  Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
21
21
  Requires-Python: >=3.10
22
22
  Description-Content-Type: text/markdown
23
- License-File: LICENSE
24
23
  Requires-Dist: openai>=1.0
25
- Dynamic: license-file
26
24
 
27
25
  <div align="center">
28
26
 
29
27
  # DeepRefine-Skill
30
28
 
31
29
 
32
- [![PyPi](https://img.shields.io/badge/PyPi-v0.1.5-blue.svg)](https://pypi.org/project/deeprefine-cli/0.1.5/)
33
- [![Python](https://img.shields.io/badge/Python-3.10,3.11,3.12-blue.svg)](https://pypi.org/project/deeprefine-cli/0.1.5/)
30
+ [![PyPi](https://img.shields.io/badge/PyPi-v0.1.7-blue.svg)](https://pypi.org/project/deeprefine-cli/0.1.7/)
31
+ [![Python](https://img.shields.io/badge/Python-3.10,3.11,3.12-blue.svg)](https://pypi.org/project/deeprefine-cli/0.1.7/)
34
32
  [![Paper](https://img.shields.io/badge/Paper-DeepRefine-b31b1b.svg)](https://arxiv.org/pdf/2605.10488)
35
33
  [![Project](https://img.shields.io/badge/Project-DeepRefine-green.svg)](https://github.com/HKUST-KnowComp/DeepRefine)
36
34
 
@@ -38,17 +36,13 @@ Dynamic: license-file
38
36
 
39
37
  Type `/deeprefine` in your AI coding assistant after you've built a **[graphify](https://github.com/safishamsi/graphify)** knowledge base — it patches `graphify-out/graph.json` from your session's query history to evolve your LLM-Wiki.
40
38
 
41
- Works in **Cursor** (install the skill once with `deeprefine cursor install`). In Cursor, `/deeprefine` runs in **agent-native mode** (no extra API key needed). The runtime CLI (`deeprefine refine`) is still available for vLLM/API execution.
39
+ Works in **Cursor** (`deeprefine cursor install`). **`/deeprefine`** runs the **same control flow as `Reafiner.refine()`** in the agent: `graphify query` / k-hop reads (no FAISS), session LLM for judgement/abduction/actions, mandatory `loop_trace_*.json`, then `deeprefine apply`. Optional **`deeprefine refine`** = full FAISS + API runtime.
42
40
 
43
41
  ```
44
42
  /deeprefine
45
43
  ```
46
44
 
47
- **Typical flow:** `graphify .` → `graphify query "..."` → record the question`/deeprefine`.
48
-
49
- ```bash
50
- deeprefine history add --query "your question" # or let the agent do this
51
- ```
45
+ **Typical flow:** `graphify .` → `graphify query "..."` → `/deeprefine` (agent loop: search judge → abduct → actions → `deeprefine apply`).
52
46
 
53
47
  That's it. Under `graphify-out/.deeprefine/` you get:
54
48
 
@@ -62,14 +56,13 @@ graphify-out/
62
56
  ```
63
57
 
64
58
  > **Standalone repo.** Model code (`autorefiner`, `atlas_rag`) lives in a separate [DeepRefine](https://github.com/HKUST-KnowComp/DeepRefine) checkout.
65
- > `pip install deeprefine-cli` ships the CLI and `SKILL.md`. In Cursor, `/deeprefine` uses agent-native refinement; `deeprefine refine` uses DeepRefine runtime (vLLM/API) when you explicitly choose CLI execution.
59
+ > `pip install deeprefine-cli` ships the CLI and `SKILL.md`. **Agent mode** (`/deeprefine`): same loop logic, no FAISS retriever, no extra API keys. **CLI mode** (`deeprefine refine`): full `Reafiner` + embeddings (vLLM or API).
66
60
 
67
61
  ---
68
62
 
69
63
  ## News
70
64
 
71
- - [2026/6/2] deeprefine-cli v0.1.5 has been released! Agent-native `/deeprefine` flow + clearer CLI/Agent split.
72
- - [2026/6/2] deeprefine-cli v0.1.4 has been released! Customize your LLM api.
65
+ - [2026/6/2] deeprefine-cli v0.1.7 agent Reafiner loop + `deeprefine loop` / `apply --trace-file`.
73
66
 
74
67
  ---
75
68
 
@@ -81,7 +74,7 @@ graphify-out/
81
74
  | 2 | `pip install deeprefine-cli` |
82
75
  | 3 | `deeprefine cursor install` in your KB project |
83
76
  | 4 | (Optional) start local vLLM, or use your API provider |
84
- | 5 | Cursor chat: `/deeprefine` *(auto uses/adds query context)*; CLI mode: `history add` + `refine` |
77
+ | 5 | Cursor: `/deeprefine` *(agent loop)*; or terminal: `history add` + `deeprefine refine` |
85
78
 
86
79
  ```bash
87
80
  # 1) DeepRefine (once)
@@ -110,21 +103,13 @@ export DEEPREFINE_EMBED_MODEL=your_embed_model
110
103
  # export DEEPREFINE_API_KEY=your_shared_api_key
111
104
  # optional model overrides:
112
105
 
113
- # 5a) Refine in Cursor chat (agent-native, recommended)
114
- # type in chat:
115
- # /deeprefine
116
-
117
- # 5b) Refine in terminal / CLI (manual flow)
106
+ # 5) Refine
107
+ # Cursor: /deeprefine (agent loop: graphify query + judgement/abduction/actions + deeprefine apply)
108
+ # Terminal CLI mode:
118
109
  deeprefine history add --query "your question"
119
110
  deeprefine refine
120
111
  ```
121
112
 
122
- In **Cursor chat** (agent):
123
-
124
- ```text
125
- /deeprefine
126
- ```
127
-
128
113
  ---
129
114
 
130
115
  ## Pipeline
@@ -141,11 +126,13 @@ In **Cursor chat** (agent):
141
126
  ▼ graphify
142
127
  graph.json ◄────────────────────────┐
143
128
  │ │
144
- ▼ graphify query │ deeprefine refine
145
- (Q&A session)
129
+ ▼ graphify query "..." │ deeprefine refine
130
+ ▼ graphify query "..." │ (per session query)
131
+ ▼ ... │
132
+ (many Q&A in session) │
146
133
  │ │
147
- ▼ deeprefine history add
148
- history.jsonl ──────────────────────┘
134
+ /deeprefine
135
+ (DeepRefine loop) ────────────────────┘
149
136
 
150
137
  ▼ graphify query (verify)
151
138
  ```
@@ -266,8 +253,8 @@ deeprefine cursor install
266
253
  |:-:|--------|
267
254
  | 1 | `graphify .` or `/graphify .` → `graphify-out/graph.json` |
268
255
  | 2 | `graphify query "..."` |
269
- | 3 | Cursor chat: `/deeprefine` *(recommended; no manual `history add` needed)* |
270
- | 4 | CLI alternative: `deeprefine history add --query "..."` → `deeprefine refine` |
256
+ | 3 | Cursor chat: `/deeprefine` *(agent loop; no manual history add)* |
257
+ | 4 | Terminal: `deeprefine history add` → `deeprefine refine` *(FAISS + API/vLLM)* |
271
258
  | 5 | *(optional)* `graphify query "..."` to verify |
272
259
 
273
260
  ---
@@ -284,6 +271,8 @@ All commands below run from **KB project root**.
284
271
  | `deeprefine refine` | Refine all pending queries |
285
272
  | `deeprefine refine --query "..."` | Refine one query (also recorded) |
286
273
  | `deeprefine refine --rebuild-index` | Rebuild FAISS before refine |
274
+ | `deeprefine loop init/validate/finish` | Agent loop trace (enforces Reafiner rules) |
275
+ | `deeprefine apply --trace-file T --refinement-file F` | Apply actions (requires valid trace) |
287
276
  | `deeprefine index --rebuild` | Rebuild FAISS cache only |
288
277
  | `deeprefine cursor install \| uninstall` | Manage Cursor skill |
289
278
 
@@ -3,8 +3,8 @@
3
3
  # DeepRefine-Skill
4
4
 
5
5
 
6
- [![PyPi](https://img.shields.io/badge/PyPi-v0.1.5-blue.svg)](https://pypi.org/project/deeprefine-cli/0.1.5/)
7
- [![Python](https://img.shields.io/badge/Python-3.10,3.11,3.12-blue.svg)](https://pypi.org/project/deeprefine-cli/0.1.5/)
6
+ [![PyPi](https://img.shields.io/badge/PyPi-v0.1.7-blue.svg)](https://pypi.org/project/deeprefine-cli/0.1.7/)
7
+ [![Python](https://img.shields.io/badge/Python-3.10,3.11,3.12-blue.svg)](https://pypi.org/project/deeprefine-cli/0.1.7/)
8
8
  [![Paper](https://img.shields.io/badge/Paper-DeepRefine-b31b1b.svg)](https://arxiv.org/pdf/2605.10488)
9
9
  [![Project](https://img.shields.io/badge/Project-DeepRefine-green.svg)](https://github.com/HKUST-KnowComp/DeepRefine)
10
10
 
@@ -12,17 +12,13 @@
12
12
 
13
13
  Type `/deeprefine` in your AI coding assistant after you've built a **[graphify](https://github.com/safishamsi/graphify)** knowledge base — it patches `graphify-out/graph.json` from your session's query history to evolve your LLM-Wiki.
14
14
 
15
- Works in **Cursor** (install the skill once with `deeprefine cursor install`). In Cursor, `/deeprefine` runs in **agent-native mode** (no extra API key needed). The runtime CLI (`deeprefine refine`) is still available for vLLM/API execution.
15
+ Works in **Cursor** (`deeprefine cursor install`). **`/deeprefine`** runs the **same control flow as `Reafiner.refine()`** in the agent: `graphify query` / k-hop reads (no FAISS), session LLM for judgement/abduction/actions, mandatory `loop_trace_*.json`, then `deeprefine apply`. Optional **`deeprefine refine`** = full FAISS + API runtime.
16
16
 
17
17
  ```
18
18
  /deeprefine
19
19
  ```
20
20
 
21
- **Typical flow:** `graphify .` → `graphify query "..."` → record the question`/deeprefine`.
22
-
23
- ```bash
24
- deeprefine history add --query "your question" # or let the agent do this
25
- ```
21
+ **Typical flow:** `graphify .` → `graphify query "..."` → `/deeprefine` (agent loop: search judge → abduct → actions → `deeprefine apply`).
26
22
 
27
23
  That's it. Under `graphify-out/.deeprefine/` you get:
28
24
 
@@ -36,14 +32,13 @@ graphify-out/
36
32
  ```
37
33
 
38
34
  > **Standalone repo.** Model code (`autorefiner`, `atlas_rag`) lives in a separate [DeepRefine](https://github.com/HKUST-KnowComp/DeepRefine) checkout.
39
- > `pip install deeprefine-cli` ships the CLI and `SKILL.md`. In Cursor, `/deeprefine` uses agent-native refinement; `deeprefine refine` uses DeepRefine runtime (vLLM/API) when you explicitly choose CLI execution.
35
+ > `pip install deeprefine-cli` ships the CLI and `SKILL.md`. **Agent mode** (`/deeprefine`): same loop logic, no FAISS retriever, no extra API keys. **CLI mode** (`deeprefine refine`): full `Reafiner` + embeddings (vLLM or API).
40
36
 
41
37
  ---
42
38
 
43
39
  ## News
44
40
 
45
- - [2026/6/2] deeprefine-cli v0.1.5 has been released! Agent-native `/deeprefine` flow + clearer CLI/Agent split.
46
- - [2026/6/2] deeprefine-cli v0.1.4 has been released! Customize your LLM api.
41
+ - [2026/6/2] deeprefine-cli v0.1.7 agent Reafiner loop + `deeprefine loop` / `apply --trace-file`.
47
42
 
48
43
  ---
49
44
 
@@ -55,7 +50,7 @@ graphify-out/
55
50
  | 2 | `pip install deeprefine-cli` |
56
51
  | 3 | `deeprefine cursor install` in your KB project |
57
52
  | 4 | (Optional) start local vLLM, or use your API provider |
58
- | 5 | Cursor chat: `/deeprefine` *(auto uses/adds query context)*; CLI mode: `history add` + `refine` |
53
+ | 5 | Cursor: `/deeprefine` *(agent loop)*; or terminal: `history add` + `deeprefine refine` |
59
54
 
60
55
  ```bash
61
56
  # 1) DeepRefine (once)
@@ -84,21 +79,13 @@ export DEEPREFINE_EMBED_MODEL=your_embed_model
84
79
  # export DEEPREFINE_API_KEY=your_shared_api_key
85
80
  # optional model overrides:
86
81
 
87
- # 5a) Refine in Cursor chat (agent-native, recommended)
88
- # type in chat:
89
- # /deeprefine
90
-
91
- # 5b) Refine in terminal / CLI (manual flow)
82
+ # 5) Refine
83
+ # Cursor: /deeprefine (agent loop: graphify query + judgement/abduction/actions + deeprefine apply)
84
+ # Terminal CLI mode:
92
85
  deeprefine history add --query "your question"
93
86
  deeprefine refine
94
87
  ```
95
88
 
96
- In **Cursor chat** (agent):
97
-
98
- ```text
99
- /deeprefine
100
- ```
101
-
102
89
  ---
103
90
 
104
91
  ## Pipeline
@@ -115,11 +102,13 @@ In **Cursor chat** (agent):
115
102
  ▼ graphify
116
103
  graph.json ◄────────────────────────┐
117
104
  │ │
118
- ▼ graphify query │ deeprefine refine
119
- (Q&A session)
105
+ ▼ graphify query "..." │ deeprefine refine
106
+ ▼ graphify query "..." │ (per session query)
107
+ ▼ ... │
108
+ (many Q&A in session) │
120
109
  │ │
121
- ▼ deeprefine history add
122
- history.jsonl ──────────────────────┘
110
+ /deeprefine
111
+ (DeepRefine loop) ────────────────────┘
123
112
 
124
113
  ▼ graphify query (verify)
125
114
  ```
@@ -240,8 +229,8 @@ deeprefine cursor install
240
229
  |:-:|--------|
241
230
  | 1 | `graphify .` or `/graphify .` → `graphify-out/graph.json` |
242
231
  | 2 | `graphify query "..."` |
243
- | 3 | Cursor chat: `/deeprefine` *(recommended; no manual `history add` needed)* |
244
- | 4 | CLI alternative: `deeprefine history add --query "..."` → `deeprefine refine` |
232
+ | 3 | Cursor chat: `/deeprefine` *(agent loop; no manual history add)* |
233
+ | 4 | Terminal: `deeprefine history add` → `deeprefine refine` *(FAISS + API/vLLM)* |
245
234
  | 5 | *(optional)* `graphify query "..."` to verify |
246
235
 
247
236
  ---
@@ -258,6 +247,8 @@ All commands below run from **KB project root**.
258
247
  | `deeprefine refine` | Refine all pending queries |
259
248
  | `deeprefine refine --query "..."` | Refine one query (also recorded) |
260
249
  | `deeprefine refine --rebuild-index` | Rebuild FAISS before refine |
250
+ | `deeprefine loop init/validate/finish` | Agent loop trace (enforces Reafiner rules) |
251
+ | `deeprefine apply --trace-file T --refinement-file F` | Apply actions (requires valid trace) |
261
252
  | `deeprefine index --rebuild` | Rebuild FAISS cache only |
262
253
  | `deeprefine cursor install \| uninstall` | Manage Cursor skill |
263
254
 
@@ -0,0 +1,311 @@
1
+ ---
2
+ name: deeprefine
3
+ description: >-
4
+ Agent-native DeepRefine Reafiner loop — same control flow as Reafiner.refine(),
5
+ graphify search instead of FAISS, session LLM, deeprefine apply for graph writes.
6
+ disable-model-invocation: false
7
+ ---
8
+
9
+ # DeepRefine — Agent Reafiner loop (strict)
10
+
11
+ You **MUST** implement the **same control flow** as `Reafiner.refine()` in DeepRefine (`autorefiner/src/reafiner.py`).
12
+
13
+ | Component | Agent mode | CLI `deeprefine refine` |
14
+ |-----------|------------|-------------------------|
15
+ | Retrieval | `graphify query` + k-hop from `graph.json` | FAISS retriever |
16
+ | LLM | **Your session model** | External API / vLLM |
17
+ | Graph writes | `deeprefine apply` + validated `loop_trace_*.json` | In-process |
18
+
19
+ ---
20
+
21
+ ## FORBIDDEN (hard stop)
22
+
23
+ Do **NOT**:
24
+
25
+ 1. Run `deeprefine refine` (unless the user explicitly asks for CLI/FAISS mode).
26
+ 2. Call `deeprefine apply` without a valid `loop_trace_<query_id>.json` (CLI will reject).
27
+ 3. Skip any hop’s `<judge>Yes</judge>` / `<judge>No</judge>` judgement.
28
+ 4. Skip error abduction when `len(interaction_history) > 1`.
29
+ 5. Write `<refinement>` before abduction when refinement is required.
30
+ 6. Hand-edit `graph.json` with Python or ad-hoc JSON patches.
31
+ 7. Run `deeprefine history add` at the start of `/deeprefine` (only `loop finish` updates history).
32
+ 8. Invent a shorter pipeline (“read file → write refinement → apply”).
33
+
34
+ If validation fails, **fix the trace and re-run the missing step** — do not bypass with `--skip-trace-check`.
35
+
36
+ ---
37
+
38
+ ## Constants (match `refine_runner.py` / Reafiner)
39
+
40
+ ```text
41
+ MAX_HOPS = 4
42
+ INCREMENT_HOP = 1
43
+ BASE_TOP_K = 10
44
+ MAX_TRIPLE_NUM_BY_STEP = [5, 10, 15, 20] # cap triples per step
45
+ HISTORY_HORIZON = 4 # abduction uses last N steps
46
+ ```
47
+
48
+ ---
49
+
50
+ ## Mandatory artifact
51
+
52
+ For each query, maintain:
53
+
54
+ `graphify-out/.deeprefine/loop_trace_<query_id>.json`
55
+
56
+ Create template:
57
+
58
+ ```bash
59
+ deeprefine loop init --query "<exact question>"
60
+ ```
61
+
62
+ Append each hop to `interaction_history` **before** starting the next hop.
63
+ Run `deeprefine loop validate --trace-file ...` after abduction and before `apply`.
64
+
65
+ ---
66
+
67
+ ## Control flow (must match `Reafiner.refine()`)
68
+
69
+ Pseudocode — follow **exactly**:
70
+
71
+ ```text
72
+ interaction_history = []
73
+ for step in 1..MAX_HOPS:
74
+ print "[Step: {step}]" # show in chat
75
+
76
+ if step == 1:
77
+ # Vector-retrieval equivalent: graphify query on full question
78
+ RUN: graphify query "<question>"
79
+ triples = parse NODE/EDGE → [{subject, relation, object}, ...]
80
+ cap = MAX_TRIPLE_NUM_BY_STEP[0] # 5
81
+ record retrieval.method = "graphify_query"
82
+ else:
83
+ # k-hop expansion from entities in previous hop (NOT a new random search)
84
+ entities = unique subjects/objects from previous triples
85
+ expand 1-hop neighbors from graphify-out/graph.json (or graphify query on entities)
86
+ cap = MAX_TRIPLE_NUM_BY_STEP[step-1]
87
+ record retrieval.method = "k_hop_expansion" or "graphify_query+k_hop_expansion"
88
+
89
+ triples = dedupe; len(triples) <= cap
90
+
91
+ # Answerable judgement — session LLM, prompts below
92
+ answerable, judgement_raw = LLM_judge(question, triples)
93
+ MUST output ONLY: <judge>Yes</judge> or <judge>No</judge>
94
+
95
+ append interaction_history with:
96
+ step, query, num_hops=(step-1)*INCREMENT_HOP, base_top_k=10,
97
+ retrieved_subgraph, answerable, judgement_raw,
98
+ retrieval: {method, evidence: "<command output excerpt>"}
99
+
100
+ if answerable:
101
+ BREAK # stop hop loop
102
+
103
+ # --- same branch as Reafiner.refine() line 314+ ---
104
+ if len(interaction_history) <= 1:
105
+ # Early exit: first hop was answerable — NO graph refinement
106
+ set trace.early_exit = true
107
+ deeprefine loop finish --trace-file ... # no --refinement-file
108
+ STOP
109
+ else:
110
+ # len > 1 → ALWAYS error abduction + actions (even if last hop was Yes)
111
+ error_abduction = LLM_abduction(interaction_history[-HISTORY_HORIZON:])
112
+ MUST output: <abduction>...</abduction>
113
+
114
+ actions = LLM_kg_refinement(
115
+ last_hop.retrieved_subgraph,
116
+ error_abduction,
117
+ question,
118
+ source file hints from triples,
119
+ )
120
+ MUST output: <refinement>insert_edge(...)|...</refinement>
121
+
122
+ save refinement to graphify-out/.deeprefine/refinement_actions_<id>.txt
123
+ deeprefine loop validate --trace-file ... --refinement-file ...
124
+ deeprefine apply --trace-file ... --refinement-file ...
125
+ deeprefine loop finish --trace-file ... --refinement-file ...
126
+ ```
127
+
128
+ **Critical Reafiner rule:** refinement runs when `len(interaction_history) > 1`, not only when all judgements are `No`.
129
+
130
+ ---
131
+
132
+ ## LLM prompts (verbatim — do not paraphrase)
133
+
134
+ ### Judgement (`_answerable_judgement`)
135
+
136
+ **System:**
137
+
138
+ ```text
139
+ As an advanced judgement assistant, your task is to judge whether the given question is answerable based on the provided KG context.
140
+
141
+ Evaluate whether the given question is answerable based on the provided KG context. Output your judgment in the following format:
142
+ <judge>Yes</judge> or <judge>No</judge>
143
+
144
+ **Important:** You must think carefully about the question and the KG context before making your judgment. And output your judgment result directly in the specified format.
145
+ ```
146
+
147
+ **User:**
148
+
149
+ ```text
150
+ Question: {question}
151
+ Knowledge Graph (KG) context: {triples_string}
152
+ ```
153
+
154
+ `{triples_string}` = one triple per line: `subject | relation | object`
155
+
156
+ ### Error abduction (`_error_abduction`) — only if `len(interaction_history) > 1`
157
+
158
+ **System:**
159
+
160
+ ```text
161
+ As an advanced error abduction assistant, your task is to analyze the error reasons based on the given interaction history.
162
+
163
+ Analyze the reasons of the unanswerable questions based on the given interaction history from the incompleteness, incorrectness, and redundancy perspectives. Output your analysis in the following format:
164
+ <abduction>...</abduction>
165
+
166
+ **Important:** You must think carefully about the interaction history before making your analysis. And output your analysis result directly in the specified format.
167
+ ```
168
+
169
+ **User:**
170
+
171
+ ```text
172
+ Interaction history: {interaction_history}
173
+ ```
174
+
175
+ `{interaction_history}` format (same as Reafiner):
176
+
177
+ ```text
178
+ Step1:
179
+ ['Query': ..., 'Subgraph_hop': ..., 'Subgraph_content': ..., 'Answerable': ...]
180
+
181
+ Step2:
182
+ ...
183
+ ```
184
+
185
+ ### KG refinement actions (`_kg_refinement_action`) — only if `len(interaction_history) > 1`
186
+
187
+ **System:**
188
+
189
+ ```text
190
+ As an advanced knowledge graph refinement assistant, your task is to generate a series of actions (**within 10 actions**) to refine the given KG to make it more suitable for answering the given question.
191
+
192
+ Based on the given KG and the analysed error reasons, refine the given KG to make it more easily for retrieval and answering the given question. You have the following three types of actions to conduct:
193
+
194
+ - insert_edge(subject, relation, object): Insert a new edge into the KG to complete the missing information.
195
+ - delete_edge(subject, relation, object): Delete an edge from the KG to remove the redundant information or conflicting information.
196
+ - replace_node(old_entity, new_entity): Replace an entity in the KG to correct the errors or deal with disambiguation.
197
+
198
+ Output a series of actions (**within 10 actions**) in the following format:
199
+ <refinement>insert_edge("...", "...", "...")|delete_edge("...", "...", "...")|replace_node("...", "...")|...</refinement>
200
+
201
+ **Important:** You must think carefully about the given KG and the analysed error reasons before making your refinement. DO NOT DELETE ANY IRRELEVANT TRIPLES FROM THE ORIGINAL KG. TRY TO KEEP THE ORIGINAL KG AS MUCH AS POSSIBLE. DO NOT GENERATE TOO MANY ACTIONS. And output your refinement result directly in the specified format.
202
+ ```
203
+
204
+ **User:**
205
+
206
+ ```text
207
+ Original Text: {original_text}
208
+ KG: {triples_string}
209
+ Question: {question}
210
+ Error reasons: {error_reasons}
211
+ ```
212
+
213
+ Use **last hop’s** `retrieved_subgraph` as `{triples_string}` (JSON list is OK in trace; string form for the prompt).
214
+
215
+ ---
216
+
217
+ ## Per-query checklist (report in chat)
218
+
219
+ Copy and tick each item in your final message:
220
+
221
+ ```text
222
+ [ ] Backup: graphify-out/.deeprefine/graph.json.bak
223
+ [ ] loop_trace_<id>.json created
224
+ [ ] Step 1: graphify query executed (evidence in trace)
225
+ [ ] Each step: <judge>Yes|No</judge> shown in chat
226
+ [ ] Hops stopped on Yes OR reached MAX_HOPS
227
+ [ ] early_exit OR (abduction + refinement) per Reafiner branch
228
+ [ ] deeprefine loop validate passed
229
+ [ ] deeprefine apply (only if refinement path)
230
+ [ ] deeprefine loop finish
231
+ ```
232
+
233
+ ---
234
+
235
+ ## Commands (in order)
236
+
237
+ ```bash
238
+ # 0. KB project root; graphify-out/graph.json exists
239
+ mkdir -p graphify-out/.deeprefine
240
+ cp graphify-out/graph.json graphify-out/.deeprefine/graph.json.bak
241
+ deeprefine loop init --query "<question>"
242
+
243
+ # 1–4. For each hop: graphify/graph read → judge → append to loop_trace_*.json
244
+
245
+ # 5a. Early exit (len(history)==1 and answerable)
246
+ deeprefine loop validate --trace-file graphify-out/.deeprefine/loop_trace_<id>.json
247
+ deeprefine loop finish --trace-file graphify-out/.deeprefine/loop_trace_<id>.json
248
+
249
+ # 5b. Refinement path (len(history)>1)
250
+ deeprefine loop validate --trace-file ... --refinement-file graphify-out/.deeprefine/refinement_actions_<id>.txt
251
+ deeprefine apply --trace-file ... --refinement-file graphify-out/.deeprefine/refinement_actions_<id>.txt
252
+ deeprefine loop finish --trace-file ... --refinement-file graphify-out/.deeprefine/refinement_actions_<id>.txt
253
+ ```
254
+
255
+ ---
256
+
257
+ ## `loop_trace_*.json` schema
258
+
259
+ ```json
260
+ {
261
+ "schema_version": 1,
262
+ "mode": "agent-loop",
263
+ "query": "what is dulce?",
264
+ "query_id": "5cdc0798eb59b486",
265
+ "constants": { "max_hops": 4, "increment_hop": 1, "base_top_k": 10,
266
+ "max_triple_num_by_step": [5, 10, 15, 20], "history_horizon_size": 4 },
267
+ "interaction_history": [
268
+ {
269
+ "step": 1,
270
+ "num_hops": 0,
271
+ "base_top_k": 10,
272
+ "query": "what is dulce?",
273
+ "retrieval": {
274
+ "method": "graphify_query",
275
+ "evidence": "graphify query \"what is dulce?\" → …"
276
+ },
277
+ "retrieved_subgraph": [
278
+ {"subject": "A", "relation": "r", "object": "B"}
279
+ ],
280
+ "answerable": false,
281
+ "judgement_raw": "<judge>No</judge>"
282
+ }
283
+ ],
284
+ "error_abduction_reason": "…",
285
+ "error_abduction_raw": "<abduction>…</abduction>",
286
+ "refinement_action_file": "graphify-out/.deeprefine/refinement_actions_<id>.txt",
287
+ "early_exit": false
288
+ }
289
+ ```
290
+
291
+ ---
292
+
293
+ ## Optional: CLI mode (FAISS + API)
294
+
295
+ Only when the user **explicitly** requests `deeprefine refine` / full runtime:
296
+
297
+ ```bash
298
+ conda activate atlastune
299
+ export DEEPREFINE_EMBED_URL=... DEEPREFINE_LLM_URL=...
300
+ deeprefine refine --query "..."
301
+ ```
302
+
303
+ ---
304
+
305
+ ## Paths
306
+
307
+ - `graphify-out/graph.json`
308
+ - `graphify-out/.deeprefine/loop_trace_*.json` (**required**)
309
+ - `graphify-out/.deeprefine/refinement_actions_*.txt`
310
+ - `graphify-out/.deeprefine/refinement_results_*.jsonl`
311
+ - `graphify-out/.deeprefine/graph.json.bak`