hydrag-core 1.0.5__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.
- hydrag_core-1.0.5/.gitignore +19 -0
- hydrag_core-1.0.5/LICENSE +191 -0
- hydrag_core-1.0.5/PKG-INFO +298 -0
- hydrag_core-1.0.5/README.md +256 -0
- hydrag_core-1.0.5/docs/API_REFERENCE.md +395 -0
- hydrag_core-1.0.5/pyproject.toml +85 -0
- hydrag_core-1.0.5/src/hydrag/__init__.py +71 -0
- hydrag_core-1.0.5/src/hydrag/_version.py +1 -0
- hydrag_core-1.0.5/src/hydrag/config.py +138 -0
- hydrag_core-1.0.5/src/hydrag/core.py +619 -0
- hydrag_core-1.0.5/src/hydrag/fusion.py +123 -0
- hydrag_core-1.0.5/src/hydrag/protocols.py +42 -0
- hydrag_core-1.0.5/src/hydrag/py.typed +0 -0
- hydrag_core-1.0.5/src/hydrag/sanitize.py +39 -0
- hydrag_core-1.0.5/src/hydrag/tune.py +684 -0
- hydrag_core-1.0.5/tests/conftest.py +95 -0
- hydrag_core-1.0.5/tests/test_config.py +156 -0
- hydrag_core-1.0.5/tests/test_core.py +576 -0
- hydrag_core-1.0.5/tests/test_fusion.py +243 -0
- hydrag_core-1.0.5/tests/test_sanitize.py +72 -0
- hydrag_core-1.0.5/tests/test_tune.py +182 -0
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding any notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
Copyright 2026 G.Romanchenko <goshka@gmail.com>
|
|
180
|
+
|
|
181
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
182
|
+
you may not use this file except in compliance with the License.
|
|
183
|
+
You may obtain a copy of the License at
|
|
184
|
+
|
|
185
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
186
|
+
|
|
187
|
+
Unless required by applicable law or agreed to in writing, software
|
|
188
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
189
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
190
|
+
See the License for the specific language governing permissions and
|
|
191
|
+
limitations under the License.
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: hydrag-core
|
|
3
|
+
Version: 1.0.5
|
|
4
|
+
Summary: HydRAG — Multi-Headed Retrieval-Augmented Generation with CRAG supervision
|
|
5
|
+
Project-URL: Homepage, https://github.com/studio-playbook/hydrag-core
|
|
6
|
+
Project-URL: Documentation, https://github.com/studio-playbook/hydrag-core#readme
|
|
7
|
+
Project-URL: Repository, https://github.com/studio-playbook/hydrag-core
|
|
8
|
+
Project-URL: Changelog, https://github.com/studio-playbook/hydrag-core/blob/main/CHANGELOG.md
|
|
9
|
+
Project-URL: Issues, https://github.com/studio-playbook/hydrag-core/issues
|
|
10
|
+
Author: Studio Playbook Contributors
|
|
11
|
+
License-Expression: Apache-2.0
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Keywords: crag,hydrag,llm,multi-headed,rag,reciprocal-rank-fusion,retrieval,search
|
|
14
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: Intended Audience :: Science/Research
|
|
17
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
18
|
+
Classifier: Operating System :: OS Independent
|
|
19
|
+
Classifier: Programming Language :: Python :: 3
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
24
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
25
|
+
Classifier: Topic :: Text Processing :: Indexing
|
|
26
|
+
Classifier: Typing :: Typed
|
|
27
|
+
Requires-Python: >=3.10
|
|
28
|
+
Provides-Extra: chromadb
|
|
29
|
+
Requires-Dist: chromadb>=0.4; extra == 'chromadb'
|
|
30
|
+
Provides-Extra: dev
|
|
31
|
+
Requires-Dist: mypy>=1.10; extra == 'dev'
|
|
32
|
+
Requires-Dist: pytest>=8.0; extra == 'dev'
|
|
33
|
+
Requires-Dist: ruff>=0.4; extra == 'dev'
|
|
34
|
+
Provides-Extra: firecrawl
|
|
35
|
+
Requires-Dist: firecrawl-py>=1.0; extra == 'firecrawl'
|
|
36
|
+
Provides-Extra: tune
|
|
37
|
+
Requires-Dist: numpy>=1.24; extra == 'tune'
|
|
38
|
+
Requires-Dist: onnxruntime>=1.17; extra == 'tune'
|
|
39
|
+
Requires-Dist: torch>=2.0; extra == 'tune'
|
|
40
|
+
Requires-Dist: transformers>=4.40; extra == 'tune'
|
|
41
|
+
Description-Content-Type: text/markdown
|
|
42
|
+
|
|
43
|
+
# hydrag-core
|
|
44
|
+
|
|
45
|
+
[](https://pypi.org/project/hydrag-core/)
|
|
46
|
+
[](https://pypi.org/project/hydrag-core/)
|
|
47
|
+
[](https://github.com/studio-playbook/hydrag-core/blob/main/LICENSE)
|
|
48
|
+
|
|
49
|
+
**HydRAG** — Multi-Headed Retrieval-Augmented Generation with CRAG supervision.
|
|
50
|
+
|
|
51
|
+
A standalone, domain-agnostic retrieval pipeline that fuses multiple retrieval
|
|
52
|
+
heads via Reciprocal Rank Fusion (RRF) and uses a Corrective RAG (CRAG)
|
|
53
|
+
supervisor to judge context sufficiency before triggering fallback strategies.
|
|
54
|
+
|
|
55
|
+
## Features
|
|
56
|
+
|
|
57
|
+
- **Multi-headed retrieval** — Five pipeline heads: BM25 fast-path → Primary (hybrid / code-aware) → CRAG supervisor → Semantic fallback → Web fallback
|
|
58
|
+
- **Per-head control** — Enable or disable individual heads at runtime via config or environment variables
|
|
59
|
+
- **Domain-agnostic by default** — `prose` profile works with any corpus (documentation, legal, medical, etc.)
|
|
60
|
+
- **Code-aware opt-in** — `code` profile detects symbols (CamelCase, snake_case, dotted paths) and routes to code-aware search
|
|
61
|
+
- **CRAG supervisor** — LLM-graded context sufficiency with streaming verdict parsing and confidence-gated fast-path skip
|
|
62
|
+
- **CRAG classifier** — Optional distilled DistilBERT binary classifier (<15 ms inference) replaces LLM-based CRAG via teacher-student training pipeline
|
|
63
|
+
- **Pluggable adapters** — Implement the `VectorStoreAdapter` protocol to connect any vector store
|
|
64
|
+
- **Pluggable LLM** — Implement `LLMProvider` protocol or use the built-in `OllamaProvider`
|
|
65
|
+
- **Zero required dependencies** — stdlib-only core; optional extras for ChromaDB, Firecrawl, fine-tuning, and dev tools
|
|
66
|
+
- **Fully typed** — PEP 561 compatible with `py.typed` marker; strict mypy passes
|
|
67
|
+
|
|
68
|
+
## Installation
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
pip install hydrag-core
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
With optional extras:
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
pip install hydrag-core[chromadb] # ChromaDB adapter support
|
|
78
|
+
pip install hydrag-core[firecrawl] # Web fallback via Firecrawl
|
|
79
|
+
pip install hydrag-core[tune] # CRAG classifier fine-tuning (transformers, torch, onnxruntime)
|
|
80
|
+
pip install hydrag-core[dev] # Development tools (pytest, ruff, mypy)
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Quick Start
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
from hydrag import HydRAG, HydRAGConfig
|
|
87
|
+
|
|
88
|
+
# 1. Implement the VectorStoreAdapter protocol for your store
|
|
89
|
+
class MyAdapter:
|
|
90
|
+
def semantic_search(self, query: str, n_results: int = 5) -> list[str]: ...
|
|
91
|
+
def keyword_search(self, query: str, n_results: int = 5) -> list[str]: ...
|
|
92
|
+
def hybrid_search(self, query: str, n_results: int = 5) -> list[str]: ...
|
|
93
|
+
|
|
94
|
+
# 2. Create engine and search
|
|
95
|
+
adapter = MyAdapter()
|
|
96
|
+
engine = HydRAG(adapter)
|
|
97
|
+
results = engine.search("How do I configure logging?")
|
|
98
|
+
|
|
99
|
+
for r in results:
|
|
100
|
+
print(f"[{r.head_origin}] score={r.score:.4f}: {r.text[:80]}")
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Code Profile
|
|
104
|
+
|
|
105
|
+
```python
|
|
106
|
+
config = HydRAGConfig(profile="code")
|
|
107
|
+
engine = HydRAG(adapter, config=config)
|
|
108
|
+
# Queries with symbols (e.g. "HttpRequest class") trigger code-aware retrieval
|
|
109
|
+
results = engine.search("How does `HttpRequest` handle timeouts?")
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### Custom LLM Provider
|
|
113
|
+
|
|
114
|
+
```python
|
|
115
|
+
from hydrag import LLMProvider
|
|
116
|
+
|
|
117
|
+
class MyLLM:
|
|
118
|
+
def generate(self, prompt: str, model: str = "", timeout: int = 30) -> str | None:
|
|
119
|
+
# Your LLM inference here
|
|
120
|
+
...
|
|
121
|
+
|
|
122
|
+
engine = HydRAG(adapter, llm=MyLLM())
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Streaming CRAG Provider
|
|
126
|
+
|
|
127
|
+
For lower-latency CRAG verdict parsing, implement `StreamingLLMProvider`:
|
|
128
|
+
|
|
129
|
+
```python
|
|
130
|
+
from hydrag import StreamingLLMProvider
|
|
131
|
+
|
|
132
|
+
class MyStreamingLLM:
|
|
133
|
+
def generate(self, prompt: str, model: str = "", timeout: int = 30) -> str | None: ...
|
|
134
|
+
def generate_stream(self, prompt: str, model: str = "", timeout: int = 30) -> str | None:
|
|
135
|
+
# Return full response; first token is parsed for early verdict
|
|
136
|
+
...
|
|
137
|
+
|
|
138
|
+
engine = HydRAG(adapter, llm=MyStreamingLLM())
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### Per-Head Control
|
|
142
|
+
|
|
143
|
+
```python
|
|
144
|
+
# Run only BM25 fast-path + primary retrieval (skip CRAG, semantic, web)
|
|
145
|
+
config = HydRAGConfig(
|
|
146
|
+
enable_head_0=True,
|
|
147
|
+
enable_head_1=True,
|
|
148
|
+
enable_head_2_crag=False,
|
|
149
|
+
enable_head_3a_semantic=False,
|
|
150
|
+
enable_head_3b_web=False,
|
|
151
|
+
)
|
|
152
|
+
engine = HydRAG(adapter, config=config)
|
|
153
|
+
results = engine.search("quick keyword lookup")
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### CRAG Classifier (Optional)
|
|
157
|
+
|
|
158
|
+
```python
|
|
159
|
+
from hydrag import tune, CRAGClassifier
|
|
160
|
+
|
|
161
|
+
# Fine-tune a fast classifier from your corpus
|
|
162
|
+
tune(
|
|
163
|
+
adapter=adapter,
|
|
164
|
+
llm=llm,
|
|
165
|
+
output_dir="models/crag_classifier",
|
|
166
|
+
n_samples=500,
|
|
167
|
+
)
|
|
168
|
+
|
|
169
|
+
# Use it — auto-detected when crag_mode="auto" and path is set
|
|
170
|
+
config = HydRAGConfig(
|
|
171
|
+
crag_mode="auto",
|
|
172
|
+
crag_classifier_path="models/crag_classifier",
|
|
173
|
+
)
|
|
174
|
+
engine = HydRAG(adapter, config=config)
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
## Configuration
|
|
178
|
+
|
|
179
|
+
All settings can be set via environment variables (`HYDRAG_` prefix) or passed directly:
|
|
180
|
+
|
|
181
|
+
| Setting | Env Var | Default | Description |
|
|
182
|
+
|---------|---------|---------|-------------|
|
|
183
|
+
| `profile` | `HYDRAG_PROFILE` | `"prose"` | `"prose"` or `"code"` |
|
|
184
|
+
| `embedding_model` | `HYDRAG_EMBEDDING_MODEL` | `"qwen3-embedding-0.6b"` | Embedding model name |
|
|
185
|
+
| `crag_model` | `CRAG_MODEL` | `"qwen3.5-4B"` | Model for CRAG supervisor |
|
|
186
|
+
| `crag_timeout` | `CRAG_TIMEOUT` | `30` | Timeout (seconds) for CRAG calls |
|
|
187
|
+
| `ollama_host` | `OLLAMA_HOST` | `"http://localhost:11434"` | Ollama API endpoint |
|
|
188
|
+
| `enable_web_fallback` | `HYDRAG_ENABLE_WEB_FALLBACK` | `false` | Enable Firecrawl web fallback |
|
|
189
|
+
| `allow_web_on_empty_primary` | `HYDRAG_ALLOW_WEB_ON_EMPTY` | `false` | Allow web fallback when Head 1 returns empty |
|
|
190
|
+
| `allow_markdown_in_web_fallback` | `HYDRAG_ALLOW_MARKDOWN_WEB` | `false` | Preserve markdown in web fallback content |
|
|
191
|
+
| `rrf_k` | `HYDRAG_RRF_K` | `60` | RRF smoothing constant |
|
|
192
|
+
| `min_candidate_pool` | `HYDRAG_MIN_CANDIDATE_POOL` | `8` | Minimum candidates per head |
|
|
193
|
+
| `web_chunk_limit` | `HYDRAG_WEB_CHUNK_LIMIT` | `3000` | Max chars per web chunk after sanitization |
|
|
194
|
+
| `crag_min_relevance` | `HYDRAG_CRAG_MIN_RELEVANCE` | `0.12` | Minimum relevance threshold for CRAG |
|
|
195
|
+
| `crag_context_chunks` | `HYDRAG_CRAG_CONTEXT_CHUNKS` | `5` | Chunks sent to CRAG supervisor |
|
|
196
|
+
| `crag_char_limit` | `HYDRAG_CRAG_CHAR_LIMIT` | `1500` | Per-chunk character limit in CRAG prompt |
|
|
197
|
+
| `enable_fast_path` | `HYDRAG_ENABLE_FAST_PATH` | `true` | Head 0 BM25 fast-path |
|
|
198
|
+
| `fast_path_bm25_threshold` | `HYDRAG_FAST_PATH_BM25_THRESHOLD` | `0.6` | BM25 score threshold for fast-path |
|
|
199
|
+
| `fast_path_confidence_threshold` | `HYDRAG_FAST_PATH_CONFIDENCE_THRESHOLD` | `0.7` | Score threshold to skip CRAG entirely |
|
|
200
|
+
| `crag_stream` | `HYDRAG_CRAG_STREAM` | `true` | Parse first token for early CRAG verdict |
|
|
201
|
+
| `crag_mode` | `HYDRAG_CRAG_MODE` | `"auto"` | `"auto"`, `"llm"`, or `"classifier"` |
|
|
202
|
+
| `crag_classifier_path` | `HYDRAG_CRAG_CLASSIFIER_PATH` | `""` | Path to ONNX classifier model |
|
|
203
|
+
| `enable_head_0` | `HYDRAG_ENABLE_HEAD_0` | `true` | BM25 fast-path head |
|
|
204
|
+
| `enable_head_1` | `HYDRAG_ENABLE_HEAD_1` | `true` | Primary retrieval head |
|
|
205
|
+
| `enable_head_2_crag` | `HYDRAG_ENABLE_HEAD_2_CRAG` | `true` | CRAG supervisor head |
|
|
206
|
+
| `enable_head_3a_semantic` | `HYDRAG_ENABLE_HEAD_3A_SEMANTIC` | `true` | Semantic fallback head |
|
|
207
|
+
| `enable_head_3b_web` | `HYDRAG_ENABLE_HEAD_3B_WEB` | `false` | Web fallback head |
|
|
208
|
+
| `fallback_timeout_s` | `HYDRAG_FALLBACK_TIMEOUT_S` | `30.0` | Timeout for fallback head futures |
|
|
209
|
+
| `rrf_head_weights` | `HYDRAG_RRF_HEAD_WEIGHTS` | *(JSON)* | Per-head RRF weight map |
|
|
210
|
+
|
|
211
|
+
```python
|
|
212
|
+
# From environment
|
|
213
|
+
config = HydRAGConfig.from_env()
|
|
214
|
+
|
|
215
|
+
# Direct
|
|
216
|
+
config = HydRAGConfig(profile="code", rrf_k=100, enable_web_fallback=True)
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
## Adapter Protocol
|
|
220
|
+
|
|
221
|
+
Required methods (must implement all three):
|
|
222
|
+
|
|
223
|
+
```python
|
|
224
|
+
class VectorStoreAdapter(Protocol):
|
|
225
|
+
def semantic_search(self, query: str, n_results: int = 5) -> list[str]: ...
|
|
226
|
+
def keyword_search(self, query: str, n_results: int = 5) -> list[str]: ...
|
|
227
|
+
def hybrid_search(self, query: str, n_results: int = 5) -> list[str]: ...
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
Optional methods (gracefully handled if missing):
|
|
231
|
+
|
|
232
|
+
```python
|
|
233
|
+
def crag_search(self, query: str, n_results: int = 5) -> list[str]: ...
|
|
234
|
+
def graph_search(self, query: str, n_results: int = 5) -> list[str]: ...
|
|
235
|
+
def rewrite_query(self, query: str) -> str: ...
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
## Pipeline Architecture
|
|
239
|
+
|
|
240
|
+
```
|
|
241
|
+
Query → [Profile Router]
|
|
242
|
+
│
|
|
243
|
+
├─ Head 0: BM25 Fast-Path
|
|
244
|
+
│ └─ score ≥ threshold? → early return
|
|
245
|
+
│ └─ score ≥ confidence threshold? → skip CRAG
|
|
246
|
+
│
|
|
247
|
+
├─ Head 1: Primary Retrieval
|
|
248
|
+
│ ├─ prose → hybrid_search()
|
|
249
|
+
│ └─ code + symbols → code-aware (semantic + keyword RRF)
|
|
250
|
+
│
|
|
251
|
+
├─ Head 2: CRAG Supervisor
|
|
252
|
+
│ ├─ classifier (ONNX, <15ms) ← crag_mode="auto"|"classifier"
|
|
253
|
+
│ └─ LLM (streaming verdict) ← crag_mode="auto"|"llm"
|
|
254
|
+
│ ├─ SUFFICIENT → return Head 1 results
|
|
255
|
+
│ └─ INSUFFICIENT ↓
|
|
256
|
+
│
|
|
257
|
+
├─ Head 3a: Semantic Fallback
|
|
258
|
+
│ └─ rewrite + CRAG search + keyword + graph → RRF
|
|
259
|
+
│
|
|
260
|
+
└─ Head 3b: Web Fallback (optional)
|
|
261
|
+
└─ Firecrawl search → sanitize → RRF
|
|
262
|
+
|
|
263
|
+
Final: RRF Fusion across all active heads → list[RetrievalResult]
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
## Development
|
|
267
|
+
|
|
268
|
+
```bash
|
|
269
|
+
# Clone and install in development mode
|
|
270
|
+
git clone https://github.com/studio-playbook/hydrag-core.git
|
|
271
|
+
cd hydrag-core
|
|
272
|
+
pip install -e ".[dev]"
|
|
273
|
+
|
|
274
|
+
# Run tests
|
|
275
|
+
python -m pytest tests/
|
|
276
|
+
|
|
277
|
+
# Lint
|
|
278
|
+
ruff check src/ tests/
|
|
279
|
+
|
|
280
|
+
# Type check
|
|
281
|
+
mypy src/
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
## Versioning
|
|
285
|
+
|
|
286
|
+
HydRAG uses SemVer. Version sources that must stay in sync:
|
|
287
|
+
|
|
288
|
+
- `pyproject.toml` (`[project].version`)
|
|
289
|
+
- `src/hydrag/_version.py` (`__version__`)
|
|
290
|
+
|
|
291
|
+
## Type Checking
|
|
292
|
+
|
|
293
|
+
This package ships a PEP 561 `py.typed` marker. Downstream projects using
|
|
294
|
+
`mypy --strict` will get full type coverage out of the box.
|
|
295
|
+
|
|
296
|
+
## License
|
|
297
|
+
|
|
298
|
+
Apache-2.0 — see [LICENSE](LICENSE) for details.
|