Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
4620980
Fluent api for working with LLMs and Capr API for allowing power user…
azimov Jan 21, 2026
e440739
Missing functionality added to fluent api
azimov Jan 21, 2026
e64fb14
Updated skill and added a system prompt for usage with GPT-4o
azimov Jan 21, 2026
7b2fb9d
Updated api and skill to cover complex nested cases with toy secondar…
azimov Jan 21, 2026
beecd7d
Updated gpt system prompt
azimov Jan 22, 2026
6980fd5
Fixes for crazy prompt hallucinations
azimov Jan 22, 2026
2669c41
Merge remote-tracking branch 'origin/fluent-api-tooling' into fluent-…
azimov Jan 22, 2026
8241626
Added automatic skill update (deterministic from codebase, not via llm)
azimov Jan 22, 2026
40bcea1
Prompt added to prompts dir
azimov Jan 22, 2026
e285968
Prompt types and examples suited to different model classes
azimov Jan 22, 2026
ba2d921
Prompt builder utility function
azimov Jan 22, 2026
111c01b
Prompt builder refactor
azimov Jan 22, 2026
5781486
Merge branch 'develop' into fluent-api-tooling
azimov Jan 27, 2026
4821dcd
Simplified the skill and LLM usage by exploiting context managers whi…
azimov Jan 27, 2026
1370c2d
Updated the skill to help the dumbest of models
azimov Jan 27, 2026
fe92d16
removed skill file commit hooks
azimov Jan 29, 2026
33a9483
Default list behaviour for cohort elements prevents some downstream m…
azimov Jan 29, 2026
832aa93
Updated markdown skill to refine phenotype builder
azimov Feb 3, 2026
80c12c6
Validation utilities for more inline checking
azimov Feb 3, 2026
df1f160
Utility functions for making modifications to existing cohorts
azimov Feb 3, 2026
4d17deb
cleaned up artifact
azimov Feb 3, 2026
1761b99
Code cleanup
azimov Feb 3, 2026
2fa408b
validators and tests
azimov Feb 3, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
227 changes: 0 additions & 227 deletions .agent/cohort_builder_enhancements.md

This file was deleted.

125 changes: 87 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,64 +83,113 @@ circe process cohort.json --validate --sql --markdown

See the [CLI Documentation](#command-line-interface) section below for more details.

### Python API
## High-Level Building APIs

CIRCE Python provides two high-level APIs for building cohorts without manually constructing complex JSON/Pydantic models.

### 1. Context Manager API (`circe.cohort_builder`)
**Best for: LLMs, beginners, and interactive development.**
This Pythonic API uses `with` blocks and auto-builds on exit.

```python
from circe.cohort_builder import CohortBuilder
from circe.vocabulary import concept_set, descendants
from circe.api import build_cohort_query

# 1. Define concept sets
t2dm = concept_set(descendants(201826), id=1, name="T2DM")
metformin = concept_set(descendants(1503297), id=2, name="Metformin")

# 2. Build cohort using context manager
with CohortBuilder("New Metformin Users with T2DM") as cohort:
cohort.with_concept_sets(t2dm, metformin)
cohort.with_drug(concept_set_id=2) # Entry: metformin exposure
cohort.first_occurrence() # First exposure only
cohort.with_observation_window(prior_days=365) # 365 days prior
cohort.min_age(18) # Adults only
cohort.require_condition(concept_set_id=1, within_days_before=365)

with cohort.include_rule("No Prior Insulin") as rule:
rule.exclude_drug(3, anytime_before=True)

# 3. Access the built expression and generate SQL
sql = build_cohort_query(cohort.expression)
```

### 2. Capr-style API (`circe.capr`)
**Best for: Power users familiar with the R `Capr` package.**
A functional, declarative API for programmatic cohort definition.

```python
from circe.capr import (
cohort, entry, condition_occurrence, drug_exposure,
at_least, exactly, with_all, during_interval, event_starts
)

# Build using functional composition
my_cohort = cohort(
title="T2DM on Metformin",
entry=entry(
drug_exposure(concept_set_id=2, first_occurrence=True),
observation_window=(365, 0)
),
attrition=attrition(
has_t2dm=with_all(
at_least(1, condition_occurrence(1),
during_interval(event_starts(before=365)))
)
)
).build()
```

## Advanced Usage: Raw Pydantic Models

For full control, you can use the underlying Pydantic models that replicate the Java CIRCE-BE internal structure.

```python
from circe import CohortExpression
from circe.cohortdefinition import PrimaryCriteria, ConditionOccurrence
from circe.cohortdefinition.core import ObservationFilter, ResultLimit
from circe.vocabulary import ConceptSet, ConceptSetExpression, ConceptSetItem, Concept

# Create a cohort expression
# Create a cohort expression using raw models
cohort = CohortExpression(
title="Type 2 Diabetes Cohort",
title="Raw Model Example",
primary_criteria=PrimaryCriteria(
criteria_list=[
ConditionOccurrence(
codeset_id=1,
first=True
)
],
criteria_list=[ConditionOccurrence(codeset_id=1, first=True)],
observation_window=ObservationFilter(prior_days=0, post_days=0),
primary_limit=ResultLimit(type="All")
),
concept_sets=[
ConceptSet(
id=1,
name="Type 2 Diabetes",
expression=ConceptSetExpression(
items=[
ConceptSetItem(
concept=Concept(
concept_id=201826,
concept_name="Type 2 diabetes mellitus"
),
include_descendants=True
)
]
)
)
]
concept_sets=[...]
)
```

# Generate SQL using the API
from circe.api import build_cohort_query
from circe.cohortdefinition import BuildExpressionQueryOptions

options = BuildExpressionQueryOptions()
options.cdm_schema = 'cdm'
options.vocabulary_schema = 'cdm'
options.cohort_id = 1
options.target_table = 'scratch.cohort'
sql = build_cohort_query(cohort, options)
print(sql)
## AI Agent Integration

CIRCE Python provides skill documentation for AI agents that need to generate cohort definitions programmatically.

```python
from circe import get_cohort_builder_skill, list_skills

# List available skills
print(list_skills()) # ['cohort_builder']

# Get skill documentation for an AI agent
skill_docs = get_cohort_builder_skill()
# Returns markdown documentation describing the CohortBuilder API
```

The skill documentation includes:
- Context manager API usage patterns
- Available entry event methods
- Inclusion/exclusion criteria syntax
- Named rule contexts for attrition tracking

## What's Included

This package provides a complete Python implementation of CIRCE-BE with:

- **3,400+ passing tests** with focused coverage on core logic
- **800+ passing tests** with focused coverage on core logic
- **18+ SQL builders** for all OMOP CDM domains:
- Condition Occurrence/Era
- Drug Exposure/Era
Expand Down
8 changes: 7 additions & 1 deletion circe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
build_cohort_query,
cohort_print_friendly,
)
from .skills import get_cohort_builder_skill, get_skill, list_skills

from circe.cohortdefinition import (
CohortExpression, Criteria, CorelatedCriteria, DemographicCriteria,
Expand Down Expand Up @@ -186,5 +187,10 @@ def get_json_schema() -> dict:
"cohort_expression_from_json",
"build_cohort_query",
"cohort_print_friendly",
"safe_model_rebuild"
"safe_model_rebuild",
# Skills for AI agents
"get_cohort_builder_skill",
"get_skill",
"list_skills",
]

Loading