How MSD uses Amazon Bedrock to translate natural language into SQL for complex healthcare databases

Tesfagabir Meharizghi, Aude Genevay, Shinan Zhang, Rifat Jafreen, Henry Wang, Vladimir Turzhitsky, Varun Kumar Nomula, Yezhou Sun18 Nov 2024

Problem

Analysts at the pharmaceutical company MSD write SQL by hand against healthcare claims databases. Out-of-the-box text-to-SQL from LangChain or LlamaIndex fails on four things in this data. Categorical attributes are stored as numeric codes, so "female" must become sex = 2. Column names are not self-explanatory. Questions can carry code lists too long for the model to copy reliably. Questions are also ambiguous, so a user cannot tell whether the SQL answers what they meant.

Example from the article: the DE-SynPUF synthetic Medicare claims dataset (2008–2010), where the question "How many female patients have been admitted to a hospital in 2008?" needs the right sex code and the right claims table.

Solution in brief

Put the schema, sample rows, column descriptions and a few question-SQL examples into the system prompt. Add lookup tools for code columns via function calling, swap long code lists for a placeholder, and have the model explain its reading of the question next to the SQL.

Questions this case answers
  • How to generate correct SQL over a database whose columns are coded values and cryptic names, so that naive schema-only prompting returns wrong filters
  • How to keep an LLM from corrupting long literal lists (identifiers, codes) when it writes a query
  • How to make an LLM filter on coded columns (sex, race, state) when the database stores numbers, not labels?
  • How to let analysts ask questions about long lists of medical codes without the LLM mangling the lists?

Tools and methods

  • Amazon Bedrock Converse API with Claude 3.5 Sonnet — runs the model, passes the tool definitions and returns tool calls.
  • Tool calling — lookup functions for sex, race and state codes.
  • Few-shot prompting — question-SQL pairs that show tool use and code placeholders.
  • Prompt engineering — XML-tagged schema, descriptions and examples in one system prompt.
  • SQLite, Streamlit — demo database and model-picker UI.
Continue reading — it's free

Google or a one-time email code. No password.