Spaces:
Running
Running
Download examples/quickstart.py from dino1x/beam-verified: direct link, hf CLI and curl.
- Browser
- Download file 1.65 kB
-
https://huggingface.co/spaces/dino1x/beam-verified/resolve/main/examples/quickstart.py
- Command line
-
hf download hf://spaces/dino1x/beam-verified/examples/quickstart.py
-
curl -L -o quickstart.py https://huggingface.co/spaces/dino1x/beam-verified/resolve/main/examples/quickstart.py
1.65 kB
| """Put CiteBeforeAct in front of your own agent's tools. Stdlib only, no model, no key: | |
| pip install git+https://huggingface.co/spaces/dino1x/beam-verified | |
| python examples/quickstart.py | |
| """ | |
| import sys | |
| from pathlib import Path | |
| sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) # also runs from a clone without installing | |
| from beamv.toolgate.firewall import CONTRACTS, CiteBeforeAct, Contract, requires_observation # noqa: E402 | |
| # Your own tool: a refund is allowed only after get_order showed that order as delivered. | |
| contracts = CONTRACTS | {"refund_order": Contract("refund_order", free_params=("reason",), preconditions=[ | |
| requires_observation("get_order", {"order_id": "order_id"}, "status", "delivered", "order delivered")])} | |
| fw = CiteBeforeAct(contracts=contracts, derive=True) | |
| orders = {"SO-1": {"status": "delivered", "lines": [{"qty": 3, "unit_price": 19.99}, {"qty": 2, "unit_price": 100.5}]}} | |
| get_order = lambda order_id: orders[order_id] # noqa: E731 | |
| refund = lambda order_id, amount, reason="": "refunded" # noqa: E731 | |
| # In your agent loop: every tool call goes through fw.call; its return value is the tool result the model sees. | |
| fw.record_user("Refund order SO-1 in full, the customer returned everything.") | |
| print(fw.call("refund_order", {"order_id": "SO-1", "amount": 260.97}, refund)) # order never read: blocked | |
| fw.call("get_order", {"order_id": "SO-1"}, get_order) | |
| print(fw.call("refund_order", {"order_id": "SO-1", "amount": 300}, refund)) # 300 is in no message or read: blocked | |
| print(fw.call("refund_order", {"order_id": "SO-1", "amount": 260.97}, refund)) # the order's line total: allowed | |