graphragzen.prompt_tuning.entities.generate_entity_relationship_examples

graphragzen.prompt_tuning.entities.generate_entity_relationship_examples(llm, documents, persona, entity_categories, prompt='\nYou are an intelligent assistant that helps a human to analyze the information in a text document and extract a knowledg graph from it. A knowledge graph consists of nodes and their edges (relationships between the nodes in the graph).\n\n-Goal-\nGiven a text document that is potentially relevant to this activity and a list of categories, identify all nodes of those categories from the text and all edges among the identified nodes.\n\n-Steps-\n1. Identify all nodes. For each identified node, extract the following information:\n- name: Name of the node, capitalized\n- category: One of the following categories: [{entity_categories}]\n- description: Comprehensive description of the node\'s attributes and activities\nFormat each node as a JSON with the following format:\n{{"type": "node", "name": <name>, "category": <category>, "description": <description>}}\nfor example: {{"type": "node", "name": "Microsoft", "category": "organization", "description": "Microsoft is a technology company"}}\n\n2. From the nodes identified in step 1, identify all pairs of (source_node, target_node) that are *clearly related* to each other.\nFor each edge, extract the following information:\n- source: name of the source node, as identified in step 1\n- target: name of the target node, as identified in step 1\n- description: explanation as to why you think the source node and the target node are related to each other\n- weight: a numeric score indicating strength of the edge between the source node and target node\nFormat each edge as a JSON with the following format:\n{{"type": "edge", "source": <source>, "target": <target>, "description": <description>, "weight": <weight>}}\nfor eaxmple: {{"type": "edge", "source": "company A", "target": "person A", "description": "company A is currently owned by person A", "weight": 8}}\n\n3. Return output in English as a single list of all JSON entities and relationships identified in steps 1 and 2.\n\n-Real Data-\n######################\nentity_categories: {entity_categories}\ntext: {input_text}\n######################\noutput:\n', example_template='\nExample {n}:\n\nentity_categories: [{entity_categories}]\ntext:\n{input_text}\n------------------------\noutput:\n{output}\n#############################\n\n', max_examples=5, async_llm_calls=False)[source]

Generate a list of entity/relationships examples for use in generating an entity extraction prompt.

Will return in tuple_delimiter format depending

Parameters:
  • llm (LLM)

  • documents (List[str]) – Sample of documents that later will be used to create a graph. You likely want this to be chunks of the whole documents.

  • persona (str) – Relevant to the domain

  • entity_categories (list[str]) – Generated from the documents by generate_entity_categories

  • prompt (str, optional) – Prompt to use for generating entity/relationships examples. Defaults to graphragzen.prompts.prompt_tuning.entity_relationship.ENTITY_RELATIONSHIPS_GENERATION_PROMPT

  • example_template (str, optional) – The template of example extracted entities that will be formatted using, among others, the entity relationships extracted using the prompt. Defaults to graphragzen.prompts.prompt_tuning.entity_relationship.EXAMPLE_EXTRACTION_TEMPLATE

  • max_examples (int, optional) – Number of examples to create. Defaults to 5.

  • async_llm_calls (bool) – If True will call the LLM asynchronously. Only applies to communication with an LLM using OpenAICompatibleClient, in-memory LLM’s loaded using llama-cpp-python will always be called synchronously. Defaults to False.

Returns:

Entity relationship examples

Return type:

list[str]