Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      The Ultimate Guide to Node.js Development Pricing for Enterprises

      July 29, 2025

      Stack Overflow: Developers’ trust in AI outputs is worsening year over year

      July 29, 2025

      Web Components: Working With Shadow DOM

      July 28, 2025

      Google’s new Opal tool allows users to create mini AI apps with no coding required

      July 28, 2025

      I replaced my Samsung OLED TV with this Sony Mini LED model for a week – and didn’t regret it

      July 29, 2025

      I tested the most popular robot mower on the market – and it was a $5,000 crash out

      July 29, 2025

      5 gadgets and accessories that leveled up my gaming setup (including a surprise console)

      July 29, 2025

      Why I’m patiently waiting for the Samsung Z Fold 8 next year (even though the foldable is already great)

      July 29, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Performance Analysis with Laravel’s Measurement Tools

      July 29, 2025
      Recent

      Performance Analysis with Laravel’s Measurement Tools

      July 29, 2025

      Memoization and Function Caching with this PHP Package

      July 29, 2025

      Laracon US 2025 Livestream

      July 29, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Microsoft mysteriously offered a Windows 11 upgrade to this unsupported Windows 10 PC — despite it failing to meet the “non-negotiable” TPM 2.0 requirement

      July 29, 2025
      Recent

      Microsoft mysteriously offered a Windows 11 upgrade to this unsupported Windows 10 PC — despite it failing to meet the “non-negotiable” TPM 2.0 requirement

      July 29, 2025

      With Windows 10’s fast-approaching demise, this Linux migration tool could let you ditch Microsoft’s ecosystem with your data and apps intact — but it’s limited to one distro

      July 29, 2025

      Windows 10 is 10 years old today — let’s look back at 10 controversial and defining moments in its history

      July 29, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Machine Learning»Creating a Knowledge Graph Using an LLM

    Creating a Knowledge Graph Using an LLM

    July 28, 2025

    In this tutorial, we’ll show how to create a Knowledge Graph from an unstructured document using an LLM. While traditional NLP methods have been used for extracting entities and relationships, Large Language Models (LLMs) like GPT-4o-mini make this process more accurate and context-aware. LLMs are especially useful when working with messy, unstructured data. Using Python, Mirascope, and OpenAI’s GPT-4o-mini, we’ll build a simple knowledge graph from a sample medical log.

    Installing the dependencies

    Copy CodeCopiedUse a different Browser
    !pip install "mirascope[openai]" matplotlib networkx 

    OpenAI API Key

    To get an OpenAI API key, visit https://platform.openai.com/settings/organization/api-keys and generate a new key. If you’re a new user, you may need to add billing details and make a minimum payment of $5 to activate API access. Check out the full Codes here.

    Copy CodeCopiedUse a different Browser
    import os
    from getpass import getpass
    os.environ["OPENAI_API_KEY"] = getpass('Enter OpenAI API Key: ')

    Defining Graph Schema

    Before we extract information, we need a structure to represent it. In this step, we define a simple schema for our Knowledge Graph using Pydantic. The schema includes:

    • Node: Represents an entity with an ID, a type (such as “Doctor” or “Medication”), and optional properties.
    • Edge: Represents a relationship between two nodes.
    • KnowledgeGraph: A container for all nodes and edges.

    Check out the full Codes here.

    Copy CodeCopiedUse a different Browser
    from pydantic import BaseModel, Field
    
    class Edge(BaseModel):
        source: str
        target: str
        relationship: str
    
    class Node(BaseModel):
        id: str
        type: str
        properties: dict | None = None
    
    class KnowledgeGraph(BaseModel):
        nodes: list[Node]
        edges: list[Edge]

    Defining the Patient Log

    Now that we have a schema, let’s define the unstructured data we’ll use to generate our Knowledge Graph. Below is a sample patient log, written in natural language. It contains key events, symptoms, and observations related to a patient named Mary. Check out the full Codes here.

    Copy CodeCopiedUse a different Browser
    patient_log = """
    Mary called for help at 3:45 AM, reporting that she had fallen while going to the bathroom. This marks the second fall incident within a week. She complained of dizziness before the fall.
    
    Earlier in the day, Mary was observed wandering the hallway and appeared confused when asked basic questions. She was unable to recall the names of her medications and asked the same question multiple times.
    
    Mary skipped both lunch and dinner, stating she didn't feel hungry. When the nurse checked her room in the evening, Mary was lying in bed with mild bruising on her left arm and complained of hip pain.
    
    Vital signs taken at 9:00 PM showed slightly elevated blood pressure and a low-grade fever (99.8°F). Nurse also noted increased forgetfulness and possible signs of dehydration.
    
    This behavior is similar to previous episodes reported last month.
    """

    Generating the Knowledge Graph

    To transform unstructured patient logs into structured insights, we use an LLM-powered function that extracts a Knowledge Graph. Each patient entry is analyzed to identify entities (like people, symptoms, events) and their relationships (such as “reported”, “has symptom”).

    The generate_kg function is decorated with @openai.call, leveraging the GPT-4o-mini model and the previously defined KnowledgeGraph schema. The prompt clearly instructs the model on how to map the log into nodes and edges. Check out the full Codes here.

    Copy CodeCopiedUse a different Browser
    from mirascope.core import openai, prompt_template
    
    @openai.call(model="gpt-4o-mini", response_model=KnowledgeGraph)
    @prompt_template(
        """
        SYSTEM:
        Extract a knowledge graph from this patient log.
        Use Nodes to represent people, symptoms, events, and observations.
        Use Edges to represent relationships like "has symptom", "reported", "noted", etc.
    
        The log:
        {log_text}
    
        Example:
        Mary said help, I've fallen.
        Node(id="Mary", type="Patient", properties={{}})
        Node(id="Fall Incident 1", type="Event", properties={{"time": "3:45 AM"}})
        Edge(source="Mary", target="Fall Incident 1", relationship="reported")
        """
    )
    def generate_kg(log_text: str) -> openai.OpenAIDynamicConfig:
        return {"log_text": log_text}
    kg = generate_kg(patient_log)
    print(kg)

    Querying the graph

    Once the KnowledgeGraph has been generated from the unstructured patient log, we can use it to answer medical or behavioral queries. We define a function run() that takes a natural language question and the structured graph, and passes them into a prompt for the LLM to interpret and respond. Check out the full Codes here.

    Copy CodeCopiedUse a different Browser
    @openai.call(model="gpt-4o-mini")
    @prompt_template(
        """
        SYSTEM:
        Use the knowledge graph to answer the user's question.
    
        Graph:
        {knowledge_graph}
    
        USER:
        {question}
        """
    )
    def run(question: str, knowledge_graph: KnowledgeGraph): ...
    Copy CodeCopiedUse a different Browser
    question = "What health risks or concerns does Mary exhibit based on her recent behavior and vitals?"
    print(run(question, kg))

    Visualizing the Graph

    At last, we use render_graph(kg) to generate a clear and interactive visual representation of the knowledge graph, helping us better understand the patient’s condition and the connections between observed symptoms, behaviors, and medical concerns.

    Copy CodeCopiedUse a different Browser
    import matplotlib.pyplot as plt
    import networkx as nx
    
    def render_graph(kg: KnowledgeGraph):
        G = nx.DiGraph()
    
        for node in kg.nodes:
            G.add_node(node.id, label=node.type, **(node.properties or {}))
    
        for edge in kg.edges:
            G.add_edge(edge.source, edge.target, label=edge.relationship)
    
        plt.figure(figsize=(15, 10))
        pos = nx.spring_layout(G)
        nx.draw_networkx_nodes(G, pos, node_size=2000, node_color="lightgreen")
        nx.draw_networkx_edges(G, pos, arrowstyle="->", arrowsize=20)
        nx.draw_networkx_labels(G, pos, font_size=12, font_weight="bold")
        edge_labels = nx.get_edge_attributes(G, "label")
        nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_color="blue")
        plt.title("Healthcare Knowledge Graph", fontsize=15)
        plt.show()
    
    render_graph(kg)

    Check out the Codes. All credit for this research goes to the researchers of this project. Also, feel free to follow us on Twitter and don’t forget to join our 100k+ ML SubReddit and Subscribe to our Newsletter.

    The post Creating a Knowledge Graph Using an LLM appeared first on MarkTechPost.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleMicrosoft Edge Launches Copilot Mode to Redefine Web Browsing for the AI Era
    Next Article Build a drug discovery research assistant using Strands Agents and Amazon Bedrock

    Related Posts

    Machine Learning

    How to Evaluate Jailbreak Methods: A Case Study with the StrongREJECT Benchmark

    July 29, 2025
    Machine Learning

    Amazon Develops an AI Architecture that Cuts Inference Time 30% by Activating Only Relevant Neurons

    July 29, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    CVE-2025-45346 – Bacula-web SQL Injection

    Common Vulnerabilities and Exposures (CVEs)

    CVE-2024-46506 – NetAlertX Unauthenticated Command Injection Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    CVE-2023-53139 – Linux Kernel NFC fdp Null Pointer Dereference

    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-28384 – OpenC3 COSMOS Directory Traversal Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Highlights

    X Minecraft Launcher – modern Minecraft launcher

    July 13, 2025

    X Minecraft Launcher is a feature-rich Minecraft launcher to manage resources like modpacks, resource packs,…

    CVE-2025-43016 – JetBrains Rider Unvalidated Archive Unpacking Vulnerability

    April 25, 2025

    CVE-2025-26795 – Apache IoTDB JDBC Driver Information Exposure and Log Injection Vulnerability

    May 14, 2025

    CVE-2024-42655 – NanoMQ MQTT Wildcard Access Control Bypass

    July 29, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.