Bell Eapen MD, PhD.

Bringing Digital health & Gen AI research to life!

LLM-in-the Loop CQL execution 

TL;DR: I have added experimental support for using LLMs to interpret DocumentReference-based definitions in CQL of the type:

define HasVisualFootExamThisMonth: 
exists( 
[DocumentReference] D 
where D.status.value = ‘current’ 

 (Read Part I here)

CQL - Clinical Quality Language

Clinical Quality Language (CQL)

Image credit: Grufo, CC BY-SA 4.0 https://creativecommons.org/licenses/by-sa/4.0, via Wikimedia Commons

CQL is a domain-specific language that allows clinicians and researchers to express queries and retrieve data from electronic health records (EHRs) in a standardized and interoperable way. CQL can be used to define clinical quality measures, decision support rules, cohort identification criteria, and other clinical logic. Though CQL can be based on the HL7 FHIR standard, which defines a common data model and terminology for health information, the language itself is schema-independent. CQL aims to improve the quality and efficiency of healthcare by enabling the reuse and sharing of clinical knowledge across different systems and platforms. Here is an example.

CQL is a high-level language that needs to be translated into a lower-level language that can be executed. To facilitate this process, CQL supports a mechanism for transforming CQL expressions into an intermediate representation called Expression Logical Model (ELM). ELM is a platform-independent XML/JSON format that preserves the semantics and structure of CQL expressions but removes the syntactic variations and ambiguities of natural languages. ELM can then be converted into executable formats such as SQL or FHIRPath, depending on the target data source and system. This way, CQL to ELM translation enables the portability and interoperability of clinical queries across different platforms and environments. Here is an example.

CQL can leverage FHIRPath to express queries over FHIR resources in a consistent and interoperable way. However, FHIRPath alone is not sufficient to handle the semantic variations and complexity of clinical data. For example, different systems may use different codes or terminologies to represent the same concept, such as diabetes or foot exam. To address this issue, CQL supports the use of terminology services, which are external services that provide mappings and translations between different code systems and value sets. CQL can invoke terminology services to resolve the codes and values used in the queries and align them with the ones used in the data source. This way, CQL can execute queries over FHIR data using both syntactic and semantic interoperability. 

One limitation of FHIRPath-based CQL execution is that it cannot handle assertions in the FHIR DocumentReference resource. I have forked the nodejs CQL execution engine to add a hook that can call an LLM when it encounters a DocumentReference here:

https://github.com/dermatologist/cql-execution 

I will post a link to an end-to-end application that uses this hook. Also, I have added experimental support for other FHIR servers to the VSAC enabled code service engine. Do comment below, if you find this useful and use it for your project! 

Clinical Query Language – Part 1

Clinical Query Language (CQL) is a high-level query language to represent and generate unambiguous quality measures or clinical decision rules. I am not a CQL expert. These are my notes from a system development perspective. I am trying to make sense of this emerging concept and add my notes here in the hope that others may find this useful.

U.S. Navy photo by Chief Warrant Officer 4 Seth Rossman. / Public domain (wikimedia)

Clinical Query Language is designed to be intuitive for clinicians authoring the queries for quality measures and clinical decision support. The decision support rules are mostly alert type rules at the individual and population level that is calculated from a database (not usually diagnostic decision support). You can use any data model with CQL.

Here is an example segment of CQL:

define “InDemographic”:
AgeInYearsAt(start of MeasurementPeriod) >= 16 and AgeInYearsAt(start of MeasurementPeriod) < 24
and “Patient”.”gender” in “Female Administrative Sex”

As Clinical Query Language follows strict semantics, you can autogenerate lexers, parsers and visitors using ANTLR. In simple terms, CQL’s semantics can be represented as a ‘grammar’ that ANTLR can read and generate code to process any CQL in a variety of programming languages, including Java, Javascript, Python, C# and Go. The CQL grammar files are here: https://cql.hl7.org/08-a-cqlsyntax.html. Incidentally, CQL grammar inherits from fhirpath.

If you wish to generate code from these files, there are two things to note:

  • You need to rename CQL.g4 to cql.g4 as the library names are case-sensitive and should correspond to the filename.
  • Put fhirpath.g4 in the same folder as cql.g4, and cql refers to fhirpath grammar.

Clinical Query Language aims to provide a high-level domain-independent language for clinicians that can be translated into low-level database logic. As CQL does not prescribe a data model, an intermediary format linking CQL to the data management logic is required. That is called the Expression Logical Model (ELM) which we will discuss in part 2.

Update: cql-exec-vsac is a VSAC-enabled code service for the JavaScript CQL Execution project. This allows the CQL Execution Engine to execute CQL containing references to Value Sets that are published in the National Library of Medicine’s (NLM) Value Set Authority Center (VSAC). I have added a feature that adds support for any FHIR server other than VSAC to support private terminology servers. Check it out!