
    Zǻi%                        d dl mZ d dlZd dlmZmZ d dlmZmZ  G d d      Z	 G d de	      Z
 G d	 d
e	      Z G d de	      Z G d de	      Zy)    )annotationsN)AnyOptional)PromptMissingInputErrorPromptMissingPlaceholderErrorc                  t    e Zd ZU dZdZded<   dZded<    e       Zded<   	 	 	 d	 	 	 	 	 	 	 dd	Z	dd
Z
ddZy)PromptTemplatea  This class is used to generate a parameterized prompt. It is defined
    from a string (the template) using the Python format syntax (parameters
    between curly braces `{}`) and a list of required inputs.
    Before sending the instructions to an LLM, call the `format` method that will
    replace parameters with the provided values. If any of the expected inputs is
    missing, a `PromptMissingInputError` is raised.
     strDEFAULT_SYSTEM_INSTRUCTIONSDEFAULT_TEMPLATEz	list[str]EXPECTED_INPUTSNc                    |xs | j                   | _        |xs | j                  | _        |xs | j                  | _        | j                  D ]"  }d| d| j                  vst        d|        y )N{}z"`template` is missing placeholder )r   templater   expected_inputsr   system_instructionsr   )selfr   r   r   es        _/opt/lhia/marcimex/agent/venv/lib/python3.12/site-packages/neo4j_graphrag/generation/prompts.py__init__zPromptTemplate.__init__'   s     !9D$9$9.F$2F2FC4#C#C 	  %% 	AA3bz.38< 	    c                    | j                   D ]  }||vst        d| d        | j                  j                  di |S )NzMissing input '' )r   r   r   format)r   kwargsr   s      r   _formatzPromptTemplate._format9   sP    %% 	FA-s!.DEE	F $t}}##-f--r   c                    |}|j                  t        | j                  |      D ci c]  \  }}||
 c}}        | j                  di |S c c}}w )a1  This method is used to replace parameters with the provided values.
        Parameters must be provided:
        - as kwargs
        - as args if using the same order as in the expected inputs

        Example:

        .. code-block:: python

            prompt_template = PromptTemplate(
                template='''Explain the following concept to {target_audience}:
                Concept: {concept}
                Answer:
                ''',
                expected_inputs=['target_audience', 'concept']
            )
            prompt = prompt_template.format('12 yo children', concept='graph database')
            print(prompt)

            # Result:
            # '''Explain the following concept to 12 yo children:
            # Concept: graph database
            # Answer:
            # '''

        r   )updatezipr   r   )r   argsr   datakvs         r   r   zPromptTemplate.format?   sO    6 c$*>*>&EFdaQTFGt||#d## Gs   A
)NNN)r   Optional[str]r   zOptional[list[str]]r   r'   returnNone)r   r   r(   r   )r#   r   r   r   r(   r   )__name__
__module____qualname____doc__r   __annotations__r   listr   r   r   r   r   r   r   r	   r	      sk     (*)c!%OY' #'/3-1	 - +	
 
$.$r   r	   c                  0     e Zd ZdZdZg dZd fdZ xZS )RagTemplatez4Answer the user question using the provided context.zJContext:
{context}

Examples:
{examples}

Question:
{query_text}

Answer:
)context
query_textexamplesc                (    t         |   |||      S )N)r3   r2   r4   superr   )r   r3   r2   r4   	__class__s       r   r   zRagTemplate.formatn   s    w~Wx~XXr   )r3   r   r2   r   r4   r   r(   r   )r*   r+   r,   r   r   r   r   __classcell__r8   s   @r   r1   r1   _   s$    "X
 <OY Yr   r1   c                  J     e Zd ZdZdgZ	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 d fdZ xZS )Text2CypherTemplateal  
Task: Generate a Cypher statement for querying a Neo4j graph database from a user input.

Schema:
{schema}

Examples (optional):
{examples}

Input:
{query_text}

