Session#

class scikitplot.cleanprompt.Session(**options)[source]#

A conversation: consistent placeholders, and a vault with a lifetime.

Parameters:
**options

Any keyword encode accepts. They apply to every turn.

Parameters:

options (Any)

Notes

User notes. Use it as a context manager so the values are cleared when the block ends, however it ends:

with session(ner=True) as chat:
    reply = chat.roundtrip("Summarise this for ada@example.com", send=model)

Across turns the same value keeps the same placeholder, so a model reading the thread sees one consistent participant rather than a new stranger each time.

Developer notes. Continuity is carried by passing the previous turn’s entries into the next Redactor.redact call as a seed. The redactor itself stays stateless — that is invariant I4 and the upstream defect CP-003 — and the session is the object that deliberately owns the link between two documents.

A session is not thread-safe: it is a conversation, and a conversation has one voice. Use one session per thread.

Examples

>>> with session() as chat:
...     first = chat.encode("Mail ada@example.com")
...     second = chat.encode("Ping ada@example.com again")
>>> first, second
('Mail [EMAIL-1]', 'Ping [EMAIL-1] again')
clear()[source]#

Drop every value this session holds.

Return type:

None

decode(reply, *, strict=False)[source]#

Restore values in reply from everything seen this session.

Parameters:
Return type:

str

decode_report(reply)[source]#

Restore reply and report what was and was not resolved.

Returns:
RestorationResult

Carries restored, unknown and unused.

Parameters:

reply (str)

encode(text)[source]#

Redact text, keeping placeholders consistent with earlier turns.

Parameters:
textstr

This turn’s prompt.

Returns:
str

Safe to send.

Raises:
TypeError

If text is not a string.

Parameters:

text (str)

Return type:

str

property handle: Handle#

everything seen this session.

Type:

Handle

property policy: RedactionPolicy#

the policy every turn uses.

Type:

RedactionPolicy

report()[source]#

Return what this session has done and what it can and cannot detect.

Returns:
dict

turns, entries, labels (placeholder to category, in issue order) and detection, which is the full diagnose report.

Return type:

dict[str, Any]

Notes

User notes. Safe to print, log or attach to a ticket: it names placeholders and categories, never values.

Developer notes. This returned the diagnosis alone, which is what the session can detect and says nothing about what it did. On a session object report() reads as “tell me about this session”, and a caller who wanted the turn count had to reach past the method to .turns. The diagnosis is still here, under detection; the session’s own state is now the top level.

labels maps each placeholder to its category and deliberately stops there. The placeholder is already in the text that was sent, so it discloses nothing, while the value it stands for is exactly what this submodule exists to keep out of anything that gets copied elsewhere.

roundtrip(text, send, *, strict=False)[source]#

Encode, hand to send, decode the answer.

Parameters:
textstr

This turn’s prompt.

sendcallable

Takes the safe text, returns the model’s reply. Any provider.

strictbool, default=False

Passed to decode.

Returns:
str

The reply with your values restored.

Raises:
CleanPromptError

If send does not return a string. Anything send itself raises propagates unchanged: it is the caller’s client and the caller’s error.

Parameters:
Return type:

str

Examples

>>> with session() as chat:
...     chat.roundtrip(
...         "Mail ada@example.com",
...         send=lambda safe: "Sent to " + safe.split()[-1],
...     )
'Sent to ada@example.com'
property turns: int#

How many prompts have been encoded.

Type:

int