Skip to content

feat(intelligence): implement form generation with LLM #7

@rsalus

Description

@rsalus

Note for implementers: The design outlined here is a guideline, not a strict specification. Feel free to pivot from the suggested approach if you discover a better solution during implementation. Update the issue with your rationale when deviating significantly.

Summary

Generate PA form field mappings and clinical summary from extracted evidence.

Context

This function is the final step in the Intelligence Service reasoning pipeline. It receives:

  1. ClinicalBundle - structured FHIR data
  2. EvidenceItems - output from evidence extractor (feat(intelligence): implement evidence extraction with LLM #6)
  3. Policy - the matched policy definition from feat(intelligence): implement policy matching for demo procedure #8

It produces a complete PAFormResponse ready for PDF stamping by the Gateway.

Tasks

  • Create prompt template for clinical summary generation (see §4.4)
  • Implement recommendation calculation logic (see below)
  • Generate field_mappings from clinical bundle + policy
  • Call LLM via llm_client.chat_completion()
  • Parse LLM response and build PAFormResponse
  • Add unit tests

Recommendation Logic

def calculate_recommendation(evidence: list[EvidenceItem]) -> tuple[str, float]:
    """
    Returns (recommendation, confidence_score).
    
    Rules:
    - APPROVE: All required criteria MET, confidence >= 0.8
    - NEED_INFO: Any required criteria UNCLEAR
    - MANUAL_REVIEW: Any required criteria NOT_MET, or confidence < 0.6
    """
    met = [e for e in evidence if e.status == "MET"]
    not_met = [e for e in evidence if e.status == "NOT_MET"]
    unclear = [e for e in evidence if e.status == "UNCLEAR"]
    
    if not_met:
        return ("MANUAL_REVIEW", 0.5)
    if unclear:
        return ("NEED_INFO", 0.7)
    
    avg_confidence = sum(e.confidence for e in met) / len(met) if met else 0.0
    if avg_confidence >= 0.8:
        return ("APPROVE", avg_confidence)
    return ("MANUAL_REVIEW", avg_confidence)

Files

File Action
apps/intelligence/src/reasoning/form_generator.py Modify
apps/intelligence/src/llm_client.py Reference
apps/intelligence/src/models/pa_form.py Reference

Dependencies

Issue Relationship
#6 Provides EvidenceItem input
#8 Provides policy with field mappings
#5 Determines PDF field names

Design References

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions