Skip to main content

Knowledge Graph

By registering a single function that takes a Pydantic data type as input, the knowledge contained by an LLM (or in documents given as context), can be converted into structured data. Here, the data is visualized with GraphViz.

[1]:
from graphviz import Digraph
from pydantic import BaseModel, Field
from typing import List
from chatlab import Chat, system


class Node(BaseModel):
id: int
label: str
color: str


class Edge(BaseModel):
source: int
target: int
label: str
color: str = "black"


class KnowledgeGraph(BaseModel):
nodes: List[Node] = Field(..., default_factory=list)
edges: List[Edge] = Field(..., default_factory=list)


def visualize_knowledge_graph(kg: KnowledgeGraph, comment: str = "Knowledge Graph"):
"""Visualizes a knowledge graph using graphviz."""
dot = Digraph(comment=comment)

for node in kg.nodes:
dot.node(str(node.id), node.label, color=node.color)

for edge in kg.edges:
dot.edge(str(edge.source), str(edge.target), label=edge.label, color=edge.color)

display(dot)

return "<<Graphic displayed inline for the user to see>>"


chat = Chat(
system(
"You are running inside a jupyter notebook. Your responses appear as markdown in the notebook. Functions you run can produce side effects."
),
model="gpt-4-1106-preview",
chat_functions=[visualize_knowledge_graph],
)

await chat("Visually teach me about the process of brewing coffee.", temperature=0.9)
[1]:
𝑓Ranvisualize_knowledge_graph
Input:
{ "kg": { "nodes": [ { "id": 1, "label": "Water", "color": "blue" }, { "id": 2, "label": "Coffee Grinder", "color": "grey" }, { "id": 3, "label": "Coffee Beans", "color": "brown" }, { "id": 4, "label": "Grind Beans", "color": "orange" }, { "id": 5, "label": "Coffee Filter", "color": "white" }, { "id": 6, "label": "Coffee Machine", "color": "black" }, { "id": 7, "label": "Heat Water", "color": "orange" }, { "id": 8, "label": "Place Grounds in Filter", "color": "orange" }, { "id": 9, "label": "Pour Water into Machine", "color": "orange" }, { "id": 10, "label": "Brew", "color": "orange" }, { "id": 11, "label": "Coffee", "color": "darkbrown" } ], "edges": [ { "source": 3, "target": 2, "label": "is ground by" }, { "source": 2, "target": 4, "label": "to" }, { "source": 4, "target": 5, "label": "place into" }, { "source": 5, "target": 6, "label": "insert into" }, { "source": 1, "target": 7, "label": "is used in" }, { "source": 7, "target": 6, "label": "pour into" }, { "source": 9, "target": 6, "label": "start" }, { "source": 6, "target": 10, "label": "to" }, { "source": 10, "target": 11, "label": "produce" } ] }, "comment": "Coffee Brewing Process" }
Output:
<<Graphic displayed inline for the user to see>>

Warning: darkbrown is not a known color.
WaterHeat Wateris used inCoffee GrinderGrind BeanstoCoffee Beansis ground byCoffee Filterplace intoCoffee Machineinsert intoBrewtopour intoPlace Grounds in FilterPour Water into MachinestartCoffeeproduce

The process of brewing coffee involves several key steps and components as illustrated in the knowledge graph:

  1. Coffee Beans are the starting material. They are whole, raw beans that need to be ground before brewing.
  2. Coffee Grinder is used to grind the coffee beans into grounds.
  3. Grind Beans is the process of breaking down coffee beans into smaller particles to increase the surface area for extraction.
  4. Coffee Filter is used to hold the coffee grounds and prevent them from getting into the final brew.
  5. Coffee Machine is the apparatus that will heat the water, pour it over the grounds, and brew the coffee.
  6. Water is a crucial ingredient in coffee brewing.
  7. Heat Water is the process to bring the water to the right temperature for brewing.
  8. Place Grounds in Filter involves putting the ground coffee into the filter.
  9. Pour Water into Machine is the action of adding water to the coffee machine for heating and brewing.
  10. Brew is the actual process where hot water is passed through the grounds, extracting flavors and compounds to create coffee.
  11. Coffee is the final product, a brewed beverage ready for consumption.

To summarize the brewing process, you start with whole coffee beans, grind them, place the grounds in a filter, insert the filter into the coffee machine, heat water, pour the water into the machine, and then start the machine to brew the coffee, resulting in the final beverage.

[2]:
await chat("A warning came up saying that darkbrown is not a known color.")
[2]:
𝑓Ranvisualize_knowledge_graph
Input:
{ "kg": { "nodes": [ { "id": 1, "label": "Water", "color": "blue" }, { "id": 2, "label": "Coffee Grinder", "color": "grey" }, { "id": 3, "label": "Coffee Beans", "color": "brown" }, { "id": 4, "label": "Grind Beans", "color": "orange" }, { "id": 5, "label": "Coffee Filter", "color": "white" }, { "id": 6, "label": "Coffee Machine", "color": "black" }, { "id": 7, "label": "Heat Water", "color": "orange" }, { "id": 8, "label": "Place Grounds in Filter", "color": "orange" }, { "id": 9, "label": "Pour Water into Machine", "color": "orange" }, { "id": 10, "label": "Brew", "color": "orange" }, { "id": 11, "label": "Coffee", "color": "maroon" } ], "edges": [ { "source": 3, "target": 2, "label": "is ground by" }, { "source": 2, "target": 4, "label": "to" }, { "source": 4, "target": 5, "label": "place into" }, { "source": 5, "target": 6, "label": "insert into" }, { "source": 1, "target": 7, "label": "is used in" }, { "source": 7, "target": 6, "label": "pour into" }, { "source": 9, "target": 6, "label": "start" }, { "source": 6, "target": 10, "label": "to" }, { "source": 10, "target": 11, "label": "produce" } ] }, "comment": "Coffee Brewing Process" }
Output:
<<Graphic displayed inline for the user to see>>

WaterHeat Wateris used inCoffee GrinderGrind BeanstoCoffee Beansis ground byCoffee Filterplace intoCoffee Machineinsert intoBrewtopour intoPlace Grounds in FilterPour Water into MachinestartCoffeeproduce

Here is the corrected visualization of the coffee brewing process:

  1. Coffee Beans are the starting material. They are whole, raw beans that need to be ground before brewing.
  2. Coffee Grinder is used to grind the coffee beans into grounds.
  3. Grind Beans is the process of breaking down coffee beans into smaller particles to increase the surface area for extraction.
  4. Coffee Filter is used to hold the coffee grounds and prevent them from getting into the final brew.
  5. Coffee Machine is the apparatus that will heat the water, pour it over the grounds, and brew the coffee.
  6. Water is a crucial ingredient in coffee brewing.
  7. Heat Water is the process to bring the water to the right temperature for brewing.
  8. Place Grounds in Filter involves putting the ground coffee into the filter.
  9. Pour Water into Machine is the action of adding water to the coffee machine for heating and brewing.
  10. Brew is the actual process where hot water is passed through the grounds, extracting flavors and compounds to create coffee.
  11. Coffee is the final product, a brewed beverage ready for consumption, represented here with the color maroon.

The process flow is as follows: Coffee beans are ground by a coffee grinder, the grounds are placed into a coffee filter, which is then inserted into the coffee machine. Water is heated and poured into the machine, which then starts the brewing process, resulting in the production of coffee.