How to generate summaries with AI before clinical visits
Bernard Aceituno
@bernaceitunoDelivering the best healthcare involves gathering and processing a lot of information about patients, their conditions and past events—usually on a tight schedule, minutes before an appointment. With primitive IT search and summarization tools, medical staff are wasting time interacting with electronic health records, a productivity loss that could be costing the entire US health sector between $142 to $371 billion over a 15-year period.
But implementing a solution to this problem isn’t hard or costly: Stack AI combines its robust platform connection features with secure and HIPAA-compliant AI models to help your team get the information they need, without ever exposing personal data. In this step-by-step tutorial, you’ll learn how to build a clinical history summarization tool that generates a report from a patient ID.
How to build a clinical history summarization tool
- Connect inputs, run API calls and summarize results
- Save the reports to an external database
- Set up the reporting user interface
- Share it with your team
- Keep track of usage and analytics
- Improve your clinical history summarization tool
Connect inputs, run API calls and summarize results
With this tool, your medical staff will get a complete and accurate report on a patient by typing a patient ID into a form. Stack AI will search your data sources for the patient records, pull all past appointment information, pack it into a report and then store it in a separate database for easy tracking.
This tutorial gives you the basic understanding on how to build a tool with this functionality, but the actual steps may be different depending on the database technologies your practice uses. Always remember to refer to their API documentation and technical specifications to retrieve and send data.
Calling APIs with the Python node
We have a database with 2 tables:
- Patients holds patient information, such as their ID, full name and email.
- Events stores past appointments, such as doctor, appointment type and notes.
We’ll be using a Python script to search for the patient by their ID. Then, the script will also search all events and retrieve those related to the patient.
On a new Stack AI project, expand the Logic section, drag and drop a Python node onto the canvas.
Drop Python Node
Connect the input node to the Python node. This will make the Stack AI variable in-0 available to use in the code.
Connect Input to Python Node
Click the cogwheel icon at the top-right of the node to open the code area.
Click Settings Python Node
Copy and paste the following Python code snippet into the code area. Notice the comments at the top of every section.
Paste Code into Python Settings
This code won’t work straight away for you because it’s tailored for the database we created for this tutorial. Here are the instructions on what each part of this code achieves, so you can easily edit based on your database documentation.
Section 1 - Database IDs and table names: this section contains variables we used to interact with our specific database. In this case, they identify the database ID and the table IDs for Patients and events. These are stored in variables for easy referencing later in the code.
Section 2 - API endpoints: store all the API endpoint URLs in variables to use later.
Section 3 - API key: paste the API key from your database here to authenticate this request. Keep this key safe—whoever has it can access the database the same way you do.
Section 4 - Headers for authentication: this passes the headers of the API call. Unless your database has special requirements, this section doesn’t need to be edited.
Section 5 - Function to retrieve a patient record by Patient ID: here, we’re defining the function that will run the search—we’re not executing it yet. The parameters are specific to the database we set up for this tutorial, refer to your data source’s API documentation to understand how to run searches there.
Section 6 - Make a GET request to the Patients table: this sets up how the first API call to the Patients table will run. It combines the variables declared so far, with instructions to return the first record that matches the user’s input.
Section 7 - Function to retrieve all events related to a specific patient by Patient ID: this sets up the second API call to the Events table, retrieving all the appointments connected to this patient. Again, the parameters shown here are specific to this tutorial’s database. Refer to the API documentation for your data source.
Section 8 - User input (patient code) from the Stack AI's connected input node: this takes the connected input node’s value in Stack AI (the user’s input) and adds it to a variable.
Section 9 - Retrieve patient data and related events: this part of the code starts running everything, combining all the variables we’ve declared so far, combining them into a single result.
Section 10 - Expose output to Stack AI: to use the data inside Stack AI, the result needs to be stored inside the output variable. As you can see, we’re passing the result as a JSON-formatted output.
Edit the code to match your database’s API documentation. It’s useful to test as you go, so connect the output node to the Python Code node’s output connector temporarily to read API responses.
Connect Python Node to Output
ChatGPT or Claude are both great tools to help you write, edit and fix code, especially if you share your API documentation with them. Consider using these tools if you’re having trouble adapting this code snippet.
Setting up an LLM to format the results
Before we begin this step: if you connected the output to the Python Code node for troubleshooting, disconnect it now.
Disconnect Python from Output
We’ll be processing the JSON output using an LLM to improve readability and create a report. The Azure LLM is the best match for this task: it runs the OpenAI models in a containerized environment, offering better performance and HIPAA compliance. Click LLMs to expand the section, drag and drop the Azure LLM node onto the canvas.
Drop Azure LLM
Connect the Python Code node to the input of the Azure LLM.
Connect Python Node to Azure LLM
The Azure LLM needs instructions on how to process the data it will get from the Python Code node. Copy and paste the following instructions into the corresponding field.
Your job is to parse JSON and render it as a list. If there is no JSON data, say that the patient may not exist in the system or that the patient code is not spelled correctly._
Format your response like this example:
<response_format\>
### Patient Information
- **Full Name:** John Doe
- **Patient ID:** P-001
- **Date of Birth:** 1980-05-14
- **Email:** johndoe@example.com
- **Phone Number:** 555-1234
- **Address:** 123 Maple St
- **Insurance Provider:** HealthPlus
- **Created Time:** 2024-10-22T17:02:57.000Z
### Events
- **[Event Type]** with [Doctor/Provider] on [Event Date]: [Notes]
</response_format>
Paste System Instructions
These system instructions contain two important characteristics:
- XML tags (<response_format> and </response_format>) clearly label the formatting rules we want to pass to the LLM. While optional, using these tags improves response consistency and accuracy, so we recommend using them in situations like these.
- The formatting includes Markdown syntax to provide a guideline on how to style the output. You can see this syntax in ### (header 3) or **Full Name:** (bold).
You can edit or expand these guidelines to summarize information, correlate events or even hide personal information. Write those instructions in the prompt and the LLM will always follow them when generating new reports.
With the instructions in place, it’s time to change the user prompt. Paste the following text into the corresponding input field.
User Message: {python-0}
Paste User Prompt
The curly braces indicate a Stack AI variable. {python-0}
will contain all the data retrieved from the API call made in the Python Code node.
All that’s left is connecting the Azure LLM’s completion connector to the output.
Connect Azure LLM to Output
Save the reports to an external database
In highly-regulated environments, it’s important to keep records of who accessed which information. We’re going to explore how to save the generated report to an external database, helping you create an auditing trail if data misuse issues arise in the future.
Using a Python Code node to prepare the data
Most database systems use JSON to send and receive structured information without errors. Currently, the Azure LLM’s output is plain text, so it won’t be accepted. We’ll format this output into JSON before posting it to the external database.
Expand the Logic section and drag another Python Code node onto the canvas.
Drop Second Python Node
Connect the completion connector of the Azure LLM to the Python Code node’s input.
Connect Azure LLM to Second Python Node
Click the cogwheel icon in the Python Code node and paste the following code there.
Paste Code into Second Python Node
This code snippet builds the API call’s request body and formats it into JSON. The actual structure of your API call may be different depending on your database—please refer to its documentation and edit this code. Here’s a quick overview of what each section does.
Section 1 - Inputs for the database: this section declares the variables we’ll be using in the code snippet. This includes the report’s title, content and the database ID (required by the database we’ve set up for this tutorial). Notice that the text variable is equal to llm_0: this is the Azure LLM’s output inside Stack AI. When used later in the code, the text variable will contain the report.
Section 2 - JSON body to create a new database record: here we’re setting the structure of the JSON only—no API calls will be made with this script. This is highly dependent on your database’s specifications, so please refer to its API documentation.
Section 3 - Convert to JSON string: this final step converts the text into a JSON string so it can be accepted by the external database.
Using an API node to save the report on the external database
While you can use a Python Code node to make API calls with complete flexibility—as you saw in earlier steps in this tutorial—Stack AI also includes a dedicated node for making API calls. Instead of writing code, you can paste values inside the input fields for a more guided experience.
Click to expand the Data Loaders section on the left side, drag and drop the API node onto the canvas.
Drop API Node
Input the method, parameter placement, endpoint URL and headers in the node. For further reference, please consult the documentation on Stack AI’s API node. Here’s the configuration we used with the tutorial database:
- Method: POST to request a writing action to the database.
- Params: Body. This specifies that the API call’s parameters will be sent in the body of the request, not encoded in the URL.
- URL: paste the endpoint’s URL here.
- Headers: add authentication keys, content type and other key/value pairs as required by your database.
Once you finish configuring this node, connect the output of the second Python Code node to the Params (JSON) connector of the API node.
Connect Python Node to API
Every report generated by this tool will be saved to this external database.
Publish the project
We finished setting up the workflow part of the tool. Click Publish on the top right corner of the screen. Whenever making changes to this architecture in the future, remember to click Publish to make them available.
Click Publish
Set up the reporting user interface
Let’s configure the user interface the medical staff will use to generate reports. On the top left side of your screen, click the Export tab.
Export Tab
Stack AI publishes tools with a form interface by default. This is what we’re looking for, so we’ll leave this setting as it is. Notice that you can change the URL of the tool with a custom domain, as well as change the name and description to let users know what they can do.
General Settings
In Fields, you can show or hide inputs and outputs as well as label them. This is useful to expose the right combination of fields to your users, as well as improving user experience by letting them know what kinds of content they can expect. For this tutorial, you can change the input’s name to “Patient ID”, make it a required input, and the output’s name to “Report”.
Fields
The Style section lets you change the icon of this tool as well as the label of the form’s submit button.
Style
Stack AI offers strong security features to prevent unauthorized users from accessing this interface. At a basic level, you can set password protection. If you’d like more granularity, you can turn on SSO and assign which kinds of users can interact with the tool. And, if you’re embedding this tool into an internal platform, you can whitelist its URLs: doing so will prevent the tool from functioning even if it’s embedded somewhere else.
Security
When you’re done making changes, click the Save interface button at the top right side of the screen.
Save Interface
Share it with your team
Sharing your clinical history summarization tool is as easy as copying the link at the top of the preview and sending it over email, text message or via internal communication channels.
Copy Link and Share
If there are other builders using Stack AI on your team, you can share this workflow as inspiration and reference for future tools. Click the Share button at the top right of the screen and enter your team’s emails. This only shares a copy of your workflow: any changes they make will not reflect on your work.
Click Share
Keep track of usage and analytics
You can keep track of the performance of your tool over time in Stack AI. On the top left side of the screen click the Analytics tab.![][image26]
Here, you can see a summary of runs, users, errors and tokens used. Below these top-level statistics, there’s a list of recent runs with in-depth information about each of them. You can use the columns filter on the right side of the screen to show or hide columns.
Click Analytics
Analytics Screen
If you want a big-picture view of usage and issues, you can download an AI-generated report. It will provide a summary of the tool’s usage, including common inputs, outputs, errors and a quick assessment of the tool’s performance. You can also download logs in CSV and JSON formats for long-term storage or analysis on other platforms.
AI Report and Logs
Improve your clinical history summarization tool
Keep improving your clinical history summarization tool by noting the feedback from your team and regularly visiting the Analytics tab. Beyond upgrading the architecture with more nodes or editing prompts, Stack AI makes it easy to upgrade to the latest AI model technology. To do so, click the cogwheel icon in your LLM nodes.
Click Settings
Here you can change the provider and model to the newest, highest-performance models as they’re released. Remember to run a quick test before hitting the Publish button.
Provider and Model
Wrapping up
Now that your medical staff doesn’t have to navigate across multiple platforms to gather all the information they need, they can focus on their appointment schedule with full confidence that they have everything to provide the best care possible. All while maintaining the highest data privacy and security standards.
And there’s a lot more you can automate with Stack AI. Create a free account and explore our tutorials: