Add search interface to lightrag.
This commit is contained in:
parent
14fe3e4387
commit
dc7ce98c7e
2 changed files with 52 additions and 0 deletions
|
|
@ -78,6 +78,7 @@ from .operate import (
|
||||||
extract_entities,
|
extract_entities,
|
||||||
merge_nodes_and_edges,
|
merge_nodes_and_edges,
|
||||||
kg_query,
|
kg_query,
|
||||||
|
kg_search,
|
||||||
naive_query,
|
naive_query,
|
||||||
_rebuild_knowledge_from_chunks,
|
_rebuild_knowledge_from_chunks,
|
||||||
)
|
)
|
||||||
|
|
@ -2105,6 +2106,55 @@ class LightRAG:
|
||||||
await self._query_done()
|
await self._query_done()
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
def search(
|
||||||
|
self,
|
||||||
|
query: str,
|
||||||
|
param: QueryParam = QueryParam(),
|
||||||
|
) -> dict[str, Any]:
|
||||||
|
"""
|
||||||
|
Synchronous search API: returns structured retrieval results without LLM generation.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
query: Query text.
|
||||||
|
param: Query parameters (reuse the same QueryParam as query/aquery).
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
dict[str, Any]: {"entities": [...], "relationships": [...], "chunks": [...], "metadata": {...}}
|
||||||
|
"""
|
||||||
|
loop = always_get_an_event_loop()
|
||||||
|
return loop.run_until_complete(self.asearch(query, param))
|
||||||
|
|
||||||
|
async def asearch(
|
||||||
|
self,
|
||||||
|
query: str,
|
||||||
|
param: QueryParam = QueryParam(),
|
||||||
|
) -> dict[str, Any]:
|
||||||
|
"""
|
||||||
|
Asynchronous search API: calls kg_search and returns retrieval-only results
|
||||||
|
(entities, relationships, and merged chunks).
|
||||||
|
|
||||||
|
Args:
|
||||||
|
query: Query text.
|
||||||
|
param: Query parameters (reuse the same QueryParam as query/aquery).
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
dict[str, Any]: Structured search result
|
||||||
|
"""
|
||||||
|
global_config = asdict(self)
|
||||||
|
response = await kg_search(
|
||||||
|
query.strip(),
|
||||||
|
self.chunk_entity_relation_graph,
|
||||||
|
self.entities_vdb,
|
||||||
|
self.relationships_vdb,
|
||||||
|
self.text_chunks,
|
||||||
|
param,
|
||||||
|
global_config,
|
||||||
|
hashing_kv=self.llm_response_cache,
|
||||||
|
chunks_vdb=self.chunks_vdb,
|
||||||
|
)
|
||||||
|
await self._query_done()
|
||||||
|
return response
|
||||||
|
|
||||||
async def _query_done(self):
|
async def _query_done(self):
|
||||||
await self.llm_response_cache.index_done_callback()
|
await self.llm_response_cache.index_done_callback()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2814,6 +2814,7 @@ async def kg_search(
|
||||||
knowledge_graph_inst,
|
knowledge_graph_inst,
|
||||||
entities_vdb,
|
entities_vdb,
|
||||||
relationships_vdb,
|
relationships_vdb,
|
||||||
|
text_chunks_db,
|
||||||
query_param,
|
query_param,
|
||||||
chunks_vdb,
|
chunks_vdb,
|
||||||
)
|
)
|
||||||
|
|
@ -3118,6 +3119,7 @@ async def _build_query_context(
|
||||||
knowledge_graph_inst,
|
knowledge_graph_inst,
|
||||||
entities_vdb,
|
entities_vdb,
|
||||||
relationships_vdb,
|
relationships_vdb,
|
||||||
|
text_chunks_db,
|
||||||
query_param,
|
query_param,
|
||||||
chunks_vdb,
|
chunks_vdb,
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue