#![allow(
clippy::unwrap_used,
clippy::expect_used,
reason = "example code — panics are acceptable"
)]
use cognee_llm::schema::{build_schema_prompt, generate_json_schema, generate_json_schema_string};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, JsonSchema)]
struct Node {
id: String,
entity_type: String,
label: String,
#[serde(default)]
properties: std::collections::HashMap<String, String>,
}
#[derive(Debug, Serialize, Deserialize, JsonSchema)]
struct Edge {
source: String,
target: String,
relationship: String,
#[serde(default)]
weight: Option<f32>,
}
#[derive(Debug, Serialize, Deserialize, JsonSchema)]
struct KnowledgeGraph {
nodes: Vec<Node>,
edges: Vec<Edge>,
#[serde(default)]
metadata: Option<ExtractionMetadata>,
}
#[derive(Debug, Serialize, Deserialize, JsonSchema)]
struct ExtractionMetadata {
source: String,
entity_count: usize,
confidence: f32,
}
fn main() {
println!("=== JSON Schema Generation Examples ===\n");
println!("1. Generate schema for Node:");
let node_schema = generate_json_schema::<Node>();
println!("{}\n", serde_json::to_string_pretty(&node_schema).unwrap());
println!("2. Generate compact schema string for Edge:");
let edge_schema_compact = generate_json_schema_string::<Edge>(false);
println!("{edge_schema_compact}\n");
println!("3. Generate pretty schema string for KnowledgeGraph:");
let kg_schema = generate_json_schema_string::<KnowledgeGraph>(true);
println!("{kg_schema}\n");
println!("4. Build complete LLM prompt with schema:");
let prompt = build_schema_prompt::<KnowledgeGraph>(
"Extract a knowledge graph from the following text. \
Identify all entities (people, organizations, locations) as nodes \
and their relationships as edges. Include confidence scores where applicable.",
);
println!("{prompt}\n");
println!("5. How an LLM adapter would use this:");
println!(" a) Generate schema from the expected type");
let schema = generate_json_schema::<KnowledgeGraph>();
println!(" b) Include schema in the request (OpenAI function calling example):");
let function_def = serde_json::json!({
"name": "extract_knowledge_graph",
"description": "Extract entities and relationships into a knowledge graph",
"parameters": schema
});
println!(
" {}\n",
serde_json::to_string_pretty(&function_def).unwrap()
);
println!(" c) Or embed schema in system prompt:");
let system_prompt = build_schema_prompt::<KnowledgeGraph>(
"You are an expert at extracting structured information. \
Extract entities and relationships from the user's text.",
);
println!(" [System Prompt Length: {} chars]\n", system_prompt.len());
println!("6. Validating LLM JSON response:");
let sample_response = r#"{
"nodes": [
{
"id": "person_1",
"entity_type": "Person",
"label": "Alice",
"properties": {"role": "engineer"}
},
{
"id": "org_1",
"entity_type": "Organization",
"label": "TechCorp",
"properties": {}
}
],
"edges": [
{
"source": "person_1",
"target": "org_1",
"relationship": "WORKS_FOR",
"weight": 0.95
}
],
"metadata": {
"source": "Alice works at TechCorp as an engineer",
"entity_count": 2,
"confidence": 0.9
}
}"#;
match serde_json::from_str::<KnowledgeGraph>(sample_response) {
Ok(kg) => {
println!(" ✓ Valid response parsed successfully!");
println!(" - Nodes: {}", kg.nodes.len());
println!(" - Edges: {}", kg.edges.len());
if let Some(meta) = kg.metadata {
println!(" - Confidence: {}", meta.confidence);
}
}
Err(e) => {
println!(" ✗ Invalid response: {e}");
}
}
}