idia 0.3.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.
idia/__init__.py ADDED
@@ -0,0 +1,171 @@
1
+ """Ask judgement models many closed questions about one input, packed into calls within each
2
+ provider's limits, and get their probabilities back, with whether they are claimed calibrated.
3
+
4
+ The public API is what ``__all__`` lists. Names and modules that start with an underscore are
5
+ internal.
6
+ """
7
+
8
+ from idia._provider import leaf, provider, provider_names
9
+ from idia.answers import validate, validate_answers
10
+ from idia.core import (
11
+ CANDIDATE_ELICITATIONS,
12
+ CANDIDATE_KINDS,
13
+ DETAILS,
14
+ REASONS,
15
+ UNBOUNDED,
16
+ WARNING_CHOSEN_BELOW_MAX,
17
+ WARNING_EXTRA_ANSWER_KEY,
18
+ WARNING_LABEL_ABSENT_FROM_LOGPROBS,
19
+ WARNING_NO_SNAPSHOT,
20
+ WARNING_OVERFLOW_POLICY,
21
+ WARNING_PROVIDER_TRUNCATED,
22
+ WARNING_UNCHECKED_LIMIT,
23
+ WARNING_UNKNOWN_OPTION,
24
+ Answer,
25
+ Box,
26
+ BoxAnswer,
27
+ Capabilities,
28
+ Deadline,
29
+ DeadlineExceeded,
30
+ DoesNotFit,
31
+ Elicitation,
32
+ ElicitationNotInRelease,
33
+ ExtractionAnswer,
34
+ FailedAttempt,
35
+ Fault,
36
+ IdiaError,
37
+ ImageCarrier,
38
+ ImagePart,
39
+ JsonPart,
40
+ JsonValue,
41
+ Kind,
42
+ KindNotInRelease,
43
+ Label,
44
+ LabelAnswer,
45
+ Leaf,
46
+ Limit,
47
+ MappedAnswer,
48
+ Modality,
49
+ Notice,
50
+ NoulAnswer,
51
+ Outcome,
52
+ Part,
53
+ ProvenanceDetail,
54
+ Provider,
55
+ ProviderError,
56
+ Question,
57
+ Refusal,
58
+ Request,
59
+ RequestFailed,
60
+ Response,
61
+ Rounding,
62
+ Rule,
63
+ RuleFailure,
64
+ Span,
65
+ State,
66
+ TextPart,
67
+ TokenEstimate,
68
+ TokenEstimator,
69
+ Unbounded,
70
+ UnreportedReason,
71
+ Usage,
72
+ Value,
73
+ ValueType,
74
+ WireRequest,
75
+ WireResponse,
76
+ check_elicitation,
77
+ noul_complement,
78
+ unknown_options,
79
+ )
80
+ from idia.core import VERSION as __version__ # noqa: F401
81
+ from idia.dispatch import Dispatcher, Fallback, Router
82
+ from idia.fit import Fail, FitPlan, OverflowPolicy
83
+ from idia.limits import ConcurrencyLimit, RateLimiter
84
+ from idia.providers import ProviderNotFound
85
+ from idia.retry import RetryPolicy
86
+
87
+ __all__ = [
88
+ "CANDIDATE_ELICITATIONS",
89
+ "CANDIDATE_KINDS",
90
+ "DETAILS",
91
+ "REASONS",
92
+ "UNBOUNDED",
93
+ "WARNING_CHOSEN_BELOW_MAX",
94
+ "WARNING_EXTRA_ANSWER_KEY",
95
+ "WARNING_LABEL_ABSENT_FROM_LOGPROBS",
96
+ "WARNING_NO_SNAPSHOT",
97
+ "WARNING_OVERFLOW_POLICY",
98
+ "WARNING_PROVIDER_TRUNCATED",
99
+ "WARNING_UNCHECKED_LIMIT",
100
+ "WARNING_UNKNOWN_OPTION",
101
+ "Answer",
102
+ "Box",
103
+ "BoxAnswer",
104
+ "Capabilities",
105
+ "ConcurrencyLimit",
106
+ "Deadline",
107
+ "DeadlineExceeded",
108
+ "Dispatcher",
109
+ "DoesNotFit",
110
+ "Elicitation",
111
+ "ElicitationNotInRelease",
112
+ "ExtractionAnswer",
113
+ "Fail",
114
+ "FailedAttempt",
115
+ "Fallback",
116
+ "Fault",
117
+ "FitPlan",
118
+ "IdiaError",
119
+ "ImageCarrier",
120
+ "ImagePart",
121
+ "JsonPart",
122
+ "JsonValue",
123
+ "Kind",
124
+ "KindNotInRelease",
125
+ "Label",
126
+ "LabelAnswer",
127
+ "Leaf",
128
+ "Limit",
129
+ "MappedAnswer",
130
+ "Modality",
131
+ "Notice",
132
+ "NoulAnswer",
133
+ "Outcome",
134
+ "OverflowPolicy",
135
+ "Part",
136
+ "ProvenanceDetail",
137
+ "Provider",
138
+ "ProviderError",
139
+ "ProviderNotFound",
140
+ "Question",
141
+ "RateLimiter",
142
+ "Refusal",
143
+ "Request",
144
+ "RequestFailed",
145
+ "Response",
146
+ "RetryPolicy",
147
+ "Rounding",
148
+ "Router",
149
+ "Rule",
150
+ "RuleFailure",
151
+ "Span",
152
+ "State",
153
+ "TextPart",
154
+ "TokenEstimate",
155
+ "TokenEstimator",
156
+ "Unbounded",
157
+ "UnreportedReason",
158
+ "Usage",
159
+ "Value",
160
+ "ValueType",
161
+ "WireRequest",
162
+ "WireResponse",
163
+ "check_elicitation",
164
+ "leaf",
165
+ "noul_complement",
166
+ "provider",
167
+ "provider_names",
168
+ "unknown_options",
169
+ "validate",
170
+ "validate_answers",
171
+ ]
idia/_provider.py ADDED
@@ -0,0 +1,281 @@
1
+ """``idia.provider``: a registered leaf, built by name and wrapped in a dispatcher;
2
+ ``idia.leaf``: the same leaf, unwrapped; ``idia.provider_names``: the registered names.
3
+
4
+ Each registered name has its own overload, typed with its leaf's constructor keywords, so a
5
+ type checker refuses an option the leaf does not take. The overloads exist for the checker
6
+ alone, under ``TYPE_CHECKING``, and name no SDK type: a ``client`` is typed ``object``, so a
7
+ program without an extra type-checks as one with it.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ from typing import TYPE_CHECKING, Literal, overload
13
+
14
+ from idia.core import Leaf
15
+ from idia.dispatch import Dispatcher
16
+ from idia.limits import ConcurrencyLimit, RateLimiter
17
+ from idia.providers import ( # pyright: ignore[reportPrivateUsage]
18
+ _load_leaf, # pyright: ignore[reportPrivateUsage]
19
+ _registered, # pyright: ignore[reportPrivateUsage]
20
+ )
21
+ from idia.retry import RetryPolicy
22
+
23
+ if TYPE_CHECKING:
24
+ from typing_extensions import deprecated
25
+
26
+ from idia.core import Capabilities
27
+ from idia.providers._gliner import GlinerRuntime
28
+ from idia.providers.cloudflare import ClefModel
29
+ from idia.providers.system_one import SystemOneModel
30
+
31
+ _DEFAULT_RETRY = RetryPolicy()
32
+
33
+ if TYPE_CHECKING:
34
+
35
+ @overload
36
+ def provider(
37
+ name: Literal["system-one"],
38
+ *,
39
+ model: SystemOneModel = ...,
40
+ api_key: str | None = ...,
41
+ base_url: str = ...,
42
+ client: object | None = ...,
43
+ long_context: bool = ...,
44
+ images: bool = ...,
45
+ capabilities: Capabilities | None = ...,
46
+ retry: RetryPolicy = ...,
47
+ concurrency: ConcurrencyLimit | None = ...,
48
+ rate: RateLimiter | None = ...,
49
+ ) -> Dispatcher: ...
50
+
51
+ @overload
52
+ def provider(
53
+ name: Literal["openai-decisions"],
54
+ *,
55
+ model: str = ...,
56
+ api_key: str | None = ...,
57
+ client: object | None = ...,
58
+ max_questions: int = ...,
59
+ capabilities: Capabilities | None = ...,
60
+ retry: RetryPolicy = ...,
61
+ concurrency: ConcurrencyLimit | None = ...,
62
+ rate: RateLimiter | None = ...,
63
+ ) -> Dispatcher: ...
64
+
65
+ @overload
66
+ def provider(
67
+ name: Literal["openai-chat"],
68
+ *,
69
+ model: str,
70
+ api_key: str | None = ...,
71
+ client: object | None = ...,
72
+ elicitation: str = ...,
73
+ max_off_label_mass: float | None = ...,
74
+ capabilities: Capabilities | None = ...,
75
+ retry: RetryPolicy = ...,
76
+ concurrency: ConcurrencyLimit | None = ...,
77
+ rate: RateLimiter | None = ...,
78
+ ) -> Dispatcher: ...
79
+
80
+ @overload
81
+ def provider(
82
+ name: Literal["cloudflare-clef"],
83
+ *,
84
+ model: ClefModel = ...,
85
+ account_id: str | None = ...,
86
+ api_token: str | None = ...,
87
+ client: object | None = ...,
88
+ capabilities: Capabilities | None = ...,
89
+ retry: RetryPolicy = ...,
90
+ concurrency: ConcurrencyLimit | None = ...,
91
+ rate: RateLimiter | None = ...,
92
+ ) -> Dispatcher: ...
93
+
94
+ @overload
95
+ def provider(
96
+ name: Literal["cloudflare-clairic"],
97
+ *,
98
+ model: Literal["clairic", "@cf/cloudflare/clairic"] = ...,
99
+ account_id: str | None = ...,
100
+ api_token: str | None = ...,
101
+ client: object | None = ...,
102
+ capabilities: Capabilities | None = ...,
103
+ retry: RetryPolicy = ...,
104
+ concurrency: ConcurrencyLimit | None = ...,
105
+ rate: RateLimiter | None = ...,
106
+ ) -> Dispatcher: ...
107
+
108
+ @overload
109
+ def provider(
110
+ name: Literal["gliner"],
111
+ *,
112
+ model: Literal["fastino/gliner2.5-small-v1"] = ...,
113
+ revision: str | None = ...,
114
+ threshold: float = ...,
115
+ runtime: GlinerRuntime | None = ...,
116
+ capabilities: Capabilities | None = ...,
117
+ retry: RetryPolicy = ...,
118
+ concurrency: ConcurrencyLimit | None = ...,
119
+ rate: RateLimiter | None = ...,
120
+ ) -> Dispatcher: ...
121
+
122
+ @overload
123
+ @deprecated(
124
+ "a registered provider takes its leaf's keywords, each of its type and every required "
125
+ "one given, and retry, concurrency and rate; an unknown or missing keyword raises TypeError"
126
+ )
127
+ def provider(
128
+ name: Literal[
129
+ "system-one",
130
+ "openai-decisions",
131
+ "openai-chat",
132
+ "cloudflare-clef",
133
+ "cloudflare-clairic",
134
+ "gliner",
135
+ ],
136
+ *,
137
+ retry: RetryPolicy = ...,
138
+ concurrency: ConcurrencyLimit | None = ...,
139
+ rate: RateLimiter | None = ...,
140
+ **options: object,
141
+ ) -> Dispatcher: ...
142
+
143
+ @overload
144
+ def provider(
145
+ name: str,
146
+ *,
147
+ retry: RetryPolicy = ...,
148
+ concurrency: ConcurrencyLimit | None = ...,
149
+ rate: RateLimiter | None = ...,
150
+ **options: object,
151
+ ) -> Dispatcher: ...
152
+
153
+ @overload
154
+ def leaf(
155
+ name: Literal["system-one"],
156
+ *,
157
+ model: SystemOneModel = ...,
158
+ api_key: str | None = ...,
159
+ base_url: str = ...,
160
+ client: object | None = ...,
161
+ long_context: bool = ...,
162
+ images: bool = ...,
163
+ capabilities: Capabilities | None = ...,
164
+ ) -> Leaf: ...
165
+
166
+ @overload
167
+ def leaf(
168
+ name: Literal["openai-decisions"],
169
+ *,
170
+ model: str = ...,
171
+ api_key: str | None = ...,
172
+ client: object | None = ...,
173
+ max_questions: int = ...,
174
+ capabilities: Capabilities | None = ...,
175
+ ) -> Leaf: ...
176
+
177
+ @overload
178
+ def leaf(
179
+ name: Literal["openai-chat"],
180
+ *,
181
+ model: str,
182
+ api_key: str | None = ...,
183
+ client: object | None = ...,
184
+ elicitation: str = ...,
185
+ max_off_label_mass: float | None = ...,
186
+ capabilities: Capabilities | None = ...,
187
+ ) -> Leaf: ...
188
+
189
+ @overload
190
+ def leaf(
191
+ name: Literal["cloudflare-clef"],
192
+ *,
193
+ model: ClefModel = ...,
194
+ account_id: str | None = ...,
195
+ api_token: str | None = ...,
196
+ client: object | None = ...,
197
+ capabilities: Capabilities | None = ...,
198
+ ) -> Leaf: ...
199
+
200
+ @overload
201
+ def leaf(
202
+ name: Literal["cloudflare-clairic"],
203
+ *,
204
+ model: Literal["clairic", "@cf/cloudflare/clairic"] = ...,
205
+ account_id: str | None = ...,
206
+ api_token: str | None = ...,
207
+ client: object | None = ...,
208
+ capabilities: Capabilities | None = ...,
209
+ ) -> Leaf: ...
210
+
211
+ @overload
212
+ def leaf(
213
+ name: Literal["gliner"],
214
+ *,
215
+ model: Literal["fastino/gliner2.5-small-v1"] = ...,
216
+ revision: str | None = ...,
217
+ threshold: float = ...,
218
+ runtime: GlinerRuntime | None = ...,
219
+ capabilities: Capabilities | None = ...,
220
+ ) -> Leaf: ...
221
+
222
+ @overload
223
+ @deprecated(
224
+ "a registered leaf takes its own keywords, each of its type and every required one "
225
+ "given; an unknown or missing keyword raises TypeError"
226
+ )
227
+ def leaf(
228
+ name: Literal[
229
+ "system-one",
230
+ "openai-decisions",
231
+ "openai-chat",
232
+ "cloudflare-clef",
233
+ "cloudflare-clairic",
234
+ "gliner",
235
+ ],
236
+ **options: object,
237
+ ) -> Leaf: ...
238
+
239
+ @overload
240
+ def leaf(
241
+ name: str,
242
+ **options: object,
243
+ ) -> Leaf: ...
244
+
245
+
246
+ def provider(
247
+ name: str,
248
+ *,
249
+ retry: RetryPolicy = _DEFAULT_RETRY,
250
+ concurrency: ConcurrencyLimit | None = None,
251
+ rate: RateLimiter | None = None,
252
+ **options: object,
253
+ ) -> Dispatcher:
254
+ """The provider registered as ``name``: a :class:`~idia.dispatch.Dispatcher` over
255
+ its leaf, built from ``options``, with the given policy and limits.
256
+
257
+ The re-ask after a fault stays off; build a ``Dispatcher`` by hand to turn it on. A
258
+ keyword the leaf does not take raises ``TypeError`` naming this call and the registered
259
+ name.
260
+ """
261
+ built = _load_leaf(name, "provider", **options)
262
+ return Dispatcher(built, retry=retry, concurrency=concurrency, rate=rate)
263
+
264
+
265
+ def leaf(name: str, **options: object) -> Leaf:
266
+ """The leaf registered as ``name``, built from ``options`` and not wrapped: the leaf
267
+ ``idia.provider(name, **options)`` wraps. Wrap it in ``Dispatcher(leaf, ...)`` to ask it
268
+ anything, around a leaf of your own if you like, such as one counting every call.
269
+
270
+ An unknown name raises ``ProviderNotFound``, and a missing extra ``ModuleNotFoundError``
271
+ naming it. A keyword the leaf does not take raises ``TypeError`` naming this call and the
272
+ name; for ``retry``, ``concurrency``, ``rate``, ``estimator``, ``overflow`` or ``reask``
273
+ it adds that they are the ``Dispatcher``'s.
274
+ """
275
+ return _load_leaf(name, "leaf", **options)
276
+
277
+
278
+ def provider_names() -> tuple[str, ...]:
279
+ """The names ``idia.provider`` and ``idia.leaf`` accept, sorted: the names
280
+ ``ProviderNotFound`` lists. A name is listed whether or not its extra is installed."""
281
+ return _registered()