Prompt templates and context
A template is a named instruction you want to reuse. A pack collects several templates in one JSON file or Python object. For example, the same pack can ask for findings from radiology reports, ask for follow-up recommendations from all documents, and later build a timeline for each patient.
Write a prompt pack
Save this as research_prompts.json in your working directory, or download the identical file.
{
"prompts": [
{
"name": "findings",
"categories": ["radiology"],
"prompt": "Extract the findings explicitly documented in the report."
},
{
"name": "follow_up",
"prompt": "Extract each follow-up recommendation and quote its supporting sentence."
},
{
"name": "timeline",
"prompt": "Summarize the supplied material chronologically. Cite each source label and preserve disagreements."
}
]
}| Field | What to put there |
|---|---|
name | A unique, nonblank identifier. Python uses this value to select a question, and Fyron copies it to the result’s prompt_name column. |
prompt | The instruction the model should follow. Specify the evidence to look for and how to report it. For example, ask for a supporting quote and say what to return if no recommendation is recorded. |
categories | Optional exact values from your category column. Here, "radiology" selects radiology reports. Omit the field or use [] to apply the question to every category. |
Use only these fields. The loader accepts a JSON path, a Python list of definitions, or a dictionary containing the prompts list. Duplicate names and invalid definitions raise before model requests. Loading validates the pack locally.
Run selected questions
Start with the model setup. Use one row per source document and a unique document ID. This run selects findings and follow_up from the pack. Saving timeline in the same file makes it available for a later step. Omitting prompt_names runs every template in the pack.
import pandas as pd
from fyron.llm import LLMAgent, load_prompt_templates
agent = LLMAgent() # Reads your configured LLM environment variables.
pack = load_prompt_templates("research_prompts.json")
documents = pd.DataFrame([
("0007", "A", "2025-01-01", "radiology", "Repeat imaging in six weeks."),
("0007", "B", "2025-01-08", "discharge", "Outpatient imaging was scheduled."),
("0008", "C", "2025-01-03", "radiology", "No interval change described."),
], columns=["patient_id", "doc_id", "date", "category", "text"])
answers = agent.run_prompts_on_dataframe(
documents, prompts=pack, prompt_names=["findings", "follow_up"],
text_col="text", document_id_col="doc_id", category_col="category",
context_cols=["date"], keep_cols=["patient_id", "doc_id", "date", "category"],
max_workers=4,
)
print(answers[["doc_id", "prompt_name", "status", "answer"]])Check the result
| Document | Category | Expected prompt names |
|---|---|---|
| A | radiology | findings, follow_up |
| B | discharge | follow_up |
| C | radiology | findings, follow_up |
Five rows are expected even if a model request fails. A missing B/findings row is deliberate: the category rule excluded it. A row with status="failed" records an attempted request. Inspect reason and the local sources mapping before using an answer. Even an ok answer needs review against its source text.
What context reaches the model?
Fyron appends the evidence to the instruction as JSON. Writing {date} inside a template leaves those characters in the instruction. To supply a different date for each document, use context_cols=["date"]. For the first row, the model receives the selected instruction followed by this evidence:
[{"source": "S1", "text": "Repeat imaging in six weeks.", "context": {"date": "2025-01-01"}}]| Option | What it does |
|---|---|
context_cols=["date"] | Sends the date beside each text so the model can interpret relative dates. Omit this option to send evidence without metadata. |
keep_cols=["patient_id", "date", "category"] | Copies these columns into the answer table for review, joins or later summaries. A field can be both kept and sent. |
category_col="category" | Selects rows for category-specific prompts. Add the category to context_cols if the model also needs its value. |
max_workers=4 | Allows up to four concurrent model requests with independent HTTP sessions. The default is one. Output order stays deterministic. |
Put instructions shared by every row directly in the template. For example, this Python pack works wherever the example uses prompts=pack. The same structure can be saved as JSON.
follow_up_pack = {"prompts": [{
"name": "follow_up",
"prompt": "Quote the follow-up recommendation. Use the supplied date to interpret relative timing. If timing is unclear, say so."
}]}
custom_answers = agent.run_prompts_on_dataframe(
documents, text_col="text", document_id_col="doc_id",
prompts=follow_up_pack, context_cols=["date"], keep_cols=["doc_id"],
)Reuse the answers in a summary
prompt_names=["timeline"] selects the new instruction. source_prompt_names=["follow_up"] selects the previous answers that will supply its evidence. Start with the original answers table above so each answer still carries its patient ID and date.
timelines = agent.summarize_dataframe(
answers, prompts=pack, group_cols=["patient_id"],
source_prompt_names=["follow_up"], prompt_names=["timeline"],
order_by="date", context_cols=["date"],
)
print(timelines[["patient_id", "status", "answer"]])The three follow-up rows produce two patient groups. Leave failed rows in the input so Fyron can mark an affected summary incomplete. The grouped summary guide compares this route with summarizing original documents directly.
Keep patient identifiers local unless the endpoint needs them and is approved to receive them. Review the document text itself for identifying details.
For a prompt with a fixed vocabulary, see direct classification and decision models for a complete example that validates labels and routes ambiguous answers to review.
Retry failed document questions
In the development checkout, pass the original documents and options again with retry_from=answers. Fyron returns a copy of the previous result table, replacing only eligible document/prompt pairs. Successful answers, review columns and row order are preserved. Keep the original sources dictionaries intact (for example, keep the DataFrame in memory).
answers = agent.run_prompts_on_dataframe(
documents, text_col="text", document_id_col="doc_id", prompts=pack,
prompt_names=["findings", "follow_up"], category_col="category",
context_cols=["date"], keep_cols=["patient_id", "doc_id", "date", "category"],
retry_from=answers,
)The default retries failed and incomplete. To narrow a retry, select a single template with prompt_names=["follow_up"]. Use retry_statuses=["oversized"] after adjusting the input limit, or select partial for a partial summary. For URL inputs only documents required by eligible tasks are fetched. Reuse the same model, prompt definitions, document identities and group membership for comparable results. New input rows are a separate run.