Repository navigation
docs: create the client in every snippet #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,9 @@ Event.tool_result("send_email", {"status": "sent"}, call_id="call_1") | |
| Send the same `call_id` on both, or set the result's `parent_event_id` to the call's `event_id`: | ||
|
|
||
| ```python | ||
| from whitecircle import Event, WhiteCircle | ||
|
|
||
| client = WhiteCircle() | ||
| client.events.check_many( | ||
| [ | ||
| Event.tool_call("lookup_order", {"order_id": "4815"}, event_id="lookup_args"), | ||
|
|
@@ -39,8 +42,9 @@ client.events.check_many( | |
| - `links` are outputs the Event consumed, from this run or another run in the Environment, up to 50. | ||
|
|
||
| ```python | ||
| from whitecircle import EventLink | ||
| from whitecircle import Event, EventLink, WhiteCircle | ||
|
|
||
| client = WhiteCircle() | ||
| run = "run_pr_1234" | ||
| client.events.check(Event.agent("Review this PR", field="input", event_id="main", name="orchestrator"), run_id=run) | ||
| client.events.check( | ||
|
|
@@ -66,6 +70,9 @@ Record how long a step took with timezone-aware datetimes (the API needs RFC 333 | |
| ```python | ||
| from datetime import UTC, datetime | ||
|
|
||
| from whitecircle import Event, WhiteCircle | ||
|
|
||
| client = WhiteCircle() | ||
| started = datetime.now(UTC) | ||
| output = run_search() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make the timed-step example self-contained. The block calls undefined As per coding guidelines, “every snippet must run against the current API.”
|
||
| client.events.check( | ||
|
|
@@ -76,6 +83,9 @@ client.events.check( | |
| ## Load the run graph | ||
|
|
||
| ```python | ||
| from whitecircle import WhiteCircle | ||
|
|
||
| client = WhiteCircle() | ||
| for page in client.runs.iter_graph("run_pr_1234"): | ||
| for node in page.nodes: | ||
| print(node.event_id, node.type, node.status, node.effective_violation) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,7 @@ checked text, and checks its tool calls. Every call sends only the new Event; ea | |
| ```python | ||
| from typing import Any | ||
|
|
||
| from whitecircle import AsyncWhiteCircle, Event | ||
| from whitecircle import AsyncWhiteCircle | ||
|
|
||
| wc = AsyncWhiteCircle() # WHITECIRCLE_API_KEY, WHITECIRCLE_ENVIRONMENT_ID | ||
|
|
||
|
|
@@ -20,6 +20,11 @@ def metadata_for(user: User) -> dict[str, Any]: | |
| ## 1. Check the user's message | ||
|
|
||
| ```python | ||
| from whitecircle import AsyncWhiteCircle, Event | ||
|
|
||
| wc = AsyncWhiteCircle() | ||
|
|
||
|
|
||
| async def handle_message(user: User, conversation_id: str, text: str) -> None: | ||
| meta = metadata_for(user) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Include the recipe helpers in the message-check example. After
|
||
| check = await wc.events.check(Event.user(text, metadata=meta), external_session_id=conversation_id) | ||
|
|
@@ -32,6 +37,13 @@ async def handle_message(user: User, conversation_id: str, text: str) -> None: | |
| ## 2. Stream the reply | ||
|
|
||
| ```python | ||
| from typing import Any | ||
|
|
||
| from whitecircle import AsyncWhiteCircle | ||
|
|
||
| wc = AsyncWhiteCircle() | ||
|
|
||
|
|
||
| async def answer(conversation_id: str, meta: dict[str, Any]) -> None: | ||
| reply = wc.events.check_stream( | ||
| llm_tokens(conversation_id), # AsyncIterator[str] from your model | ||
|
|
@@ -54,6 +66,13 @@ Tool output is a common source of leaked data. Check arguments before running th | |
| sees it; the same `call_id` pairs them. | ||
|
|
||
| ```python | ||
| from typing import Any | ||
|
|
||
| from whitecircle import AsyncWhiteCircle, Event | ||
|
|
||
| wc = AsyncWhiteCircle() | ||
|
|
||
|
|
||
| async def lookup_order(conversation_id: str, meta: dict[str, Any], order_id: str) -> dict[str, Any]: | ||
| call_id = f"call-{order_id}" | ||
| call = await wc.events.check( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,8 +11,9 @@ the Event you sent. | |
| | neither | none: the Event is checked alone | | ||
|
|
||
| ```python | ||
| from whitecircle import Event | ||
| from whitecircle import Event, WhiteCircle | ||
|
|
||
| client = WhiteCircle() | ||
| session = "conversation_456" | ||
| client.events.check(Event.user("My account number is 4471"), external_session_id=session) | ||
| client.events.check(Event.tool_result("lookup_account", {"holder": "A. Rivera"}), external_session_id=session) | ||
|
|
@@ -25,6 +26,9 @@ with the `system` role. In a `check_many()` batch, list order is context order. | |
| ## Start the context over | ||
|
|
||
| ```python | ||
| from whitecircle import Event, WhiteCircle | ||
|
|
||
| client = WhiteCircle() | ||
| client.events.check(Event.reset(), external_session_id=session) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Define the session in the reset block. When copied on its own, this block raises As per coding guidelines, “every snippet must run against the current API.”
|
||
| client.events.check(Event.reset("The user disputes a charge on order 4815."), external_session_id=session) | ||
| ``` | ||
|
|
@@ -39,6 +43,9 @@ Store history from before you integrated as context, without checking it, with ` | |
| replaces anything stored before: | ||
|
|
||
| ```python | ||
| from whitecircle import Event, WhiteCircle | ||
|
|
||
| client = WhiteCircle() | ||
| events = [Event.reset()] + [Event.message(m.text, role=m.role) for m in previous_messages] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Provide example history for the backfill block.
As per coding guidelines, “every snippet must run against the current API.”
|
||
| for start in range(0, len(events), 50): | ||
| client.events.check_many(events[start : start + 50], external_session_id=session, skip=True) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,8 +7,9 @@ such as a missing `environment_id` or two user identifiers at once, raise `Value | |
|
|
||
| ```python | ||
| import whitecircle | ||
| from whitecircle import Event, WhiteCircle | ||
| from whitecircle import WhiteCircle | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Define the event in the exceptions example. If a reader copies this block alone, As per coding guidelines, “every snippet must run against the current API.”
|
||
|
|
||
| client = WhiteCircle() | ||
| try: | ||
| result = client.events.check(event) | ||
| except whitecircle.AuthenticationError: # 401 | ||
|
|
@@ -35,6 +36,12 @@ in `note`. | |
| ## Fail open or closed | ||
|
|
||
| ```python | ||
| import whitecircle | ||
| from whitecircle import Event, WhiteCircle | ||
|
|
||
| client = WhiteCircle() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Define the fail-open example’s inputs. If a reader calls As per coding guidelines, “every snippet must run against the current API.”
|
||
|
|
||
|
|
||
| def is_allowed(text: str) -> bool: | ||
| try: | ||
| result = client.events.check(Event.user(text, metadata=meta)) | ||
|
|
@@ -48,6 +55,8 @@ def is_allowed(text: str) -> bool: | |
| ## Retries and timeouts | ||
|
|
||
| ```python | ||
| from whitecircle import WhiteCircle | ||
|
|
||
| client = WhiteCircle(timeout=10.0, max_retries=3) | ||
| quick = client.with_options(timeout=2.0, max_retries=0) | ||
| ``` | ||
|
|
@@ -143,6 +152,9 @@ fake.events.check.return_value = EventCheckResult.model_validate( | |
| ## Endpoints the SDK does not wrap | ||
|
|
||
| ```python | ||
| from whitecircle import WhiteCircle | ||
|
|
||
| client = WhiteCircle() | ||
| client.request("GET", "/api/some/new/endpoint", params={"x": 1}) | ||
| client.request("POST", "/api/some/new/endpoint", body={"field": "value"}) | ||
| ``` | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,19 +36,27 @@ Events and run-graph calls need an Environment. Pass `environment_id=` to overri | |
| with neither, the call raises `ValueError`: | ||
|
|
||
| ```python | ||
| from whitecircle import WhiteCircle | ||
|
|
||
| client = WhiteCircle() | ||
| client.events.check(event, environment_id="env_staging") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Define the event in the override example. The block creates a client but never defines As per coding guidelines, “every snippet must run against the current API.”
|
||
| ``` | ||
|
|
||
| For several settings at once, make a copy that shares the connection pool: | ||
|
|
||
| ```python | ||
| from whitecircle import WhiteCircle | ||
|
|
||
| client = WhiteCircle() | ||
| fast = client.with_options(timeout=5.0, max_retries=0) | ||
| ``` | ||
|
|
||
| ## Check and read the result | ||
|
|
||
| ```python | ||
| from whitecircle import Event | ||
| from whitecircle import Event, WhiteCircle | ||
|
|
||
| client = WhiteCircle() | ||
|
|
||
| result = client.events.check( | ||
| Event.user("Hello, can you help me prescribe antibiotics?"), | ||
|
|
@@ -76,6 +84,9 @@ else: | |
| Fetch a stored result later: | ||
|
|
||
| ```python | ||
| from whitecircle import WhiteCircle | ||
|
|
||
| client = WhiteCircle() | ||
| client.events.get(internal_event_id) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Provide an ID for the stored-result lookup. The block never defines As per coding guidelines, “every snippet must run against the current API.”
|
||
| client.events.get_latest(run_id="run_1", event_id="evt_user_1") | ||
| ``` | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,9 @@ inline = ArtifactContent.from_bytes(Path("upload.jpg").read_bytes()) # sent as | |
| ## Standalone images | ||
|
|
||
| ```python | ||
| from whitecircle import WhiteCircle | ||
|
|
||
| client = WhiteCircle() | ||
| result = client.artifacts.check(by_url, role="user", external_artifact_id="upload_123") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Define the image content in this block. The standalone image block uses As per coding guidelines, “every snippet must run against the current API.”
|
||
| if result.flagged: | ||
| reject_upload() | ||
|
|
@@ -37,8 +40,9 @@ result is stored. | |
| ## Images in a conversation | ||
|
|
||
| ```python | ||
| from whitecircle import Event | ||
| from whitecircle import Event, WhiteCircle | ||
|
|
||
| client = WhiteCircle() | ||
| session = "conversation_456" | ||
| client.events.check(Event.user("Is this rash serious?", metadata=meta), external_session_id=session) | ||
| client.events.check( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,8 +5,9 @@ Keep Policies, Metrics and Conditions in your repo and sync them from a script o | |
| ## Create a Policy | ||
|
|
||
| ```python | ||
| from whitecircle import EventSelector, Rule | ||
| from whitecircle import EventSelector, Rule, WhiteCircle | ||
|
|
||
| client = WhiteCircle() | ||
| policy = client.policies.create( | ||
| name="No medical advice", | ||
| rules=[ | ||
|
|
@@ -28,8 +29,9 @@ At least one rule must flag. `event_selector_list` picks Event types and fields, | |
| A shadow Policy is evaluated but never changes `flagged`. Ask for its results with `include_shadow=True`: | ||
|
|
||
| ```python | ||
| from whitecircle import Event | ||
| from whitecircle import Event, WhiteCircle | ||
|
|
||
| client = WhiteCircle() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Supply the shadow-mode example’s inputs. If a reader copies this block alone, As per coding guidelines, “every snippet must run against the current API.”
|
||
| result = client.events.check(Event.user(sample_prompt, metadata=meta), include_shadow=True) | ||
| for policy_result in (result.policies or {}).values(): | ||
| if policy_result.shadow: | ||
|
|
@@ -44,6 +46,9 @@ Only the fields you pass change; `None` means "keep". `rules` replaces the whole | |
| (they carry their IDs) and add to them: | ||
|
|
||
| ```python | ||
| from whitecircle import Rule, WhiteCircle | ||
|
|
||
| client = WhiteCircle() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use a defined policy ID in the update example. If a reader copies this block alone, As per coding guidelines, “every snippet must run against the current API.”
|
||
| policy = client.policies.get(policy.id) | ||
| client.policies.update(policy.id, rules=[*policy.rules, Rule.allow("Store opening hours")]) | ||
| client.policies.update(policy.id, severity_level_id=None) # the one field None clears | ||
|
|
@@ -55,8 +60,9 @@ and `client.policies.list()` returns active Policies only. | |
| ## Attach it with a Condition | ||
|
|
||
| ```python | ||
| from whitecircle import field | ||
| from whitecircle import WhiteCircle, field | ||
|
|
||
| client = WhiteCircle() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Supply the policy IDs for the condition example. If a reader copies this block alone, condition creation references an undefined As per coding guidelines, “every snippet must run against the current API.”
|
||
| condition = client.conditions.create( | ||
| name="Pharmacy bot", | ||
| conditions=field("app") == "pharmacy-bot", | ||
|
|
@@ -78,6 +84,9 @@ On update, leaving `policy_ids`, `metric_ids` or `environment_ids` out keeps the | |
| results appear only in the dashboard. | ||
|
|
||
| ```python | ||
| from whitecircle import Rule, WhiteCircle | ||
|
|
||
| client = WhiteCircle() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Supply a condition for the metrics example. If a reader copies this block alone, metric creation can succeed, but the next line raises As per coding guidelines, “every snippet must run against the current API.”
|
||
| metric = client.metrics.create(name="Escalation risk", rules=[Rule.flag("The user threatens to churn or escalate")]) | ||
| client.conditions.update(condition.id, metric_ids=[metric.id]) | ||
| ``` | ||
|
|
@@ -88,6 +97,9 @@ client.conditions.update(condition.id, metric_ids=[metric.id]) | |
| import json | ||
| from pathlib import Path | ||
|
|
||
| from whitecircle import Rule, WhiteCircle | ||
|
|
||
| client = WhiteCircle() | ||
| desired = json.loads(Path("policies.json").read_text()) # [{"name": ..., "flag": [...], "allow": [...]}] | ||
| existing = {policy.name: policy for policy in client.policies.list()} | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Provide runnable streaming inputs.
When a reader copies this block,
model_tokensis undefined.send_to_useris also undefined. Define example implementations or show how to supply both.As per coding guidelines, “every snippet must run against the current API.”
Source: Coding guidelines