Metadata-Version: 2.4
Name: agnost-evals
Version: 0.1.0
Summary: Runs your AI agent on an Agnost experiment's dataset and reports each answer back for scoring.
Author-email: Agnost AI <founders@agnost.ai>
License-Expression: MIT
Project-URL: Homepage, https://agnost.ai
Keywords: agnost,evals,llm-evaluation,ai-agents,experiments
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Operating System :: OS Independent
Classifier: Typing :: Typed
Requires-Python: >=3.10
Description-Content-Type: text/markdown
Provides-Extra: dev
Requires-Dist: pytest>=8.0; extra == "dev"
Requires-Dist: jsonschema>=4.0; extra == "dev"
Requires-Dist: pyyaml>=6.0; extra == "dev"
Requires-Dist: tomli>=1.1.0; python_version < "3.11" and extra == "dev"

# agnost-evals

Run the rows of an Agnost AI experiment's dataset through your own agent, on your
machine or in CI. Agnost sends the rows, your agent answers each one, and Agnost
scores the answers with the experiment's evaluators. Your agent runs only on your
infrastructure and sees each row's input and metadata, never its expected output.

```bash
pip install agnost-evals
```

Requires Python 3.10 or newer. It has no dependencies.

## Write an adapter

The adapter is the one function you write. It receives a row's `input`, sets up
your agent from it, runs the agent, and returns the agent's final text. It can be
a plain or an `async` function.

```python
# experiment.py
from agnost_evals import run_experiment
from booking import booking_agent


async def adapter(input, context):
    reply = await booking_agent.run(input)
    return reply.text


run_experiment(adapter)
```

When a row times out or the run is cancelled, an `async` adapter is cancelled
with `asyncio.CancelledError`. A plain function runs on its own thread and is not
stopped; check `context.cancelled`, a `threading.Event`, to stop it early.

`context` also carries the row's `row_id` and its `metadata`, the row's metadata
dict from the dataset (`{}` when it has none). Use it for per-row setup that isn't
part of the user's message, such as a customer tier or locale:

```python
async def adapter(input, context):
    agent = booking_agent.with_tier(context.metadata.get("tier"))
    reply = await agent.run(input)
    return reply.text
```

All `async` adapters run on one event loop for the whole run, so clients your
agent creates once, such as `AsyncOpenAI`, keep working from row to row. Call
`run_experiment` from synchronous code, such as the top level of your script,
not from inside `asyncio.run`.

## Run an experiment

```bash
export AGNOST_API_KEY=agnost_...   # your project's API key
python experiment.py --experiment-id 7f3e2a10-5c4b-4d8e-9a61-2b0c8d4e6f19
```

```text
agnost-evals: started run 0b7c5d1e-2f3a-4b6c-8d9e-1a2b3c4d5e6f of experiment 7f3e2a10-5c4b-4d8e-9a61-2b0c8d4e6f19: 2 rows

  ✓ [1/2] 3c2b1a09-8f7e-4d6c-b5a4-0e9d8c7b6a51  2.4s  Booked BK123: 3 nights in Mumbai for ₹7,500.
  ✗ [2/2] 4d3c2b1a-9e8f-4a7b-8c6d-1f0e9d8c7b62  120.0s  Timed out after 120s

2 rows: 1 answered, 1 failed.
Failed rows:
  4d3c2b1a-9e8f-4a7b-8c6d-1f0e9d8c7b62: Timed out after 120s
Reported 2 of 2 responses to Agnost (run 0b7c5d1e-2f3a-4b6c-8d9e-1a2b3c4d5e6f).
Agnost scores them with the experiment's evaluators.
```

Each command starts a new run. Each row's response is sent to Agnost as soon as
the row finishes, so if the run stops partway, the rows already reported stay
saved in that run. Scores appear in Agnost; the SDK doesn't wait for them.

If Agnost can't be reached, times out, or answers with a 5xx, 409, or 429, the
request is retried, up to 4 attempts. A 409 that persists means the row already
has a different response; start a new run instead. Any other error stops the run
with Agnost's reason, such as "Agnost at https://api.agnost.ai rejected the API
key (401: Invalid API key); check AGNOST_API_KEY and AGNOST_ENDPOINT". A rejected
API key or a missing experiment names the endpoint, because a key or experiment
from another Agnost environment fails the same way. The API key is never printed. If starting a run times out and is
retried, Agnost can keep an extra run with no responses.

