Skip to content

Add LLM function calling for dynamic HealthKit data retrieval #57

@ahmedrezik

Description

@ahmedrezik

Use Case

Currently, HealthGPT fetches 6 fixed metrics over a hard-coded 14-day window
at session start and dumps all of it into the system prompt. The LLM cannot
access data outside this window, query additional metrics, or fetch fresh data
mid-conversation.

With LLM function calling (tool use), the LLM would dynamically fetch only the
data relevant to the user's question, over any time range, enabling questions
like "How was my sleep last month?" or "Compare my steps this week vs last
week" — none of which are possible today.

Problem

  • The LLM is blind to anything beyond 6 metrics and 14 days
  • Every session fetches all data upfront, even if the user only asks about one
    metric
  • Data goes stale immediately — if the user exercises after opening the app,
    the LLM doesn't know
  • The system prompt is bloated with data the user may never ask about, wasting
    context window
  • HealthKit offers 100+ data types, but only 6 are accessible

Solution

Replace the static data dump with LLM tool use (function calling). Instead of
pre-fetching all data into the system prompt, define tools like
get_health_metric(metric, days) that the LLM calls on demand based on the
user's question.

SpeziLLMOpenAI v0.12.3 already supports this via the LLMFunction protocol
and @LLMFunctionBuilder. Fog and Local backends would continue using the
current system-prompt approach as a fallback.

Alternatives considered

  • Expand the static prompt: Fetch more metrics and longer time ranges
    upfront. Simple to implement but worsens the context window bloat and still
    can't handle arbitrary user questions.
  • Re-fetch on every message: Query all data before each LLM call to keep
    it fresh. Most messages don't need updated data, and it still can't
    vary the time range or metrics based on the question.
  • User-selected metrics/date range: Add UI controls for the user to pick
    which metrics and time period to include. Shifts the burden to the user and
    breaks the natural conversation flow.

Additional context

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct and Contributing Guidelines

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions