triagesim.agents¶
Agents and the LLM backends that drive them.
See Agents and LLM backends for usage and for how to write a custom backend.
LLM backends¶
BaseLLM
¶
Bases: ABC
Abstract interface for ALL language model backends.
Agents will call .generate() to produce structured outputs.
generate
abstractmethod
¶
generate(
prompt: str,
max_tokens: Optional[int] = None,
stop: Optional[List[str]] = None,
**kwargs,
) -> Union[str, BaseAgentOutput]
Generate a response given a prompt.
This method must return either: - raw text (for unstructured outputs), or - a structured Pydantic model (if output_type was provided).
The calling agent assumes the correct model/schema is applied
via the backend's output_type during instantiation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt | str | the model input text | required |
max_tokens | Optional[int] | optional token cap | None |
stop | Optional[List[str]] | optional stop sequences | None |
**kwargs | backend-specific overrides | {} |
Returns:
| Type | Description |
|---|---|
Union[str, BaseAgentOutput] | Model output (string or Pydantic output model) |
OpenRouterLLM
¶
OpenRouterLLM(
model_name: str,
output_type: Type[BaseAgentOutput],
**agent_kwargs,
)
Bases: BaseLLM
LLM backend using OpenRouter via the pydantic_ai SDK.
This wraps an Agent configured with a given output model type
so that .generate() returns a structured output or text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name | str | OpenRouter model specifier (e.g., "google/gemini-3-pro-preview") | required |
output_type | Type[BaseAgentOutput] | a subclass of BaseAgentOutput Pydantic model that the Agent will produce directly. | required |
Usage
llm = OpenRouterLLM("google/gemini-3-pro-preview", output_type=NurseOutput) nurse_action = llm.generate(prompt)
Create an Agent configured to produce the right output type.
The API key is resolved from triagesim.config.OPENROUTER_API_KEY.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name | str | the OpenRouter model identifier | required |
output_type | Type[BaseAgentOutput] | Pydantic model to enforce output structure | required |
agent_kwargs | any extra fields passed to Agent() | {} |
generate
¶
generate(
prompt: str,
max_tokens: Optional[int] = None,
stop: Optional[List[str]] = None,
**kwargs,
) -> Union[str, BaseAgentOutput]
Run a synchronous request against the configured agent.
Because the agent is configured with output_type, this method
should return a structured Pydantic model or a simple string
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt | str | the text prompt | required |
max_tokens | Optional[int] | optionally limit tokens | None |
stop | Optional[List[str]] | optional stop sequences | None |
**kwargs | passed to agent.run_sync() | {} |
Returns:
| Type | Description |
|---|---|
Union[str, BaseAgentOutput] | Model output from the LLM call |
Agents¶
NurseAgent
¶
NurseAgent(
llm: BaseLLM,
persona: NursePersona,
algorithm: str = "esi",
)
Nurse agent responsible for: - Choosing the next triage action (NurseOutput) - Inferring belief updates from the nurse's perspective
The SAME LLM + persona is used for both decision-making and belief inference.
act
¶
act(history: str, known_vitals: set[str]) -> NurseOutput
Given dialogue history, produce the next nurse action as a structured NurseOutput.
infer_belief_updates
¶
Infer belief updates from the nurse's perspective.
This is called by the environment AFTER a patient utterance. Failures must never crash the episode.
PatientAgent
¶
PatientAgent(llm: BaseLLM, persona: PatientPersona)
PatientAgent generates patient utterances conditioned on: - dialogue history - a categorical persona - explicit response budget rules
act
¶
act(
history: str, chief_complaint: str, pain: int
) -> PatientOutput
Generate the patient's next utterance as a structured PatientOutput.
The LLM backend MUST have been initialized with
output_type = PatientOutput