Feature: Add /query/data API Endpoint for Raw Retrieval Data - #2100
Merged
Conversation
…cts (entities, relations, chunks) in their raw data format.
• Skip cache when only_need_prompt is set • Update only_need_context condition logic • Prevent cache bypass in prompt-only mode
• Remove get_conversation_turns utility • Pass history_messages to LLM directly • Clean up prompt template formatting
- Replace json.dumps with line-by-line format - Apply to entities, relations, text units - Update truncation key functions - Maintain ensure_ascii=False setting - Improve context readability
- Delete history section from prompt
- Simplify user query response format
- Remove {history} placeholder variable
• Clarify max_tokens vs max_completion_tokens • Add Gemini exception note • Update parameter descriptions • Add new completion tokens option
…parison - Implement description length comparison in gleaning merge logic (extract_entities) - Apply same logic to knowledge graph reconstruction (_rebuild_knowledge_from_chunks) - Prioritize entities/relationships with longer descriptions for better quality - Use list() instead of extend() for performance optimization when replacing
* Support `( and `( start patterns * Support )` and )` end patterns * Graceful fallback to warning logs * Strip 2 chars for backtick variants * Maintain existing bracket logic
- Expand bracket tolerance to support additional characters: < > " ' - Implement symmetric handling for both leading and trailing characters - Replace simple string matching with robust regex-based pattern detection - Maintain full backward compatibility with existing bracket formats
…elimiter to <|SEP|> • Add robust delimiter fixing logic • Update prompts for single-line format
• Consolidate 6 regex patterns into 3 • More efficient pattern matching • Clearer comments and examples • Same functionality, less code • Better maintainability
- Extract regex fixes to utils module - Add case-insensitive delimiter handling
- Track timestamps for cache hits/misses - Add timestamp to entity/relationship objects - Sort descriptions by timestamp order - Preserve temporal ordering in merges
…port both format for safty
…|#: -> <|#|> pattern
• Fix completion_delimiter reference bug • Add case check before lowercase conversion • Improve delimiter corruption handling • Optimize redundant processing logic
• Fix typo in error log message • Clarify format requirements in prompts • Make extraction instructions clearer • Improve user prompt consistency
• Fix typo in error log message • Clarify format requirements in prompts • Make extraction instructions clearer • Improve user prompt consistency
- Add debug logs for malformed records - Fix regex for consecutive delimiters - Handle missing closing brackets
* Remove trailing whitespace * Fix function signature ellipsis style
- Add debug logs for malformed records - Fix regex for consecutive delimiters - Handle missing closing brackets
- Add QueryDataResponse model - Implement /query/data endpoint - Add aquery_data method to LightRAG - Return entities, relationships, chunks
- Remove processing_info generation from _convert_to_user_format function - Move all metadata generation (keywords, processing_info) to kg_query and naive_query functions - Simplify _convert_to_user_format to focus only on data format conversion
…preserved in aquery_data responses
2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add /query/data API Endpoint for Raw Retrieval Data
Overview
This update introduces a new API endpoint,
/query/data, designed to return raw retrieval data from the RAG process without LLM generation. This feature is crucial for data analysis, and use cases that require direct access to knowledge graph retrieval results (entities, relationships, text chunks).By decoupling data retrieval from LLM generation, developers can gain clearer insights into the retrieval capabilities of the knowledge graph and perform fine-grained processing on the results.
Key Changes
1. New
/query/dataAPI Endpointlightrag/api/routers/query_routes.pyQueryDataResponsePydantic model to define the structured data format for the response, includingentities,relationships,chunks, andmetadata./query/datathat calls therag.aquery_datamethod to directly return the retrieved raw data without passing it to an LLM.2. Introduction of the
aquery_dataCore Methodlightrag/lightrag.pyasearchmethod toaquery_datato clarify its function: performing asynchronous data queries only.aquery_datamethod now invokes different query strategies (e.g.,kg_query,naive_query) based on theparam.modequery parameter and uses thereturn_raw_data=Trueargument to fetch the final data before LLM processing.3. Extended Functionality in
kg_queryandnaive_querylightrag/operate.pykg_queryandnaive_queryusing@overload. Whenreturn_raw_data=True, these functions return a dictionary containing the raw data instead of a generated text response.kg_query, when raw data is requested, the process returns early after_build_query_context, bypassing the LLM call.4. Optimized Data Handling Process
lightrag/operate.py_apply_token_truncation, data copies are now created for processing to prevent accidental modification of original data during truncation, enhancing code robustness._merge_all_chunksfunction has been simplified for a more focused and clear responsibility.How to Use
Send a POST request to the
/query/dataendpoint, compatible with the/queryendpoint. The response will be a JSON object containing the following fields:entities: A list of entities retrieved from the knowledge graph.relationships: A list of retrieved relationships.chunks: A list of relevant text chunks retrieved from documents.metadata: Metadata about the query, including the query mode, keywords used, etc.