usegradient 1.3.1__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.
- gradient_sdk/__init__.py +122 -0
- gradient_sdk/agent.py +368 -0
- gradient_sdk/client.py +1059 -0
- gradient_sdk/context.py +91 -0
- gradient_sdk/credentials.py +92 -0
- gradient_sdk/decorators.py +202 -0
- gradient_sdk/errors.py +30 -0
- gradient_sdk/graph.py +190 -0
- gradient_sdk/integrations/__init__.py +7 -0
- gradient_sdk/integrations/agent.py +263 -0
- gradient_sdk/integrations/anthropic.py +51 -0
- gradient_sdk/integrations/openai.py +105 -0
- gradient_sdk/model_catalog.py +75 -0
- gradient_sdk/models.py +370 -0
- gradient_sdk/py.typed +1 -0
- gradient_sdk/runtime.py +1474 -0
- gradient_sdk/session.py +132 -0
- gradient_sdk/tools.py +156 -0
- gradient_sdk/tracing.py +361 -0
- usegradient-1.3.1.dist-info/METADATA +235 -0
- usegradient-1.3.1.dist-info/RECORD +22 -0
- usegradient-1.3.1.dist-info/WHEEL +4 -0
gradient_sdk/models.py
ADDED
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import asdict, dataclass, field, is_dataclass
|
|
4
|
+
from typing import Any, Mapping
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
JsonMapping = Mapping[str, Any]
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def to_payload(value: Any) -> Any:
|
|
11
|
+
"""Convert SDK models into JSON-serializable API payloads."""
|
|
12
|
+
|
|
13
|
+
if is_dataclass(value):
|
|
14
|
+
return to_payload(asdict(value))
|
|
15
|
+
if isinstance(value, Mapping):
|
|
16
|
+
return {
|
|
17
|
+
str(k): to_payload(v)
|
|
18
|
+
for k, v in value.items()
|
|
19
|
+
if v is not None
|
|
20
|
+
}
|
|
21
|
+
if isinstance(value, (list, tuple)):
|
|
22
|
+
return [to_payload(v) for v in value]
|
|
23
|
+
return value
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@dataclass(slots=True)
|
|
27
|
+
class PortEntry:
|
|
28
|
+
port: int = 443
|
|
29
|
+
handlers: list[str] | None = None
|
|
30
|
+
|
|
31
|
+
@classmethod
|
|
32
|
+
def default_https(cls) -> "PortEntry":
|
|
33
|
+
return cls(port=443, handlers=["tls", "http"])
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@dataclass(slots=True)
|
|
37
|
+
class ServiceConfig:
|
|
38
|
+
internal_port: int = 8080
|
|
39
|
+
protocol: str = "tcp"
|
|
40
|
+
ports: list[PortEntry] | list[dict[str, Any]] | None = None
|
|
41
|
+
|
|
42
|
+
@classmethod
|
|
43
|
+
def default_https(cls) -> "ServiceConfig":
|
|
44
|
+
return cls(internal_port=8080, protocol="tcp", ports=[PortEntry.default_https()])
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@dataclass(slots=True)
|
|
48
|
+
class Guest:
|
|
49
|
+
cpu_kind: str = "shared"
|
|
50
|
+
cpus: int = 2
|
|
51
|
+
memory_mb: int = 1024
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@dataclass(slots=True)
|
|
55
|
+
class Restart:
|
|
56
|
+
policy: str = "no"
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
@dataclass(slots=True)
|
|
60
|
+
class MachineSpec:
|
|
61
|
+
image: str
|
|
62
|
+
guest: Guest | dict[str, Any] | None = None
|
|
63
|
+
env: dict[str, str] | None = None
|
|
64
|
+
services: list[ServiceConfig] | list[dict[str, Any]] | None = None
|
|
65
|
+
restart: Restart | dict[str, Any] | None = None
|
|
66
|
+
metadata: dict[str, str] | None = None
|
|
67
|
+
auto_destroy: bool | None = None
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
@dataclass(slots=True)
|
|
71
|
+
class MachineCreateRequest:
|
|
72
|
+
config: MachineSpec | dict[str, Any]
|
|
73
|
+
name: str | None = None
|
|
74
|
+
region: str | None = None
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
@dataclass(slots=True)
|
|
78
|
+
class WhoamiResponse:
|
|
79
|
+
slug: str
|
|
80
|
+
name: str
|
|
81
|
+
fly_app_slug: str
|
|
82
|
+
registry_url: str
|
|
83
|
+
proxy_url: str
|
|
84
|
+
scopes: list[str]
|
|
85
|
+
|
|
86
|
+
@classmethod
|
|
87
|
+
def from_dict(cls, data: JsonMapping) -> "WhoamiResponse":
|
|
88
|
+
return cls(
|
|
89
|
+
slug=str(data.get("slug", "")),
|
|
90
|
+
name=str(data.get("name", "")),
|
|
91
|
+
fly_app_slug=str(data.get("flyAppSlug", data.get("fly_app_slug", ""))),
|
|
92
|
+
registry_url=str(data.get("registryUrl", data.get("registry_url", ""))),
|
|
93
|
+
proxy_url=str(data.get("proxyUrl", data.get("proxy_url", ""))),
|
|
94
|
+
scopes=list(data.get("scopes", [])),
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
@dataclass(slots=True)
|
|
99
|
+
class CreateMachineResponse:
|
|
100
|
+
id: str
|
|
101
|
+
name: str
|
|
102
|
+
state: str
|
|
103
|
+
region: str
|
|
104
|
+
private_ip: str
|
|
105
|
+
host: str
|
|
106
|
+
slug: str
|
|
107
|
+
proxy_url: str
|
|
108
|
+
proxy_session_token: str = ""
|
|
109
|
+
proxy_session_expires_at: str = ""
|
|
110
|
+
|
|
111
|
+
@classmethod
|
|
112
|
+
def from_dict(cls, data: JsonMapping) -> "CreateMachineResponse":
|
|
113
|
+
return cls(
|
|
114
|
+
id=str(data.get("id", "")),
|
|
115
|
+
name=str(data.get("name", "")),
|
|
116
|
+
state=str(data.get("state", "")),
|
|
117
|
+
region=str(data.get("region", "")),
|
|
118
|
+
private_ip=str(data.get("private_ip", "")),
|
|
119
|
+
host=str(data.get("host", "")),
|
|
120
|
+
slug=str(data.get("slug", "")),
|
|
121
|
+
proxy_url=str(data.get("proxy_url", "")),
|
|
122
|
+
proxy_session_token=str(data.get("proxy_session_token", "")),
|
|
123
|
+
proxy_session_expires_at=str(data.get("proxy_session_expires_at", "")),
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
@dataclass(slots=True)
|
|
128
|
+
class MachineListItem:
|
|
129
|
+
id: str
|
|
130
|
+
name: str
|
|
131
|
+
state: str
|
|
132
|
+
region: str
|
|
133
|
+
created_at: str
|
|
134
|
+
updated_at: str
|
|
135
|
+
private_ip: str
|
|
136
|
+
metadata: dict[str, str]
|
|
137
|
+
|
|
138
|
+
@classmethod
|
|
139
|
+
def from_dict(cls, data: JsonMapping) -> "MachineListItem":
|
|
140
|
+
return cls(
|
|
141
|
+
id=str(data.get("id", "")),
|
|
142
|
+
name=str(data.get("name", "")),
|
|
143
|
+
state=str(data.get("state", "")),
|
|
144
|
+
region=str(data.get("region", "")),
|
|
145
|
+
created_at=str(data.get("created_at", "")),
|
|
146
|
+
updated_at=str(data.get("updated_at", "")),
|
|
147
|
+
private_ip=str(data.get("private_ip", "")),
|
|
148
|
+
metadata=dict(data.get("metadata", {})),
|
|
149
|
+
)
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
@dataclass(slots=True)
|
|
153
|
+
class ExecMachineResponse:
|
|
154
|
+
exit_code: int
|
|
155
|
+
exit_signal: int
|
|
156
|
+
stdout: str
|
|
157
|
+
stderr: str
|
|
158
|
+
|
|
159
|
+
@classmethod
|
|
160
|
+
def from_dict(cls, data: JsonMapping) -> "ExecMachineResponse":
|
|
161
|
+
return cls(
|
|
162
|
+
exit_code=int(data.get("exit_code", 0)),
|
|
163
|
+
exit_signal=int(data.get("exit_signal", 0)),
|
|
164
|
+
stdout=str(data.get("stdout", "")),
|
|
165
|
+
stderr=str(data.get("stderr", "")),
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
@dataclass(slots=True)
|
|
170
|
+
class AppSecret:
|
|
171
|
+
name: str
|
|
172
|
+
digest: str
|
|
173
|
+
created_at: str
|
|
174
|
+
updated_at: str
|
|
175
|
+
|
|
176
|
+
@classmethod
|
|
177
|
+
def from_dict(cls, data: JsonMapping) -> "AppSecret":
|
|
178
|
+
return cls(
|
|
179
|
+
name=str(data.get("name", "")),
|
|
180
|
+
digest=str(data.get("digest", "")),
|
|
181
|
+
created_at=str(data.get("created_at", "")),
|
|
182
|
+
updated_at=str(data.get("updated_at", "")),
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
@dataclass(slots=True)
|
|
187
|
+
class TraceRun:
|
|
188
|
+
id: str
|
|
189
|
+
machine_id: str
|
|
190
|
+
trace_mode: str
|
|
191
|
+
status: str
|
|
192
|
+
proxy_url: str
|
|
193
|
+
created_at: str
|
|
194
|
+
started_at: str
|
|
195
|
+
ended_at: str
|
|
196
|
+
metadata: dict[str, Any]
|
|
197
|
+
capture_policy: dict[str, Any]
|
|
198
|
+
expires_at: str
|
|
199
|
+
parent_run_id: str = ""
|
|
200
|
+
replay_config: dict[str, Any] = field(default_factory=dict)
|
|
201
|
+
|
|
202
|
+
@classmethod
|
|
203
|
+
def from_dict(cls, data: JsonMapping) -> "TraceRun":
|
|
204
|
+
return cls(
|
|
205
|
+
id=str(data.get("id", "")),
|
|
206
|
+
machine_id=str(data.get("machine_id") or ""),
|
|
207
|
+
trace_mode=str(data.get("trace_mode", "")),
|
|
208
|
+
status=str(data.get("status", "")),
|
|
209
|
+
proxy_url=str(data.get("proxy_url") or ""),
|
|
210
|
+
created_at=str(data.get("created_at", "")),
|
|
211
|
+
started_at=str(data.get("started_at") or ""),
|
|
212
|
+
ended_at=str(data.get("ended_at") or ""),
|
|
213
|
+
metadata=dict(data.get("metadata") or {}),
|
|
214
|
+
capture_policy=dict(data.get("capture_policy") or {}),
|
|
215
|
+
expires_at=str(data.get("expires_at") or ""),
|
|
216
|
+
parent_run_id=str(data.get("parent_run_id") or ""),
|
|
217
|
+
replay_config=dict(data.get("replay_config") or {}),
|
|
218
|
+
)
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
@dataclass(slots=True)
|
|
222
|
+
class CreateTraceRunResponse(TraceRun):
|
|
223
|
+
ingest_token: str = ""
|
|
224
|
+
ingest_url: str = ""
|
|
225
|
+
|
|
226
|
+
@classmethod
|
|
227
|
+
def from_dict(cls, data: JsonMapping) -> "CreateTraceRunResponse":
|
|
228
|
+
base = TraceRun.from_dict(data)
|
|
229
|
+
return cls(
|
|
230
|
+
id=base.id,
|
|
231
|
+
machine_id=base.machine_id,
|
|
232
|
+
trace_mode=base.trace_mode,
|
|
233
|
+
status=base.status,
|
|
234
|
+
proxy_url=base.proxy_url,
|
|
235
|
+
created_at=base.created_at,
|
|
236
|
+
started_at=base.started_at,
|
|
237
|
+
ended_at=base.ended_at,
|
|
238
|
+
metadata=base.metadata,
|
|
239
|
+
capture_policy=base.capture_policy,
|
|
240
|
+
expires_at=base.expires_at,
|
|
241
|
+
parent_run_id=base.parent_run_id,
|
|
242
|
+
replay_config=base.replay_config,
|
|
243
|
+
ingest_token=str(data.get("ingest_token", "")),
|
|
244
|
+
ingest_url=str(data.get("ingest_url", "")),
|
|
245
|
+
)
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
@dataclass(slots=True)
|
|
249
|
+
class ReplayTraceRunResponse(CreateTraceRunResponse):
|
|
250
|
+
template: dict[str, Any] = field(default_factory=dict)
|
|
251
|
+
|
|
252
|
+
@classmethod
|
|
253
|
+
def from_dict(cls, data: JsonMapping) -> "ReplayTraceRunResponse":
|
|
254
|
+
base = CreateTraceRunResponse.from_dict(data)
|
|
255
|
+
return cls(
|
|
256
|
+
id=base.id,
|
|
257
|
+
machine_id=base.machine_id,
|
|
258
|
+
trace_mode=base.trace_mode,
|
|
259
|
+
status=base.status,
|
|
260
|
+
proxy_url=base.proxy_url,
|
|
261
|
+
created_at=base.created_at,
|
|
262
|
+
started_at=base.started_at,
|
|
263
|
+
ended_at=base.ended_at,
|
|
264
|
+
metadata=base.metadata,
|
|
265
|
+
capture_policy=base.capture_policy,
|
|
266
|
+
expires_at=base.expires_at,
|
|
267
|
+
parent_run_id=base.parent_run_id,
|
|
268
|
+
replay_config=base.replay_config,
|
|
269
|
+
ingest_token=base.ingest_token,
|
|
270
|
+
ingest_url=base.ingest_url,
|
|
271
|
+
template=dict(data.get("template") or {}),
|
|
272
|
+
)
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
@dataclass(slots=True)
|
|
276
|
+
class TraceEvent:
|
|
277
|
+
id: str
|
|
278
|
+
run_id: str
|
|
279
|
+
machine_id: str
|
|
280
|
+
source: str
|
|
281
|
+
type: str
|
|
282
|
+
ts: str
|
|
283
|
+
data: dict[str, Any]
|
|
284
|
+
created_at: str
|
|
285
|
+
|
|
286
|
+
@classmethod
|
|
287
|
+
def from_dict(cls, data: JsonMapping) -> "TraceEvent":
|
|
288
|
+
return cls(
|
|
289
|
+
id=str(data.get("id", "")),
|
|
290
|
+
run_id=str(data.get("run_id", "")),
|
|
291
|
+
machine_id=str(data.get("machine_id") or ""),
|
|
292
|
+
source=str(data.get("source", "")),
|
|
293
|
+
type=str(data.get("type", "")),
|
|
294
|
+
ts=str(data.get("ts", "")),
|
|
295
|
+
data=dict(data.get("data") or {}),
|
|
296
|
+
created_at=str(data.get("created_at", "")),
|
|
297
|
+
)
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
@dataclass(slots=True)
|
|
301
|
+
class TraceSpan:
|
|
302
|
+
id: str
|
|
303
|
+
run_id: str
|
|
304
|
+
machine_id: str
|
|
305
|
+
project_name: str
|
|
306
|
+
session_id: str
|
|
307
|
+
user_id: str
|
|
308
|
+
model_name: str
|
|
309
|
+
provider: str
|
|
310
|
+
trace_id: str
|
|
311
|
+
span_id: str
|
|
312
|
+
parent_span_id: str
|
|
313
|
+
name: str
|
|
314
|
+
kind: str
|
|
315
|
+
start_time: str
|
|
316
|
+
end_time: str
|
|
317
|
+
duration_ms: float
|
|
318
|
+
status_code: str
|
|
319
|
+
status_message: str
|
|
320
|
+
prompt_tokens: int | None
|
|
321
|
+
completion_tokens: int | None
|
|
322
|
+
total_tokens: int | None
|
|
323
|
+
total_cost: float | None
|
|
324
|
+
attributes: dict[str, Any]
|
|
325
|
+
events: list[dict[str, Any]]
|
|
326
|
+
resource_attributes: dict[str, Any]
|
|
327
|
+
scope_name: str
|
|
328
|
+
scope_version: str
|
|
329
|
+
|
|
330
|
+
@classmethod
|
|
331
|
+
def from_dict(cls, data: JsonMapping) -> "TraceSpan":
|
|
332
|
+
return cls(
|
|
333
|
+
id=str(data.get("id", "")),
|
|
334
|
+
run_id=str(data.get("run_id", "")),
|
|
335
|
+
machine_id=str(data.get("machine_id") or ""),
|
|
336
|
+
project_name=str(data.get("project_name", "")),
|
|
337
|
+
session_id=str(data.get("session_id") or ""),
|
|
338
|
+
user_id=str(data.get("user_id") or ""),
|
|
339
|
+
model_name=str(data.get("model_name") or ""),
|
|
340
|
+
provider=str(data.get("provider") or ""),
|
|
341
|
+
trace_id=str(data.get("trace_id", "")),
|
|
342
|
+
span_id=str(data.get("span_id", "")),
|
|
343
|
+
parent_span_id=str(data.get("parent_span_id") or ""),
|
|
344
|
+
name=str(data.get("name", "")),
|
|
345
|
+
kind=str(data.get("kind", "")),
|
|
346
|
+
start_time=str(data.get("start_time", "")),
|
|
347
|
+
end_time=str(data.get("end_time", "")),
|
|
348
|
+
duration_ms=float(data.get("duration_ms") or 0),
|
|
349
|
+
status_code=str(data.get("status_code", "")),
|
|
350
|
+
status_message=str(data.get("status_message") or ""),
|
|
351
|
+
prompt_tokens=(
|
|
352
|
+
int(data["prompt_tokens"]) if data.get("prompt_tokens") is not None else None
|
|
353
|
+
),
|
|
354
|
+
completion_tokens=(
|
|
355
|
+
int(data["completion_tokens"])
|
|
356
|
+
if data.get("completion_tokens") is not None
|
|
357
|
+
else None
|
|
358
|
+
),
|
|
359
|
+
total_tokens=(
|
|
360
|
+
int(data["total_tokens"]) if data.get("total_tokens") is not None else None
|
|
361
|
+
),
|
|
362
|
+
total_cost=(
|
|
363
|
+
float(data["total_cost"]) if data.get("total_cost") is not None else None
|
|
364
|
+
),
|
|
365
|
+
attributes=dict(data.get("attributes") or {}),
|
|
366
|
+
events=list(data.get("events") or []),
|
|
367
|
+
resource_attributes=dict(data.get("resource_attributes") or {}),
|
|
368
|
+
scope_name=str(data.get("scope_name") or ""),
|
|
369
|
+
scope_version=str(data.get("scope_version") or ""),
|
|
370
|
+
)
|
gradient_sdk/py.typed
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|