razorwire 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- razorwire/__init__.py +49 -0
- razorwire/_urls.py +58 -0
- razorwire/_version.py +1 -0
- razorwire/admin.py +368 -0
- razorwire/agent.py +101 -0
- razorwire/executor.py +257 -0
- razorwire/notes.py +178 -0
- razorwire/proxy.py +300 -0
- razorwire/py.typed +0 -0
- razorwire/signing.py +108 -0
- razorwire-0.1.0.dist-info/METADATA +210 -0
- razorwire-0.1.0.dist-info/RECORD +14 -0
- razorwire-0.1.0.dist-info/WHEEL +4 -0
- razorwire-0.1.0.dist-info/licenses/LICENSE +21 -0
razorwire/__init__.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"""Razorwire for Python.
|
|
2
|
+
|
|
3
|
+
Razorwire is a proxy between an AI agent and its model provider that checks
|
|
4
|
+
every tool call against a policy before the agent can run it. This package
|
|
5
|
+
helps on both sides of it:
|
|
6
|
+
|
|
7
|
+
- Agents: `openai_client()`, `base_url()`, and `blocked()` to see which
|
|
8
|
+
calls were stopped and why.
|
|
9
|
+
- Admins: `Admin`, for held calls, approvals, reloads and rollbacks.
|
|
10
|
+
- Executor: `Executor`, the service that carries out rollbacks and approved
|
|
11
|
+
parked calls, with signatures and retries handled.
|
|
12
|
+
- Alerts: `verify()`, to check a signed JSON alert.
|
|
13
|
+
- Tests: `Proxy`, to run Razorwire with a policy inside a test.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from ._version import __version__
|
|
17
|
+
from .admin import Admin, Approvals, Decision, HeldCall, RazorwireError, Rollback, RollbackStep
|
|
18
|
+
from .agent import async_openai_client, base_url, openai_client
|
|
19
|
+
from .executor import Executor, Job
|
|
20
|
+
from .notes import BLOCKED_HEADER, NOTE_PREFIX, Block, blocked, blocked_count
|
|
21
|
+
from .proxy import Proxy
|
|
22
|
+
from .signing import SIGNATURE_HEADER, TIMESTAMP_HEADER, SignatureError, sign, verify
|
|
23
|
+
|
|
24
|
+
__all__ = [
|
|
25
|
+
"__version__",
|
|
26
|
+
"Admin",
|
|
27
|
+
"Approvals",
|
|
28
|
+
"BLOCKED_HEADER",
|
|
29
|
+
"Block",
|
|
30
|
+
"Decision",
|
|
31
|
+
"Executor",
|
|
32
|
+
"HeldCall",
|
|
33
|
+
"Job",
|
|
34
|
+
"NOTE_PREFIX",
|
|
35
|
+
"Proxy",
|
|
36
|
+
"RazorwireError",
|
|
37
|
+
"Rollback",
|
|
38
|
+
"RollbackStep",
|
|
39
|
+
"SIGNATURE_HEADER",
|
|
40
|
+
"SignatureError",
|
|
41
|
+
"TIMESTAMP_HEADER",
|
|
42
|
+
"async_openai_client",
|
|
43
|
+
"base_url",
|
|
44
|
+
"blocked",
|
|
45
|
+
"blocked_count",
|
|
46
|
+
"openai_client",
|
|
47
|
+
"sign",
|
|
48
|
+
"verify",
|
|
49
|
+
]
|
razorwire/_urls.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"""Where the proxy is, and whether a key may be sent there."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import ipaddress
|
|
6
|
+
import os
|
|
7
|
+
from typing import Optional
|
|
8
|
+
from urllib.parse import urlsplit
|
|
9
|
+
|
|
10
|
+
DEFAULT_URL = "http://127.0.0.1:8080"
|
|
11
|
+
KEY_PREFIX = "rw_"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def is_loopback_host(host: Optional[str]) -> bool:
|
|
15
|
+
"""True for localhost and loopback addresses, the same test the proxy uses."""
|
|
16
|
+
if not host:
|
|
17
|
+
return False
|
|
18
|
+
host = host.strip("[]")
|
|
19
|
+
if host.lower() == "localhost":
|
|
20
|
+
return True
|
|
21
|
+
try:
|
|
22
|
+
return ipaddress.ip_address(host).is_loopback
|
|
23
|
+
except ValueError:
|
|
24
|
+
return False
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def proxy_url(url: Optional[str] = None) -> str:
|
|
28
|
+
"""The proxy's address: `url`, else RAZORWIRE_URL, else http://127.0.0.1:8080."""
|
|
29
|
+
value = url if url is not None else os.environ.get("RAZORWIRE_URL", "")
|
|
30
|
+
return (value.strip() or DEFAULT_URL).rstrip("/")
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def check_url(url: str, setting: str, allow_plain_http: bool = False) -> None:
|
|
34
|
+
"""Refuse an address a key should not be sent to.
|
|
35
|
+
|
|
36
|
+
Plain http is only allowed to this machine, unless `allow_plain_http`,
|
|
37
|
+
because the key in each request could otherwise be read on the network.
|
|
38
|
+
The URL is never repeated in the error, in case it carries a secret.
|
|
39
|
+
"""
|
|
40
|
+
parts = urlsplit(url)
|
|
41
|
+
if parts.scheme not in ("http", "https") or not parts.hostname:
|
|
42
|
+
raise ValueError(f"{setting} is not an http or https URL")
|
|
43
|
+
if parts.username is not None or parts.password is not None:
|
|
44
|
+
raise ValueError(f"{setting} must not carry a user name or password")
|
|
45
|
+
if parts.scheme == "http" and not allow_plain_http and not is_loopback_host(parts.hostname):
|
|
46
|
+
raise ValueError(
|
|
47
|
+
f"{setting} must use https unless it points at this machine, or the key would cross the network unencrypted"
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def shown_url(url: str) -> str:
|
|
52
|
+
"""Scheme, host and port only, for messages."""
|
|
53
|
+
parts = urlsplit(url)
|
|
54
|
+
host = parts.hostname or "?"
|
|
55
|
+
if ":" in host:
|
|
56
|
+
host = f"[{host}]"
|
|
57
|
+
port = f":{parts.port}" if parts.port else ""
|
|
58
|
+
return f"{parts.scheme}://{host}{port}"
|
razorwire/_version.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.1.0"
|
razorwire/admin.py
ADDED
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
"""The admin API: held calls, approvals, reloads and rollbacks.
|
|
2
|
+
|
|
3
|
+
from razorwire import Admin
|
|
4
|
+
|
|
5
|
+
admin = Admin() # RAZORWIRE_URL and RAZORWIRE_ADMIN_KEY
|
|
6
|
+
for call in admin.approvals().pending:
|
|
7
|
+
print(call.id, call.tool, call.arguments)
|
|
8
|
+
admin.approve(call.id, reason="checked the order")
|
|
9
|
+
|
|
10
|
+
An admin key is only sent over https, or plain http to this machine.
|
|
11
|
+
Redirects are never followed, so the key never goes anywhere else.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import datetime as _dt
|
|
17
|
+
import http.client
|
|
18
|
+
import json
|
|
19
|
+
import os
|
|
20
|
+
import re
|
|
21
|
+
import ssl
|
|
22
|
+
import time
|
|
23
|
+
import urllib.error
|
|
24
|
+
import urllib.request
|
|
25
|
+
from dataclasses import dataclass, field
|
|
26
|
+
from typing import Any, Dict, Iterable, List, Optional, Union
|
|
27
|
+
from urllib.parse import urlsplit
|
|
28
|
+
|
|
29
|
+
from ._urls import KEY_PREFIX, check_url, proxy_url, shown_url
|
|
30
|
+
from ._version import __version__
|
|
31
|
+
|
|
32
|
+
MAX_ANSWER_BYTES = 32 * 1024 * 1024
|
|
33
|
+
_APPROVAL_ID = re.compile(r"[A-Za-z0-9_]{1,128}")
|
|
34
|
+
_SPAN = re.compile(r"(\d+)([smhd])")
|
|
35
|
+
_SPAN_SECONDS = {"s": 1, "m": 60, "h": 3600, "d": 86400}
|
|
36
|
+
|
|
37
|
+
Since = Union[int, float, str, _dt.timedelta, _dt.datetime]
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class RazorwireError(Exception):
|
|
41
|
+
"""Razorwire refused a request, or could not be reached."""
|
|
42
|
+
|
|
43
|
+
def __init__(self, message: str, status: Optional[int] = None, code: Optional[str] = None):
|
|
44
|
+
super().__init__(message)
|
|
45
|
+
self.status = status
|
|
46
|
+
"""The HTTP status, when Razorwire answered."""
|
|
47
|
+
self.code = code
|
|
48
|
+
"""Razorwire's error code, such as "not_pending" or "no_executor"."""
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
@dataclass(frozen=True)
|
|
52
|
+
class HeldCall:
|
|
53
|
+
"""A tool call waiting for a person, or parked after nobody decided in time."""
|
|
54
|
+
|
|
55
|
+
id: str
|
|
56
|
+
tool: Optional[str]
|
|
57
|
+
agent: str
|
|
58
|
+
rule: Optional[str]
|
|
59
|
+
message: str
|
|
60
|
+
arguments: Any
|
|
61
|
+
call_id: Optional[str]
|
|
62
|
+
created_ms: Optional[int]
|
|
63
|
+
expires_ms: Optional[int]
|
|
64
|
+
"""When a waiting call stops waiting."""
|
|
65
|
+
parked_until_ms: Optional[int]
|
|
66
|
+
"""When a parked call is dropped. None for a call that is still waiting."""
|
|
67
|
+
raw: Dict[str, Any] = field(repr=False, compare=False)
|
|
68
|
+
|
|
69
|
+
@property
|
|
70
|
+
def parked(self) -> bool:
|
|
71
|
+
return self.parked_until_ms is not None
|
|
72
|
+
|
|
73
|
+
@classmethod
|
|
74
|
+
def _read(cls, raw: Dict[str, Any]) -> HeldCall:
|
|
75
|
+
return cls(
|
|
76
|
+
id=str(raw.get("id", "")),
|
|
77
|
+
tool=raw.get("tool"),
|
|
78
|
+
agent=raw.get("agent") or "",
|
|
79
|
+
rule=raw.get("rule"),
|
|
80
|
+
message=raw.get("message") or "",
|
|
81
|
+
arguments=raw.get("arguments"),
|
|
82
|
+
call_id=raw.get("call_id"),
|
|
83
|
+
created_ms=raw.get("created_ms"),
|
|
84
|
+
expires_ms=raw.get("expires_ms"),
|
|
85
|
+
parked_until_ms=raw.get("parked_until_ms"),
|
|
86
|
+
raw=raw,
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
@dataclass(frozen=True)
|
|
91
|
+
class Approvals:
|
|
92
|
+
pending: List[HeldCall]
|
|
93
|
+
"""Calls whose agent is waiting, oldest first."""
|
|
94
|
+
parked: List[HeldCall]
|
|
95
|
+
"""Calls nobody decided in time. Approving one has the executor carry it out."""
|
|
96
|
+
now_ms: int
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
@dataclass(frozen=True)
|
|
100
|
+
class Decision:
|
|
101
|
+
approval_id: str
|
|
102
|
+
decision: str
|
|
103
|
+
""""approved" or "denied"."""
|
|
104
|
+
by: str
|
|
105
|
+
reason: Optional[str]
|
|
106
|
+
agent: Optional[str]
|
|
107
|
+
tool: Optional[str]
|
|
108
|
+
parked: bool
|
|
109
|
+
ran: bool
|
|
110
|
+
"""True when a parked call was approved and the executor carried it out."""
|
|
111
|
+
output: Any
|
|
112
|
+
"""What the executor answered, when it ran."""
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
@dataclass(frozen=True)
|
|
116
|
+
class RollbackStep:
|
|
117
|
+
id: str
|
|
118
|
+
"""The undo id, und_ and 24 hex characters."""
|
|
119
|
+
ts_ms: Optional[int]
|
|
120
|
+
agent: Optional[str]
|
|
121
|
+
tool: Optional[str]
|
|
122
|
+
call_id: Optional[str]
|
|
123
|
+
arguments: Any
|
|
124
|
+
result: Any
|
|
125
|
+
undo: Optional[Dict[str, Any]]
|
|
126
|
+
"""The reversing call, {"tool": ..., "arguments": ...}, or None."""
|
|
127
|
+
cannot_undo: Optional[str]
|
|
128
|
+
"""Why there is no reversing call."""
|
|
129
|
+
done: bool
|
|
130
|
+
"""Already undone by an earlier rollback."""
|
|
131
|
+
last_error: Optional[str]
|
|
132
|
+
outcome: Optional[str]
|
|
133
|
+
"""After a run: "undone", "failed", "already_undone", "cannot_undo" or "not_tried"."""
|
|
134
|
+
output: Any
|
|
135
|
+
error: Optional[str]
|
|
136
|
+
raw: Dict[str, Any] = field(repr=False, compare=False)
|
|
137
|
+
|
|
138
|
+
@classmethod
|
|
139
|
+
def _read(cls, raw: Dict[str, Any]) -> RollbackStep:
|
|
140
|
+
return cls(
|
|
141
|
+
id=str(raw.get("id", "")),
|
|
142
|
+
ts_ms=raw.get("ts_ms"),
|
|
143
|
+
agent=raw.get("agent"),
|
|
144
|
+
tool=raw.get("tool"),
|
|
145
|
+
call_id=raw.get("call_id"),
|
|
146
|
+
arguments=raw.get("arguments"),
|
|
147
|
+
result=raw.get("result"),
|
|
148
|
+
undo=raw.get("undo"),
|
|
149
|
+
cannot_undo=raw.get("cannot_undo"),
|
|
150
|
+
done=raw.get("done") is True,
|
|
151
|
+
last_error=raw.get("last_error"),
|
|
152
|
+
outcome=raw.get("outcome"),
|
|
153
|
+
output=raw.get("output"),
|
|
154
|
+
error=raw.get("error"),
|
|
155
|
+
raw=raw,
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
@dataclass(frozen=True)
|
|
160
|
+
class Rollback:
|
|
161
|
+
run: bool
|
|
162
|
+
"""False for a plan only."""
|
|
163
|
+
steps: List[RollbackStep]
|
|
164
|
+
"""Newest first, the order they are undone in."""
|
|
165
|
+
undone: int
|
|
166
|
+
failed: bool
|
|
167
|
+
now_ms: int
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
class _NoRedirects(urllib.request.HTTPRedirectHandler):
|
|
171
|
+
def redirect_request(
|
|
172
|
+
self, req: urllib.request.Request, fp: Any, code: int, msg: str, headers: Any, newurl: str
|
|
173
|
+
) -> None:
|
|
174
|
+
return None
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
class Admin:
|
|
178
|
+
"""A client for Razorwire's admin API."""
|
|
179
|
+
|
|
180
|
+
def __init__(
|
|
181
|
+
self,
|
|
182
|
+
url: Optional[str] = None,
|
|
183
|
+
key: Optional[str] = None,
|
|
184
|
+
*,
|
|
185
|
+
ca_cert: Optional[str] = None,
|
|
186
|
+
timeout: float = 90.0,
|
|
187
|
+
):
|
|
188
|
+
"""
|
|
189
|
+
url: the proxy, default RAZORWIRE_URL or http://127.0.0.1:8080
|
|
190
|
+
key: an admin key, default RAZORWIRE_ADMIN_KEY
|
|
191
|
+
ca_cert: a PEM certificate to trust as well, for a self-signed
|
|
192
|
+
proxy, default RAZORWIRE_CA_CERT
|
|
193
|
+
timeout: seconds per request. Approving a parked call waits for
|
|
194
|
+
the executor, which may take up to 60 seconds.
|
|
195
|
+
"""
|
|
196
|
+
self.url = proxy_url(url)
|
|
197
|
+
check_url(self.url, "RAZORWIRE_URL")
|
|
198
|
+
key = key if key is not None else os.environ.get("RAZORWIRE_ADMIN_KEY", "")
|
|
199
|
+
key = key.strip()
|
|
200
|
+
if not key.startswith(KEY_PREFIX):
|
|
201
|
+
raise ValueError(
|
|
202
|
+
"Pass key= or set RAZORWIRE_ADMIN_KEY to an admin key. "
|
|
203
|
+
"Create one with `razorwire new-key admin <name>`."
|
|
204
|
+
)
|
|
205
|
+
self._key = key
|
|
206
|
+
self.timeout = timeout
|
|
207
|
+
if ca_cert is None:
|
|
208
|
+
ca_cert = os.environ.get("RAZORWIRE_CA_CERT", "").strip() or None
|
|
209
|
+
context = ssl.create_default_context()
|
|
210
|
+
if ca_cert:
|
|
211
|
+
context.load_verify_locations(cafile=ca_cert)
|
|
212
|
+
handlers: List[Any] = [_NoRedirects(), urllib.request.HTTPSHandler(context=context)]
|
|
213
|
+
if urlsplit(self.url).scheme == "http":
|
|
214
|
+
# Plain http only goes to this machine, never through a proxy.
|
|
215
|
+
handlers.append(urllib.request.ProxyHandler({}))
|
|
216
|
+
self._opener = urllib.request.build_opener(*handlers)
|
|
217
|
+
|
|
218
|
+
def __repr__(self) -> str:
|
|
219
|
+
return f"Admin(url={self.url!r})"
|
|
220
|
+
|
|
221
|
+
def approvals(self) -> Approvals:
|
|
222
|
+
"""Held calls, waiting and parked."""
|
|
223
|
+
answer = self._call("GET", "/razorwire/approvals")
|
|
224
|
+
return Approvals(
|
|
225
|
+
pending=[HeldCall._read(item) for item in answer.get("pending") or []],
|
|
226
|
+
parked=[HeldCall._read(item) for item in answer.get("parked") or []],
|
|
227
|
+
now_ms=answer.get("now_ms") or int(time.time() * 1000),
|
|
228
|
+
)
|
|
229
|
+
|
|
230
|
+
def approve(self, approval_id: Union[str, HeldCall], reason: Optional[str] = None) -> Decision:
|
|
231
|
+
"""Let a held call through, or have the executor carry out a parked one."""
|
|
232
|
+
return self._decide(approval_id, "approve", reason)
|
|
233
|
+
|
|
234
|
+
def deny(self, approval_id: Union[str, HeldCall], reason: Optional[str] = None) -> Decision:
|
|
235
|
+
"""Block a held or parked call. The reason is shown to the agent."""
|
|
236
|
+
return self._decide(approval_id, "deny", reason)
|
|
237
|
+
|
|
238
|
+
def reload(self) -> Dict[str, Any]:
|
|
239
|
+
"""Re-read the policy and keys files. Nothing changes if either is invalid."""
|
|
240
|
+
return self._call("POST", "/razorwire/reload").get("config") or {}
|
|
241
|
+
|
|
242
|
+
def rollback(
|
|
243
|
+
self,
|
|
244
|
+
*,
|
|
245
|
+
agent: Optional[str] = None,
|
|
246
|
+
since: Optional[Since] = None,
|
|
247
|
+
ids: Optional[Iterable[str]] = None,
|
|
248
|
+
run: bool = False,
|
|
249
|
+
) -> Rollback:
|
|
250
|
+
"""What would be undone, or with `run=True`, undo it.
|
|
251
|
+
|
|
252
|
+
Say at least one of: `agent`, `since` (seconds, a timedelta, a
|
|
253
|
+
datetime, or a span like "30m", "2h" or "1d") and `ids`. Steps are
|
|
254
|
+
undone newest first, stopping at the first failure.
|
|
255
|
+
"""
|
|
256
|
+
body: Dict[str, Any] = {"run": bool(run)}
|
|
257
|
+
if agent is not None:
|
|
258
|
+
body["agent"] = agent
|
|
259
|
+
if since is not None:
|
|
260
|
+
body.update(_since(since))
|
|
261
|
+
if ids is not None:
|
|
262
|
+
body["ids"] = list(ids)
|
|
263
|
+
if len(body) == 1:
|
|
264
|
+
raise ValueError("say what to roll back: agent=, since= or ids=")
|
|
265
|
+
answer = self._call("POST", "/razorwire/rollback", body)
|
|
266
|
+
return Rollback(
|
|
267
|
+
run=answer.get("run") is True,
|
|
268
|
+
steps=[RollbackStep._read(item) for item in answer.get("steps") or []],
|
|
269
|
+
undone=answer.get("undone") or 0,
|
|
270
|
+
failed=answer.get("failed") is True,
|
|
271
|
+
now_ms=answer.get("now_ms") or int(time.time() * 1000),
|
|
272
|
+
)
|
|
273
|
+
|
|
274
|
+
def wait_for_held(
|
|
275
|
+
self,
|
|
276
|
+
*,
|
|
277
|
+
tool: Optional[str] = None,
|
|
278
|
+
agent: Optional[str] = None,
|
|
279
|
+
timeout: float = 60.0,
|
|
280
|
+
interval: float = 0.5,
|
|
281
|
+
) -> HeldCall:
|
|
282
|
+
"""Wait until a call is held, optionally for this tool or agent, and return the oldest."""
|
|
283
|
+
deadline = time.monotonic() + timeout
|
|
284
|
+
while True:
|
|
285
|
+
for call in self.approvals().pending:
|
|
286
|
+
if (tool is None or call.tool == tool) and (agent is None or call.agent == agent):
|
|
287
|
+
return call
|
|
288
|
+
if time.monotonic() >= deadline:
|
|
289
|
+
raise TimeoutError("no matching call was held in time")
|
|
290
|
+
time.sleep(interval)
|
|
291
|
+
|
|
292
|
+
def _decide(self, approval_id: Union[str, HeldCall], action: str, reason: Optional[str]) -> Decision:
|
|
293
|
+
if isinstance(approval_id, HeldCall):
|
|
294
|
+
approval_id = approval_id.id
|
|
295
|
+
if not isinstance(approval_id, str) or not _APPROVAL_ID.fullmatch(approval_id):
|
|
296
|
+
raise ValueError("that is not an approval id; approvals() lists them")
|
|
297
|
+
body = {"reason": reason} if reason is not None else None
|
|
298
|
+
answer = self._call("POST", f"/razorwire/approvals/{approval_id}/{action}", body)
|
|
299
|
+
return Decision(
|
|
300
|
+
approval_id=answer.get("approval_id") or approval_id,
|
|
301
|
+
decision=answer.get("decision") or ("approved" if action == "approve" else "denied"),
|
|
302
|
+
by=answer.get("by") or "",
|
|
303
|
+
reason=answer.get("reason"),
|
|
304
|
+
agent=answer.get("agent"),
|
|
305
|
+
tool=answer.get("tool"),
|
|
306
|
+
parked=answer.get("parked") is True,
|
|
307
|
+
ran=answer.get("ran") is True,
|
|
308
|
+
output=answer.get("output"),
|
|
309
|
+
)
|
|
310
|
+
|
|
311
|
+
def _call(self, method: str, path: str, body: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
|
|
312
|
+
data = json.dumps(body).encode() if body is not None else (b"" if method == "POST" else None)
|
|
313
|
+
request = urllib.request.Request(self.url + path, data=data, method=method)
|
|
314
|
+
request.add_header("Authorization", f"Bearer {self._key}")
|
|
315
|
+
request.add_header("Accept", "application/json")
|
|
316
|
+
request.add_header("User-Agent", f"razorwire-python/{__version__}")
|
|
317
|
+
if body is not None:
|
|
318
|
+
request.add_header("Content-Type", "application/json")
|
|
319
|
+
where = shown_url(self.url)
|
|
320
|
+
try:
|
|
321
|
+
with self._opener.open(request, timeout=self.timeout) as response:
|
|
322
|
+
status, raw = response.status, response.read(MAX_ANSWER_BYTES)
|
|
323
|
+
except urllib.error.HTTPError as err:
|
|
324
|
+
status = err.code
|
|
325
|
+
raw = err.read(MAX_ANSWER_BYTES) if err.fp is not None else b""
|
|
326
|
+
err.close()
|
|
327
|
+
except (urllib.error.URLError, OSError, http.client.HTTPException) as err:
|
|
328
|
+
reason = getattr(err, "reason", err)
|
|
329
|
+
raise RazorwireError(f"cannot reach Razorwire at {where}: {reason}") from None
|
|
330
|
+
if 300 <= status < 400:
|
|
331
|
+
raise RazorwireError(
|
|
332
|
+
f"Razorwire at {where} answered {status} with a redirect, which is not followed "
|
|
333
|
+
"so the admin key goes nowhere else",
|
|
334
|
+
status,
|
|
335
|
+
)
|
|
336
|
+
try:
|
|
337
|
+
answer = json.loads(raw)
|
|
338
|
+
except ValueError:
|
|
339
|
+
answer = None
|
|
340
|
+
if not isinstance(answer, dict):
|
|
341
|
+
raise RazorwireError(f"Razorwire answered {status} with something that is not JSON", status)
|
|
342
|
+
if status >= 400:
|
|
343
|
+
error = answer.get("error")
|
|
344
|
+
if not isinstance(error, dict):
|
|
345
|
+
error = {}
|
|
346
|
+
message = error.get("message") or "no reason given"
|
|
347
|
+
raise RazorwireError(f"Razorwire said no ({status}): {message}", status, error.get("code"))
|
|
348
|
+
return answer
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
def _since(since: Since) -> Dict[str, int]:
|
|
352
|
+
if isinstance(since, _dt.datetime):
|
|
353
|
+
moment = since if since.tzinfo else since.astimezone()
|
|
354
|
+
return {"since_ms": max(0, int(moment.timestamp() * 1000))}
|
|
355
|
+
if isinstance(since, _dt.timedelta):
|
|
356
|
+
seconds = since.total_seconds()
|
|
357
|
+
elif isinstance(since, str):
|
|
358
|
+
match = _SPAN.fullmatch(since.strip())
|
|
359
|
+
if match is None:
|
|
360
|
+
raise ValueError('since takes a span like "30m", "2h" or "1d"')
|
|
361
|
+
seconds = int(match.group(1)) * _SPAN_SECONDS[match.group(2)]
|
|
362
|
+
elif isinstance(since, (int, float)) and not isinstance(since, bool):
|
|
363
|
+
seconds = since
|
|
364
|
+
else:
|
|
365
|
+
raise TypeError('since takes seconds, a timedelta, a datetime or a span like "2h"')
|
|
366
|
+
if seconds < 0:
|
|
367
|
+
raise ValueError("since must not be negative")
|
|
368
|
+
return {"since_secs": int(seconds)}
|
razorwire/agent.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"""Point an agent at Razorwire.
|
|
2
|
+
|
|
3
|
+
import razorwire
|
|
4
|
+
|
|
5
|
+
client = razorwire.openai_client() # an openai.OpenAI that talks to the proxy
|
|
6
|
+
|
|
7
|
+
The proxy's address comes from RAZORWIRE_URL (default
|
|
8
|
+
http://127.0.0.1:8080) and the agent's key from RAZORWIRE_AGENT_KEY. Without
|
|
9
|
+
a key, the OpenAI SDK sends OPENAI_API_KEY as usual, which is right when the
|
|
10
|
+
proxy has no agent keys set up.
|
|
11
|
+
|
|
12
|
+
Any framework that takes an OpenAI-compatible base URL works the same way:
|
|
13
|
+
|
|
14
|
+
ChatOpenAI(base_url=razorwire.base_url(), api_key=os.environ["RAZORWIRE_AGENT_KEY"])
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
import os
|
|
20
|
+
from typing import Any, Dict, Optional
|
|
21
|
+
|
|
22
|
+
from ._urls import check_url, proxy_url
|
|
23
|
+
|
|
24
|
+
AGENT_NAME_HEADER = "x-agent-name"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def base_url(url: Optional[str] = None) -> str:
|
|
28
|
+
"""The proxy's OpenAI-compatible base URL, such as http://127.0.0.1:8080/v1."""
|
|
29
|
+
return proxy_url(url) + "/v1"
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def openai_client(
|
|
33
|
+
*,
|
|
34
|
+
url: Optional[str] = None,
|
|
35
|
+
key: Optional[str] = None,
|
|
36
|
+
agent: Optional[str] = None,
|
|
37
|
+
allow_plain_http: bool = False,
|
|
38
|
+
**kwargs: Any,
|
|
39
|
+
) -> Any:
|
|
40
|
+
"""An `openai.OpenAI` client that talks to Razorwire.
|
|
41
|
+
|
|
42
|
+
url: the proxy, default RAZORWIRE_URL or http://127.0.0.1:8080
|
|
43
|
+
key: the agent's rw_ key, default RAZORWIRE_AGENT_KEY. Without one,
|
|
44
|
+
the OpenAI SDK's own api_key or OPENAI_API_KEY is sent.
|
|
45
|
+
agent: the agent's name, sent as x-agent-name. Only used when the proxy
|
|
46
|
+
has no agent keys; with keys, the name comes from the key.
|
|
47
|
+
allow_plain_http: let a key go over plain http to another machine,
|
|
48
|
+
for a private network you trust. Off by default.
|
|
49
|
+
|
|
50
|
+
Other keyword arguments go to `openai.OpenAI`.
|
|
51
|
+
"""
|
|
52
|
+
try:
|
|
53
|
+
from openai import OpenAI
|
|
54
|
+
except ImportError:
|
|
55
|
+
raise ImportError("openai_client() needs the openai package: pip install openai") from None
|
|
56
|
+
return OpenAI(**client_arguments(url, key, agent, allow_plain_http, kwargs))
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def async_openai_client(
|
|
60
|
+
*,
|
|
61
|
+
url: Optional[str] = None,
|
|
62
|
+
key: Optional[str] = None,
|
|
63
|
+
agent: Optional[str] = None,
|
|
64
|
+
allow_plain_http: bool = False,
|
|
65
|
+
**kwargs: Any,
|
|
66
|
+
) -> Any:
|
|
67
|
+
"""The same as `openai_client()`, for `openai.AsyncOpenAI`."""
|
|
68
|
+
try:
|
|
69
|
+
from openai import AsyncOpenAI
|
|
70
|
+
except ImportError:
|
|
71
|
+
raise ImportError("async_openai_client() needs the openai package: pip install openai") from None
|
|
72
|
+
return AsyncOpenAI(**client_arguments(url, key, agent, allow_plain_http, kwargs))
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def client_arguments(
|
|
76
|
+
url: Optional[str],
|
|
77
|
+
key: Optional[str],
|
|
78
|
+
agent: Optional[str],
|
|
79
|
+
allow_plain_http: bool,
|
|
80
|
+
kwargs: Dict[str, Any],
|
|
81
|
+
) -> Dict[str, Any]:
|
|
82
|
+
"""The keyword arguments for an OpenAI client that talks to Razorwire."""
|
|
83
|
+
if "base_url" in kwargs:
|
|
84
|
+
raise TypeError("pass the proxy as url=, not base_url=")
|
|
85
|
+
root = proxy_url(url)
|
|
86
|
+
check_url(root, "RAZORWIRE_URL", allow_plain_http)
|
|
87
|
+
arguments = dict(kwargs)
|
|
88
|
+
arguments["base_url"] = root + "/v1"
|
|
89
|
+
if "api_key" in kwargs:
|
|
90
|
+
if key is not None:
|
|
91
|
+
raise TypeError("pass the agent's key as key= or api_key=, not both")
|
|
92
|
+
else:
|
|
93
|
+
if key is None:
|
|
94
|
+
key = os.environ.get("RAZORWIRE_AGENT_KEY")
|
|
95
|
+
if key and key.strip():
|
|
96
|
+
arguments["api_key"] = key.strip()
|
|
97
|
+
if agent:
|
|
98
|
+
headers = dict(arguments.get("default_headers") or {})
|
|
99
|
+
headers[AGENT_NAME_HEADER] = agent
|
|
100
|
+
arguments["default_headers"] = headers
|
|
101
|
+
return arguments
|