pentestllm-sdk 0.1.0__tar.gz
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.
- pentestllm_sdk-0.1.0/.gitignore +6 -0
- pentestllm_sdk-0.1.0/LICENSE +9 -0
- pentestllm_sdk-0.1.0/PKG-INFO +29 -0
- pentestllm_sdk-0.1.0/README.md +13 -0
- pentestllm_sdk-0.1.0/pyproject.toml +22 -0
- pentestllm_sdk-0.1.0/src/pentestllm_sdk/__init__.py +126 -0
- pentestllm_sdk-0.1.0/src/pentestllm_sdk/models.py +1162 -0
- pentestllm_sdk-0.1.0/src/pentestllm_sdk/py.typed +0 -0
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Uplink Switch LLC
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: pentestllm-sdk
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: PentestLLM typed Python SDK with model-id autocomplete and cost estimates
|
|
5
|
+
Project-URL: Homepage, https://pentestllm.ai
|
|
6
|
+
Project-URL: Repository, https://gitlab.com/uplinkswitch/uplinkswitch-sdk
|
|
7
|
+
Author: Uplink Switch LLC
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: ai,llm,openai-compatible,pentestllm,sdk
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Typing :: Typed
|
|
14
|
+
Requires-Python: >=3.9
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
|
|
17
|
+
# pentestllm-sdk
|
|
18
|
+
|
|
19
|
+
Typed Python SDK for PentestLLM. No dependencies.
|
|
20
|
+
|
|
21
|
+
```python
|
|
22
|
+
from pentestllm_sdk import PentestLLM, estimate_cost
|
|
23
|
+
|
|
24
|
+
client = PentestLLM() # reads PENTESTLLM_API_KEY
|
|
25
|
+
r = client.chat.completions.create(model="abliterated-model", messages=[{"role": "user", "content": "hi"}])
|
|
26
|
+
print(estimate_cost(r), r["_uplink"]["charge_usd"])
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Model list and prices: https://pentestllm.ai/models
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# pentestllm-sdk
|
|
2
|
+
|
|
3
|
+
Typed Python SDK for PentestLLM. No dependencies.
|
|
4
|
+
|
|
5
|
+
```python
|
|
6
|
+
from pentestllm_sdk import PentestLLM, estimate_cost
|
|
7
|
+
|
|
8
|
+
client = PentestLLM() # reads PENTESTLLM_API_KEY
|
|
9
|
+
r = client.chat.completions.create(model="abliterated-model", messages=[{"role": "user", "content": "hi"}])
|
|
10
|
+
print(estimate_cost(r), r["_uplink"]["charge_usd"])
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Model list and prices: https://pentestllm.ai/models
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "pentestllm-sdk"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "PentestLLM typed Python SDK with model-id autocomplete and cost estimates"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
requires-python = ">=3.9"
|
|
12
|
+
authors = [{ name = "Uplink Switch LLC" }]
|
|
13
|
+
keywords = ["llm", "ai", "sdk", "openai-compatible", "pentestllm"]
|
|
14
|
+
classifiers = ["Typing :: Typed", "License :: OSI Approved :: MIT License", "Programming Language :: Python :: 3"]
|
|
15
|
+
dependencies = []
|
|
16
|
+
|
|
17
|
+
[project.urls]
|
|
18
|
+
Homepage = "https://pentestllm.ai"
|
|
19
|
+
Repository = "https://gitlab.com/uplinkswitch/uplinkswitch-sdk"
|
|
20
|
+
|
|
21
|
+
[tool.hatch.build.targets.wheel]
|
|
22
|
+
packages = ["src/pentestllm_sdk"]
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"""PentestLLM typed Python SDK (standard library only)."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import json
|
|
5
|
+
import os
|
|
6
|
+
import random
|
|
7
|
+
import time
|
|
8
|
+
import urllib.error
|
|
9
|
+
import urllib.request
|
|
10
|
+
from typing import Any, Dict, Iterator, List, Optional, Union
|
|
11
|
+
|
|
12
|
+
from .models import MODELS, ModelId, ModelInfo
|
|
13
|
+
|
|
14
|
+
__all__ = ["PentestLLM", "APIError", "MODELS", "ModelId", "ModelInfo", "estimate_cost"]
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class APIError(Exception):
|
|
18
|
+
def __init__(self, status: int, body: str) -> None:
|
|
19
|
+
super().__init__("API error %d: %s" % (status, body[:300]))
|
|
20
|
+
self.status = status
|
|
21
|
+
self.body = body
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class _Completions:
|
|
25
|
+
def __init__(self, client: "PentestLLM") -> None:
|
|
26
|
+
self._c = client
|
|
27
|
+
|
|
28
|
+
def create(self, *, model: ModelId, messages: List[Dict[str, Any]], stream: bool = False, **kwargs: Any) -> Union[Dict[str, Any], Iterator[Dict[str, Any]]]:
|
|
29
|
+
c = self._c
|
|
30
|
+
last: Optional[BaseException] = None
|
|
31
|
+
for m in [model, *c.fallback_models]:
|
|
32
|
+
try:
|
|
33
|
+
resp = c._send("/chat/completions", "POST", dict(kwargs, model=m, messages=messages, stream=stream))
|
|
34
|
+
if stream:
|
|
35
|
+
return _parse_sse(resp)
|
|
36
|
+
with resp:
|
|
37
|
+
data = json.loads(resp.read().decode("utf-8"))
|
|
38
|
+
charge = resp.headers.get("x-uplink-charge")
|
|
39
|
+
try:
|
|
40
|
+
charge_usd: Optional[float] = float(charge) if charge is not None else None
|
|
41
|
+
except ValueError:
|
|
42
|
+
charge_usd = None
|
|
43
|
+
data["_uplink"] = {"charge_usd": charge_usd, "request_id": resp.headers.get("x-request-id")}
|
|
44
|
+
return data
|
|
45
|
+
except APIError as e:
|
|
46
|
+
last = e
|
|
47
|
+
if e.status in (404, 503):
|
|
48
|
+
continue
|
|
49
|
+
raise
|
|
50
|
+
assert last is not None
|
|
51
|
+
raise last
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class _Chat:
|
|
55
|
+
def __init__(self, client: "PentestLLM") -> None:
|
|
56
|
+
self.completions = _Completions(client)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class _Models:
|
|
60
|
+
def __init__(self, client: "PentestLLM") -> None:
|
|
61
|
+
self._c = client
|
|
62
|
+
|
|
63
|
+
def list(self) -> Dict[str, Any]:
|
|
64
|
+
with self._c._send("/models", "GET") as r:
|
|
65
|
+
return json.loads(r.read().decode("utf-8"))
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class PentestLLM:
|
|
69
|
+
def __init__(self, api_key: Optional[str] = None, base_url: Optional[str] = None, max_retries: int = 2, fallback_models: Optional[List[ModelId]] = None) -> None:
|
|
70
|
+
self.api_key = api_key if api_key is not None else os.environ.get("PENTESTLLM_API_KEY")
|
|
71
|
+
self.base_url = (base_url or "https://api.pentestllm.ai/v1").rstrip("/")
|
|
72
|
+
self.max_retries = max_retries
|
|
73
|
+
self.fallback_models: List[str] = list(fallback_models or [])
|
|
74
|
+
self.chat = _Chat(self)
|
|
75
|
+
|
|
76
|
+
def models(self) -> _Models:
|
|
77
|
+
return _Models(self)
|
|
78
|
+
|
|
79
|
+
def _send(self, path: str, method: str, body: Optional[Dict[str, Any]] = None) -> Any:
|
|
80
|
+
headers = {"content-type": "application/json"}
|
|
81
|
+
if self.api_key:
|
|
82
|
+
headers["authorization"] = "Bearer " + self.api_key
|
|
83
|
+
data = json.dumps(body).encode("utf-8") if body is not None else None
|
|
84
|
+
last: Optional[BaseException] = None
|
|
85
|
+
for attempt in range(self.max_retries + 1):
|
|
86
|
+
if attempt:
|
|
87
|
+
time.sleep(0.25 * 2 ** (attempt - 1) + random.random() * 0.1)
|
|
88
|
+
req = urllib.request.Request(self.base_url + path, data=data, headers=headers, method=method)
|
|
89
|
+
try:
|
|
90
|
+
return urllib.request.urlopen(req, timeout=120)
|
|
91
|
+
except urllib.error.HTTPError as e:
|
|
92
|
+
err = APIError(e.code, e.read().decode("utf-8", "replace"))
|
|
93
|
+
if e.code == 429 or e.code >= 500:
|
|
94
|
+
last = err
|
|
95
|
+
continue
|
|
96
|
+
raise err
|
|
97
|
+
except (urllib.error.URLError, OSError) as e:
|
|
98
|
+
last = e
|
|
99
|
+
assert last is not None
|
|
100
|
+
raise last
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _parse_sse(resp: Any) -> Iterator[Dict[str, Any]]:
|
|
104
|
+
with resp:
|
|
105
|
+
for raw in resp:
|
|
106
|
+
line = raw.decode("utf-8").strip()
|
|
107
|
+
if line.startswith("data:"):
|
|
108
|
+
d = line[5:].strip()
|
|
109
|
+
if d and d != "[DONE]":
|
|
110
|
+
yield json.loads(d)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def estimate_cost(response: Dict[str, Any]) -> Optional[float]:
|
|
114
|
+
"""Estimated USD cost from catalog prices; None when the model is unknown."""
|
|
115
|
+
m = MODELS.get(response.get("model", ""))
|
|
116
|
+
u = response.get("usage")
|
|
117
|
+
if m is None or not u or m.input is None or m.output is None:
|
|
118
|
+
return None
|
|
119
|
+
prompt = u.get("prompt_tokens", 0)
|
|
120
|
+
details = u.get("prompt_tokens_details") or {}
|
|
121
|
+
if "cached_tokens" in details:
|
|
122
|
+
cached = details["cached_tokens"]
|
|
123
|
+
pc = (prompt - cached) * m.input + cached * (m.cache_read if m.cache_read is not None else m.input)
|
|
124
|
+
else:
|
|
125
|
+
pc = prompt * m.input
|
|
126
|
+
return (pc + u.get("completion_tokens", 0) * m.output) / 1e6
|