Knwl Examples
What classic RAG can’t do
Classic RAG finds similar vectors and this means that given words must be near each other. A counter-example is ‘gravity’ and ‘photoshynthesis’, which are typically not found within the same paragraphs. It’s an example of a faraway relationship not captured by vector embeddings.
With knowledge graphs you can explicitly make this connection and it’s being picked up by the augmentation strategy. It goes as follows, you first add the facts to the Knwl:
from knwl import Knwl
kg = Knwl()
await knwl.add_fact(
"gravity",
"Gravity is a universal force that attracts two bodies toward each other.",
type="Fact",
)
await knwl.add_fact(
"photosynthesis",
"Photosynthesis is the process by which green plants and some other organisms use sunlight to synthesize foods from carbon dioxide and water.",
type="Fact",
)and make an explicit connection like so:
await knwl.connect(
source_name="gravity",
target_name="photosynthesis",
relation="Both are fundamental natural processes.",
)Now, let’s ask what photosynthesis is. Augmentation will fetch the gravity node, despite that it does not directly relate to photosynthesis This 1-hop result would not happen with classic RAG since the vector similarity is too low:
augmentation = await knwl.augment("What is photosynthesis?")
# pretty print the augmentation result
print_knwl(augmentation)╭───────────────────────────────── 🎯 Context ─────────────────────────────────╮
│ │
│ │
│ 💬 Question: Chloroplasts, Chlorophyll, Light-dependent reactions, Calvin │
│ cycle, Carbon dioxide, Water, Glucose, Oxygen, Sunlight, ATP, NADPH, │
│ Stomata, Thylakoid membranes │
│ │
│ │
│ 🔵 Nodes: │
│ │
│ Name Type Description │
│ ────────────────────────────────────────────────────────────────────────── │
│ photosynthesis Fact Photosynthesis is the process by which green │
│ plants and some other organisms use sunlight to │
│ synthesize foods from carbon dioxide and water. │
│ gravity Fact Gravity is a universal force that attracts two │
│ bodies toward each other. │
│ │
╰───────────────────────── 0 chunks, 2 nodes, 1 edges ─────────────────────────╯
You can also use Kwnl to get an explicit answer based on the augmentation:
a = await knwl.ask("What is photosynthesis?")
print_knwl(a.answer)The provided context does not include specific details about chloroplasts, chlorophyll, light-dependent reactions, Calvin cycle, carbon dioxide, water, glucose, oxygen, sunlight, ATP, NADPH, stomata, thylakoid membranes, or their relationships within the process of photosynthesis. However, I can provide a general overview grounded in common knowledge and typical information found in comprehensive knowledge graphs.
### Overview of Photosynthesis
Photosynthesis is the process by which green plants and some other organisms use sunlight to synthesize foods from carbon dioxide (CO₂) and water (H₂O). This process primarily occurs in the chloroplasts, specialized organelles within plant cells. Chlorophyll, a pigment found in these chloroplasts, plays a crucial role in capturing light energy.
#### Key Components and Processes
1. **Light-Dependent Reactions**:
- These reactions occur in the thylakoid membranes of chloroplasts.
- Light energy is absorbed by chlorophyll and other pigments, initiating a series of chemical reactions that produce ATP (adenosine triphosphate) and NADPH (nicotinamide adenine dinucleotide phosphate).
- Water molecules are split in the process, releasing oxygen as a byproduct.
2. **Calvin Cycle**:
- Also known as the light-independent reactions or dark reactions, this cycle takes place in the stroma of chloroplasts.
- It uses ATP and NADPH produced during the light-dependent reactions to convert carbon dioxide into glucose through a series of enzymatic steps.
3. **Stomata**:
- These are small pores on the surface of leaves that regulate gas exchange, allowing CO₂ to enter and oxygen to exit.
4. **Thylakoid Membranes**:
- These are part of the chloroplast structure where light-dependent reactions occur.
- They contain pigments like chlorophyll that absorb sunlight.
### Relationships
- **Chloroplasts**: Contain chlorophyll and perform photosynthesis.
- **Chlorophyll**: Absorbs light energy, essential for initiating photosynthesis.
- **Light-Dependent Reactions**: Produce ATP and NADPH using light energy captured by chlorophyll.
- **Calvin Cycle**: Utilizes ATP and NADPH to convert CO₂ into glucose.
- **Stomata**: Regulate gas exchange, allowing CO₂ entry and oxygen release.
- **Thylakoid Membranes**: Site of light-dependent reactions.
### Limitations
The provided context does not include specific details about these components or their interactions. For a more detailed understanding, additional information from the knowledge graph would be beneficial.
**Sources:**
- The relationships between chloroplasts and photosynthesis are grounded in common biological knowledge.
- Specific text chunks related to each component (chlorophyll, light-dependent reactions, Calvin cycle, etc.) were not provided in this context.
Note that the above is based on the Qwen2.5 (7b) model and you can get better quality with larger models, if you wish.
Charles Dickens
Knwl makes it particularly simple to explore articles from Wikipedia. For example, if you want to fetch the article related to Charles Dickens:
text = await get_library_article("literature", "Charles Dickens")the first parameter being the category (directory) where the text will be stored for re-use (cache). To ingest, simply use:
kg = Knwl()
gr = await kg.ingest(text)
print_knwl(gr)By default Kwnl uses Ollama with qwen2.5:7b but you get better results with slightly larger models, say gemma3:7b. You can set the model and lots of other parameters in the config. If you run this locally, it can take 10 minutes to output the knowledge graph. With the default settings (ie. NetworkX with GraphML output) you get a GraphML file you can open with yEd Live or the desktop version of yEd. With a bit of layout you get something like the adjacent image:

You can download the GraphML data here, if you wish.
It couldn’t be easier to turn Wikipedia into knowledge graphs. If you re-run the code above with different articles titles you agument the KG automatically. Consolidation of information and content happens automatically.