Do not use any properties or relationships not included in the schema.
Do not include triple backticks ``` or any additional text except the generated Cypher statement in your response.

Cypher query:
r3   c                    |M|rt        j                  dt        d       n.t        |t              rt        j                  dt        d       |}t        |   d|||d|S )NzFBoth 'query' and 'query_text' are provided, 'query_text' will be used.   )
stacklevelz_'query' is deprecated and will be removed in a future version, please use 'query_text' instead.)r3   schemar4   r   )warningswarnDeprecationWarning
isinstancer   r7   r   )r   r@   r4   r3   queryr   r8   s         r   r   zText2CypherTemplate.format   sp     \& 
 E3'u& 
 #
w~ 
!&8
GM
 	
r   )NNr
   N)r@   r'   r4   r'   r3   r   rE   r'   r   r   r(   r   r*   r+   r,   r   r   r   r9   r:   s   @r   r<   r<   r   sf    " $nO !%"&#

  
 	

 
 
 

 
r   r<   c                  <     e Zd ZdZdgZ	 d	 	 	 	 	 	 	 d fdZ xZS )ERExtractionTemplateaz  
You are a top-tier algorithm designed for extracting
information in structured formats to build a knowledge graph.

Extract the entities (nodes) and specify their type from the following text.
Also extract the relationships between these nodes.

Return result as JSON using the following format:
{{"nodes": [ {{"id": "0", "label": "Person", "properties": {{"name": "John"}} }}],
"relationships": [{{"type": "KNOWS", "start_node_id": "0", "end_node_id": "1", "properties": {{"since": "2024-08-01"}} }}] }}

Use only the following node and relationship types (if provided):
{schema}

Assign a unique ID (string) to each node, and reuse it to define relationships.
Do respect the source and target node types for relationship and
the relationship direction.

Make sure you adhere to the following rules to produce valid JSON objects:
- Do not return any additional information other than the JSON in it.
- Omit any backticks around the JSON - simply output the JSON on its own.
- The JSON object must not wrapped into a list - it is its own JSON object.
- Property names must be enclosed in double quotes

Examples:
{examples}

Input text:

{text}
textc                (    t         |   |||      S )N)rI   r@   r4   r6   )r   r@   r4   rI   r8   s       r   r   zERExtractionTemplate.format   s     w~4~JJr   )r
   )r@   zdict[str, Any]r4   r   rI   r   r(   r   rF   r:   s   @r   rH   rH      sO    > hO 	KK K 	K
 
K Kr   rH   c                  :     e Zd ZdZdgZ	 	 d	 	 	 	 	 d fdZ xZS )SchemaExtractionTemplatea
  
You are a top-tier algorithm designed for extracting a labeled property graph schema in
structured formats.

Generate a generalized graph schema based on the input text. Identify key node types,
their relationship types, and property types.

IMPORTANT RULES:
1. Return only abstract schema information, not concrete instances.
2. Use singular PascalCase labels for node types (e.g., Person, Company, Product).
3. Use UPPER_SNAKE_CASE labels for relationship types (e.g., WORKS_FOR, MANAGES).
4. Include property definitions only when the type can be confidently inferred, otherwise omit them.
5. When defining patterns, ensure that every node label and relationship label mentioned exists in your lists of node types and relationship types.
6. Do not create node types that aren't clearly mentioned in the text.
7. Keep your schema minimal and focused on clearly identifiable patterns in the text.
8. UNIQUENESS CONSTRAINTS:
8.1 UNIQUENESS is optional; each node_type may or may not have exactly one uniqueness constraint.
8.2 Only use properties that seem to not have too many missing values in the sample.
8.3 Constraints reference node_types by label and specify which property is unique.
8.4 If a property appears in a uniqueness constraint it MUST also appear in the corresponding node_type as a property.
9. REQUIRED PROPERTIES:
9.1 Mark a property as "required": true if every instance of that node/relationship type MUST have this property (non-nullable).
9.2 Mark a property as "required": false if the property is optional and may be absent on some instances.
9.3 Properties that are identifiers, names, or essential characteristics are typically required.
9.4 Properties that are supplementary information (phone numbers, descriptions, metadata) are typically optional.
9.5 When uncertain, default to "required": false.
9.6 If a property has a UNIQUENESS constraint, it MUST be marked as "required": true.

Accepted property types are: BOOLEAN, DATE, DURATION, FLOAT, INTEGER, LIST,
LOCAL_DATETIME, LOCAL_TIME, POINT, STRING, ZONED_DATETIME, ZONED_TIME.

Return a valid JSON object that follows this precise structure:
{{
  "node_types": [
    {{
      "label": "Person",
      "properties": [
        {{
          "name": "name",
          "type": "STRING",
          "required": true
        }},
        {{
          "name": "email",
          "type": "STRING",
          "required": false
        }}
      ]
    }}
    ...
  ],
  "relationship_types": [
    {{
      "label": "WORKS_FOR"
    }}
    ...
  ],
  "patterns": [
    {{"source": "Person", "relationship": "WORKS_FOR", "target": "Company"}},
    ...
  ],
  "constraints": [
    {{
      "type": "UNIQUENESS",
      "node_type": "Person",
      "property_name": "name"
    }}
    ...
  ]
}}

Examples:
{examples}

Input text:
{text}
rI   c                &    t         |   ||      S )N)rI   r4   r6   )r   rI   r4   r8   s      r   r   zSchemaExtractionTemplate.format  s    
 w~4(~;;r   )r
   r
   )rI   r   r4   r   r(   r   rF   r:   s   @r   rL   rL      sD    LZ hO << < 
	< <r   rL   )
__future__r   rA   typingr   r   neo4j_graphrag.exceptionsr   r   r	   r1   r<   rH   rL   r   r   r   <module>rQ      sZ    #   B$ B$JY. Y&-
. -
`(K> (KVU<~ U<r   