Skip to content

Latest commit

 

History

History
265 lines (216 loc) · 6.9 KB

File metadata and controls

265 lines (216 loc) · 6.9 KB

LLM Insights Feature

Overview

The DAML validation tool now preserves full LLM context in addition to structured JSON extraction. This ensures we get maximum value from the LLM analysis we're paying for.

What Changed

Before: JSON Only

# Extract JSON
result = json.loads(content)

# Discard everything else
return AuthorizationExtractionResult(
    model=model,
    confidence=confidence,
    reasoning="Brief explanation from JSON"
)

Problem: LLM often adds valuable context/explanations that got thrown away!

After: JSON + Full Context

# Extract JSON for structured data
json_content = content[first_brace:last_brace + 1]
result = json.loads(json_content)

# BUT ALSO preserve full LLM response
return AuthorizationExtractionResult(
    model=model,
    confidence=confidence,
    reasoning="Brief explanation from JSON",
    llm_full_response=content  # Full context preserved!
)

Benefit: We keep both structure AND insights!

Example: Choice Parameter Pattern

User's DAML Code

template PaymentApprovalRequest
  with
    requester: Party
    approvers: [Party]
  where
    signatory requester
    observer approvers
    
    choice ApprovePayment : ()
      with
        approver: Party  -- Choice parameter
      controller approver
      do
        assertMsg "Approver must be authorized" (approver `elem` approvers)
        return ()

What LLM Might Return

JSON (structured):

{
  "template_name": "PaymentApprovalRequest",
  "signatories": ["requester"],
  "observers": ["approvers"],
  "controllers": {"ApprovePayment": ["approver"]},
  "confidence": 0.8,
  "reasoning": "Choice parameter with runtime validation"
}

Extra Context (insights):

The controller 'approver' is a choice parameter that gets validated
at runtime against the 'approvers' field using `elem`. This is a 
common DAML pattern for dynamic authorization where any member of 
a list can exercise the choice.

The confidence is 0.8 (not 1.0) because this pattern requires 
understanding the semantic relationship between the parameter and 
the validation logic. The actual controller set is 'approvers', 
but it's expressed through runtime validation rather than compile-time 
declaration.

This is generally safe IF the validation is correct (which it is here).

What User Receives

{
  "valid": true,
  "confidence": 0.8,
  "issues": [],
  "suggestions": [],
  "llm_insights": "The controller 'approver' is a choice parameter that gets validated at runtime against the 'approvers' field using `elem`. This is a common DAML pattern for dynamic authorization where any member of a list can exercise the choice.\n\nThe confidence is 0.8 (not 1.0) because this pattern requires understanding the semantic relationship between the parameter and the validation logic..."
}

Benefits

1. Educational Value

Users learn DAML patterns from the LLM's explanations:

  • "This is a common DAML pattern for..."
  • "The confidence is X because..."
  • "This is safe IF..."

2. Debugging Aid

When confidence is lower:

  • Users understand WHY the LLM is uncertain
  • Clear indication of complex patterns
  • Helps users decide if manual review is needed

3. Audit Trail

Full LLM responses are valuable for:

  • Understanding why certain decisions were made
  • Improving prompts over time
  • Training/fine-tuning models

4. Transparency

Users see exactly what the LLM "thinks":

  • Not a black box
  • Can validate LLM reasoning
  • Build trust in the system

5. Better Value for Payment

If users are paying via x402:

  • They get EVERYTHING the LLM produced
  • Not just extracted data
  • Maximum ROI on the LLM call

Implementation Details

Data Flow

LLM Response (raw text)
    │
    ├─> Parse JSON → Structured data
    │   └─> AuthorizationModel
    │       └─> confidence, reasoning
    │
    └─> Extract insights → Additional context
        └─> Text before/after JSON
            └─> llm_full_response
                └─> llm_insights (in tool response)

Storage

@dataclass
class AuthorizationExtractionResult:
    model: Optional[AuthorizationModel]  # Structured
    confidence: float                    # Structured
    reasoning: str                       # Structured (from JSON)
    llm_full_response: Optional[str]     # Unstructured (full context)

Exposure

class ValidateDamlResult:
    valid: bool
    confidence: float
    issues: List[str]
    suggestions: List[str]
    llm_insights: Optional[str]  # NEW: Full LLM context exposed

User Experience

High Confidence (1.0)

{
  "valid": true,
  "confidence": 1.0,
  "llm_insights": "Simple, clear authorization pattern with single signatory and observer."
}

Value: Confirms the pattern is straightforward

Medium Confidence (0.8)

{
  "valid": true,
  "confidence": 0.8,
  "llm_insights": "Choice parameter with runtime validation. The controller 'approver' is validated against 'approvers' at runtime, which is a common DAML pattern..."
}

Value: Explains the complexity and validates the pattern

Low Confidence (0.5)

{
  "valid": false,
  "should_delegate": true,
  "confidence": 0.5,
  "llm_insights": "This code uses conditional expressions in the observer declaration that I cannot fully analyze. Manual review recommended to ensure..."
}

Value: Clear explanation of why delegation is needed

Future Enhancements

1. Structured Insights

Parse the insights into categories:

{
  "llm_insights": {
    "pattern_explanation": "...",
    "confidence_reasoning": "...",
    "safety_notes": "...",
    "recommendations": ["..."]
  }
}

2. Insight Highlighting

Identify key insights automatically:

  • Security warnings → ⚠️
  • Best practices → 💡
  • Pattern explanations → 📚

3. Learning Mode

Use insights to improve:

  • Identify common misconceptions
  • Build FAQ from repeated patterns
  • Fine-tune prompts based on useful insights

4. Confidence Calibration

Track correlation between:

  • LLM stated confidence
  • Actual accuracy
  • Adjust thresholds accordingly

Cost Implications

No additional cost! The LLM generates this context anyway (we were just throwing it away).

By preserving it:

  • ✅ Zero extra LLM calls
  • ✅ Maximum value extraction
  • ✅ Better ROI on x402 payments
  • ✅ Richer user experience

Summary

The LLM Insights feature ensures we extract maximum value from every LLM analysis:

  1. Structured data (JSON) for programmatic use
  2. Full context (insights) for understanding
  3. No extra cost - just better use of existing calls
  4. Educational - users learn from LLM explanations
  5. Transparent - users see full LLM reasoning

This aligns perfectly with the "Boom! Payment = Boom! Value" philosophy:

  • If users are paying for LLM analysis via x402
  • They deserve to see ALL the value the LLM provides
  • Not just the extracted bits!