omni-experiments 1.0.0__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.
@@ -0,0 +1,59 @@
1
+ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ # SPDX-License-Identifier: Apache-2.0
3
+ """Omni Experiments SDK: run an agent over a dataset, score it, report to Omni Experiments."""
4
+
5
+ from __future__ import annotations
6
+
7
+ from omni_experiments._version import __version__
8
+ from omni_experiments.agentcore import (
9
+ AgentCoreDatasetSource,
10
+ AgentCoreEngine,
11
+ AgentCoreRuntimeInvoker,
12
+ )
13
+ from omni_experiments.api import ExperimentsApi
14
+ from omni_experiments.client import (
15
+ BotoExperimentsApi,
16
+ ClientSettings,
17
+ create_client,
18
+ )
19
+ from omni_experiments.errors import ApiError, ExperimentsError, UsageError
20
+ from omni_experiments.hosted import (
21
+ HostedCreate,
22
+ StartedRun,
23
+ WaitEndedError,
24
+ start_run,
25
+ wait_for_run,
26
+ )
27
+ from omni_experiments.providers import AgentInvoker, CaseEngine, DatasetSource, Evaluator
28
+ from omni_experiments.runner import InProcessEngine, run_experiment
29
+ from omni_experiments.types import AgentOutput, Case, EvaluatorResult, RunResult, TurnInput
30
+
31
+ __all__ = [
32
+ "AgentCoreDatasetSource",
33
+ "AgentCoreEngine",
34
+ "AgentCoreRuntimeInvoker",
35
+ "AgentInvoker",
36
+ "AgentOutput",
37
+ "ApiError",
38
+ "BotoExperimentsApi",
39
+ "Case",
40
+ "CaseEngine",
41
+ "ClientSettings",
42
+ "DatasetSource",
43
+ "Evaluator",
44
+ "EvaluatorResult",
45
+ "ExperimentsApi",
46
+ "ExperimentsError",
47
+ "HostedCreate",
48
+ "InProcessEngine",
49
+ "RunResult",
50
+ "StartedRun",
51
+ "TurnInput",
52
+ "UsageError",
53
+ "WaitEndedError",
54
+ "__version__",
55
+ "create_client",
56
+ "run_experiment",
57
+ "start_run",
58
+ "wait_for_run",
59
+ ]
@@ -0,0 +1,7 @@
1
+ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ # SPDX-License-Identifier: Apache-2.0
3
+ """``python -m omni_experiments``: the ``experiments`` command, without installing the package."""
4
+
5
+ from omni_experiments.cli import main
6
+
7
+ raise SystemExit(main())
@@ -0,0 +1,120 @@
1
+ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ # SPDX-License-Identifier: Apache-2.0
3
+ """Keep text that comes from outside from acting on a terminal or on a CI log.
4
+
5
+ The CLI prints text it did not write: the service's error messages, a run's ``statusDetail`` and
6
+ verdict reason, experiment names other people chose, and anything a library puts in an exception.
7
+ Printed raw, such text can do two things.
8
+
9
+ * **Act on a terminal.** An escape sequence can clear a line, move the cursor up, recolour text or
10
+ retitle the window, so a ``FAILED`` verdict can be made to read ``PASSED``. Carriage return and
11
+ backspace overwrite what is already on the line. Bidirectional controls reorder it.
12
+ * **Start a line of its own in a CI log.** GitHub Actions, Azure Pipelines and TeamCity act on a
13
+ line that begins ``::``, ``##vso[`` or ``##teamcity[``, so a newline inside a value lets the
14
+ rest of it be read as a command.
15
+
16
+ Two defences, one for each. :func:`escape` makes the text safe where the CLI shows a value, and
17
+ :func:`protect` wraps ``stdout`` and ``stderr`` so that nothing, whatever its source, can reach
18
+ them as a control character. Neither touches ``--output json``: ``json.dumps`` already escapes
19
+ every control and non-ASCII character, and the data there should be what the service sent.
20
+
21
+ A control character is shown as ``\\x1b`` (or ``\\u202e``), so what it was stays visible. Text that
22
+ already contains a literal ``\\x1b`` reads the same, which is the price of not hiding anything.
23
+ """
24
+
25
+ from __future__ import annotations
26
+
27
+ import contextlib
28
+ import dataclasses
29
+ import sys
30
+ import unicodedata
31
+ from typing import TYPE_CHECKING, Any, TextIO
32
+
33
+ if TYPE_CHECKING:
34
+ from collections.abc import Iterator
35
+
36
+ # Format characters that reorder or hide text (bidirectional marks, embeddings, overrides and
37
+ # isolates) and the Unicode line and paragraph separators, which some viewers treat as a newline.
38
+ # Other format characters, such as the zero-width joiner inside an emoji sequence, are left alone.
39
+ _UNSAFE_FORMAT = frozenset(
40
+ [0x061C, 0x200E, 0x200F, 0x2028, 0x2029, *range(0x202A, 0x202F), *range(0x2066, 0x206A)]
41
+ )
42
+ _KEPT_ON_A_STREAM = "\n\t"
43
+
44
+
45
+ def _is_unsafe(char: str, *, keep: str) -> bool:
46
+ if char in keep:
47
+ return False
48
+ return unicodedata.category(char) == "Cc" or ord(char) in _UNSAFE_FORMAT
49
+
50
+
51
+ _LATIN1_MAX = 0xFF
52
+
53
+
54
+ def _shown(char: str) -> str:
55
+ code = ord(char)
56
+ return f"\\x{code:02x}" if code <= _LATIN1_MAX else f"\\u{code:04x}"
57
+
58
+
59
+ def escape(text: str, *, keep: str = "") -> str:
60
+ """``text`` with every control character written out (``\\x1b``, ``\\n``, ``\\u202e``).
61
+
62
+ Newline and tab are written out too, so the result is one line; pass ``keep="\\n"`` to leave
63
+ a newline alone. Characters that need no escaping are returned unchanged.
64
+ """
65
+ if all(not _is_unsafe(c, keep=keep) for c in text):
66
+ return text
67
+ return "".join(_shown(c) if _is_unsafe(c, keep=keep) else c for c in text)
68
+
69
+
70
+ def safe(value: Any) -> Any:
71
+ """``value`` with the text in it made safe to show: strings, and those held in lists, tuples,
72
+ dicts (values only) and dataclasses. Anything else is returned as it is.
73
+ """
74
+ if isinstance(value, str):
75
+ return escape(value)
76
+ if isinstance(value, list):
77
+ return [safe(v) for v in value]
78
+ if isinstance(value, tuple):
79
+ return tuple(safe(v) for v in value)
80
+ if isinstance(value, dict):
81
+ return {k: safe(v) for k, v in value.items()}
82
+ if dataclasses.is_dataclass(value) and not isinstance(value, type):
83
+ return dataclasses.replace(
84
+ value, **{f.name: safe(getattr(value, f.name)) for f in dataclasses.fields(value)}
85
+ )
86
+ return value
87
+
88
+
89
+ class _SafeStream:
90
+ """A text stream that writes out every control character except newline and tab.
91
+
92
+ It works one character at a time, so an escape sequence split across two writes is still
93
+ caught, and it passes everything else (``flush``, ``isatty``, ``fileno``, ``buffer``)
94
+ through, so the terminal checks the rest of the program makes see the real stream.
95
+ """
96
+
97
+ def __init__(self, stream: TextIO) -> None:
98
+ self._stream = stream
99
+
100
+ def write(self, text: str) -> int:
101
+ self._stream.write(escape(text, keep=_KEPT_ON_A_STREAM))
102
+ return len(text)
103
+
104
+ def writelines(self, lines: Any) -> None:
105
+ for line in lines:
106
+ self.write(line)
107
+
108
+ def __getattr__(self, name: str) -> Any:
109
+ return getattr(self._stream, name)
110
+
111
+
112
+ @contextlib.contextmanager
113
+ def protect() -> Iterator[None]:
114
+ """Wrap ``sys.stdout`` and ``sys.stderr`` for the duration, then put them back."""
115
+ out, err = sys.stdout, sys.stderr
116
+ sys.stdout, sys.stderr = _SafeStream(out), _SafeStream(err) # type: ignore[assignment]
117
+ try:
118
+ yield
119
+ finally:
120
+ sys.stdout, sys.stderr = out, err
@@ -0,0 +1,3 @@
1
+ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ # SPDX-License-Identifier: Apache-2.0
3
+ __version__ = "1.0.0"