efficientai-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.
- efficientai_sdk-0.1.0/.gitignore +4 -0
- efficientai_sdk-0.1.0/CHANGELOG.md +22 -0
- efficientai_sdk-0.1.0/LICENSE +21 -0
- efficientai_sdk-0.1.0/PKG-INFO +168 -0
- efficientai_sdk-0.1.0/README.md +135 -0
- efficientai_sdk-0.1.0/examples/ci/simulate.yml +24 -0
- efficientai_sdk-0.1.0/examples/custom_agent_tracing.py +84 -0
- efficientai_sdk-0.1.0/examples/customer_http_chat_agent.py +71 -0
- efficientai_sdk-0.1.0/pyproject.toml +92 -0
- efficientai_sdk-0.1.0/src/efficientai_sdk/__init__.py +40 -0
- efficientai_sdk-0.1.0/src/efficientai_sdk/_base_client.py +313 -0
- efficientai_sdk-0.1.0/src/efficientai_sdk/_client.py +196 -0
- efficientai_sdk-0.1.0/src/efficientai_sdk/_config.py +139 -0
- efficientai_sdk-0.1.0/src/efficientai_sdk/_exceptions.py +142 -0
- efficientai_sdk-0.1.0/src/efficientai_sdk/_version.py +1 -0
- efficientai_sdk-0.1.0/src/efficientai_sdk/cli.py +99 -0
- efficientai_sdk-0.1.0/src/efficientai_sdk/integrations/__init__.py +4 -0
- efficientai_sdk-0.1.0/src/efficientai_sdk/integrations/livekit.py +134 -0
- efficientai_sdk-0.1.0/src/efficientai_sdk/integrations/pipecat.py +126 -0
- efficientai_sdk-0.1.0/src/efficientai_sdk/py.typed +0 -0
- efficientai_sdk-0.1.0/src/efficientai_sdk/simulations/__init__.py +25 -0
- efficientai_sdk-0.1.0/src/efficientai_sdk/simulations/api.py +682 -0
- efficientai_sdk-0.1.0/src/efficientai_sdk/simulations/models.py +186 -0
- efficientai_sdk-0.1.0/src/efficientai_sdk/simulations/polling.py +62 -0
- efficientai_sdk-0.1.0/src/efficientai_sdk/tracing/__init__.py +110 -0
- efficientai_sdk-0.1.0/src/efficientai_sdk/tracing/api.py +835 -0
- efficientai_sdk-0.1.0/src/efficientai_sdk/tracing/context.py +57 -0
- efficientai_sdk-0.1.0/src/efficientai_sdk/tracing/correlation.py +191 -0
- efficientai_sdk-0.1.0/src/efficientai_sdk/tracing/handshake.py +42 -0
- efficientai_sdk-0.1.0/src/efficientai_sdk/tracing/otel.py +535 -0
- efficientai_sdk-0.1.0/src/efficientai_sdk/tracing/session.py +385 -0
- efficientai_sdk-0.1.0/tests/__init__.py +0 -0
- efficientai_sdk-0.1.0/tests/conftest.py +106 -0
- efficientai_sdk-0.1.0/tests/fake_otlp.py +28 -0
- efficientai_sdk-0.1.0/tests/test_cli.py +76 -0
- efficientai_sdk-0.1.0/tests/test_client.py +306 -0
- efficientai_sdk-0.1.0/tests/test_config.py +91 -0
- efficientai_sdk-0.1.0/tests/test_livekit_integration.py +81 -0
- efficientai_sdk-0.1.0/tests/test_package.py +37 -0
- efficientai_sdk-0.1.0/tests/test_simulations.py +280 -0
- efficientai_sdk-0.1.0/tests/test_tracing_api.py +407 -0
- efficientai_sdk-0.1.0/tests/test_tracing_chat.py +121 -0
- efficientai_sdk-0.1.0/tests/test_tracing_correlation.py +108 -0
- efficientai_sdk-0.1.0/tests/test_tracing_otel.py +172 -0
- efficientai_sdk-0.1.0/tests/test_tracing_session.py +284 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to `efficientai-sdk` are recorded here. The project follows [semantic versioning](https://semver.org); while in 0.x, minor versions may include breaking changes.
|
|
4
|
+
|
|
5
|
+
## 0.1.0 (unreleased)
|
|
6
|
+
|
|
7
|
+
- `EfficientAI` and `AsyncEfficientAI` clients, configured from arguments or `EFFICIENTAI_*` environment variables. The base URL defaults to `http://localhost:8000` for self-hosted deployments.
|
|
8
|
+
- `verify()` checks an API key against the server.
|
|
9
|
+
- Typed exception hierarchy rooted at `EfficientAIError`.
|
|
10
|
+
- Automatic retries with exponential backoff, jitter and `Retry-After` support.
|
|
11
|
+
- `efficientai_sdk.tracing`: trace sessions, OTLP export with correlation headers and span attributes, handshake parsing, and SIP/webhook correlation helpers.
|
|
12
|
+
- `efficientai_sdk.integrations.pipecat`: `configure_pipecat_tracing` and related helpers.
|
|
13
|
+
- `client.trace.session(...)` (sync and async): one trace session per call, with `turn`, `stt`, `llm`, `tts`, `s2s` and `span` helpers that emit spans in the format EfficientAI maps to turns and latencies. Reuses the platform's call id from a handshake, HTTP headers or SIP headers.
|
|
14
|
+
- `client.trace.instrument()` exports an existing OpenTelemetry setup to EfficientAI.
|
|
15
|
+
- `efficientai_sdk.integrations.pipecat.task_kwargs(call)`.
|
|
16
|
+
- `efficientai_sdk.integrations.livekit`: `trace_session` and `job_handshake` for LiveKit Agents jobs (metadata handshake + OTLP instrument).
|
|
17
|
+
- Chat agents: `session(channel="chat")`, `call.user_message()`, `call.assistant_message()`, `call.tool()` and `call.retrieval()`. Long attribute values are truncated at 8,192 characters.
|
|
18
|
+
- Call correlation is now tracked per asyncio task / context, so concurrent calls in one process no longer mix spans. This also applies to the older `setup_worker_tracing` / `setup_efficientai_tracing` helpers.
|
|
19
|
+
- Fix: when the app already has its own `TracerProvider`, the EfficientAI exporter is now added to it. Previously spans were never exported in that case.
|
|
20
|
+
- Trace contexts now carry `evaluator_result_id` from the platform handshake, so spans link to simulation results.
|
|
21
|
+
- `client.simulations`, `client.suites`, `client.evaluators` and `client.results` start runs and poll them. `run.wait()` returns when the run finishes; `run.passed` is whether it met the threshold.
|
|
22
|
+
- CLI: `efficientai simulate --suite <id> --fail-under 0.8` exits non-zero when the suite pass rate is too low.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 EfficientAI.tech
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: efficientai-sdk
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python SDK for EfficientAI: live traces, simulations and evaluations for voice and chat agents.
|
|
5
|
+
Project-URL: Homepage, https://efficientai.cloud
|
|
6
|
+
Project-URL: Documentation, https://docs.efficientai.cloud
|
|
7
|
+
Project-URL: Source, https://github.com/EfficientAI-tech/efficientAI/tree/main/sdks/python
|
|
8
|
+
Project-URL: Changelog, https://github.com/EfficientAI-tech/efficientAI/blob/main/sdks/python/CHANGELOG.md
|
|
9
|
+
Author: EfficientAI
|
|
10
|
+
License-Expression: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: efficientai,evaluation,observability,opentelemetry,simulation,voice agents
|
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
23
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
24
|
+
Classifier: Typing :: Typed
|
|
25
|
+
Requires-Python: >=3.9
|
|
26
|
+
Requires-Dist: httpx<1,>=0.25
|
|
27
|
+
Requires-Dist: pydantic<3,>=2.5
|
|
28
|
+
Requires-Dist: python-dotenv>=1.0.0
|
|
29
|
+
Provides-Extra: otel
|
|
30
|
+
Requires-Dist: opentelemetry-exporter-otlp-proto-http>=1.27.0; extra == 'otel'
|
|
31
|
+
Requires-Dist: opentelemetry-sdk>=1.27.0; extra == 'otel'
|
|
32
|
+
Description-Content-Type: text/markdown
|
|
33
|
+
|
|
34
|
+
# EfficientAI Python SDK
|
|
35
|
+
|
|
36
|
+
The official Python client for [EfficientAI](https://efficientai.cloud). Use it to send live traces from your voice and chat agents, run simulations, and read evaluation results.
|
|
37
|
+
|
|
38
|
+
> **Status: alpha (0.x).** The client core is in place. Live tracing and simulations are being added; see the [changelog](CHANGELOG.md).
|
|
39
|
+
|
|
40
|
+
## Install
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
pip install efficientai-sdk
|
|
44
|
+
# with OpenTelemetry tracing support
|
|
45
|
+
pip install "efficientai-sdk[otel]"
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Requires Python 3.9 or newer.
|
|
49
|
+
|
|
50
|
+
## Configure
|
|
51
|
+
|
|
52
|
+
EfficientAI is self-hosted, so point the SDK at your own deployment. If no URL is set, it uses `http://localhost:8000`.
|
|
53
|
+
|
|
54
|
+
Create an API key in your EfficientAI dashboard. Then either set environment variables:
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
export EFFICIENTAI_BASE_URL="https://efficientai.your-company.com" # your deployment; default http://localhost:8000
|
|
58
|
+
export EFFICIENTAI_API_KEY="..."
|
|
59
|
+
export EFFICIENTAI_WORKSPACE_ID="..." # optional; the key's default workspace is used otherwise
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
or pass the same values to the client:
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
from efficientai_sdk import EfficientAI
|
|
66
|
+
|
|
67
|
+
client = EfficientAI(
|
|
68
|
+
base_url="https://efficientai.your-company.com",
|
|
69
|
+
api_key="...",
|
|
70
|
+
workspace_id="...",
|
|
71
|
+
)
|
|
72
|
+
client.verify() # True, or raises AuthenticationError
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
An async client with the same arguments is available:
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
from efficientai_sdk import AsyncEfficientAI
|
|
79
|
+
|
|
80
|
+
async with AsyncEfficientAI() as client:
|
|
81
|
+
await client.verify()
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Live call tracing
|
|
85
|
+
|
|
86
|
+
Install the `otel` extra, then wrap each call in a trace session. Spans created inside the session are tagged with the call's id and exported to EfficientAI. The session closes when the block ends.
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
from efficientai_sdk import AsyncEfficientAI
|
|
90
|
+
|
|
91
|
+
efficientai = AsyncEfficientAI()
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
async def handle_call(first_message=None):
|
|
95
|
+
async with efficientai.trace.session(transport="websocket", handshake=first_message) as call:
|
|
96
|
+
with call.turn():
|
|
97
|
+
with call.stt(model="nova-3", provider="deepgram") as stt:
|
|
98
|
+
stt.set_transcript(user_text)
|
|
99
|
+
with call.llm(model="gpt-4o", provider="openai") as llm:
|
|
100
|
+
llm.set_output(reply).set_ttfb(0.31)
|
|
101
|
+
with call.tts(model="sonic", provider="cartesia") as tts:
|
|
102
|
+
tts.set_text(reply)
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
- **Chat agents:** `session(channel="chat")` plus `call.user_message()`, `call.tool()`, `call.retrieval()` and `call.llm()`. Each turn shows the user message, the final reply, tool calls and response time.
|
|
106
|
+
- **Pipecat:** `PipelineTask(pipeline, **task_kwargs(call))` with `from efficientai_sdk.integrations.pipecat import task_kwargs`.
|
|
107
|
+
- **LiveKit Agents:** `async with livekit.trace_session(client, ctx) as call:` with `from efficientai_sdk.integrations import livekit`.
|
|
108
|
+
- **Existing OpenTelemetry setup:** call `efficientai.trace.instrument()` once at startup.
|
|
109
|
+
- **Test calls from EfficientAI:** pass the platform's `handshake=`, `headers=` or `sip_headers=` so traces link to the simulation result.
|
|
110
|
+
- **Concurrent calls:** each session is tracked per asyncio task, so one process can serve many calls.
|
|
111
|
+
- **Failures:** tracing never breaks a call. If EfficientAI is unreachable, the call runs untraced and a warning is logged. Pass `strict=True` to raise instead.
|
|
112
|
+
|
|
113
|
+
A runnable example is in [`examples/custom_agent_tracing.py`](examples/custom_agent_tracing.py). The full guide is on the "Python SDK" page of the EfficientAI docs.
|
|
114
|
+
|
|
115
|
+
## Run simulations
|
|
116
|
+
|
|
117
|
+
```python
|
|
118
|
+
run = client.simulations.run(
|
|
119
|
+
agent_id="...", scenario_ids=["..."], persona_ids=["..."], n_attempts=2
|
|
120
|
+
)
|
|
121
|
+
run = run.wait(timeout=1800)
|
|
122
|
+
assert run.passed, run.summary
|
|
123
|
+
|
|
124
|
+
suite = client.suites.run("suite-id", runs_per_combination=3)
|
|
125
|
+
suite = suite.wait()
|
|
126
|
+
assert suite.passed, suite.pass_rate
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
From CI:
|
|
130
|
+
|
|
131
|
+
```bash
|
|
132
|
+
efficientai simulate --suite "$EFFICIENTAI_SUITE_ID" --fail-under 0.8
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
The command exits `0` at or above the threshold, `1` when the suite scores lower or a result errors, and `2` when the arguments or the API call fail. See `examples/ci/simulate.yml`.
|
|
136
|
+
|
|
137
|
+
## Errors and retries
|
|
138
|
+
|
|
139
|
+
Every error the SDK raises inherits from `EfficientAIError`.
|
|
140
|
+
|
|
141
|
+
| Exception | When |
|
|
142
|
+
|---|---|
|
|
143
|
+
| `ConfigurationError` | Missing API key or invalid settings |
|
|
144
|
+
| `APIConnectionError` / `APITimeoutError` | The server could not be reached |
|
|
145
|
+
| `AuthenticationError` | 401: the key is invalid or inactive |
|
|
146
|
+
| `PermissionDeniedError` | 403 |
|
|
147
|
+
| `NotFoundError` | 404 |
|
|
148
|
+
| `UnprocessableEntityError` | 422: the request failed validation |
|
|
149
|
+
| `RateLimitError` | 429; `retry_after` holds the server's hint |
|
|
150
|
+
| `InternalServerError` | 5xx |
|
|
151
|
+
|
|
152
|
+
The client retries failed requests up to `max_retries` times (default 2), with exponential backoff and jitter. It honours the server's `Retry-After` header.
|
|
153
|
+
- `GET`, `PUT` and `DELETE` retry on timeouts, 408, 429 and 5xx responses.
|
|
154
|
+
- `POST` and `PATCH` retry only when the server certainly didn't process the request: 429, 503, or a failed connection.
|
|
155
|
+
|
|
156
|
+
Set `max_retries=0` to turn retries off.
|
|
157
|
+
|
|
158
|
+
## Development
|
|
159
|
+
|
|
160
|
+
```bash
|
|
161
|
+
cd sdks/python
|
|
162
|
+
uv sync
|
|
163
|
+
uv run pytest
|
|
164
|
+
uv run ruff check . && uv run ruff format --check .
|
|
165
|
+
uv run mypy
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
Releases are published to PyPI from tags named `sdk-py-v<version>`, which must match `src/efficientai_sdk/_version.py`.
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
# EfficientAI Python SDK
|
|
2
|
+
|
|
3
|
+
The official Python client for [EfficientAI](https://efficientai.cloud). Use it to send live traces from your voice and chat agents, run simulations, and read evaluation results.
|
|
4
|
+
|
|
5
|
+
> **Status: alpha (0.x).** The client core is in place. Live tracing and simulations are being added; see the [changelog](CHANGELOG.md).
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install efficientai-sdk
|
|
11
|
+
# with OpenTelemetry tracing support
|
|
12
|
+
pip install "efficientai-sdk[otel]"
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Requires Python 3.9 or newer.
|
|
16
|
+
|
|
17
|
+
## Configure
|
|
18
|
+
|
|
19
|
+
EfficientAI is self-hosted, so point the SDK at your own deployment. If no URL is set, it uses `http://localhost:8000`.
|
|
20
|
+
|
|
21
|
+
Create an API key in your EfficientAI dashboard. Then either set environment variables:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
export EFFICIENTAI_BASE_URL="https://efficientai.your-company.com" # your deployment; default http://localhost:8000
|
|
25
|
+
export EFFICIENTAI_API_KEY="..."
|
|
26
|
+
export EFFICIENTAI_WORKSPACE_ID="..." # optional; the key's default workspace is used otherwise
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
or pass the same values to the client:
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
from efficientai_sdk import EfficientAI
|
|
33
|
+
|
|
34
|
+
client = EfficientAI(
|
|
35
|
+
base_url="https://efficientai.your-company.com",
|
|
36
|
+
api_key="...",
|
|
37
|
+
workspace_id="...",
|
|
38
|
+
)
|
|
39
|
+
client.verify() # True, or raises AuthenticationError
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
An async client with the same arguments is available:
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
from efficientai_sdk import AsyncEfficientAI
|
|
46
|
+
|
|
47
|
+
async with AsyncEfficientAI() as client:
|
|
48
|
+
await client.verify()
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Live call tracing
|
|
52
|
+
|
|
53
|
+
Install the `otel` extra, then wrap each call in a trace session. Spans created inside the session are tagged with the call's id and exported to EfficientAI. The session closes when the block ends.
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
from efficientai_sdk import AsyncEfficientAI
|
|
57
|
+
|
|
58
|
+
efficientai = AsyncEfficientAI()
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
async def handle_call(first_message=None):
|
|
62
|
+
async with efficientai.trace.session(transport="websocket", handshake=first_message) as call:
|
|
63
|
+
with call.turn():
|
|
64
|
+
with call.stt(model="nova-3", provider="deepgram") as stt:
|
|
65
|
+
stt.set_transcript(user_text)
|
|
66
|
+
with call.llm(model="gpt-4o", provider="openai") as llm:
|
|
67
|
+
llm.set_output(reply).set_ttfb(0.31)
|
|
68
|
+
with call.tts(model="sonic", provider="cartesia") as tts:
|
|
69
|
+
tts.set_text(reply)
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
- **Chat agents:** `session(channel="chat")` plus `call.user_message()`, `call.tool()`, `call.retrieval()` and `call.llm()`. Each turn shows the user message, the final reply, tool calls and response time.
|
|
73
|
+
- **Pipecat:** `PipelineTask(pipeline, **task_kwargs(call))` with `from efficientai_sdk.integrations.pipecat import task_kwargs`.
|
|
74
|
+
- **LiveKit Agents:** `async with livekit.trace_session(client, ctx) as call:` with `from efficientai_sdk.integrations import livekit`.
|
|
75
|
+
- **Existing OpenTelemetry setup:** call `efficientai.trace.instrument()` once at startup.
|
|
76
|
+
- **Test calls from EfficientAI:** pass the platform's `handshake=`, `headers=` or `sip_headers=` so traces link to the simulation result.
|
|
77
|
+
- **Concurrent calls:** each session is tracked per asyncio task, so one process can serve many calls.
|
|
78
|
+
- **Failures:** tracing never breaks a call. If EfficientAI is unreachable, the call runs untraced and a warning is logged. Pass `strict=True` to raise instead.
|
|
79
|
+
|
|
80
|
+
A runnable example is in [`examples/custom_agent_tracing.py`](examples/custom_agent_tracing.py). The full guide is on the "Python SDK" page of the EfficientAI docs.
|
|
81
|
+
|
|
82
|
+
## Run simulations
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
run = client.simulations.run(
|
|
86
|
+
agent_id="...", scenario_ids=["..."], persona_ids=["..."], n_attempts=2
|
|
87
|
+
)
|
|
88
|
+
run = run.wait(timeout=1800)
|
|
89
|
+
assert run.passed, run.summary
|
|
90
|
+
|
|
91
|
+
suite = client.suites.run("suite-id", runs_per_combination=3)
|
|
92
|
+
suite = suite.wait()
|
|
93
|
+
assert suite.passed, suite.pass_rate
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
From CI:
|
|
97
|
+
|
|
98
|
+
```bash
|
|
99
|
+
efficientai simulate --suite "$EFFICIENTAI_SUITE_ID" --fail-under 0.8
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
The command exits `0` at or above the threshold, `1` when the suite scores lower or a result errors, and `2` when the arguments or the API call fail. See `examples/ci/simulate.yml`.
|
|
103
|
+
|
|
104
|
+
## Errors and retries
|
|
105
|
+
|
|
106
|
+
Every error the SDK raises inherits from `EfficientAIError`.
|
|
107
|
+
|
|
108
|
+
| Exception | When |
|
|
109
|
+
|---|---|
|
|
110
|
+
| `ConfigurationError` | Missing API key or invalid settings |
|
|
111
|
+
| `APIConnectionError` / `APITimeoutError` | The server could not be reached |
|
|
112
|
+
| `AuthenticationError` | 401: the key is invalid or inactive |
|
|
113
|
+
| `PermissionDeniedError` | 403 |
|
|
114
|
+
| `NotFoundError` | 404 |
|
|
115
|
+
| `UnprocessableEntityError` | 422: the request failed validation |
|
|
116
|
+
| `RateLimitError` | 429; `retry_after` holds the server's hint |
|
|
117
|
+
| `InternalServerError` | 5xx |
|
|
118
|
+
|
|
119
|
+
The client retries failed requests up to `max_retries` times (default 2), with exponential backoff and jitter. It honours the server's `Retry-After` header.
|
|
120
|
+
- `GET`, `PUT` and `DELETE` retry on timeouts, 408, 429 and 5xx responses.
|
|
121
|
+
- `POST` and `PATCH` retry only when the server certainly didn't process the request: 429, 503, or a failed connection.
|
|
122
|
+
|
|
123
|
+
Set `max_retries=0` to turn retries off.
|
|
124
|
+
|
|
125
|
+
## Development
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
cd sdks/python
|
|
129
|
+
uv sync
|
|
130
|
+
uv run pytest
|
|
131
|
+
uv run ruff check . && uv run ruff format --check .
|
|
132
|
+
uv run mypy
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Releases are published to PyPI from tags named `sdk-py-v<version>`, which must match `src/efficientai_sdk/_version.py`.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Example only. Copy this into your repo as .github/workflows/simulate.yml.
|
|
2
|
+
# It is not run by the EfficientAI repository itself.
|
|
3
|
+
name: Simulate
|
|
4
|
+
|
|
5
|
+
on:
|
|
6
|
+
pull_request:
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
simulate:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
steps:
|
|
12
|
+
- uses: actions/checkout@v4
|
|
13
|
+
- uses: actions/setup-python@v5
|
|
14
|
+
with:
|
|
15
|
+
python-version: "3.12"
|
|
16
|
+
- name: Install EfficientAI SDK
|
|
17
|
+
run: pip install "efficientai-sdk @ git+https://github.com/EfficientAI-tech/efficientAI.git#subdirectory=sdks/python"
|
|
18
|
+
- name: Run suite
|
|
19
|
+
env:
|
|
20
|
+
EFFICIENTAI_BASE_URL: ${{ secrets.EFFICIENTAI_BASE_URL }}
|
|
21
|
+
EFFICIENTAI_API_KEY: ${{ secrets.EFFICIENTAI_API_KEY }}
|
|
22
|
+
EFFICIENTAI_WORKSPACE_ID: ${{ secrets.EFFICIENTAI_WORKSPACE_ID }}
|
|
23
|
+
EFFICIENTAI_SUITE_ID: ${{ secrets.EFFICIENTAI_SUITE_ID }}
|
|
24
|
+
run: efficientai simulate --suite "$EFFICIENTAI_SUITE_ID" --fail-under 0.8
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"""Trace a (simulated) custom voice agent into EfficientAI.
|
|
2
|
+
|
|
3
|
+
Runs three fake calls concurrently, each with a few turns, and prints their Call IDs.
|
|
4
|
+
Open Observability -> Calls in EfficientAI to see the transcripts and latencies.
|
|
5
|
+
|
|
6
|
+
pip install "efficientai-sdk[otel]"
|
|
7
|
+
export EFFICIENTAI_BASE_URL=http://localhost:8000
|
|
8
|
+
export EFFICIENTAI_API_KEY=...
|
|
9
|
+
export EFFICIENTAI_WORKSPACE_ID=...
|
|
10
|
+
python examples/custom_agent_tracing.py
|
|
11
|
+
|
|
12
|
+
Replace the fake_* functions with your real STT, LLM and TTS calls.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
import asyncio
|
|
18
|
+
import logging
|
|
19
|
+
import random
|
|
20
|
+
|
|
21
|
+
from efficientai_sdk import AsyncEfficientAI
|
|
22
|
+
|
|
23
|
+
SCRIPTS = [
|
|
24
|
+
[
|
|
25
|
+
("Hi, I'd like to book a table for tonight", "Sure! How many people?"),
|
|
26
|
+
("Four of us, around eight", "Booked: four people at 8pm. Anything else?"),
|
|
27
|
+
("No, that's all, thanks", "Enjoy your dinner!"),
|
|
28
|
+
],
|
|
29
|
+
[
|
|
30
|
+
(
|
|
31
|
+
"My internet keeps dropping every evening",
|
|
32
|
+
"Sorry to hear that. Have you restarted the router?",
|
|
33
|
+
),
|
|
34
|
+
("Yes, twice today", "Thanks. I'll schedule a technician visit for tomorrow."),
|
|
35
|
+
],
|
|
36
|
+
[
|
|
37
|
+
("What's my account balance?", "Your balance is $42.10."),
|
|
38
|
+
("When is the next payment due?", "Your next payment is due on the 15th."),
|
|
39
|
+
],
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
async def fake_stt(text: str) -> str:
|
|
44
|
+
await asyncio.sleep(random.uniform(0.15, 0.3))
|
|
45
|
+
return text
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
async def fake_llm(reply: str) -> tuple[str, float]:
|
|
49
|
+
first_token = random.uniform(0.2, 0.5)
|
|
50
|
+
await asyncio.sleep(first_token + 0.2)
|
|
51
|
+
return reply, first_token
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
async def fake_tts(text: str) -> float:
|
|
55
|
+
first_byte = random.uniform(0.08, 0.2)
|
|
56
|
+
await asyncio.sleep(first_byte + 0.3)
|
|
57
|
+
return first_byte
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
async def run_call(client: AsyncEfficientAI, script: list[tuple[str, str]]) -> str:
|
|
61
|
+
async with client.trace.session(transport="websocket") as call:
|
|
62
|
+
for user_says, agent_says in script:
|
|
63
|
+
with call.turn():
|
|
64
|
+
with call.stt(model="nova-3", provider="deepgram") as stt:
|
|
65
|
+
stt.set_transcript(await fake_stt(user_says))
|
|
66
|
+
with call.llm(model="gpt-4o", provider="openai") as llm:
|
|
67
|
+
reply, ttfb = await fake_llm(agent_says)
|
|
68
|
+
llm.set_output(reply).set_ttfb(ttfb)
|
|
69
|
+
with call.tts(model="sonic", provider="cartesia") as tts:
|
|
70
|
+
tts.set_text(reply).set_ttfb(await fake_tts(reply))
|
|
71
|
+
return call.call_short_id or "(tracing disabled, see warnings above)"
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
async def main() -> None:
|
|
75
|
+
logging.basicConfig(level=logging.INFO)
|
|
76
|
+
async with AsyncEfficientAI() as client:
|
|
77
|
+
await client.verify()
|
|
78
|
+
call_ids = await asyncio.gather(*(run_call(client, script) for script in SCRIPTS))
|
|
79
|
+
for call_id in call_ids:
|
|
80
|
+
print(f"Traced call {call_id}")
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
if __name__ == "__main__":
|
|
84
|
+
asyncio.run(main())
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Minimal HTTP customer chat agent for EFF-111 local simulation testing.
|
|
3
|
+
|
|
4
|
+
export EFFICIENTAI_BASE_URL=http://localhost:8000
|
|
5
|
+
export EFFICIENTAI_API_KEY=...
|
|
6
|
+
export EFFICIENTAI_WORKSPACE_ID=...
|
|
7
|
+
uv run python examples/customer_http_chat_agent.py
|
|
8
|
+
|
|
9
|
+
Point a chat agent (HTTP API) at http://localhost:9000 with path /chat, then run a suite.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import json
|
|
15
|
+
import os
|
|
16
|
+
from http.server import BaseHTTPRequestHandler, HTTPServer
|
|
17
|
+
|
|
18
|
+
PORT = int(os.environ.get("MOCK_HTTP_CHAT_PORT", "9000"))
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class Handler(BaseHTTPRequestHandler):
|
|
22
|
+
def do_POST(self) -> None:
|
|
23
|
+
length = int(self.headers.get("Content-Length", "0"))
|
|
24
|
+
raw = self.rfile.read(length) if length else b"{}"
|
|
25
|
+
body = json.loads(raw.decode("utf-8"))
|
|
26
|
+
call_short = self.headers.get("X-EfficientAI-Call-Short-Id", "")
|
|
27
|
+
run_id = self.headers.get("X-EfficientAI-Run-Id", "")
|
|
28
|
+
print(f"POST {self.path} call_short_id={call_short!r} run_id={run_id!r}")
|
|
29
|
+
|
|
30
|
+
messages = body.get("messages") or []
|
|
31
|
+
last_user = ""
|
|
32
|
+
for msg in reversed(messages):
|
|
33
|
+
if msg.get("role") == "user":
|
|
34
|
+
last_user = (msg.get("content") or "").strip()
|
|
35
|
+
break
|
|
36
|
+
reply = f"Mock reply to: {last_user[:120]}" if last_user else "Hello from mock HTTP agent"
|
|
37
|
+
|
|
38
|
+
headers = {k: v for k, v in self.headers.items()}
|
|
39
|
+
try:
|
|
40
|
+
from efficientai_sdk import EfficientAI
|
|
41
|
+
|
|
42
|
+
with (
|
|
43
|
+
EfficientAI() as client,
|
|
44
|
+
client.trace.from_request(headers, channel="chat") as call,
|
|
45
|
+
call.turn(),
|
|
46
|
+
):
|
|
47
|
+
call.user_message(last_user or "(empty)")
|
|
48
|
+
with call.llm(model="mock", provider="local") as llm:
|
|
49
|
+
llm.set_output(reply).set_ttfb(0.05)
|
|
50
|
+
except Exception as exc:
|
|
51
|
+
print(f"tracing skipped: {exc}")
|
|
52
|
+
|
|
53
|
+
payload = json.dumps({"reply": reply}).encode("utf-8")
|
|
54
|
+
self.send_response(200)
|
|
55
|
+
self.send_header("Content-Type", "application/json")
|
|
56
|
+
self.send_header("Content-Length", str(len(payload)))
|
|
57
|
+
self.end_headers()
|
|
58
|
+
self.wfile.write(payload)
|
|
59
|
+
|
|
60
|
+
def log_message(self, format: str, *args: object) -> None:
|
|
61
|
+
return
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def main() -> None:
|
|
65
|
+
server = HTTPServer(("localhost", PORT), Handler)
|
|
66
|
+
print(f"Customer HTTP chat mock listening on http://localhost:{PORT}/chat")
|
|
67
|
+
server.serve_forever()
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
if __name__ == "__main__":
|
|
71
|
+
main()
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.24"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "efficientai-sdk"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "Python SDK for EfficientAI: live traces, simulations and evaluations for voice and chat agents."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
license-files = ["LICENSE"]
|
|
12
|
+
requires-python = ">=3.9"
|
|
13
|
+
authors = [{ name = "EfficientAI" }]
|
|
14
|
+
keywords = ["efficientai", "voice agents", "observability", "opentelemetry", "evaluation", "simulation"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 3 - Alpha",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
21
|
+
"Programming Language :: Python :: 3.9",
|
|
22
|
+
"Programming Language :: Python :: 3.10",
|
|
23
|
+
"Programming Language :: Python :: 3.11",
|
|
24
|
+
"Programming Language :: Python :: 3.12",
|
|
25
|
+
"Programming Language :: Python :: 3.13",
|
|
26
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
27
|
+
"Typing :: Typed",
|
|
28
|
+
]
|
|
29
|
+
dependencies = [
|
|
30
|
+
"httpx>=0.25,<1",
|
|
31
|
+
"pydantic>=2.5,<3",
|
|
32
|
+
"python-dotenv>=1.0.0",
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
[project.optional-dependencies]
|
|
36
|
+
# Live tracing over OTLP. Framework-specific extras (pipecat, livekit) are
|
|
37
|
+
# added with the tracing API in EFF-109.
|
|
38
|
+
otel = [
|
|
39
|
+
"opentelemetry-sdk>=1.27.0",
|
|
40
|
+
"opentelemetry-exporter-otlp-proto-http>=1.27.0",
|
|
41
|
+
]
|
|
42
|
+
|
|
43
|
+
[project.urls]
|
|
44
|
+
Homepage = "https://efficientai.cloud"
|
|
45
|
+
Documentation = "https://docs.efficientai.cloud"
|
|
46
|
+
Source = "https://github.com/EfficientAI-tech/efficientAI/tree/main/sdks/python"
|
|
47
|
+
Changelog = "https://github.com/EfficientAI-tech/efficientAI/blob/main/sdks/python/CHANGELOG.md"
|
|
48
|
+
|
|
49
|
+
[dependency-groups]
|
|
50
|
+
dev = [
|
|
51
|
+
"opentelemetry-sdk>=1.27.0",
|
|
52
|
+
"opentelemetry-exporter-otlp-proto-http>=1.27.0",
|
|
53
|
+
"pytest>=8",
|
|
54
|
+
"pytest-asyncio>=0.23",
|
|
55
|
+
"respx>=0.21",
|
|
56
|
+
"ruff>=0.6",
|
|
57
|
+
"mypy>=1.10",
|
|
58
|
+
]
|
|
59
|
+
|
|
60
|
+
[project.scripts]
|
|
61
|
+
efficientai = "efficientai_sdk.cli:entry"
|
|
62
|
+
|
|
63
|
+
[tool.hatch.version]
|
|
64
|
+
path = "src/efficientai_sdk/_version.py"
|
|
65
|
+
|
|
66
|
+
[tool.hatch.build.targets.wheel]
|
|
67
|
+
packages = ["src/efficientai_sdk"]
|
|
68
|
+
|
|
69
|
+
[tool.hatch.build.targets.sdist]
|
|
70
|
+
include = ["src/efficientai_sdk", "tests", "examples", "README.md", "CHANGELOG.md", "LICENSE"]
|
|
71
|
+
|
|
72
|
+
[tool.pytest.ini_options]
|
|
73
|
+
testpaths = ["tests"]
|
|
74
|
+
asyncio_mode = "auto"
|
|
75
|
+
asyncio_default_fixture_loop_scope = "function"
|
|
76
|
+
addopts = "-ra"
|
|
77
|
+
|
|
78
|
+
[tool.ruff]
|
|
79
|
+
line-length = 100
|
|
80
|
+
target-version = "py39"
|
|
81
|
+
src = ["src", "tests"]
|
|
82
|
+
|
|
83
|
+
[tool.ruff.lint]
|
|
84
|
+
select = ["E", "F", "W", "I", "B", "UP", "SIM", "RUF"]
|
|
85
|
+
# Keep Optional[...]: pydantic evaluates annotations at runtime and `X | None` breaks on 3.9.
|
|
86
|
+
ignore = ["UP007", "UP045"]
|
|
87
|
+
|
|
88
|
+
[tool.mypy]
|
|
89
|
+
python_version = "3.10" # oldest target current mypy supports
|
|
90
|
+
strict = true
|
|
91
|
+
packages = ["efficientai_sdk"]
|
|
92
|
+
mypy_path = "src"
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"""EfficientAI Python SDK."""
|
|
2
|
+
|
|
3
|
+
from efficientai_sdk._client import AsyncEfficientAI, EfficientAI
|
|
4
|
+
from efficientai_sdk._config import DEFAULT_BASE_URL
|
|
5
|
+
from efficientai_sdk._exceptions import (
|
|
6
|
+
APIConnectionError,
|
|
7
|
+
APIStatusError,
|
|
8
|
+
APITimeoutError,
|
|
9
|
+
AuthenticationError,
|
|
10
|
+
BadRequestError,
|
|
11
|
+
ConfigurationError,
|
|
12
|
+
ConflictError,
|
|
13
|
+
EfficientAIError,
|
|
14
|
+
InternalServerError,
|
|
15
|
+
NotFoundError,
|
|
16
|
+
PermissionDeniedError,
|
|
17
|
+
RateLimitError,
|
|
18
|
+
UnprocessableEntityError,
|
|
19
|
+
)
|
|
20
|
+
from efficientai_sdk._version import __version__
|
|
21
|
+
|
|
22
|
+
__all__ = [
|
|
23
|
+
"DEFAULT_BASE_URL",
|
|
24
|
+
"APIConnectionError",
|
|
25
|
+
"APIStatusError",
|
|
26
|
+
"APITimeoutError",
|
|
27
|
+
"AsyncEfficientAI",
|
|
28
|
+
"AuthenticationError",
|
|
29
|
+
"BadRequestError",
|
|
30
|
+
"ConfigurationError",
|
|
31
|
+
"ConflictError",
|
|
32
|
+
"EfficientAI",
|
|
33
|
+
"EfficientAIError",
|
|
34
|
+
"InternalServerError",
|
|
35
|
+
"NotFoundError",
|
|
36
|
+
"PermissionDeniedError",
|
|
37
|
+
"RateLimitError",
|
|
38
|
+
"UnprocessableEntityError",
|
|
39
|
+
"__version__",
|
|
40
|
+
]
|