use text_to_cypher::{ChatMessage, ChatRequest, ChatRole, TextToCypherClient, TextToCypherResponse, TokenUsage, core};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
#[cfg(feature = "server")]
tracing_subscriber::fmt()
.with_env_filter(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info")),
)
.init();
let api_key = std::env::var("OPENAI_API_KEY")
.expect("Please set OPENAI_API_KEY to run this example (e.g. export OPENAI_API_KEY=sk-...)");
let model = std::env::var("MODEL").unwrap_or_else(|_| "gpt-5.5".to_string());
let falkordb_connection =
std::env::var("FALKORDB_CONNECTION").unwrap_or_else(|_| "falkor://127.0.0.1:6379".to_string());
let graph_name = std::env::var("GRAPH_NAME").unwrap_or_else(|_| "demo_graph".to_string());
println!("=== Token Usage Tracking Example ===");
println!("model: {model}");
println!("connection: {falkordb_connection}");
println!("graph: {graph_name}\n");
let client = TextToCypherClient::new(&model, &api_key, &falkordb_connection);
seed_graph(&falkordb_connection, &graph_name).await;
println!("--- Example 1: full text_to_cypher pipeline ---");
let request = chat("How many people are in the graph?");
match client.text_to_cypher(&graph_name, request).await {
Ok(response) => report_usage(&response),
Err(e) => eprintln!("✗ Request failed: {e}"),
}
println!("\n--- Example 2: cypher_only (generation only) ---");
let request = chat("List the names of all people.");
match client.cypher_only(&graph_name, request).await {
Ok(response) => {
if let Some(query) = &response.cypher_query {
println!("Generated query: {query}");
}
report_usage(&response);
}
Err(e) => eprintln!("✗ Request failed: {e}"),
}
println!("\n=== Example completed ===");
Ok(())
}
fn chat(content: &str) -> ChatRequest {
ChatRequest {
messages: vec![ChatMessage {
role: ChatRole::User,
content: content.to_string(),
}],
}
}
fn report_usage(response: &TextToCypherResponse) {
if let Some(answer) = &response.answer {
println!("Answer: {answer}");
}
match response.token_usage {
Some(TokenUsage {
prompt_tokens,
completion_tokens,
total_tokens,
}) => {
println!("Token usage:");
println!(" prompt_tokens = {prompt_tokens}");
println!(" completion_tokens = {completion_tokens}");
println!(" total_tokens = {total_tokens}");
if total_tokens == 0 {
eprintln!("⚠ token_usage was reported but total_tokens is 0 — the provider may not return usage data.");
} else {
println!("✓ Token usage tracking is working.");
}
}
None => {
eprintln!("✗ No token_usage reported on the response — the provider may not return usage data.");
}
}
}
async fn seed_graph(
falkordb_connection: &str,
graph_name: &str,
) {
let seed_query = "MERGE (:Person {name: 'Alice'}) MERGE (:Person {name: 'Bob'}) MERGE (:Person {name: 'Charlie'})";
match core::execute_cypher_query(seed_query, graph_name, falkordb_connection, false).await {
Ok(_) => println!("Seeded graph '{graph_name}' with sample data.\n"),
Err(e) => println!("Note: seeding graph failed (continuing anyway): {e}\n"),
}
}