basal-sdk 3.0.3__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.
- basal_sdk-3.0.3/PKG-INFO +42 -0
- basal_sdk-3.0.3/README.md +31 -0
- basal_sdk-3.0.3/basal_sdk/__init__.py +21 -0
- basal_sdk-3.0.3/basal_sdk/adapters.py +75 -0
- basal_sdk-3.0.3/basal_sdk/client.py +790 -0
- basal_sdk-3.0.3/basal_sdk/presets.py +280 -0
- basal_sdk-3.0.3/basal_sdk.egg-info/PKG-INFO +42 -0
- basal_sdk-3.0.3/basal_sdk.egg-info/SOURCES.txt +17 -0
- basal_sdk-3.0.3/basal_sdk.egg-info/dependency_links.txt +1 -0
- basal_sdk-3.0.3/basal_sdk.egg-info/requires.txt +4 -0
- basal_sdk-3.0.3/basal_sdk.egg-info/top_level.txt +1 -0
- basal_sdk-3.0.3/pyproject.toml +24 -0
- basal_sdk-3.0.3/setup.cfg +4 -0
- basal_sdk-3.0.3/tests/test_client_api_key.py +38 -0
- basal_sdk-3.0.3/tests/test_client_errors.py +53 -0
- basal_sdk-3.0.3/tests/test_client_responses.py +32 -0
- basal_sdk-3.0.3/tests/test_client_retries.py +471 -0
- basal_sdk-3.0.3/tests/test_guard_presets.py +178 -0
- basal_sdk-3.0.3/tests/test_sdk_alone.py +39 -0
basal_sdk-3.0.3/PKG-INFO
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: basal-sdk
|
|
3
|
+
Version: 3.0.3
|
|
4
|
+
Summary: Python client for basal-serve: typed decisions (choice / yes-no / score) with calibrated probabilities
|
|
5
|
+
License-Expression: Apache-2.0
|
|
6
|
+
Requires-Python: >=3.10
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
Requires-Dist: httpx>=0.28
|
|
9
|
+
Provides-Extra: test
|
|
10
|
+
Requires-Dist: pytest; extra == "test"
|
|
11
|
+
|
|
12
|
+
# basal-sdk
|
|
13
|
+
|
|
14
|
+
The Python client of [basal](https://github.com/rkinas/basal)'s server, basal-serve: typed decisions (choice, yes-no,
|
|
15
|
+
score and the other question types of `POST /v1/systemone`) with calibrated probabilities. Blocking (`Basal`) or
|
|
16
|
+
asyncio (`AsyncBasal`), with timeouts, retries and fail-open; ready question sets (`basal_sdk.presets`) and tools for
|
|
17
|
+
LangChain, LlamaIndex and CrewAI (`basal_sdk.adapters`). Its only dependency is httpx: no torch, no model.
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pip install basal-sdk
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
```python
|
|
24
|
+
from basal_sdk import Basal, choice, yes_no
|
|
25
|
+
|
|
26
|
+
b = Basal("http://127.0.0.1:8000", timeout=10, retries=2)
|
|
27
|
+
a = b.decide("Since 7:40 no card terminal works in the Gdańsk branch; only cash.",
|
|
28
|
+
{"dept": choice("Which department?", {"it": "IT", "tills": "Tills"}),
|
|
29
|
+
"card": yes_no("Is it about card payments?")})
|
|
30
|
+
print(a["dept"]["choice"], a["card"]["noul"])
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Which install gives which import:
|
|
34
|
+
|
|
35
|
+
| Install | Imports |
|
|
36
|
+
|---|---|
|
|
37
|
+
| `pip install basal-sdk` (an application that calls a server) | `basal_sdk` |
|
|
38
|
+
| `pip install basal-engine` (the server; it installs basal-sdk of the same version) | `basal` and `basal_sdk`; `basal.client`, `basal.presets` and `basal.adapters` are the modules of `basal_sdk` |
|
|
39
|
+
|
|
40
|
+
basal-sdk and basal-engine are released together, at one version. The methods, errors and options are described in
|
|
41
|
+
[docs/INSTALL.md](https://github.com/rkinas/basal/blob/main/docs/INSTALL.md#python-client) and the request and answer
|
|
42
|
+
shapes in [docs/API.md](https://github.com/rkinas/basal/blob/main/docs/API.md). Apache-2.0.
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# basal-sdk
|
|
2
|
+
|
|
3
|
+
The Python client of [basal](https://github.com/rkinas/basal)'s server, basal-serve: typed decisions (choice, yes-no,
|
|
4
|
+
score and the other question types of `POST /v1/systemone`) with calibrated probabilities. Blocking (`Basal`) or
|
|
5
|
+
asyncio (`AsyncBasal`), with timeouts, retries and fail-open; ready question sets (`basal_sdk.presets`) and tools for
|
|
6
|
+
LangChain, LlamaIndex and CrewAI (`basal_sdk.adapters`). Its only dependency is httpx: no torch, no model.
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
pip install basal-sdk
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
```python
|
|
13
|
+
from basal_sdk import Basal, choice, yes_no
|
|
14
|
+
|
|
15
|
+
b = Basal("http://127.0.0.1:8000", timeout=10, retries=2)
|
|
16
|
+
a = b.decide("Since 7:40 no card terminal works in the Gdańsk branch; only cash.",
|
|
17
|
+
{"dept": choice("Which department?", {"it": "IT", "tills": "Tills"}),
|
|
18
|
+
"card": yes_no("Is it about card payments?")})
|
|
19
|
+
print(a["dept"]["choice"], a["card"]["noul"])
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Which install gives which import:
|
|
23
|
+
|
|
24
|
+
| Install | Imports |
|
|
25
|
+
|---|---|
|
|
26
|
+
| `pip install basal-sdk` (an application that calls a server) | `basal_sdk` |
|
|
27
|
+
| `pip install basal-engine` (the server; it installs basal-sdk of the same version) | `basal` and `basal_sdk`; `basal.client`, `basal.presets` and `basal.adapters` are the modules of `basal_sdk` |
|
|
28
|
+
|
|
29
|
+
basal-sdk and basal-engine are released together, at one version. The methods, errors and options are described in
|
|
30
|
+
[docs/INSTALL.md](https://github.com/rkinas/basal/blob/main/docs/INSTALL.md#python-client) and the request and answer
|
|
31
|
+
shapes in [docs/API.md](https://github.com/rkinas/basal/blob/main/docs/API.md). Apache-2.0.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"""basal-sdk: the Python client of basal-serve (POST /v1/systemone), its preset question sets and the agent-framework
|
|
2
|
+
adapters, with httpx as its only dependency (no torch, no model). basal-engine installs it at its own version, and
|
|
3
|
+
there basal.client, basal.presets and basal.adapters are these same modules.
|
|
4
|
+
|
|
5
|
+
from basal_sdk import Basal, choice, yes_no
|
|
6
|
+
b = Basal("http://127.0.0.1:8000")
|
|
7
|
+
b.decide(state, {"dept": choice("Which department?", {"billing": "Billing", "tech": "Technical support"}),
|
|
8
|
+
"urgent": yes_no("Is it urgent?")})
|
|
9
|
+
|
|
10
|
+
basal_sdk.client (Basal, AsyncBasal, the errors and question types), basal_sdk.presets (ready question sets, guard and
|
|
11
|
+
route), basal_sdk.adapters (LangChain, LlamaIndex, CrewAI tools)."""
|
|
12
|
+
from .client import (BUILTIN_RULE, AsyncBasal, AuthError, Basal, BasalError, Decision, InvalidRequest, NotReady,
|
|
13
|
+
OutOfMemory, QuestionError, ServerError, act, choice, classify, complete, compress, decision_of,
|
|
14
|
+
extract, fails_open, media_type, multi, rank, retryable, score, tool, verify, yes_no)
|
|
15
|
+
|
|
16
|
+
# basal-engine's: the two are released together (tests/test_versions.py)
|
|
17
|
+
__version__ = "3.0.3"
|
|
18
|
+
__all__ = ["BUILTIN_RULE", "AsyncBasal", "AuthError", "Basal", "BasalError", "Decision", "InvalidRequest", "NotReady",
|
|
19
|
+
"OutOfMemory", "QuestionError", "ServerError", "act", "choice", "classify", "complete", "compress",
|
|
20
|
+
"decision_of", "extract", "fails_open", "media_type", "multi", "rank", "retryable", "score", "tool",
|
|
21
|
+
"verify", "yes_no"]
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"""Thin tools for agent frameworks over a running basal server's HTTP API (2.5). Each returns the framework's own tool
|
|
2
|
+
object that calls /v1/systemone: give it a state and questions (the HTTP API's), or fix a preset (basal_sdk.presets)
|
|
3
|
+
so the agent only passes the state. The frameworks are not dependencies of basal-sdk: install the one you use. For
|
|
4
|
+
MCP hosts use basal-engine's basal-mcp / basal-serve --mcp on instead (docs/MCP.md).
|
|
5
|
+
|
|
6
|
+
from basal_sdk.adapters import langchain_tool, llamaindex_tool, crewai_tool
|
|
7
|
+
tool = langchain_tool("http://127.0.0.1:8000") # args: state, questions
|
|
8
|
+
triage = crewai_tool("http://127.0.0.1:8000", preset="triage") # args: state"""
|
|
9
|
+
import json
|
|
10
|
+
|
|
11
|
+
NAME = "basal_decide"
|
|
12
|
+
DESCRIPTION = ("Typed decisions about a text (state) with calibrated probabilities, one forward pass, no generated "
|
|
13
|
+
"text. Put every fact the decision needs in the state. questions: {name: {type: choice | noul | score | "
|
|
14
|
+
"multi, instructions: the question, criteria: {key: what the option means}}}.")
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def decide_fn(url="http://127.0.0.1:8000", preset=None, lang=None, client=None):
|
|
18
|
+
"""The function every adapter wraps: (state, questions=None) -> the answers as a dict (JSON-serialisable)."""
|
|
19
|
+
from .client import Basal
|
|
20
|
+
b = client or Basal(url)
|
|
21
|
+
|
|
22
|
+
def basal_decide(state: str, questions: dict | None = None) -> dict:
|
|
23
|
+
if isinstance(questions, str): # agents often pass JSON text
|
|
24
|
+
questions = json.loads(questions)
|
|
25
|
+
if preset:
|
|
26
|
+
return b.preset(state, preset, lang)
|
|
27
|
+
if not questions:
|
|
28
|
+
raise ValueError("questions are needed (or create the tool with a preset)")
|
|
29
|
+
return b.decide(state, questions)
|
|
30
|
+
return basal_decide
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _describe(preset):
|
|
34
|
+
return f"{DESCRIPTION} This tool asks the '{preset}' preset: pass the state only." if preset else DESCRIPTION
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _need(module, extra):
|
|
38
|
+
raise ImportError(f"this adapter needs {module}: uv pip install {extra}")
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def langchain_tool(url="http://127.0.0.1:8000", preset=None, lang=None, client=None):
|
|
42
|
+
"""A LangChain StructuredTool (langchain-core)."""
|
|
43
|
+
try:
|
|
44
|
+
from langchain_core.tools import StructuredTool
|
|
45
|
+
except ImportError:
|
|
46
|
+
_need("langchain-core", "langchain-core")
|
|
47
|
+
return StructuredTool.from_function(func=decide_fn(url, preset, lang, client), name=NAME,
|
|
48
|
+
description=_describe(preset))
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def llamaindex_tool(url="http://127.0.0.1:8000", preset=None, lang=None, client=None):
|
|
52
|
+
"""A LlamaIndex FunctionTool (llama-index-core)."""
|
|
53
|
+
try:
|
|
54
|
+
from llama_index.core.tools import FunctionTool
|
|
55
|
+
except ImportError:
|
|
56
|
+
_need("llama-index-core", "llama-index-core")
|
|
57
|
+
return FunctionTool.from_defaults(fn=decide_fn(url, preset, lang, client), name=NAME,
|
|
58
|
+
description=_describe(preset))
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def crewai_tool(url="http://127.0.0.1:8000", preset=None, lang=None, client=None):
|
|
62
|
+
"""A CrewAI BaseTool."""
|
|
63
|
+
try:
|
|
64
|
+
from crewai.tools import BaseTool
|
|
65
|
+
except ImportError:
|
|
66
|
+
_need("crewai", "crewai")
|
|
67
|
+
fn = decide_fn(url, preset, lang, client)
|
|
68
|
+
|
|
69
|
+
class BasalDecide(BaseTool):
|
|
70
|
+
name: str = NAME
|
|
71
|
+
description: str = _describe(preset)
|
|
72
|
+
|
|
73
|
+
def _run(self, state: str, questions: dict | None = None) -> str:
|
|
74
|
+
return json.dumps(fn(state, questions), ensure_ascii=False)
|
|
75
|
+
return BasalDecide()
|