Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion build_scripts/export_adversarial_benchmark_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async def _load_result_async(*, scenario_result_id: str) -> ScenarioResult:
env_files=[],
silent=True,
)
results = CentralMemory.get_memory_instance().get_scenario_results(
results = await CentralMemory.get_memory_instance().get_scenario_results_async(
scenario_result_ids=[scenario_result_id],
)
if not results:
Expand Down
2 changes: 1 addition & 1 deletion doc/code/datasets/1_loading_datasets.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@
"await memory.add_seed_datasets_to_memory_async(datasets=datasets, added_by=\"pyrit\")\n",
"\n",
"# Memory has flexible querying capabilities\n",
"memory.get_seeds(harm_categories=[\"illegal\"], seed_type=\"objective\")"
"(await memory.get_seeds_async(harm_categories=[\"illegal\"], seed_type=\"objective\"))"
]
}
],
Expand Down
2 changes: 1 addition & 1 deletion doc/code/datasets/1_loading_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,4 @@
await memory.add_seed_datasets_to_memory_async(datasets=datasets, added_by="pyrit")

# Memory has flexible querying capabilities
memory.get_seeds(harm_categories=["illegal"], seed_type="objective")
(await memory.get_seeds_async(harm_categories=["illegal"], seed_type="objective"))
2 changes: 1 addition & 1 deletion doc/code/executor/5_workflow.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@
"from pyrit.memory import CentralMemory\n",
"\n",
"memory = CentralMemory.get_memory_instance()\n",
"processing_response = memory.get_message_pieces(conversation_id=result.processing_conversation_id)\n",
"processing_response = await memory.get_message_pieces_async(conversation_id=result.processing_conversation_id)\n",
"\n",
"print(f\"Attack result status: {result.status}\")\n",
"print(f\"Response from processing callback: {processing_response}\")"
Expand Down
2 changes: 1 addition & 1 deletion doc/code/executor/5_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ async def processing_callback_async() -> str:
from pyrit.memory import CentralMemory

memory = CentralMemory.get_memory_instance()
processing_response = memory.get_message_pieces(conversation_id=result.processing_conversation_id)
processing_response = await memory.get_message_pieces_async(conversation_id=result.processing_conversation_id)

print(f"Attack result status: {result.status}")
print(f"Response from processing callback: {processing_response}")
Expand Down
2 changes: 1 addition & 1 deletion doc/code/executor/6_benchmark.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@
"seed_prompts = SeedDataset.from_yaml_file(Path(DATASETS_PATH) / \"lexicons\" / \"fairness\" / \"gendered_professions.yaml\")\n",
"await memory.add_seeds_to_memory_async(seeds=seed_prompts.prompts, added_by=\"airt\") # type: ignore\n",
"\n",
"prompt_groups = memory.get_seed_groups(dataset_name=\"2025_08_airt_fairness_gendered_professions\")\n",
"prompt_groups = await memory.get_seed_groups_async(dataset_name=\"2025_08_airt_fairness_gendered_professions\")\n",
"jobs = [prompt_group.prompts[0].value for prompt_group in prompt_groups]\n",
"\n",
"print(f\"Loaded {len(jobs)} professions\")"
Expand Down
2 changes: 1 addition & 1 deletion doc/code/executor/6_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
seed_prompts = SeedDataset.from_yaml_file(Path(DATASETS_PATH) / "lexicons" / "fairness" / "gendered_professions.yaml")
await memory.add_seeds_to_memory_async(seeds=seed_prompts.prompts, added_by="airt") # type: ignore

prompt_groups = memory.get_seed_groups(dataset_name="2025_08_airt_fairness_gendered_professions")
prompt_groups = await memory.get_seed_groups_async(dataset_name="2025_08_airt_fairness_gendered_professions")
jobs = [prompt_group.prompts[0].value for prompt_group in prompt_groups]

print(f"Loaded {len(jobs)} professions")
Expand Down
2 changes: 1 addition & 1 deletion doc/code/executor/7_promptgen.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@
" prompt_templates=prompt_templates,\n",
")\n",
"\n",
"FuzzerResultPrinter().print_result(result=result)"
"(await FuzzerResultPrinter().print_result_async(result=result))"
]
}
],
Expand Down
2 changes: 1 addition & 1 deletion doc/code/executor/7_promptgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,4 @@
prompt_templates=prompt_templates,
)

