agent-brain-rag 1.2.0__py3-none-any.whl → 3.0.0__py3-none-any.whl
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.
- {agent_brain_rag-1.2.0.dist-info → agent_brain_rag-3.0.0.dist-info}/METADATA +55 -18
- agent_brain_rag-3.0.0.dist-info/RECORD +56 -0
- {agent_brain_rag-1.2.0.dist-info → agent_brain_rag-3.0.0.dist-info}/WHEEL +1 -1
- {agent_brain_rag-1.2.0.dist-info → agent_brain_rag-3.0.0.dist-info}/entry_points.txt +0 -1
- agent_brain_server/__init__.py +1 -1
- agent_brain_server/api/main.py +146 -45
- agent_brain_server/api/routers/__init__.py +2 -0
- agent_brain_server/api/routers/health.py +85 -21
- agent_brain_server/api/routers/index.py +108 -36
- agent_brain_server/api/routers/jobs.py +111 -0
- agent_brain_server/config/provider_config.py +352 -0
- agent_brain_server/config/settings.py +22 -5
- agent_brain_server/indexing/__init__.py +21 -0
- agent_brain_server/indexing/bm25_index.py +15 -2
- agent_brain_server/indexing/document_loader.py +45 -4
- agent_brain_server/indexing/embedding.py +86 -135
- agent_brain_server/indexing/graph_extractors.py +582 -0
- agent_brain_server/indexing/graph_index.py +536 -0
- agent_brain_server/job_queue/__init__.py +11 -0
- agent_brain_server/job_queue/job_service.py +317 -0
- agent_brain_server/job_queue/job_store.py +427 -0
- agent_brain_server/job_queue/job_worker.py +434 -0
- agent_brain_server/locking.py +101 -8
- agent_brain_server/models/__init__.py +28 -0
- agent_brain_server/models/graph.py +253 -0
- agent_brain_server/models/health.py +30 -3
- agent_brain_server/models/job.py +289 -0
- agent_brain_server/models/query.py +16 -3
- agent_brain_server/project_root.py +1 -1
- agent_brain_server/providers/__init__.py +64 -0
- agent_brain_server/providers/base.py +251 -0
- agent_brain_server/providers/embedding/__init__.py +23 -0
- agent_brain_server/providers/embedding/cohere.py +163 -0
- agent_brain_server/providers/embedding/ollama.py +150 -0
- agent_brain_server/providers/embedding/openai.py +118 -0
- agent_brain_server/providers/exceptions.py +95 -0
- agent_brain_server/providers/factory.py +157 -0
- agent_brain_server/providers/summarization/__init__.py +41 -0
- agent_brain_server/providers/summarization/anthropic.py +87 -0
- agent_brain_server/providers/summarization/gemini.py +96 -0
- agent_brain_server/providers/summarization/grok.py +95 -0
- agent_brain_server/providers/summarization/ollama.py +114 -0
- agent_brain_server/providers/summarization/openai.py +87 -0
- agent_brain_server/runtime.py +2 -2
- agent_brain_server/services/indexing_service.py +39 -0
- agent_brain_server/services/query_service.py +203 -0
- agent_brain_server/storage/__init__.py +18 -2
- agent_brain_server/storage/graph_store.py +519 -0
- agent_brain_server/storage/vector_store.py +35 -0
- agent_brain_server/storage_paths.py +5 -3
- agent_brain_rag-1.2.0.dist-info/RECORD +0 -31
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"""Ollama summarization provider implementation."""
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
from typing import TYPE_CHECKING
|
|
5
|
+
|
|
6
|
+
from openai import AsyncOpenAI
|
|
7
|
+
|
|
8
|
+
from agent_brain_server.providers.base import BaseSummarizationProvider
|
|
9
|
+
from agent_brain_server.providers.exceptions import (
|
|
10
|
+
OllamaConnectionError,
|
|
11
|
+
ProviderError,
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
if TYPE_CHECKING:
|
|
15
|
+
from agent_brain_server.config.provider_config import SummarizationConfig
|
|
16
|
+
|
|
17
|
+
logger = logging.getLogger(__name__)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class OllamaSummarizationProvider(BaseSummarizationProvider):
|
|
21
|
+
"""Ollama summarization provider using local models.
|
|
22
|
+
|
|
23
|
+
Uses OpenAI-compatible API endpoint provided by Ollama.
|
|
24
|
+
|
|
25
|
+
Supports:
|
|
26
|
+
- llama4:scout (Meta's Llama 4 Scout - lightweight, fast)
|
|
27
|
+
- mistral-small3.2 (Mistral Small 3.2 - balanced)
|
|
28
|
+
- qwen3-coder (Alibaba Qwen 3 Coder - code-focused)
|
|
29
|
+
- gemma3 (Google Gemma 3 - efficient)
|
|
30
|
+
- deepseek-coder-v3 (DeepSeek Coder V3)
|
|
31
|
+
- And any other chat model available in Ollama
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
def __init__(self, config: "SummarizationConfig") -> None:
|
|
35
|
+
"""Initialize Ollama summarization provider.
|
|
36
|
+
|
|
37
|
+
Args:
|
|
38
|
+
config: Summarization configuration
|
|
39
|
+
|
|
40
|
+
Note:
|
|
41
|
+
Ollama does not require an API key as it runs locally.
|
|
42
|
+
"""
|
|
43
|
+
max_tokens = config.params.get("max_tokens", 300)
|
|
44
|
+
temperature = config.params.get("temperature", 0.1)
|
|
45
|
+
prompt_template = config.params.get("prompt_template")
|
|
46
|
+
|
|
47
|
+
super().__init__(
|
|
48
|
+
model=config.model,
|
|
49
|
+
max_tokens=max_tokens,
|
|
50
|
+
temperature=temperature,
|
|
51
|
+
prompt_template=prompt_template,
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
# Ollama uses OpenAI-compatible API
|
|
55
|
+
base_url = config.get_base_url() or "http://localhost:11434/v1"
|
|
56
|
+
self._base_url = base_url
|
|
57
|
+
self._client = AsyncOpenAI(
|
|
58
|
+
api_key="ollama", # Ollama doesn't need real key
|
|
59
|
+
base_url=base_url,
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
# Optional parameters
|
|
63
|
+
self._num_ctx = config.params.get("num_ctx", 4096)
|
|
64
|
+
|
|
65
|
+
@property
|
|
66
|
+
def provider_name(self) -> str:
|
|
67
|
+
"""Human-readable provider name."""
|
|
68
|
+
return "Ollama"
|
|
69
|
+
|
|
70
|
+
async def generate(self, prompt: str) -> str:
|
|
71
|
+
"""Generate text based on prompt using Ollama.
|
|
72
|
+
|
|
73
|
+
Args:
|
|
74
|
+
prompt: The prompt to send to Ollama
|
|
75
|
+
|
|
76
|
+
Returns:
|
|
77
|
+
Generated text response
|
|
78
|
+
|
|
79
|
+
Raises:
|
|
80
|
+
OllamaConnectionError: If Ollama is not running
|
|
81
|
+
ProviderError: If generation fails
|
|
82
|
+
"""
|
|
83
|
+
try:
|
|
84
|
+
response = await self._client.chat.completions.create(
|
|
85
|
+
model=self._model,
|
|
86
|
+
max_tokens=self._max_tokens,
|
|
87
|
+
temperature=self._temperature,
|
|
88
|
+
messages=[{"role": "user", "content": prompt}],
|
|
89
|
+
)
|
|
90
|
+
# Extract text from response
|
|
91
|
+
content = response.choices[0].message.content
|
|
92
|
+
return content if content else ""
|
|
93
|
+
except Exception as e:
|
|
94
|
+
if "connection" in str(e).lower() or "refused" in str(e).lower():
|
|
95
|
+
raise OllamaConnectionError(self._base_url, cause=e) from e
|
|
96
|
+
raise ProviderError(
|
|
97
|
+
f"Failed to generate text: {e}",
|
|
98
|
+
self.provider_name,
|
|
99
|
+
cause=e,
|
|
100
|
+
) from e
|
|
101
|
+
|
|
102
|
+
async def health_check(self) -> bool:
|
|
103
|
+
"""Check if Ollama is running and accessible.
|
|
104
|
+
|
|
105
|
+
Returns:
|
|
106
|
+
True if Ollama is healthy, False otherwise
|
|
107
|
+
"""
|
|
108
|
+
try:
|
|
109
|
+
# Try to list models to verify connection
|
|
110
|
+
await self._client.models.list()
|
|
111
|
+
return True
|
|
112
|
+
except Exception as e:
|
|
113
|
+
logger.warning(f"Ollama health check failed: {e}")
|
|
114
|
+
return False
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"""OpenAI (GPT) summarization provider implementation."""
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
from typing import TYPE_CHECKING
|
|
5
|
+
|
|
6
|
+
from openai import AsyncOpenAI
|
|
7
|
+
|
|
8
|
+
from agent_brain_server.providers.base import BaseSummarizationProvider
|
|
9
|
+
from agent_brain_server.providers.exceptions import AuthenticationError, ProviderError
|
|
10
|
+
|
|
11
|
+
if TYPE_CHECKING:
|
|
12
|
+
from agent_brain_server.config.provider_config import SummarizationConfig
|
|
13
|
+
|
|
14
|
+
logger = logging.getLogger(__name__)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class OpenAISummarizationProvider(BaseSummarizationProvider):
|
|
18
|
+
"""OpenAI (GPT) summarization provider.
|
|
19
|
+
|
|
20
|
+
Supports:
|
|
21
|
+
- gpt-5 (most capable)
|
|
22
|
+
- gpt-5-mini (fast, cost-effective)
|
|
23
|
+
- And other OpenAI chat models
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
def __init__(self, config: "SummarizationConfig") -> None:
|
|
27
|
+
"""Initialize OpenAI summarization provider.
|
|
28
|
+
|
|
29
|
+
Args:
|
|
30
|
+
config: Summarization configuration
|
|
31
|
+
|
|
32
|
+
Raises:
|
|
33
|
+
AuthenticationError: If API key is not available
|
|
34
|
+
"""
|
|
35
|
+
api_key = config.get_api_key()
|
|
36
|
+
if not api_key:
|
|
37
|
+
raise AuthenticationError(
|
|
38
|
+
f"Missing API key. Set {config.api_key_env} environment variable.",
|
|
39
|
+
self.provider_name,
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
max_tokens = config.params.get("max_tokens", 300)
|
|
43
|
+
temperature = config.params.get("temperature", 0.1)
|
|
44
|
+
prompt_template = config.params.get("prompt_template")
|
|
45
|
+
|
|
46
|
+
super().__init__(
|
|
47
|
+
model=config.model,
|
|
48
|
+
max_tokens=max_tokens,
|
|
49
|
+
temperature=temperature,
|
|
50
|
+
prompt_template=prompt_template,
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
self._client = AsyncOpenAI(api_key=api_key)
|
|
54
|
+
|
|
55
|
+
@property
|
|
56
|
+
def provider_name(self) -> str:
|
|
57
|
+
"""Human-readable provider name."""
|
|
58
|
+
return "OpenAI"
|
|
59
|
+
|
|
60
|
+
async def generate(self, prompt: str) -> str:
|
|
61
|
+
"""Generate text based on prompt using GPT.
|
|
62
|
+
|
|
63
|
+
Args:
|
|
64
|
+
prompt: The prompt to send to GPT
|
|
65
|
+
|
|
66
|
+
Returns:
|
|
67
|
+
Generated text response
|
|
68
|
+
|
|
69
|
+
Raises:
|
|
70
|
+
ProviderError: If generation fails
|
|
71
|
+
"""
|
|
72
|
+
try:
|
|
73
|
+
response = await self._client.chat.completions.create(
|
|
74
|
+
model=self._model,
|
|
75
|
+
max_tokens=self._max_tokens,
|
|
76
|
+
temperature=self._temperature,
|
|
77
|
+
messages=[{"role": "user", "content": prompt}],
|
|
78
|
+
)
|
|
79
|
+
# Extract text from response
|
|
80
|
+
content = response.choices[0].message.content
|
|
81
|
+
return content if content else ""
|
|
82
|
+
except Exception as e:
|
|
83
|
+
raise ProviderError(
|
|
84
|
+
f"Failed to generate text: {e}",
|
|
85
|
+
self.provider_name,
|
|
86
|
+
cause=e,
|
|
87
|
+
) from e
|
agent_brain_server/runtime.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"""Runtime state management for
|
|
1
|
+
"""Runtime state management for Agent Brain instances."""
|
|
2
2
|
|
|
3
3
|
import json
|
|
4
4
|
import logging
|
|
@@ -15,7 +15,7 @@ logger = logging.getLogger(__name__)
|
|
|
15
15
|
|
|
16
16
|
|
|
17
17
|
class RuntimeState(BaseModel):
|
|
18
|
-
"""Runtime state for
|
|
18
|
+
"""Runtime state for an Agent Brain instance."""
|
|
19
19
|
|
|
20
20
|
schema_version: str = "1.0"
|
|
21
21
|
mode: str = "project" # "project" or "shared"
|
|
@@ -10,6 +10,7 @@ from typing import Any, Callable, Optional, Union
|
|
|
10
10
|
|
|
11
11
|
from llama_index.core.schema import TextNode
|
|
12
12
|
|
|
13
|
+
from agent_brain_server.config import settings
|
|
13
14
|
from agent_brain_server.indexing import (
|
|
14
15
|
BM25IndexManager,
|
|
15
16
|
ContextAwareChunker,
|
|
@@ -18,6 +19,10 @@ from agent_brain_server.indexing import (
|
|
|
18
19
|
get_bm25_manager,
|
|
19
20
|
)
|
|
20
21
|
from agent_brain_server.indexing.chunking import CodeChunk, CodeChunker, TextChunk
|
|
22
|
+
from agent_brain_server.indexing.graph_index import (
|
|
23
|
+
GraphIndexManager,
|
|
24
|
+
get_graph_index_manager,
|
|
25
|
+
)
|
|
21
26
|
from agent_brain_server.models import IndexingState, IndexingStatusEnum, IndexRequest
|
|
22
27
|
from agent_brain_server.storage import VectorStoreManager, get_vector_store
|
|
23
28
|
|
|
@@ -43,6 +48,7 @@ class IndexingService:
|
|
|
43
48
|
chunker: Optional[ContextAwareChunker] = None,
|
|
44
49
|
embedding_generator: Optional[EmbeddingGenerator] = None,
|
|
45
50
|
bm25_manager: Optional[BM25IndexManager] = None,
|
|
51
|
+
graph_index_manager: Optional[GraphIndexManager] = None,
|
|
46
52
|
):
|
|
47
53
|
"""
|
|
48
54
|
Initialize the indexing service.
|
|
@@ -53,12 +59,14 @@ class IndexingService:
|
|
|
53
59
|
chunker: Text chunker instance.
|
|
54
60
|
embedding_generator: Embedding generator instance.
|
|
55
61
|
bm25_manager: BM25 index manager instance.
|
|
62
|
+
graph_index_manager: Graph index manager instance (Feature 113).
|
|
56
63
|
"""
|
|
57
64
|
self.vector_store = vector_store or get_vector_store()
|
|
58
65
|
self.document_loader = document_loader or DocumentLoader()
|
|
59
66
|
self.chunker = chunker or ContextAwareChunker()
|
|
60
67
|
self.embedding_generator = embedding_generator or EmbeddingGenerator()
|
|
61
68
|
self.bm25_manager = bm25_manager or get_bm25_manager()
|
|
69
|
+
self.graph_index_manager = graph_index_manager or get_graph_index_manager()
|
|
62
70
|
|
|
63
71
|
# Internal state
|
|
64
72
|
self._state = IndexingState(
|
|
@@ -382,6 +390,21 @@ class IndexingService:
|
|
|
382
390
|
]
|
|
383
391
|
self.bm25_manager.build_index(nodes)
|
|
384
392
|
|
|
393
|
+
# Step 6: Build graph index if enabled (Feature 113)
|
|
394
|
+
if settings.ENABLE_GRAPH_INDEX:
|
|
395
|
+
if progress_callback:
|
|
396
|
+
await progress_callback(97, 100, "Building graph index...")
|
|
397
|
+
|
|
398
|
+
def graph_progress(current: int, total: int, message: str) -> None:
|
|
399
|
+
# Synchronous callback wrapper
|
|
400
|
+
logger.debug(f"Graph indexing: {message}")
|
|
401
|
+
|
|
402
|
+
triplet_count = self.graph_index_manager.build_from_documents(
|
|
403
|
+
chunks,
|
|
404
|
+
progress_callback=graph_progress,
|
|
405
|
+
)
|
|
406
|
+
logger.info(f"Graph index built with {triplet_count} triplets")
|
|
407
|
+
|
|
385
408
|
# Mark as completed
|
|
386
409
|
self._state.status = IndexingStatusEnum.COMPLETED
|
|
387
410
|
self._state.completed_at = datetime.now(timezone.utc)
|
|
@@ -424,6 +447,9 @@ class IndexingService:
|
|
|
424
447
|
total_code_chunks = self._total_code_chunks
|
|
425
448
|
supported_languages = sorted(self._supported_languages)
|
|
426
449
|
|
|
450
|
+
# Get graph index status (Feature 113)
|
|
451
|
+
graph_status = self.graph_index_manager.get_status()
|
|
452
|
+
|
|
427
453
|
return {
|
|
428
454
|
"status": self._state.status.value,
|
|
429
455
|
"is_indexing": self._state.is_indexing,
|
|
@@ -446,6 +472,14 @@ class IndexingService:
|
|
|
446
472
|
),
|
|
447
473
|
"error": self._state.error,
|
|
448
474
|
"indexed_folders": sorted(self._indexed_folders),
|
|
475
|
+
# Graph index status (Feature 113)
|
|
476
|
+
"graph_index": {
|
|
477
|
+
"enabled": graph_status.enabled,
|
|
478
|
+
"initialized": graph_status.initialized,
|
|
479
|
+
"entity_count": graph_status.entity_count,
|
|
480
|
+
"relationship_count": graph_status.relationship_count,
|
|
481
|
+
"store_type": graph_status.store_type,
|
|
482
|
+
},
|
|
449
483
|
}
|
|
450
484
|
|
|
451
485
|
async def reset(self) -> None:
|
|
@@ -453,6 +487,8 @@ class IndexingService:
|
|
|
453
487
|
async with self._lock:
|
|
454
488
|
await self.vector_store.reset()
|
|
455
489
|
self.bm25_manager.reset()
|
|
490
|
+
# Clear graph index (Feature 113)
|
|
491
|
+
self.graph_index_manager.clear()
|
|
456
492
|
self._state = IndexingState(
|
|
457
493
|
current_job_id="",
|
|
458
494
|
folder_path="",
|
|
@@ -461,6 +497,9 @@ class IndexingService:
|
|
|
461
497
|
error=None,
|
|
462
498
|
)
|
|
463
499
|
self._indexed_folders.clear()
|
|
500
|
+
self._total_doc_chunks = 0
|
|
501
|
+
self._total_code_chunks = 0
|
|
502
|
+
self._supported_languages.clear()
|
|
464
503
|
logger.info("Indexing service reset")
|
|
465
504
|
|
|
466
505
|
|
|
@@ -7,8 +7,13 @@ from typing import Any, Optional
|
|
|
7
7
|
from llama_index.core.retrievers import BaseRetriever
|
|
8
8
|
from llama_index.core.schema import NodeWithScore, QueryBundle, TextNode
|
|
9
9
|
|
|
10
|
+
from agent_brain_server.config import settings
|
|
10
11
|
from agent_brain_server.indexing import EmbeddingGenerator, get_embedding_generator
|
|
11
12
|
from agent_brain_server.indexing.bm25_index import BM25IndexManager, get_bm25_manager
|
|
13
|
+
from agent_brain_server.indexing.graph_index import (
|
|
14
|
+
GraphIndexManager,
|
|
15
|
+
get_graph_index_manager,
|
|
16
|
+
)
|
|
12
17
|
from agent_brain_server.models import (
|
|
13
18
|
QueryMode,
|
|
14
19
|
QueryRequest,
|
|
@@ -69,6 +74,7 @@ class QueryService:
|
|
|
69
74
|
vector_store: Optional[VectorStoreManager] = None,
|
|
70
75
|
embedding_generator: Optional[EmbeddingGenerator] = None,
|
|
71
76
|
bm25_manager: Optional[BM25IndexManager] = None,
|
|
77
|
+
graph_index_manager: Optional[GraphIndexManager] = None,
|
|
72
78
|
):
|
|
73
79
|
"""
|
|
74
80
|
Initialize the query service.
|
|
@@ -77,10 +83,12 @@ class QueryService:
|
|
|
77
83
|
vector_store: Vector store manager instance.
|
|
78
84
|
embedding_generator: Embedding generator instance.
|
|
79
85
|
bm25_manager: BM25 index manager instance.
|
|
86
|
+
graph_index_manager: Graph index manager instance (Feature 113).
|
|
80
87
|
"""
|
|
81
88
|
self.vector_store = vector_store or get_vector_store()
|
|
82
89
|
self.embedding_generator = embedding_generator or get_embedding_generator()
|
|
83
90
|
self.bm25_manager = bm25_manager or get_bm25_manager()
|
|
91
|
+
self.graph_index_manager = graph_index_manager or get_graph_index_manager()
|
|
84
92
|
|
|
85
93
|
def is_ready(self) -> bool:
|
|
86
94
|
"""
|
|
@@ -115,6 +123,10 @@ class QueryService:
|
|
|
115
123
|
results = await self._execute_bm25_query(request)
|
|
116
124
|
elif request.mode == QueryMode.VECTOR:
|
|
117
125
|
results = await self._execute_vector_query(request)
|
|
126
|
+
elif request.mode == QueryMode.GRAPH:
|
|
127
|
+
results = await self._execute_graph_query(request)
|
|
128
|
+
elif request.mode == QueryMode.MULTI:
|
|
129
|
+
results = await self._execute_multi_query(request)
|
|
118
130
|
else: # HYBRID
|
|
119
131
|
results = await self._execute_hybrid_query(request)
|
|
120
132
|
|
|
@@ -318,6 +330,197 @@ class QueryService:
|
|
|
318
330
|
|
|
319
331
|
return fused_nodes
|
|
320
332
|
|
|
333
|
+
async def _execute_graph_query(
|
|
334
|
+
self,
|
|
335
|
+
request: QueryRequest,
|
|
336
|
+
traversal_depth: int = 2,
|
|
337
|
+
) -> list[QueryResult]:
|
|
338
|
+
"""Execute graph-only query using entity relationships.
|
|
339
|
+
|
|
340
|
+
Uses the knowledge graph to find documents related to
|
|
341
|
+
entities mentioned in the query.
|
|
342
|
+
|
|
343
|
+
Args:
|
|
344
|
+
request: Query request.
|
|
345
|
+
traversal_depth: How many hops to traverse in graph.
|
|
346
|
+
|
|
347
|
+
Returns:
|
|
348
|
+
List of QueryResult from graph retrieval.
|
|
349
|
+
|
|
350
|
+
Raises:
|
|
351
|
+
ValueError: If GraphRAG is not enabled.
|
|
352
|
+
"""
|
|
353
|
+
if not settings.ENABLE_GRAPH_INDEX:
|
|
354
|
+
raise ValueError(
|
|
355
|
+
"GraphRAG not enabled. Set ENABLE_GRAPH_INDEX=true in environment."
|
|
356
|
+
)
|
|
357
|
+
|
|
358
|
+
# Query the graph for related entities
|
|
359
|
+
graph_results = self.graph_index_manager.query(
|
|
360
|
+
query_text=request.query,
|
|
361
|
+
top_k=request.top_k,
|
|
362
|
+
traversal_depth=traversal_depth,
|
|
363
|
+
)
|
|
364
|
+
|
|
365
|
+
if not graph_results:
|
|
366
|
+
logger.debug("No graph results found, falling back to vector search")
|
|
367
|
+
return await self._execute_vector_query(request)
|
|
368
|
+
|
|
369
|
+
# Convert graph results to QueryResults
|
|
370
|
+
results: list[QueryResult] = []
|
|
371
|
+
chunk_ids = [
|
|
372
|
+
r.get("source_chunk_id") for r in graph_results if r.get("source_chunk_id")
|
|
373
|
+
]
|
|
374
|
+
|
|
375
|
+
if not chunk_ids:
|
|
376
|
+
# No source chunks in graph, fall back to vector search
|
|
377
|
+
return await self._execute_vector_query(request)
|
|
378
|
+
|
|
379
|
+
# Look up the actual documents from vector store
|
|
380
|
+
for graph_result in graph_results:
|
|
381
|
+
chunk_id = graph_result.get("source_chunk_id")
|
|
382
|
+
if not chunk_id:
|
|
383
|
+
continue
|
|
384
|
+
|
|
385
|
+
# Get document from vector store by ID
|
|
386
|
+
try:
|
|
387
|
+
doc = await self.vector_store.get_by_id(chunk_id)
|
|
388
|
+
if doc:
|
|
389
|
+
result = QueryResult(
|
|
390
|
+
text=doc.get("text", ""),
|
|
391
|
+
source=doc.get("metadata", {}).get(
|
|
392
|
+
"source",
|
|
393
|
+
doc.get("metadata", {}).get("file_path", "unknown"),
|
|
394
|
+
),
|
|
395
|
+
score=graph_result.get("graph_score", 0.5),
|
|
396
|
+
graph_score=graph_result.get("graph_score", 0.5),
|
|
397
|
+
chunk_id=chunk_id,
|
|
398
|
+
source_type=doc.get("metadata", {}).get("source_type", "doc"),
|
|
399
|
+
language=doc.get("metadata", {}).get("language"),
|
|
400
|
+
related_entities=[
|
|
401
|
+
graph_result.get("subject", ""),
|
|
402
|
+
graph_result.get("object", ""),
|
|
403
|
+
],
|
|
404
|
+
relationship_path=[graph_result.get("relationship_path", "")],
|
|
405
|
+
metadata={
|
|
406
|
+
k: v
|
|
407
|
+
for k, v in doc.get("metadata", {}).items()
|
|
408
|
+
if k
|
|
409
|
+
not in ("source", "file_path", "source_type", "language")
|
|
410
|
+
},
|
|
411
|
+
)
|
|
412
|
+
results.append(result)
|
|
413
|
+
except Exception as e:
|
|
414
|
+
logger.debug(f"Failed to retrieve chunk {chunk_id}: {e}")
|
|
415
|
+
continue
|
|
416
|
+
|
|
417
|
+
# If no results from graph, fall back to vector search
|
|
418
|
+
if not results:
|
|
419
|
+
logger.debug("No documents found from graph, falling back to vector search")
|
|
420
|
+
return await self._execute_vector_query(request)
|
|
421
|
+
|
|
422
|
+
return results[: request.top_k]
|
|
423
|
+
|
|
424
|
+
async def _execute_multi_query(self, request: QueryRequest) -> list[QueryResult]:
|
|
425
|
+
"""Execute multi-retrieval query combining vector, BM25, and graph.
|
|
426
|
+
|
|
427
|
+
Uses Reciprocal Rank Fusion (RRF) to combine results from
|
|
428
|
+
all three retrieval methods.
|
|
429
|
+
|
|
430
|
+
Args:
|
|
431
|
+
request: Query request.
|
|
432
|
+
|
|
433
|
+
Returns:
|
|
434
|
+
List of QueryResult with combined scores.
|
|
435
|
+
"""
|
|
436
|
+
# Get results from each retriever
|
|
437
|
+
vector_results = await self._execute_vector_query(request)
|
|
438
|
+
bm25_results = await self._execute_bm25_query(request)
|
|
439
|
+
|
|
440
|
+
# Get graph results if enabled
|
|
441
|
+
graph_results: list[QueryResult] = []
|
|
442
|
+
if settings.ENABLE_GRAPH_INDEX:
|
|
443
|
+
try:
|
|
444
|
+
graph_results = await self._execute_graph_query(request)
|
|
445
|
+
except ValueError:
|
|
446
|
+
pass # Graph not enabled, skip
|
|
447
|
+
|
|
448
|
+
# Apply Reciprocal Rank Fusion
|
|
449
|
+
rrf_k = settings.GRAPH_RRF_K # Typical value is 60
|
|
450
|
+
combined_scores: dict[str, dict[str, Any]] = {}
|
|
451
|
+
|
|
452
|
+
# Process vector results
|
|
453
|
+
for rank, result in enumerate(vector_results):
|
|
454
|
+
chunk_id = result.chunk_id
|
|
455
|
+
rrf_score = 1.0 / (rrf_k + rank + 1)
|
|
456
|
+
if chunk_id not in combined_scores:
|
|
457
|
+
combined_scores[chunk_id] = {
|
|
458
|
+
"result": result,
|
|
459
|
+
"rrf_score": 0.0,
|
|
460
|
+
"vector_rank": None,
|
|
461
|
+
"bm25_rank": None,
|
|
462
|
+
"graph_rank": None,
|
|
463
|
+
}
|
|
464
|
+
combined_scores[chunk_id]["rrf_score"] += rrf_score
|
|
465
|
+
combined_scores[chunk_id]["vector_rank"] = rank + 1
|
|
466
|
+
|
|
467
|
+
# Process BM25 results
|
|
468
|
+
for rank, result in enumerate(bm25_results):
|
|
469
|
+
chunk_id = result.chunk_id
|
|
470
|
+
rrf_score = 1.0 / (rrf_k + rank + 1)
|
|
471
|
+
if chunk_id not in combined_scores:
|
|
472
|
+
combined_scores[chunk_id] = {
|
|
473
|
+
"result": result,
|
|
474
|
+
"rrf_score": 0.0,
|
|
475
|
+
"vector_rank": None,
|
|
476
|
+
"bm25_rank": None,
|
|
477
|
+
"graph_rank": None,
|
|
478
|
+
}
|
|
479
|
+
combined_scores[chunk_id]["rrf_score"] += rrf_score
|
|
480
|
+
combined_scores[chunk_id]["bm25_rank"] = rank + 1
|
|
481
|
+
|
|
482
|
+
# Process graph results
|
|
483
|
+
for rank, result in enumerate(graph_results):
|
|
484
|
+
chunk_id = result.chunk_id
|
|
485
|
+
rrf_score = 1.0 / (rrf_k + rank + 1)
|
|
486
|
+
if chunk_id not in combined_scores:
|
|
487
|
+
combined_scores[chunk_id] = {
|
|
488
|
+
"result": result,
|
|
489
|
+
"rrf_score": 0.0,
|
|
490
|
+
"vector_rank": None,
|
|
491
|
+
"bm25_rank": None,
|
|
492
|
+
"graph_rank": None,
|
|
493
|
+
}
|
|
494
|
+
combined_scores[chunk_id]["rrf_score"] += rrf_score
|
|
495
|
+
combined_scores[chunk_id]["graph_rank"] = rank + 1
|
|
496
|
+
# Preserve graph-specific fields
|
|
497
|
+
if result.related_entities:
|
|
498
|
+
combined_scores[chunk_id][
|
|
499
|
+
"result"
|
|
500
|
+
].related_entities = result.related_entities
|
|
501
|
+
if result.relationship_path:
|
|
502
|
+
combined_scores[chunk_id][
|
|
503
|
+
"result"
|
|
504
|
+
].relationship_path = result.relationship_path
|
|
505
|
+
if result.graph_score:
|
|
506
|
+
combined_scores[chunk_id]["result"].graph_score = result.graph_score
|
|
507
|
+
|
|
508
|
+
# Sort by RRF score and take top_k
|
|
509
|
+
sorted_results = sorted(
|
|
510
|
+
combined_scores.values(),
|
|
511
|
+
key=lambda x: x["rrf_score"],
|
|
512
|
+
reverse=True,
|
|
513
|
+
)
|
|
514
|
+
|
|
515
|
+
# Update scores and return
|
|
516
|
+
final_results: list[QueryResult] = []
|
|
517
|
+
for data in sorted_results[: request.top_k]:
|
|
518
|
+
result = data["result"]
|
|
519
|
+
result.score = data["rrf_score"]
|
|
520
|
+
final_results.append(result)
|
|
521
|
+
|
|
522
|
+
return final_results
|
|
523
|
+
|
|
321
524
|
async def get_document_count(self) -> int:
|
|
322
525
|
"""
|
|
323
526
|
Get the total number of indexed documents.
|
|
@@ -1,5 +1,21 @@
|
|
|
1
|
-
"""Storage layer for vector database operations."""
|
|
1
|
+
"""Storage layer for vector database and graph operations."""
|
|
2
2
|
|
|
3
|
+
from .graph_store import (
|
|
4
|
+
GraphStoreManager,
|
|
5
|
+
get_graph_store_manager,
|
|
6
|
+
initialize_graph_store,
|
|
7
|
+
reset_graph_store_manager,
|
|
8
|
+
)
|
|
3
9
|
from .vector_store import VectorStoreManager, get_vector_store, initialize_vector_store
|
|
4
10
|
|
|
5
|
-
__all__ = [
|
|
11
|
+
__all__ = [
|
|
12
|
+
# Vector store
|
|
13
|
+
"VectorStoreManager",
|
|
14
|
+
"get_vector_store",
|
|
15
|
+
"initialize_vector_store",
|
|
16
|
+
# Graph store (Feature 113)
|
|
17
|
+
"GraphStoreManager",
|
|
18
|
+
"get_graph_store_manager",
|
|
19
|
+
"initialize_graph_store",
|
|
20
|
+
"reset_graph_store_manager",
|
|
21
|
+
]
|