graph_prompt.py


Overview

graph_prompt.py is a configuration and prompt definition file designed to support entity and relationship extraction from text documents for Knowledge Graph or Retrieval-Augmented Generation (RAG) systems. It primarily contains a collection of structured prompt templates and example data that guide language models or extraction algorithms to identify entities, their attributes, and relationships from unstructured text.

The file defines:

This file does not contain executable logic or functions but serves as a centralized repository of prompt templates and examples facilitating consistent and accurate information extraction workflows.


Detailed Explanation of Contents

Constants

Key Prompt Templates

1. entity_extraction

A comprehensive natural language prompt guiding the extraction of entities and relationships from text. It instructs the extraction system to:

Parameters (placeholders expected to be formatted at runtime):

Usage example snippet:

prompt_text = PROMPTS["entity_extraction"].format(
    language="English",
    entity_types="person, organization, location",
    tuple_delimiter=PROMPTS["DEFAULT_TUPLE_DELIMITER"],
    record_delimiter=PROMPTS["DEFAULT_RECORD_DELIMITER"],
    completion_delimiter=PROMPTS["DEFAULT_COMPLETION_DELIMITER"],
    examples=PROMPTS["entity_extraction_examples"][0],
    input_text="Sample input text here..."
)

2. entity_extraction_examples

A list of string examples demonstrating expected entity and relationship extraction output formats given sample texts. Each example includes:

These examples serve as:


3. summarize_entity_descriptions

A prompt template for summarizing or consolidating multiple entity descriptions into a single coherent summary, resolving contradictions if any.

Parameters:

Usage context:
Useful for merging fragmented or multiple partial entity descriptions into one comprehensive summary.


4. entity_continue_extraction

A prompt for continuing entity and relationship extraction when the initial extraction missed some entities. It repeats the original extraction instructions but focuses on finding only the missing entities and relationships.

Usage context:
Used in iterative extraction workflows to improve recall by prompting the model to find overlooked entities.


5. entity_if_loop_extraction

A short prompt asking the model to answer "YES" or "NO" about whether any entities are still missing after extraction.

Usage context:
Acts as a validation or stopping criterion in iterative extraction loops.


6. fail_response

A fixed prompt string for cases where the system cannot answer a query due to lack of context or capability.


7. rag_response

A detailed prompt template for generating responses in Retrieval-Augmented Generation (RAG) systems. It instructs the assistant to:

Parameters:


8. keywords_extraction

A prompt template instructing the system to extract high-level and low-level keywords from a user query for use in RAG systems to improve document retrieval relevance.

Output format: Must be a valid JSON object containing:

{
  "high_level_keywords": [ ... ],
  "low_level_keywords": [ ... ]
}

with strict rules about content and formatting.


9. keywords_extraction_examples

A list of example keyword extraction inputs and outputs demonstrating the expected JSON format and keyword types.


10. naive_rag_response

A prompt template similar to rag_response but focused solely on document chunks (no knowledge graph) for generating concise answers.


Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Visual Diagram: Class and Data Structure Overview

Since this file primarily contains prompt templates stored in a dictionary without classes or functions, the most appropriate visualization is a flowchart showing the main prompts and their relationships in the extraction and response workflows.

flowchart TD
    A[PROMPTS dictionary] --> B["entity_extraction"]
    A --> C["entity_extraction_examples"]
    A --> D["summarize_entity_descriptions"]
    A --> E["entity_continue_extraction"]
    A --> F["entity_if_loop_extraction"]
    A --> G["fail_response"]
    A --> H["rag_response"]
    A --> I["keywords_extraction"]
    A --> J["keywords_extraction_examples"]
    A --> K["naive_rag_response"]

    B --> C  %% Examples used in entity_extraction
    E --> B  %% Continuation prompt relates to main extraction
    F --> E  %% Loop check relates to continuation prompt
    H --> I  %% RAG response uses keywords extraction indirectly

Summary

graph_prompt.py is a foundational prompt configuration file that empowers entity and relationship extraction pipelines, knowledge graph construction, and RAG-based question answering systems through a carefully curated set of natural language prompts and examples. It standardizes how extraction instructions are conveyed to language models and how output should be formatted, facilitating robust and interpretable downstream processing.