Our Goals
- Understand what ontologies are and why they matter for representing domain knowledge in IT systems and AI.
- Get hands-on with building an ontology using a legal use case.
- Visualize the ontology we create.
- Develop an intuition for what can be done next, in terms of future steps involving ontologies.
Introduction to Ontologies: Organizing Knowledge for IT Systems (and AI)
Imagine you had to explain a complex domain, like the basics of contract law, to a computer. You wouldn't just list random laws. You would define key concepts (like 'Contract', 'Contracting Party', 'Obligation') and the relationships that connect them (a 'Contracting Party' signs a 'Contract', a 'Contract' creates an 'Obligation').
An ontology is precisely that: a formal, structured way of representing the knowledge of a specific domain. It explicitly defines:
- Concepts (or Classes): The main types of elements that exist in the domain (e.g., 'Contract', 'Law', 'Legal Article', 'Court').
- Properties (or Attributes): The characteristics of these concepts (e.g., a 'Contract' has a 'signing date', a 'Law' has a 'number').
- Relationships: How concepts are connected to each other (e.g., a 'Contracting Party' is bound by a 'Contract'; a 'Legal Article' is part of a 'Law').
- Rules and Constraints: Sometimes axioms or logical rules (e.g., a 'Contract' must have at least two 'Contracting Parties').
Think of it as creating an ultra-detailed dictionary and relational map for a given subject (in this case, law), designed so that a computer can understand and use it. It provides a shared vocabulary and framework, ensuring that everyone (whether flesh-and-blood or silicon-based AI) is talking about the same thing, in the same way, without ambiguity.
Why Use Ontologies in Automated Systems?
Even before the current AI boom, ontologies were valuable because they enable:
- Providing Structure: Computing needs structured data formatted in specific ways. Ontologies transform real-world knowledge, which is often unstructured and chaotic, into organized information that a system can process.
- Enabling Automated Reasoning: By defining relationships (for example, if a 'Sales Contract' is a type of 'Contract'), a system can deduce new facts without being explicitly told (e.g., if this document is a 'Sales Contract', then it is also a 'Contract' and must comply with its general rules). This enables powerful inference capabilities while keeping computational requirements under control.
- Ensuring Consistency: They enforce a common understanding and terminology across different parts of a system, or even between different collaborating systems.
- Improving Search and Data Integration: They make it easier to find precise information and correctly combine data from different sources.
Why Are Ontologies Particularly Interesting in the Age of LLMs?
Large Language Models (LLMs), such as ChatGPT, Claude, or Gemini, are incredibly good at understanding and generating text. However, they also have weaknesses:
- "Hallucinations": They can sometimes generate information that sounds plausible but is false or inconsistent.
- Lack of Deep Domain Understanding: Although trained on enormous volumes of data, they may lack the specific, structured, and nuanced knowledge of a specialized domain like law.
- Consistency Issues: They may describe the same legal concept differently at various times.
- Limited Explainability: It is often difficult to know why an LLM gave a specific answer.
This is where ontologies become extremely useful partners for LLMs:
- Grounding LLMs in Reality: Much like a stake supporting a tree, ontologies can serve as a "knowledge backbone" or "fact-checker" for LLMs. By coupling LLM processing with a structured ontology representing reliable legal knowledge, we can reduce errors and ensure that results respect the established facts and rules of the domain.
- Enhancing Legal Reasoning: Ontologies provide explicit logical relationships that LLMs might not easily grasp from text alone. Combining the linguistic capabilities of the LLM with the logical structure of the ontology enables more robust and reliable reasoning in any given domain, such as law.
- Increasing Consistency and Precision: An ontology ensures that domain concepts and relationships are used consistently by the LLM, in accordance with predefined definitions.
- Adding Explainability: When an LLM's output is guided or verified by an ontology, it becomes easier to trace the reasoning back to the underlying structure, making the AI process more transparent (explainable AI).
- Integrating Domain Expertise: They offer a practical way to inject deep, validated business expertise into powerful but generalist LLM systems. This paves the way for verticalized agents, for instance for private companies looking to scale their proprietary expertise (particularly law firms).
In short, ontologies bring the structure, consistency, and factual foundation that can make LLMs more reliable, accurate, and trustworthy, especially when applied to complex and specialized tasks (such as in law) within automated systems. They bridge the gap between the broad language understanding of LLMs and the deep, structured knowledge required for many real-world applications, knowledge that is always hard-won by very human experts.
Hands-On: Let's Build Our Own Ontology
Let's stay in the legal domain: imagine we want to represent legal reasoning through ontologies based on rulings from the Cour de cassation (France's highest court of appeal). Here are the development steps we would need to follow:
- Read the PDF document containing the court ruling and load it into our program
- Define a model of the information we want to extract from this ruling
- Have an LLM read the content of the ruling and extract a structured output matching our model's fields
- Pass this structured output as a parameter to a function that will create or update the ontology from this formatted object
- Run another function to visualize the ontology
Ingesting the PDF Document
Here we use the langchain-community package, which provides ready-to-use methods for reading and extracting text from each page of a PDF document.
Defining the Model for What We Want to Surface from a Ruling
For our ontology, we decided it was relevant to extract the following from a ruling:
- The facts (a list of strings, with one item per fact)
- The final decision of the court
- The reasoning of the court
- The legal principles or rules (laws or other regulations) applied (list of strings)
- The unique identifier of the ruling, which we'll use to distinguish elements (rulings) in our ontology
Generating Structured Instances of Our Model Using an LLM
The idea here is to:
- Give the LLM our ruling as input
- Guide its extraction with a prompt via
LangChain
- Constrain it to give us output in the desired format (our
CourtRulingAnalysis model)
Let's start with this prompt. The goal is to reuse the fields from our model, but also to guide the LLM further by specifying that we're interested in extracting generic facts and reasoning rather than the specifics of case X or Y.
This function creates a LangChain chain that applies both the prompt we wrote and the output formatting when calling the LLM. The format_docs function you see simply concatenates the PDF pages of the ruling so that the context isn't fragmented.
Finally, we have the function that wraps all this logic and lets us perform every step, from document ingestion and concatenation, to applying the prompt, all the way to generating the structured model output.
We are now ready to pass the returned object to our method that will either create or update our ontology.
Creating or Updating an Ontology
We will use the owlready2 package, which makes it easy to manipulate ontologies in Python. The idea is as follows: we create an ontology and save it to a file if it doesn't exist, or we update it with a new ruling if the file already exists.
This create-or-load logic runs at the beginning of our function, and then, further in the same function:
... we define the concepts (classes) and the relationships between concepts in our ontology using the syntax provided by owlready2. For example, we declare that a ruling can have a fact by arbitrarily naming the relationship, which should always start with a verb to better differentiate it from the concepts making up the ontology.
Similarly, we also define data properties here, meaning the attributes of field X of a given entity. Since we use strings everywhere here, each entity has a hasText attribute, plain and simple.
Now that we have defined the structure of our ontology, we are ready, still within the same function, to add items (rulings) following this format:
Here, for each ruling, we create an instance of CourtRuling, which we hydrate with the facts, principles, decision, and reasoning. Finally, we persist our ontology to disk. Let's visualize the result!
Visualizing the Ontology
This function uses the Python graphviz library (whose dependencies must also be installed on your system) to create a visualization, in the form of a directed graph going from left to right, of our ontology. If your result is pixelated, try increasing the DPI. This gives us, for example, the following result:
We now have a representation that is:
- Visual, thus aiding human understanding
- Formatted and subject to inference by a machine
What Can We Do Now?
Now that we've gotten started with creating and visualizing ontologies using LLMs, here are some practical and concrete applications we could explore, armed with this new knowledge.
Our legal ontology captures the relationships between case law, facts, principles, decision rationales, and reasoning. We could deliver real added value with features like these:
Precedent-Based Legal Research
- Case Similarity Analysis: Find cases with facts similar to a current legal problem.
- Legal Principle Mapping: Track how specific legal principles have been applied across different cases.
- Contradictory Decision Identification: Discover cases where similar facts led to different outcomes.
Legal Reasoning Assistance
- Argument Building: Construct legal arguments by identifying cases where specific principles prevailed.
- Reasoning Pattern Analysis: Identify common reasoning patterns used by judges for specific types of cases.
- Counter-Argument Anticipation: Predict opposing arguments based on historical patterns.
Legal Education and Training
- Case Study Teaching: Create interactive case studies showing the relationships between facts, principles, and outcomes.
- Legal Reasoning Visualization: Help law students understand how facts connect to legal principles.
- Knowledge Assessment: Build systems to test students' ability to apply principles to new factual situations.
Predictive Legal Analysis
- Case Outcome Prediction: Predict likely case outcomes by analyzing similar facts and applied principles.
- Judge/Court Trend Analysis: Identify how specific courts tend to interpret certain legal principles.
- Legal Risk Assessment: Evaluate litigation risk by finding similar cases and their outcomes.
Legal Knowledge Management
- Institutional Knowledge Preservation: Capture and organize firm-specific legal knowledge and reasoning.
- Expertise Location: Identify which lawyers have experience with specific legal principles.
- Knowledge Gap Identification: Discover areas where case law is limited or contradictory.
Legal Process Optimization
- Brief/Filing Drafting Assistance: Surface relevant cases and principles when preparing written submissions.
- Regulatory Compliance Mapping: Link regulations to their judicial interpretations.
- Legal Strategy Development: Support strategic decision-making by exploring potential lines of argument.
Cross-Disciplinary Applications
- Policy Impact Analysis: Assess how judicial interpretations affect policy implementation.
- Academic Legal Research: Support empirical studies on judicial decision-making.
- Comparative Law Analysis: Compare how similar principles are applied across different jurisdictions.
Advanced Legal AI Applications
- LLM Grounding: Use the ontology to anchor Large Language Model (LLM) responses in actual case law.
- Explainable Legal AI: Build AI systems capable of explaining their reasoning by reference to established cases.
- Legal Language Understanding: Improve natural language processing (NLP) specific to the legal domain by providing structured knowledge.
Each of these applications would leverage the structured knowledge and relationships captured by our ontology, enabling more sophisticated analysis than simple text searches or unstructured approaches.
More than just formatting, an ontology also enables search (with querying systems available in the main libraries), visualization, inference, and LLM grounding. It is therefore a very powerful tool that can be applied to many other domains!