Skip to main content

AI Operation ($ai)

The $ai operation is a specialized FHIR operation that allows you to interface with Large Language Models (LLMs) while maintaining clinical context. It supports conversational interactions and can suggest FHIR-based actions through function calling.

Purpose

Use the $ai operation to build natural language interfaces into your application, such as:

  • Searching for patients using natural language.
  • Drafting clinical notes or Communication resources.
  • Suggesting structured resource updates based on chat.

Request Signature

Endpoint: POST [baseUrl]/fhir/R4/$ai

Resource Type: Parameters

Core Input Parameters

ParameterTypeRequiredDescription
messagesstringYesStringified JSON array of conversation history.
modelstringYesThe specific model ID (e.g., gpt-4).
apiKeystringYesYour OpenAI API key.
toolsstringNoDefinitions for FHIR function calling.

Quick Example

{
"resourceType": "Parameters",
"parameter": [
{
"name": "messages",
"valueString": "[{\"role\":\"user\",\"content\":\"Find patient Alice\"}]"
},
{ "name": "model", "valueString": "gpt-4" },
{ "name": "apiKey", "valueString": "sk-..." }
]
}

Response

The operation returns a Parameters resource containing a content string (the AI's text response) and an optional tool_calls string containing suggested FHIR operations.

Note: The server suggests actions but does not execute them. The client application is responsible for validating and performing any suggested FHIR writes.

Read the full implementation guide