JanSahayak / docs /data-flow.md
Darshan03's picture
Deploy latest changes with LFS
33a15a9
|
Raw History Blame Contribute Delete
12 kB

Data Flow

This document illustrates how data flows through the Zynd Protocols Application with concrete examples.

πŸ”„ Complete Request Flow

Here's a complete flow from user query to response:

sequenceDiagram
    participant User
    participant Main as main.py
    participant Graph as graph.py
    participant Orc as Orchestrator
    participant Agent as Specialized Agent
    participant LLM
    participant RAG as RAG System
    participant DB as ChromaDB
    
    User->>Main: "Am I eligible for PM-KISAN?"
    Main->>Graph: invoke(input_text, messages)
    
    Graph->>Orc: orchestrator_node(state)
    Orc->>LLM: Analyze intent
    LLM-->>Orc: RouteDecision(ELIGIBILITY_VERIFIER)
    Orc-->>Graph: {current_intent: "ELIGIBILITY_VERIFIER"}
    
    Graph->>Agent: eligibility_node(state)
    Agent->>Agent: profile_extraction_node()
    Agent->>LLM: Extract citizen profile
    LLM-->>Agent: CitizenProfile
    
    Agent->>Agent: eligibility_evaluation_node()
    Agent->>LLM: Evaluate eligibility
    LLM-->>Agent: EligibilityAnalysisOutput
    
    Agent->>Agent: synthesis_node()
    Agent->>LLM: Generate markdown
    LLM-->>Agent: Markdown response
    
    Agent-->>Graph: {messages: [response]}
    Graph-->>Main: Stream events
    Main-->>User: Display response

πŸ“Š Example 1: Policy Query

User Input

"What benefits does the PM-KISAN scheme provide?"

Step-by-Step Flow

1. Entry Point (main.py)

inputs = {
    "input_text": "What benefits does the PM-KISAN scheme provide?",
    "messages": [HumanMessage(content="What benefits does the PM-KISAN scheme provide?")]
}

app.stream(inputs, config=thread_config)

2. Orchestrator Detection

State:

{
    "input_text": "What benefits does the PM-KISAN scheme provide?",
    "messages": [HumanMessage(...)],
    "current_intent": None
}

Orchestrator Output:

RouteDecision(
    next_agent="POLICY_INTERPRETER",
    reason="User asking about policy benefits"
)

Updated State:

{
    "current_intent": "POLICY_INTERPRETER",
    ...
}

3. Policy Navigator Subgraph

Subgraph Input:

{
    "input_text": "What benefits does the PM-KISAN scheme provide?"
}

a) Intent Node

{
    "intent": "benefit_analysis"
}

b) RAG Node

# Queries ChromaDB
query = "PM-KISAN benefits"
docs = retriever.invoke(query)

{
    "retrieved_docs": [
        Document(page_content="PM-KISAN provides income support of Rs. 6000 per year..."),
        Document(page_content="Benefits are distributed in three installments...")
    ]
}

c) Extraction Node

# LLM extracts structured data
{
    "analysis_output": PolicyAnalysisOutput(
        metadata=PolicyMetadata(
            policy_name="PM-KISAN",
            issuing_authority="Ministry of Agriculture"
        ),
        benefits=[
            Benefit(
                benefit_type="monetary",
                description="Direct income support",
                value="Rs. 6000 per year"
            )
        ],
        confidence_score=0.95
    )
}

d) Synthesis Node

{
    "final_markdown_response": """
## PM-KISAN Benefits

The PM-KISAN scheme provides:

### Direct Income Support
- **Amount**: β‚Ή6,000 per year
- **Distribution**: Three installments of β‚Ή2,000 each
- **Mode**: Direct bank transfer
...
    """,
    "final_json_response": {...}
}

4. Return to Main Graph

{
    "messages": [
        HumanMessage(content="What benefits..."),
        AIMessage(content="## PM-KISAN Benefits...")
    ]
}

5. Stream to User

--- Agent Response ---

[POLICY_AGENT]: ## PM-KISAN Benefits

The PM-KISAN scheme provides:
...

----------------------

πŸ“Š Example 2: Eligibility Check

User Input

"I'm a 45-year-old farmer with 2 acres of land. Am I eligible for PM-KISAN?"

Flow

1. Orchestrator Routes

RouteDecision(
    next_agent="ELIGIBILITY_VERIFIER",
    reason="User asking about eligibility"
)

2. Eligibility Subgraph

a) Profile Extraction

CitizenProfile(
    age=45,
    income=None,
    category=None,
    location=None,
    education_level=None,
    employment_status="farmer"
)

b) Eligibility Evaluation

EligibilityAnalysisOutput(
    eligibility_result=EligibilityResult(
        status="eligible",
        reasoning=[
            "Age requirement met (18-60)",
            "Owns agricultural land",
            "Small/marginal farmer category"
        ],
        failed_conditions=[]
    ),
    matched_benefits=[
        MatchedBenefit(
            scheme_name="PM-KISAN",
            benefit_type="Income Support",
            benefit_value="β‚Ή6,000/year",
            confidence_score=0.92
        )
    ],
    required_documents=[
        "Aadhaar Card",
        "Land ownership certificate",
        "Bank account details"
    ]
)

c) Synthesis

βœ… You ARE Eligible for PM-KISAN!

### Why You Qualify
- Your age (45) meets the requirement (18-60 years)
- You own agricultural land (2 acres)
- You qualify as a small/marginal farmer

### What You'll Get
- **Annual Amount**: β‚Ή6,000
- **Payment Schedule**: Three installments of β‚Ή2,000
- **Payment Mode**: Direct bank transfer

### Documents Needed
- βœ… Aadhaar Card
- βœ… Land ownership certificate
- βœ… Bank account details

### Next Steps
1. Visit the PM-KISAN portal: pmkisan.gov.in...

πŸ“Š Example 3: Benefits Discovery

User Input

"I'm a 30-year-old woman, income β‚Ή50,000/year, living in rural Maharashtra. What benefits can I get?"

Flow

1. Orchestrator Routes

RouteDecision(
    next_agent="BENEFIT_MATCHER",
    reason="User seeking benefit discovery"
)

2. Benefits Subgraph

a) Profile Extraction

CitizenProfile(
    age=30,
    income=50000.0,
    category=None,
    location="Rural Maharashtra",
    education_level=None,
    employment_status=None
)

b) RAG Retrieval + Matching

# RAG retrieves all relevant schemes
# LLM evaluates profile against each

BenefitsAnalysisOutput(
    eligible_benefits=[
        DetailedBenefit(
            scheme_name="Ayushman Bharat",
            benefit_type="Health Insurance",
            benefit_value="β‚Ή5 lakhs coverage",
            priority_rank=1,
            confidence_score=0.89,
            why_you_qualify=[
                "Income below β‚Ή1 lakh threshold",
                "Rural resident"
            ],
            how_to_claim=BenefitClaim(
                steps=["Visit nearest healthcare center", ...],
                required_documents=["Aadhaar", "Income certificate"]
            )
        ),
        DetailedBenefit(
            scheme_name="PMJDY (Bank Account)",
            benefit_type="Financial Inclusion",
            priority_rank=2,
            ...
        )
    ],
    conflicts_or_exclusions=[]
)

c) Synthesis

πŸŽ‰ You qualify for 2 benefits!

## 1. Ayushman Bharat (High Priority)
**What it is**: Health insurance coverage up to β‚Ή5 lakhs

**Why you qualify**:
- Your income (β‚Ή50,000) is below the threshold
- You live in a rural area

**How to claim**:
1. Visit your nearest primary healthcare center
2. Bring your Aadhaar card...

## 2. Pradhan Mantri Jan Dhan Yojana
...

πŸ“Š Example 4: Application Guidance

User Input

"How do I apply for Ayushman Bharat?"

Flow

1. Orchestrator Routes

RouteDecision(
    next_agent="CITIZEN_ADVOCATE",
    reason="User needs application guidance"
)

2. Advocacy Subgraph

a) Scheme Extraction

{
    "selected_scheme": "Ayushman Bharat"
}

b) Advocacy Analysis

AdvocacyAnalysisOutput(
    selected_scheme="Ayushman Bharat",
    application_path=ApplicationPath(
        mode="offline",
        portal_or_office="Nearest empanelled hospital",
        deadline=None
    ),
    document_status=DocumentStatus(
        ready=[],
        missing=["Aadhaar", "Ration card/SECC data"],
        high_risk=["Income certificate"]
    ),
    submission_guidance=SubmissionGuidance(
        steps=[
            "Verify eligibility on mera.pmjay.gov.in",
            "Visit nearest empanelled hospital",
            "Present Aadhaar and ration card",
            ...
        ],
        common_mistakes=[
            "Not checking eligibility first",
            "Missing original documents",
            ...
        ]
    )
)

c) Synthesis

# Your Application Guide for Ayushman Bharat

I'm here to help you apply successfully! πŸ₯

## Step 1: Check Eligibility
1. Visit mera.pmjay.gov.in
2. Enter your mobile number and Aadhaar
3. Verify your name appears in beneficiary list

⚠️ **If not listed**: You may not be eligible yet

## Step 2: Gather Documents

πŸ“‹ **You will need**:
- Aadhaar Card (original + photocopy)
- Ration card OR SECC 2011 data
- Any government-issued ID

⚠️ **Common mistakes to avoid**:
- Don't bring just photocopies - originals required
...

πŸ” State Transitions

Main Graph State Evolution

# Initial
{
    "input_text": "query",
    "current_intent": None,
    "messages": [HumanMessage("query")]
}

# After Orchestrator
{
    "input_text": "query",
    "current_intent": "POLICY_INTERPRETER",
    "messages": [HumanMessage("query")]
}

# After Specialized Agent
{
    "input_text": "query",
    "current_intent": "POLICY_INTERPRETER",
    "messages": [
        HumanMessage("query"),
        AIMessage("response")
    ]
}

# Multi-turn (next query)
{
    "input_text": "follow-up query",
    "current_intent": "ELIGIBILITY_VERIFIER",
    "messages": [
        HumanMessage("query"),
        AIMessage("response"),
        HumanMessage("follow-up query"),
        AIMessage("follow-up response")
    ]
}

πŸ› οΈ Tool Execution Flow

When an agent needs to call a tool:

sequenceDiagram
    participant Agent
    participant Tools as Tool Node
    participant Tool
    
    Agent->>Agent: Needs information
    Agent->>Tools: ToolCall(retrieve_policy)
    Tools->>Tool: Execute tool
    Tool-->>Tools: Tool result
    Tools-->>Agent: Updated messages
    Agent->>Agent: Process result
    Agent-->>User: Final response

Example:

# Agent decides to call tool
AIMessage(
    content="",
    tool_calls=[{
        "name": "retrieve_policy",
        "args": {"query": "PM-KISAN benefits"}
    }]
)

# Tool Node executes
ToolMessage(
    content="Retrieved documents: ...",
    tool_call_id="123"
)

# Agent processes tool result
AIMessage(
    content="Based on the documents, PM-KISAN provides..."
)

πŸ”„ Conversation Memory Flow

Thanks to checkpointing:

# Turn 1
User: "What is PM-KISAN?"
Agent: "PM-KISAN is a farmer income support scheme..."

# Turn 2 (uses memory)
User: "How do I apply for it?"
Agent: "To apply for PM-KISAN (which we just discussed)..."
       # Agent has context from previous turn

πŸ“ˆ Performance Optimizations in Flow

1. Caching

# First query: Full LLM call
cache_key = hash_query("PM-KISAN benefits", context)
result = llm.invoke(...)
cache[cache_key] = result

# Second identical query: Cache hit
cached_result = cache.get(cache_key)  # Instant!

2. Streaming

# Results stream as generated
for event in app.stream(inputs):
    # User sees partial results immediately
    print(event)

🎯 Key Takeaways

  1. Hierarchical: Main graph β†’ Orchestrator β†’ Specialized subgraph
  2. State Isolation: Each subgraph has independent state
  3. Message-based: Responses returned as messages in conversation
  4. Checkpointed: Full conversation history maintained
  5. Cached: Repeated queries use cached results
  6. Streaming: Results delivered progressively

Next: Development Guide for extending the system.