agents.rag.db_rag.graph_db.example¶
Example usage of the Graph Database RAG Agent.
This module demonstrates various ways to use the GraphDBRAGAgent for querying Neo4j databases with natural language. It includes basic usage, advanced configurations, error handling, and different execution modes.
- The examples cover:
Basic agent usage with default configuration
Custom domain configuration
Streaming execution with progress tracking
Batch query processing
Error handling and debugging
Performance monitoring
- To run these examples:
Ensure Neo4j is running and accessible
Set environment variables for Neo4j connection
Run: python example.py
Examples
Basic execution:
$ export NEO4J_URI="bolt://localhost:7687"
$ export NEO4J_USER="neo4j"
$ export NEO4J_PASSWORD="your-password"
$ python example.py
Note
These examples assume you have a Neo4j database with movie data. Adjust the queries and domain configuration for your specific use case.
Functions¶
Demonstrate asynchronous execution of the agent. |
|
Demonstrate basic usage of GraphDBRAGAgent. |
|
Demonstrate batch processing of multiple queries. |
|
Demonstrate agent configuration for a custom domain. |
|
Demonstrate error handling and recovery strategies. |
|
|
Run all examples with a menu interface. |
Demonstrate performance monitoring and optimization. |
|
Run all examples in sequence. |
|
Demonstrate streaming execution with progress tracking. |
Module Contents¶
- async agents.rag.db_rag.graph_db.example.async_example()¶
Demonstrate asynchronous execution of the agent.
This example shows how to use the agent asynchronously for better performance in async applications.
- Example Output:
>>> asyncio.run(async_example()) Async Execution Example =====================================
Processing 3 queries concurrentlyâŠ
Query 1 completed: âWhat are the top rated movies?â Query 2 completed: âWho directed The Godfather?â Query 3 completed: âWhich actors have won an Oscar?â
Total time: 2.8s (vs ~7s sequential)
- agents.rag.db_rag.graph_db.example.basic_example()¶
Demonstrate basic usage of GraphDBRAGAgent.
This example shows the simplest way to use the agent with default configuration. It relies on environment variables for Neo4j connection.
- Example Output:
>>> basic_example() Basic GraphDB RAG Agent Example =====================================
Question: What is the movie with the highest rating?
- ProcessingâŠ
Answer: The highest rated movie is âThe Shawshank Redemptionâ with a rating of 9.3.
Execution Details:
Cypher Query: MATCH (m:Movie) RETURN m.title, m.rating ORDER BY m.rating DESC LIMIT 1
Processing Time: 2.3 seconds
Steps: [âcheck_domain_relevanceâ, âgenerate_queryâ, âvalidate_queryâ, âexecute_queryâ, âgenerate_answerâ]
- agents.rag.db_rag.graph_db.example.batch_processing_example()¶
Demonstrate batch processing of multiple queries.
This example shows how to efficiently process multiple queries and collect statistics about the execution.
- Example Output:
>>> batch_processing_example() Batch Processing Example =====================================
Processing 5 queriesâŠ
- âWhat are the top 5 rated movies?â
Success (2.1s)
- âWho acted in The Godfather?â
Success (1.8s)
- âWhatâs the weather?â
Out of domain (0.5s)
Batch Statistics:
Total Queries: 5
Successful: 4 (80%)
Failed: 1 (20%)
Average Time: 1.7s
Total Time: 8.5s
- agents.rag.db_rag.graph_db.example.custom_domain_example()¶
Demonstrate agent configuration for a custom domain.
This example shows how to configure the agent for a specific domain with custom examples and categories.
- Example Output:
>>> custom_domain_example() Custom Domain (Healthcare) Example =====================================
Configuring agent for healthcare domainâŠ
Testing domain relevance: - âWhich patients have diabetes?â - Accepted - âWhatâs the weather today?â - Rejected (out of domain)
Processing healthcare query⊠Answer: The following patients have been diagnosed with diabetes: John Smith, Mary Johnson, and Robert Williams.
- agents.rag.db_rag.graph_db.example.error_handling_example()¶
Demonstrate error handling and recovery strategies.
This example shows how the agent handles various error conditions including invalid queries, connection issues, and schema mismatches.
- Example Output:
>>> error_handling_example() Error Handling Example =====================================
Testing various error scenariosâŠ
Invalid Cypher syntax: Initial query had errors: [âSyntax error at line 1â]
Successfully corrected and executed
- Non-existent labels:
Handled gracefully: Label âNonExistentNodeâ not in schema
- Complex nested query:
Successfully validated and executed
- agents.rag.db_rag.graph_db.example.main()¶
Run all examples with a menu interface.
This function provides an interactive menu to run different examples demonstrating various features of the GraphDBRAGAgent.
Examples
Running the main function:
$ python example.py GraphDB RAG Agent Examples ============================= Select an example to run: 1. Basic Usage 2. Streaming Execution 3. Custom Domain Configuration 4. Batch Processing 5. Error Handling 6. Performance Monitoring 7. Async Execution 8. Run All Examples 0. Exit Enter your choice (0-8):
- agents.rag.db_rag.graph_db.example.performance_monitoring_example()¶
Demonstrate performance monitoring and optimization.
This example shows how to monitor agent performance and identify bottlenecks in the workflow.
- Example Output:
>>> performance_monitoring_example() Performance Monitoring Example =====================================
Running performance analysisâŠ
Performance Metrics:
Step | Time (s) | % of Total ââââââââ-|----------|ââââ check_domain_relevance | 0.3 | 12% generate_query | 1.2 | 48% validate_query | 0.4 | 16% execute_query | 0.3 | 12% generate_answer | 0.3 | 12% ââââââââ-|----------|ââââ Total | 2.5 | 100%
Optimization Suggestions:
Query generation is the bottleneck (48% of time)
Consider caching frequent queries
Use more specific examples for faster generation
- agents.rag.db_rag.graph_db.example.run_all_examples()¶
Run all examples in sequence.
This function executes all example functions to demonstrate the full capabilities of the GraphDBRAGAgent.
- agents.rag.db_rag.graph_db.example.streaming_example()¶
Demonstrate streaming execution with progress tracking.
This example shows how to use the streaming interface to monitor the agentâs progress through each step of the workflow.
- Example Output:
>>> streaming_example() Streaming GraphDB RAG Agent Example =====================================
Question: Who directed The Matrix?
- Step: check_domain_relevance
Domain check passed
- Step: generate_query
Generated Cypher: MATCH (p:Person)-[:DIRECTED]->(m:Movie {title: âThe Matrixâ}) RETURN p.name
- Step: validate_query
Query validation passed
- Step: execute_query
Query executed successfully Results: [{âp.nameâ: âLana Wachowskiâ},
{âp.nameâ: âLilly Wachowskiâ}]
- Step: generate_answer
Final answer generated
Final Answer: The Matrix was directed by Lana Wachowski and Lilly Wachowski.