FuzzerResultPrinter().print_result(result=result)
(await FuzzerResultPrinter().print_result_async(result=result))
28 changes: 23 additions & 5 deletions doc/code/memory/0_memory.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,32 @@ To simplify memory interaction, the `pyrit.memory.CentralMemory` class automatic
**Manual Memory Setting**:

At the beginning of each notebook, make sure to call:
```
# Import initialize_pyrit_async
# Import the specific constant for the MemoryDatabaseType, or provide the literal value
from pyrit.setup import initialize_pyrit_async, IN_MEMORY, SQLITE, AZURE_SQL
```python
from pyrit.memory import CentralMemory
from pyrit.setup import IN_MEMORY, initialize_pyrit_async

await initialize_pyrit_async(memory_db_type: MemoryDatabaseType, memory_instance_kwargs: Any | None)
await initialize_pyrit_async(memory_db_type=IN_MEMORY)
memory = CentralMemory.get_memory_instance()
messages = await memory.get_conversation_messages_async(conversation_id="example")
```

Use the `_async` memory methods in async code. The synchronous methods remain
available during deprecation. In-memory SQLite serializes transactions across
threads and event loops to prevent shared-cache table locks.

Repeated initialization reuses the existing memory without running schema setup
again. Setup raises an error if `CentralMemory` and the requested backend singleton
disagree, or if setup tries to change an existing SQLite instance between in-memory
and persistent modes.

Do not overlap synchronous and async sessions on the same event-loop thread.
In-memory SQLite raises an error instead of blocking that loop. Use async methods
for concurrent work on an event loop.

Before an event loop stops, call `await memory.dispose_loop_resources_async()`
on that loop. After all memory work stops, call `await memory.dispose_engine_async()`
to close the remaining resources.

The `MemoryDatabaseType` is a `Literal` with 3 options: IN_MEMORY, SQLITE, AZURE_SQL. (Read more below)
- `initialize_pyrit_async` takes the `MemoryDatabaseType` and an argument list (`memory_instance_kwargs`), to initialize the shared memory instance.

Expand Down
2 changes: 1 addition & 1 deletion doc/code/memory/1_sqlite_memory.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
"\n",
"# Use in-memory database to avoid file corruption issues\n",
"memory = SQLiteMemory(db_path=\":memory:\")\n",
"memory.print_schema()"
"(await memory.print_schema_async())"
]
}
],
Expand Down
2 changes: 1 addition & 1 deletion doc/code/memory/1_sqlite_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@

# Use in-memory database to avoid file corruption issues
memory = SQLiteMemory(db_path=":memory:")
memory.print_schema()
(await memory.print_schema_async())
8 changes: 4 additions & 4 deletions doc/code/memory/2_basic_memory_programming.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@
"\n",
"memory = SQLiteMemory(db_path=\":memory:\")\n",
"\n",
"memory.add_message_to_memory(request=message_list[0].to_message())\n",
"memory.add_message_to_memory(request=message_list[1].to_message())\n",
"memory.add_message_to_memory(request=message_list[2].to_message())\n",
"(await memory.add_message_to_memory_async(request=message_list[0].to_message()))\n",
"(await memory.add_message_to_memory_async(request=message_list[1].to_message()))\n",
"(await memory.add_message_to_memory_async(request=message_list[2].to_message()))\n",
"\n",
"entries = memory.get_conversation_messages(conversation_id=conversation_id)\n",
"entries = await memory.get_conversation_messages_async(conversation_id=conversation_id)\n",
"\n",
"for entry in entries:\n",
" print(entry)"
Expand Down
8 changes: 4 additions & 4 deletions doc/code/memory/2_basic_memory_programming.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@

memory = SQLiteMemory(db_path=":memory:")

memory.add_message_to_memory(request=message_list[0].to_message())
memory.add_message_to_memory(request=message_list[1].to_message())
memory.add_message_to_memory(request=message_list[2].to_message())
(await memory.add_message_to_memory_async(request=message_list[0].to_message()))
(await memory.add_message_to_memory_async(request=message_list[1].to_message()))
(await memory.add_message_to_memory_async(request=message_list[2].to_message()))

entries = memory.get_conversation_messages(conversation_id=conversation_id)
entries = await memory.get_conversation_messages_async(conversation_id=conversation_id)

for entry in entries:
print(entry)
22 changes: 11 additions & 11 deletions doc/code/memory/5_advanced_memory.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@
"from pyrit.prompt_target import TextTarget\n",
"\n",
"memory = CentralMemory.get_memory_instance()\n",
"prompts = memory.get_message_pieces(labels={\"prompt_group\": group1})\n",
"prompts = await memory.get_message_pieces_async(labels={\"prompt_group\": group1})\n",
"\n",
"# Print original values of queried message pieces (including responses)\n",
"for piece in prompts:\n",
Expand Down Expand Up @@ -387,7 +387,7 @@
" value=filter_target_class,\n",
" )\n",
"\n",
" target_class_pieces = memory.get_message_pieces(\n",
" target_class_pieces = await memory.get_message_pieces_async(\n",
" identifier_filters=[target_class_filter],\n",
" )\n",
"\n",
Expand Down Expand Up @@ -434,7 +434,7 @@
" partial_match=True,\n",
")\n",
"\n",
"openai_pieces = memory.get_message_pieces(\n",
"openai_pieces = await memory.get_message_pieces_async(\n",
" identifier_filters=[openai_filter],\n",
")\n",
"\n",
Expand Down Expand Up @@ -479,7 +479,7 @@
" value=\"Base64Converter\",\n",
")\n",
"\n",
"base64_pieces = memory.get_message_pieces(\n",
"base64_pieces = await memory.get_message_pieces_async(\n",
" identifier_filters=[converter_filter],\n",
")\n",
"\n",
Expand Down Expand Up @@ -522,7 +522,7 @@
" value=\"TextTarget\",\n",
")\n",
"\n",
"combined_pieces = memory.get_message_pieces(\n",
"combined_pieces = await memory.get_message_pieces_async(\n",
" identifier_filters=[text_target_filter, converter_filter],\n",
")\n",
"\n",
Expand Down Expand Up @@ -561,7 +561,7 @@
],
"source": [
"# Retrieve prompts from our labeled group that specifically went through Base64Converter\n",
"labeled_and_filtered = memory.get_message_pieces(\n",
"labeled_and_filtered = await memory.get_message_pieces_async(\n",
" labels={\"prompt_group\": group1},\n",
" identifier_filters=[converter_filter],\n",
")\n",
Expand Down Expand Up @@ -613,7 +613,7 @@
") # intentionally bad scorer that matches when the phrase 'assist' is present in response. But good for demo.\n",
"\n",
"# Retrieve assistant responses from Part 1\n",
"assistant_pieces = memory.get_message_pieces(\n",
"assistant_pieces = await memory.get_message_pieces_async(\n",
" labels={\"prompt_group\": group1},\n",
" role=\"assistant\",\n",
")\n",
Expand Down Expand Up @@ -669,7 +669,7 @@
" value=\"SubStringScorer\",\n",
")\n",
"\n",
"all_substring_scores = memory.get_scores(\n",
"all_substring_scores = await memory.get_scores_async(\n",
" identifier_filters=[scorer_class_filter],\n",
")\n",
"\n",
Expand Down Expand Up @@ -722,7 +722,7 @@
" value=\"molotov\",\n",
")\n",
"\n",
"molotov_scores = memory.get_scores(\n",
"molotov_scores = await memory.get_scores_async(\n",
" identifier_filters=[molotov_scorer_filter],\n",
")\n",
"\n",
Expand All @@ -739,7 +739,7 @@
" value=\"launder\",\n",
")\n",
"\n",
"launder_scores = memory.get_scores(\n",
"launder_scores = await memory.get_scores_async(\n",
" identifier_filters=[launder_scorer_filter],\n",
")\n",
"\n",
Expand All @@ -756,7 +756,7 @@
" value=\"assist\",\n",
")\n",
"\n",
"assist_scores = memory.get_scores(\n",
"assist_scores = await memory.get_scores_async(\n",
" identifier_filters=[assist_scorer_filter],\n",
")\n",
"\n",
Expand Down
22 changes: 11 additions & 11 deletions doc/code/memory/5_advanced_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
from pyrit.prompt_target import TextTarget

memory = CentralMemory.get_memory_instance()
prompts = memory.get_message_pieces(labels={"prompt_group": group1})
prompts = await memory.get_message_pieces_async(labels={"prompt_group": group1})

# Print original values of queried message pieces (including responses)
for piece in prompts:
Expand Down Expand Up @@ -141,7 +141,7 @@
value=filter_target_class,
)

target_class_pieces = memory.get_message_pieces(
target_class_pieces = await memory.get_message_pieces_async(
identifier_filters=[target_class_filter],
)

Expand All @@ -164,7 +164,7 @@
partial_match=True,
)

openai_pieces = memory.get_message_pieces(
openai_pieces = await memory.get_message_pieces_async(
identifier_filters=[openai_filter],
)

Expand All @@ -187,7 +187,7 @@
value="Base64Converter",
)

base64_pieces = memory.get_message_pieces(
base64_pieces = await memory.get_message_pieces_async(
identifier_filters=[converter_filter],
)

Expand All @@ -208,7 +208,7 @@
value="TextTarget",
)

combined_pieces = memory.get_message_pieces(
combined_pieces = await memory.get_message_pieces_async(
identifier_filters=[text_target_filter, converter_filter],
)

Expand All @@ -225,7 +225,7 @@

# %%
# Retrieve prompts from our labeled group that specifically went through Base64Converter
labeled_and_filtered = memory.get_message_pieces(
labeled_and_filtered = await memory.get_message_pieces_async(
labels={"prompt_group": group1},
identifier_filters=[converter_filter],
)
Expand Down Expand Up @@ -257,7 +257,7 @@
) # intentionally bad scorer that matches when the phrase 'assist' is present in response. But good for demo.

# Retrieve assistant responses from Part 1
assistant_pieces = memory.get_message_pieces(
assistant_pieces = await memory.get_message_pieces_async(
labels={"prompt_group": group1},
role="assistant",
)
Expand Down Expand Up @@ -287,7 +287,7 @@
value="SubStringScorer",
)

all_substring_scores = memory.get_scores(
all_substring_scores = await memory.get_scores_async(
identifier_filters=[scorer_class_filter],
)

Expand All @@ -310,7 +310,7 @@
value="molotov",
)

molotov_scores = memory.get_scores(
molotov_scores = await memory.get_scores_async(
identifier_filters=[molotov_scorer_filter],
)

Expand All @@ -327,7 +327,7 @@
value="launder",
)

launder_scores = memory.get_scores(
launder_scores = await memory.get_scores_async(
identifier_filters=[launder_scorer_filter],
)

Expand All @@ -344,7 +344,7 @@
value="assist",
)

assist_scores = memory.get_scores(
assist_scores = await memory.get_scores_async(
identifier_filters=[assist_scorer_filter],
)

Expand Down
10 changes: 5 additions & 5 deletions doc/code/memory/6_azure_sql_memory.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
"await initialize_pyrit_async(memory_db_type=AZURE_SQL) # type: ignore\n",
"\n",
"memory = CentralMemory.get_memory_instance()\n",
"memory.print_schema() # type: ignore"
"(await memory.print_schema_async()) # type: ignore"
]
},
{
Expand Down Expand Up @@ -170,11 +170,11 @@
" ),\n",
"]\n",
"\n",
"memory.add_message_to_memory(request=Message(message_pieces=[message_list[0]]))\n",
"memory.add_message_to_memory(request=Message(message_pieces=[message_list[1]]))\n",
"memory.add_message_to_memory(request=Message(message_pieces=[message_list[2]]))\n",
"(await memory.add_message_to_memory_async(request=Message(message_pieces=[message_list[0]])))\n",
"(await memory.add_message_to_memory_async(request=Message(message_pieces=[message_list[1]])))\n",
"(await memory.add_message_to_memory_async(request=Message(message_pieces=[message_list[2]])))\n",
"\n",
"entries = memory.get_conversation_messages(conversation_id=conversation_id)\n",
"entries = await memory.get_conversation_messages_async(conversation_id=conversation_id)\n",
"\n",
"for entry in entries:\n",
" print(entry)"
Expand Down
Loading
Loading