## Try an adapter on rows from a file

A rows file runs your adapter without Agnost, so you can build and check it
first. Nothing is sent anywhere.

```json
[
  { "id": "mumbai-budget", "input": { "destination": "Mumbai", "budget": 8000 } },
  { "id": "bk91-upgrade", "input": "Upgrade booking BK91 to business class." }
]
```

```bash
python experiment.py --rows-file rows.json
```

```text
agnost-evals: running 2 rows from rows.json

  ✓ [1/2] mumbai-budget  2.4s  Booked BK123: 3 nights in Mumbai for ₹7,500.
  ✗ [2/2] bk91-upgrade  120.0s  Timed out after 120s

2 rows: 1 answered, 1 failed.
Failed rows:
  bk91-upgrade: Timed out after 120s
Local run: nothing was sent to Agnost.
```

Each row needs a unique `id` and an `input`, which can be any JSON value, and may
have a `metadata` object. Rows have the same shape as Agnost's dataset rows, so a
saved start-run response, an object with a `rows` list, works as a rows file too;
other fields are ignored.

Rows run one at a time, in file order.

## What is recorded for each row

| What the adapter does | Answer | Error |
| --- | --- | --- |
| Returns a string | The string, even if empty | none |
| Returns any other value | The value as JSON | none |
| Returns `None` | none | The adapter returned no answer |
| Raises | none | The exception's type and message, such as `KeyError: 'customer_id'` |
| Returns more than 100,000 characters | none | The answer's length |
| Runs past `timeout` | none | `Timed out after 120s` |
| Is running when you press Ctrl+C | none | `Cancelled` |

Pydantic models and dataclasses are sent as JSON too. Rows not reached after
Ctrl+C are recorded as `Not run: cancelled`, and in an experiment run they are
still reported, so every row in the run gets a response. A second Ctrl+C exits
immediately with code 130, without reporting the rest. Errors longer than 4,000
characters are truncated.

An `async` adapter that makes a blocking call, such as a request with a
synchronous HTTP client, holds up the event loop, so the timeout can't stop it.
When its answer arrives late it is recorded as `Timed out`, and a second Ctrl+C
still exits. Make blocking calls from a plain function, which runs on its own
thread.

## Exit codes

`run_experiment` exits the process when the run ends.

| Code | Meaning |
| --- | --- |
| `0` | Every row answered |
| `1` | At least one row failed |
| `2` | The run couldn't start, a response couldn't be reported to Agnost, or the run was cancelled |

Exit code `0` means every row produced an answer, not that the answers are good.
Pass `exit=False` to get a `RunResult` with the exit code, each row's result, and
the `run_id` of an experiment run instead.

## Options

| Option | Default | |
| --- | --- | --- |
| `adapter` | required | Your function, called once per row as `adapter(input, context)` |
| `experiment_id` | `--experiment-id` argument | The Agnost experiment to run |
| `api_key` | `AGNOST_API_KEY` | Your project's API key |
| `endpoint` | `AGNOST_ENDPOINT`, then `https://api.agnost.ai` | The Agnost API |
| `rows_file` | `--rows-file` argument | JSON file of rows to run locally instead |
| `timeout` | `120` | Seconds one row may run |
| `cancel` | none | A `threading.Event` that cancels the run when set, like Ctrl+C |
| `argv` | `sys.argv[1:]` | Arguments to read `--experiment-id` and `--rows-file` from; flags it doesn't know are ignored |
| `exit` | `True` | Exit the process with the exit code; `False` returns the result |

Pass either an experiment ID or a rows file, not both.

## Example

[`examples/`](./examples) has a hotel booking agent, its adapter, and a rows
file. The agent follows fixed rules instead of calling a model, so it runs
without an API key. The tests run it on every change, and every release runs it
against the built wheel:

```bash
pip install agnost-evals
python examples/experiment.py --rows-file examples/rows.json
```
