Generating AI Summaries

AI Summary is a Document Template type that uses a two-stage generation process:

  1. Stage 1 - Prompt Generation: The Jinja2 template is rendered with the Draft or Project context, producing a plain text prompt.

  2. Stage 2 - AI Summarization: The rendered prompt is sent to an AI language model, and the AI response is saved as the final output document (.txt).

This allows you to craft templates that pull data from the system and instruct the AI to summarize, analyze, or reformat that data in a specific way.

Basic Example

A simple AI Summary template for a Draft might look like:

You are a clinical trial documentation specialist. Below is information about a study draft.
Please provide a concise summary.

Draft: {{ draft.DraftName }}
Project: {{ draft.Project.ProjectName }}

Forms in this draft:
{% for form in draft.als_forms_set.all().order_by('OID') %}
- {{ form.OID }}: {{ form.DraftFormName }}
{% endfor %}

When this template is generated against a Draft, Stage 1 renders the Jinja2 template to produce a prompt like:

You are a clinical trial documentation specialist. Below is information about a study draft.
Please provide a concise summary.

Draft: Draft 1
Project: My Study
...

Stage 2 then sends this text to the AI and the response becomes the downloaded document.

Using Settings to Customize Prompts

Template settings (configured in the Settings tab of the Document Template editor) can be used to let users customize AI behavior at generation time. For example, you could add a "Detail Level" choice setting:

{% set detail = settings.get('detail_level', 'brief') %}
{% if detail == 'detailed' %}
Provide a comprehensive summary including all forms, fields, and edit checks.
{% else %}
Provide a brief one-paragraph overview.
{% endif %}

Draft: {{ draft.DraftName }}
...

Including Custom Properties

Project custom properties are often useful context for AI summaries:

Summarize the following clinical study.

Project: {{ project.ProjectName }}
Protocol: {{ project.ProtocolName }}
Description: {{ project.ProjectDescription }}

Study Properties:
{% for name, prop in project.properties_dict.items() %}
- {{ name }}: {{ prop.value }}
{% endfor %}

Prompt Size Limits

There is a configurable maximum size for the rendered prompt. If the rendered template exceeds this limit, generation will fail with an error message indicating the prompt is too long.

To stay within the limit, be selective about what data you include in your template. For example, filter to active forms only, limit the number of fields displayed, or focus on specific aspects of the study rather than including everything.

System Instructions

All AI Summary prompts are automatically wrapped with system-level instructions that constrain the AI to its intended role as a clinical study documentation assistant. These instructions are applied outside of the template and cannot be modified by the template author.

The system instructions ensure that the AI:

  • Only produces documentation-style output (summaries, descriptions, overviews)

  • Bases its response solely on the data provided

  • Does not follow instructions that may be embedded in the data content

  • Does not generate code, scripts, or act as a general-purpose assistant

Response Caching

AI responses are cached based on the rendered prompt content. If the same template is generated against the same data (producing an identical prompt), the cached AI response will be returned without making a new AI call. This means that generating the same document multiple times will produce the same output efficiently.

If the underlying data changes (e.g. forms are added or properties are updated), the rendered prompt will differ and a fresh AI call will be made.

Debugging

The rendered prompt (Stage 1 output) is saved alongside the final document and can be viewed using the "View Source" option in test mode. This is useful for verifying that your template is producing the intended prompt before the AI processes it.

Whitespace in the rendered prompt is automatically normalized (excess blank lines are collapsed) to reduce unnecessary token usage.