Crate rrag_graph

Crate rrag_graph 

Source
Expand description

§RGraph - Graph-based Agent Orchestration System

RGraph is a powerful graph-based workflow orchestration system designed for building sophisticated AI agent applications. Inspired by LangGraph, it provides a declarative way to define complex agent workflows with state management, conditional execution, and seamless integration with the RRAG framework.

§Key Features

  • Graph-Based Workflows: Define agent behavior as directed graphs
  • State Management: Persistent state across node executions
  • Conditional Routing: Dynamic workflow paths based on execution results
  • Agent Orchestration: Coordinate multiple AI agents and tools
  • RRAG Integration: Built-in support for RAG-powered agents
  • Async Execution: High-performance concurrent execution
  • Observability: Comprehensive monitoring and debugging
  • Persistence: Durable workflow state and history

§Quick Start

use rgraph::prelude::*;

// Define a simple agent workflow
let mut graph = WorkflowGraph::new("research_assistant");

// Add nodes
graph.add_node("understand_query", QueryAnalysisNode::new()).await?;
graph.add_node("search_knowledge", RagSearchNode::new()).await?;
graph.add_node("synthesize_response", ResponseGenerationNode::new()).await?;

// Define edges
graph.add_edge("understand_query", "search_knowledge")?;
graph.add_edge("search_knowledge", "synthesize_response")?;

// Execute the workflow
let initial_state = GraphState::new()
    .with_input("user_query", "What is machine learning?");

let result = graph.execute(initial_state).await?;
println!("Response: {}", result.get_output("final_response")?);

§Advanced Examples

§Multi-Agent Collaboration

use rgraph::prelude::*;

let mut graph = WorkflowGraph::new("multi_agent_system");

// Research agent
graph.add_node("researcher",
    AgentNode::new("research_agent")
        .with_system_prompt("You are a research specialist...")
        .with_tools(vec![web_search_tool(), database_query_tool()])
).await?;

// Analysis agent
graph.add_node("analyst",
    AgentNode::new("analysis_agent")
        .with_system_prompt("You analyze research data...")
        .with_tools(vec![data_analysis_tool(), visualization_tool()])
).await?;

// Writer agent
graph.add_node("writer",
    AgentNode::new("writing_agent")
        .with_system_prompt("You write comprehensive reports...")
).await?;

// Conditional routing based on research results
graph.add_conditional_edge("researcher", |state: &GraphState| {
    if state.get("research_quality_score")? > 0.8 {
        Ok("analyst".to_string())
    } else {
        Ok("researcher".to_string()) // Loop back for more research
    }
})?;

graph.add_edge("analyst", "writer")?;

let result = graph.execute(
    GraphState::new().with_input("research_topic", "Climate Change Impact")
).await?;

§RAG-Powered Knowledge Agent

use rgraph::prelude::*;
let mut graph = WorkflowGraph::new("knowledge_agent");

// RAG retrieval node
graph.add_node("retrieve_context",
    RagRetrievalNode::new()
        .with_rrag_system(rrag_system)
        .with_top_k(5)
        .with_score_threshold(0.7)
).await?;

// Context evaluation node
graph.add_node("evaluate_context",
    ContextEvaluationNode::new()
        .with_relevance_threshold(0.6)
).await?;

// Response generation with context
graph.add_node("generate_response",
    ContextualGenerationNode::new()
        .with_context_window(4096)
).await?;

// Conditional routing based on context quality
graph.add_conditional_edge("evaluate_context", |state: &GraphState| {
    let context_score: f32 = state.get("context_relevance_score")?;
    if context_score > 0.6 {
        Ok("generate_response".to_string())
    } else {
        Ok("retrieve_context".to_string()) // Retry with different strategy
    }
})?;

let result = graph.execute(
    GraphState::new().with_input("query", "Explain quantum computing")
).await?;

§Architecture

RGraph is built around several core concepts:

§Workflow Graph

A directed graph representing the agent workflow, where nodes are execution units and edges define the flow of control and data.

§Graph State

A shared state object that flows through the graph, accumulating results and providing context for decision-making.

§Nodes

Execution units that perform specific tasks:

  • Agent Nodes: LLM-powered agents with tools
  • Tool Nodes: Direct tool execution
  • RAG Nodes: Retrieval-augmented generation
  • Condition Nodes: Decision points in the workflow
  • Transform Nodes: Data transformation and processing

§Execution Engine

The runtime system that executes graphs with support for:

  • Parallel execution where possible
  • State management and persistence
  • Error handling and recovery
  • Observability and debugging

§Integration with RRAG

RGraph seamlessly integrates with the RRAG framework to provide:

  • RAG-powered agent nodes
  • Knowledge retrieval capabilities
  • Document processing workflows
  • Embedding-based routing decisions
  • Multi-modal processing support

Re-exports§

pub use crate::core::Edge;
pub use crate::core::EdgeId;
pub use crate::core::ExecutionContext;
pub use crate::core::ExecutionResult;
pub use crate::core::GraphBuilder;
pub use crate::core::Node;
pub use crate::core::NodeId;
pub use crate::core::WorkflowGraph;
pub use crate::execution::ExecutionConfig;
pub use crate::execution::ExecutionEngine;
pub use crate::execution::ExecutionError;
pub use crate::execution::ExecutionMetrics;
pub use crate::execution::ExecutionMode;
pub use crate::execution::ExecutionResults;
pub use crate::nodes::AgentNode;
pub use crate::nodes::ConditionNode;
pub use crate::nodes::ToolNode;
pub use crate::nodes::TransformNode;
pub use crate::state::GraphState;
pub use crate::state::StatePath;
pub use crate::state::StateValue;
pub use crate::rrag_integration::ContextEvaluationConfig;
pub use crate::rrag_integration::ContextEvaluationNode;
pub use crate::rrag_integration::RagGenerationConfig;
pub use crate::rrag_integration::RagGenerationNode;
pub use crate::rrag_integration::RagRetrievalConfig;
pub use crate::rrag_integration::RagRetrievalNode;
pub use crate::rrag_integration::RagWorkflowBuilder;

Modules§

agents
Agent System
core
Core Graph Abstractions
execution
Simple Execution Engine
nodes
Graph Node Implementations
observability
Observability
prelude
RGraph Prelude
routing
Routing System
rrag_integration
RRAG Integration
state
Graph State Management
tools
Tool System for RGraph Agents

Enums§

RGraphError
Error types for RGraph operations

Constants§

DESCRIPTION
NAME
VERSION
Framework constants

Type Aliases§

RGraphResult
Result type for RGraph operations