autoevals 0.2.0__tar.gz → 0.3.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.
- {autoevals-0.2.0/py/autoevals.egg-info → autoevals-0.3.0}/PKG-INFO +37 -42
- {autoevals-0.2.0 → autoevals-0.3.0}/README.md +23 -6
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals/__init__.py +7 -6
- autoevals-0.3.0/py/autoevals/litellm.py +232 -0
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals/llm.py +9 -0
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals/oai.py +34 -13
- autoevals-0.3.0/py/autoevals/test_litellm.py +164 -0
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals/test_llm.py +93 -1
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals/test_oai.py +33 -25
- autoevals-0.3.0/py/autoevals/version.py +1 -0
- {autoevals-0.2.0 → autoevals-0.3.0/py/autoevals.egg-info}/PKG-INFO +37 -42
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals.egg-info/SOURCES.txt +2 -1
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals.egg-info/requires.txt +11 -20
- autoevals-0.3.0/pyproject.toml +70 -0
- autoevals-0.2.0/py/autoevals/version.py +0 -1
- autoevals-0.2.0/pyproject.toml +0 -6
- autoevals-0.2.0/setup.py +0 -61
- {autoevals-0.2.0 → autoevals-0.3.0}/LICENSE +0 -0
- {autoevals-0.2.0 → autoevals-0.3.0}/MANIFEST.in +0 -0
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals/conftest.py +0 -0
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals/json.py +0 -0
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals/list.py +0 -0
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals/moderation.py +0 -0
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals/number.py +0 -0
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals/partial.py +0 -0
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals/ragas.py +0 -0
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals/score.py +0 -0
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals/serializable_data_class.py +0 -0
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals/string.py +0 -0
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals/templates/battle.yaml +0 -0
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals/templates/closed_q_a.yaml +0 -0
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals/templates/factuality.yaml +0 -0
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals/templates/humor.yaml +0 -0
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals/templates/possible.yaml +0 -0
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals/templates/security.yaml +0 -0
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals/templates/sql.yaml +0 -0
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals/templates/summary.yaml +0 -0
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals/templates/translation.yaml +0 -0
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals/test_embeddings.py +0 -0
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals/test_init_models.py +0 -0
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals/test_json.py +0 -0
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals/test_moderation.py +0 -0
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals/test_partial.py +0 -0
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals/test_ragas.py +0 -0
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals/test_reasoning_effort.py +0 -0
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals/test_serializable_data_class.py +0 -0
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals/test_values.py +0 -0
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals/thread_utils.py +0 -0
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals/value.py +0 -0
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals.egg-info/dependency_links.txt +0 -0
- {autoevals-0.2.0 → autoevals-0.3.0}/py/autoevals.egg-info/top_level.txt +0 -0
- {autoevals-0.2.0 → autoevals-0.3.0}/setup.cfg +0 -0
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: autoevals
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.0
|
|
4
4
|
Summary: Universal library for evaluating AI models
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
Author-email: info@braintrustdata.com
|
|
5
|
+
Author-email: BrainTrust <info@braintrustdata.com>
|
|
6
|
+
Project-URL: Homepage, https://www.braintrustdata.com
|
|
8
7
|
Project-URL: Bug Tracker, https://github.com/braintrustdata/autoevals
|
|
9
8
|
Classifier: Programming Language :: Python :: 3
|
|
10
9
|
Classifier: Programming Language :: Python :: 3.10
|
|
@@ -13,55 +12,34 @@ Requires-Python: >=3.10.0
|
|
|
13
12
|
Description-Content-Type: text/markdown
|
|
14
13
|
License-File: LICENSE
|
|
15
14
|
Requires-Dist: chevron
|
|
15
|
+
Requires-Dist: jsonschema
|
|
16
16
|
Requires-Dist: polyleven
|
|
17
17
|
Requires-Dist: pyyaml
|
|
18
|
-
Requires-Dist: jsonschema
|
|
19
18
|
Provides-Extra: dev
|
|
20
19
|
Requires-Dist: black==22.6.0; extra == "dev"
|
|
21
|
-
Requires-Dist: braintrust; extra == "dev"
|
|
22
|
-
Requires-Dist: build; extra == "dev"
|
|
23
20
|
Requires-Dist: flake8; extra == "dev"
|
|
24
21
|
Requires-Dist: flake8-isort; extra == "dev"
|
|
25
|
-
Requires-Dist: IPython; extra == "dev"
|
|
26
22
|
Requires-Dist: isort==5.12.0; extra == "dev"
|
|
27
|
-
Requires-Dist: openai; extra == "dev"
|
|
28
23
|
Requires-Dist: pre-commit; extra == "dev"
|
|
24
|
+
Requires-Dist: build; extra == "dev"
|
|
25
|
+
Requires-Dist: twine; extra == "dev"
|
|
26
|
+
Requires-Dist: braintrust; extra == "dev"
|
|
27
|
+
Requires-Dist: ipython; extra == "dev"
|
|
28
|
+
Requires-Dist: openai; extra == "dev"
|
|
29
29
|
Requires-Dist: pytest; extra == "dev"
|
|
30
|
+
Requires-Dist: pytest-asyncio; extra == "dev"
|
|
31
|
+
Requires-Dist: pytest-mock; extra == "dev"
|
|
30
32
|
Requires-Dist: respx; extra == "dev"
|
|
31
|
-
Requires-Dist: twine; extra == "dev"
|
|
32
33
|
Provides-Extra: doc
|
|
33
34
|
Requires-Dist: pydoc-markdown; extra == "doc"
|
|
34
35
|
Provides-Extra: scipy
|
|
35
36
|
Requires-Dist: numpy; extra == "scipy"
|
|
36
37
|
Requires-Dist: scipy; extra == "scipy"
|
|
38
|
+
Provides-Extra: litellm
|
|
39
|
+
Requires-Dist: litellm<1.85,>=1.60; extra == "litellm"
|
|
37
40
|
Provides-Extra: all
|
|
38
|
-
Requires-Dist:
|
|
39
|
-
Requires-Dist: black==22.6.0; extra == "all"
|
|
40
|
-
Requires-Dist: braintrust; extra == "all"
|
|
41
|
-
Requires-Dist: build; extra == "all"
|
|
42
|
-
Requires-Dist: flake8; extra == "all"
|
|
43
|
-
Requires-Dist: flake8-isort; extra == "all"
|
|
44
|
-
Requires-Dist: isort==5.12.0; extra == "all"
|
|
45
|
-
Requires-Dist: numpy; extra == "all"
|
|
46
|
-
Requires-Dist: openai; extra == "all"
|
|
47
|
-
Requires-Dist: pre-commit; extra == "all"
|
|
48
|
-
Requires-Dist: pydoc-markdown; extra == "all"
|
|
49
|
-
Requires-Dist: pytest; extra == "all"
|
|
50
|
-
Requires-Dist: respx; extra == "all"
|
|
51
|
-
Requires-Dist: scipy; extra == "all"
|
|
52
|
-
Requires-Dist: twine; extra == "all"
|
|
53
|
-
Dynamic: author
|
|
54
|
-
Dynamic: author-email
|
|
55
|
-
Dynamic: classifier
|
|
56
|
-
Dynamic: description
|
|
57
|
-
Dynamic: description-content-type
|
|
58
|
-
Dynamic: home-page
|
|
41
|
+
Requires-Dist: autoevals[scipy]; extra == "all"
|
|
59
42
|
Dynamic: license-file
|
|
60
|
-
Dynamic: project-url
|
|
61
|
-
Dynamic: provides-extra
|
|
62
|
-
Dynamic: requires-dist
|
|
63
|
-
Dynamic: requires-python
|
|
64
|
-
Dynamic: summary
|
|
65
43
|
|
|
66
44
|
# Autoevals
|
|
67
45
|
|
|
@@ -166,24 +144,24 @@ import { Factuality } from "autoevals";
|
|
|
166
144
|
|
|
167
145
|
## Using other AI providers
|
|
168
146
|
|
|
169
|
-
When you use Autoevals, it will look for an `OPENAI_BASE_URL` environment variable to use as the base for requests to an OpenAI
|
|
147
|
+
When you use Autoevals, it will look for an `OPENAI_BASE_URL` environment variable to use as the base for requests to an OpenAI-compatible API. If `OPENAI_BASE_URL` is not set, it will look for a `BRAINTRUST_AI_GATEWAY_URL` environment variable and then default to the [Braintrust Gateway](https://www.braintrust.dev/docs/deploy/gateway).
|
|
170
148
|
|
|
171
|
-
|
|
149
|
+
When you use the Braintrust Gateway, you'll also get:
|
|
172
150
|
|
|
173
151
|
- Simplified access to many AI providers
|
|
174
152
|
- Reduced costs with automatic request caching
|
|
175
153
|
- Increased observability when you enable logging to Braintrust
|
|
176
154
|
|
|
177
|
-
The
|
|
155
|
+
The Braintrust-hosted Gateway is free to use while it is in beta.
|
|
178
156
|
|
|
179
|
-
|
|
157
|
+
Set the `BRAINTRUST_API_KEY` environment variable to authenticate Gateway requests. You can also route requests to supported AI providers and models or custom models you have configured in Braintrust.
|
|
180
158
|
|
|
181
159
|
<div className="tabs">
|
|
182
160
|
|
|
183
161
|
### Python
|
|
184
162
|
|
|
185
163
|
```python
|
|
186
|
-
# NOTE: ensure BRAINTRUST_API_KEY is set in your environment
|
|
164
|
+
# NOTE: ensure BRAINTRUST_API_KEY is set in your environment
|
|
187
165
|
from autoevals.llm import *
|
|
188
166
|
|
|
189
167
|
# Create an LLM-based evaluator using the Claude 3.5 Sonnet model from Anthropic
|
|
@@ -204,7 +182,7 @@ print(f"Factuality metadata: {result.metadata['rationale']}")
|
|
|
204
182
|
### TypeScript
|
|
205
183
|
|
|
206
184
|
```typescript
|
|
207
|
-
// NOTE: ensure BRAINTRUST_API_KEY is set in your environment
|
|
185
|
+
// NOTE: ensure BRAINTRUST_API_KEY is set in your environment
|
|
208
186
|
import { Factuality } from "autoevals";
|
|
209
187
|
|
|
210
188
|
(async () => {
|
|
@@ -518,6 +496,23 @@ Nicolo also dropped this as a reference: http://spec.openapis.org/oas/v3.0.3#ope
|
|
|
518
496
|
|
|
519
497
|
</div>
|
|
520
498
|
|
|
499
|
+
## Score results
|
|
500
|
+
|
|
501
|
+
Every scorer returns a small `Score` result object. This is the public surface
|
|
502
|
+
consumers should read when they need to store, compare, or export evaluation
|
|
503
|
+
results:
|
|
504
|
+
|
|
505
|
+
- `name`: the scorer name
|
|
506
|
+
- `score`: a number between 0 and 1, or `None` / `null` when the evaluation is skipped
|
|
507
|
+
- `metadata`: optional scorer-specific details, such as rationale text or a
|
|
508
|
+
selected choice. Keys are scorer-specific; consumers should not assume
|
|
509
|
+
metadata keys are shared across scorer types.
|
|
510
|
+
- `error`: deprecated and retained for backward compatibility; some scorers may
|
|
511
|
+
still populate it, but callers should primarily handle thrown exceptions
|
|
512
|
+
|
|
513
|
+
Inputs, expected values, model prompts, and other runtime context are not part
|
|
514
|
+
of the `Score` object. Keep those separately if your application needs them.
|
|
515
|
+
|
|
521
516
|
## Creating custom scorers
|
|
522
517
|
|
|
523
518
|
You can also create your own scoring functions that do not use LLMs. For example, to test whether the word `'banana'`
|
|
@@ -101,24 +101,24 @@ import { Factuality } from "autoevals";
|
|
|
101
101
|
|
|
102
102
|
## Using other AI providers
|
|
103
103
|
|
|
104
|
-
When you use Autoevals, it will look for an `OPENAI_BASE_URL` environment variable to use as the base for requests to an OpenAI
|
|
104
|
+
When you use Autoevals, it will look for an `OPENAI_BASE_URL` environment variable to use as the base for requests to an OpenAI-compatible API. If `OPENAI_BASE_URL` is not set, it will look for a `BRAINTRUST_AI_GATEWAY_URL` environment variable and then default to the [Braintrust Gateway](https://www.braintrust.dev/docs/deploy/gateway).
|
|
105
105
|
|
|
106
|
-
|
|
106
|
+
When you use the Braintrust Gateway, you'll also get:
|
|
107
107
|
|
|
108
108
|
- Simplified access to many AI providers
|
|
109
109
|
- Reduced costs with automatic request caching
|
|
110
110
|
- Increased observability when you enable logging to Braintrust
|
|
111
111
|
|
|
112
|
-
The
|
|
112
|
+
The Braintrust-hosted Gateway is free to use while it is in beta.
|
|
113
113
|
|
|
114
|
-
|
|
114
|
+
Set the `BRAINTRUST_API_KEY` environment variable to authenticate Gateway requests. You can also route requests to supported AI providers and models or custom models you have configured in Braintrust.
|
|
115
115
|
|
|
116
116
|
<div className="tabs">
|
|
117
117
|
|
|
118
118
|
### Python
|
|
119
119
|
|
|
120
120
|
```python
|
|
121
|
-
# NOTE: ensure BRAINTRUST_API_KEY is set in your environment
|
|
121
|
+
# NOTE: ensure BRAINTRUST_API_KEY is set in your environment
|
|
122
122
|
from autoevals.llm import *
|
|
123
123
|
|
|
124
124
|
# Create an LLM-based evaluator using the Claude 3.5 Sonnet model from Anthropic
|
|
@@ -139,7 +139,7 @@ print(f"Factuality metadata: {result.metadata['rationale']}")
|
|
|
139
139
|
### TypeScript
|
|
140
140
|
|
|
141
141
|
```typescript
|
|
142
|
-
// NOTE: ensure BRAINTRUST_API_KEY is set in your environment
|
|
142
|
+
// NOTE: ensure BRAINTRUST_API_KEY is set in your environment
|
|
143
143
|
import { Factuality } from "autoevals";
|
|
144
144
|
|
|
145
145
|
(async () => {
|
|
@@ -453,6 +453,23 @@ Nicolo also dropped this as a reference: http://spec.openapis.org/oas/v3.0.3#ope
|
|
|
453
453
|
|
|
454
454
|
</div>
|
|
455
455
|
|
|
456
|
+
## Score results
|
|
457
|
+
|
|
458
|
+
Every scorer returns a small `Score` result object. This is the public surface
|
|
459
|
+
consumers should read when they need to store, compare, or export evaluation
|
|
460
|
+
results:
|
|
461
|
+
|
|
462
|
+
- `name`: the scorer name
|
|
463
|
+
- `score`: a number between 0 and 1, or `None` / `null` when the evaluation is skipped
|
|
464
|
+
- `metadata`: optional scorer-specific details, such as rationale text or a
|
|
465
|
+
selected choice. Keys are scorer-specific; consumers should not assume
|
|
466
|
+
metadata keys are shared across scorer types.
|
|
467
|
+
- `error`: deprecated and retained for backward compatibility; some scorers may
|
|
468
|
+
still populate it, but callers should primarily handle thrown exceptions
|
|
469
|
+
|
|
470
|
+
Inputs, expected values, model prompts, and other runtime context are not part
|
|
471
|
+
of the `Score` object. Keep those separately if your application needs them.
|
|
472
|
+
|
|
456
473
|
## Creating custom scorers
|
|
457
474
|
|
|
458
475
|
You can also create your own scoring functions that do not use LLMs. For example, to test whether the word `'banana'`
|
|
@@ -15,7 +15,7 @@ This library provides a collection of specialized scorers for different types of
|
|
|
15
15
|
- Both sync and async evaluation support
|
|
16
16
|
- Configurable scoring parameters
|
|
17
17
|
- Detailed feedback through metadata
|
|
18
|
-
- Integration with OpenAI and other LLM providers through Braintrust
|
|
18
|
+
- Integration with OpenAI and other LLM providers through the Braintrust Gateway
|
|
19
19
|
|
|
20
20
|
**Client setup**:
|
|
21
21
|
|
|
@@ -43,10 +43,10 @@ client = AsyncOpenAI()
|
|
|
43
43
|
evaluator = ClosedQA(client=client)
|
|
44
44
|
```
|
|
45
45
|
|
|
46
|
-
**Multi-provider support via the Braintrust
|
|
46
|
+
**Multi-provider support via the Braintrust Gateway**:
|
|
47
47
|
|
|
48
|
-
Autoevals supports multiple LLM providers (Anthropic, Azure, etc.) through the Braintrust
|
|
49
|
-
Configure your client to use the
|
|
48
|
+
Autoevals supports multiple LLM providers (Anthropic, Azure, etc.) through the Braintrust Gateway.
|
|
49
|
+
Configure your client to use the Gateway and set the default model:
|
|
50
50
|
|
|
51
51
|
```python
|
|
52
52
|
import os
|
|
@@ -54,9 +54,9 @@ from openai import AsyncOpenAI
|
|
|
54
54
|
from autoevals import init
|
|
55
55
|
from autoevals.llm import Factuality
|
|
56
56
|
|
|
57
|
-
# Configure client to use Braintrust
|
|
57
|
+
# Configure client to use the Braintrust Gateway with Claude
|
|
58
58
|
client = AsyncOpenAI(
|
|
59
|
-
base_url="https://
|
|
59
|
+
base_url=os.getenv("BRAINTRUST_AI_GATEWAY_URL") or "https://gateway.braintrust.dev",
|
|
60
60
|
api_key=os.getenv("BRAINTRUST_API_KEY"),
|
|
61
61
|
)
|
|
62
62
|
|
|
@@ -127,6 +127,7 @@ See individual module documentation for detailed usage and options.
|
|
|
127
127
|
|
|
128
128
|
from .json import *
|
|
129
129
|
from .list import *
|
|
130
|
+
from .litellm import AsyncLiteLLMClient, LiteLLMClient
|
|
130
131
|
from .llm import *
|
|
131
132
|
from .moderation import *
|
|
132
133
|
from .number import *
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
"""LiteLLM adapters — route Autoevals through the LiteLLM AI gateway for direct
|
|
2
|
+
access to 100+ LLM providers (OpenAI, Anthropic, Bedrock, Vertex, Gemini, Ollama,
|
|
3
|
+
OpenRouter, Groq, DeepSeek, etc.) using provider-native API keys.
|
|
4
|
+
|
|
5
|
+
Example::
|
|
6
|
+
|
|
7
|
+
from autoevals import init
|
|
8
|
+
from autoevals.litellm import LiteLLMClient
|
|
9
|
+
from autoevals.llm import Factuality
|
|
10
|
+
|
|
11
|
+
init(
|
|
12
|
+
client=LiteLLMClient(),
|
|
13
|
+
default_model="anthropic/claude-3-5-sonnet-20241022",
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
evaluator = Factuality()
|
|
17
|
+
result = evaluator.eval(input="...", output="...", expected="...")
|
|
18
|
+
|
|
19
|
+
Unlike the Braintrust AI Proxy path (which requires a Braintrust API key), LiteLLM
|
|
20
|
+
uses each provider's native key (``ANTHROPIC_API_KEY``, ``GEMINI_API_KEY``,
|
|
21
|
+
``AWS_*``, etc.) and routes locally. See https://docs.litellm.ai/docs/providers.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
from __future__ import annotations
|
|
25
|
+
|
|
26
|
+
from typing import Any, Optional
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class _LiteLLMChatCompletions:
|
|
30
|
+
"""Sync ``openai.chat.completions`` surface backed by ``litellm.completion``."""
|
|
31
|
+
|
|
32
|
+
def __init__(self, api_key: Optional[str], base_url: Optional[str]):
|
|
33
|
+
self._api_key = api_key
|
|
34
|
+
self._base_url = base_url
|
|
35
|
+
|
|
36
|
+
def create(self, **kwargs: Any) -> Any:
|
|
37
|
+
import litellm
|
|
38
|
+
|
|
39
|
+
if self._api_key is not None:
|
|
40
|
+
kwargs.setdefault("api_key", self._api_key)
|
|
41
|
+
if self._base_url is not None:
|
|
42
|
+
kwargs.setdefault("api_base", self._base_url)
|
|
43
|
+
return litellm.completion(**kwargs)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class _AsyncLiteLLMChatCompletions:
|
|
47
|
+
"""Async counterpart of ``_LiteLLMChatCompletions``."""
|
|
48
|
+
|
|
49
|
+
def __init__(self, api_key: Optional[str], base_url: Optional[str]):
|
|
50
|
+
self._api_key = api_key
|
|
51
|
+
self._base_url = base_url
|
|
52
|
+
|
|
53
|
+
async def create(self, **kwargs: Any) -> Any:
|
|
54
|
+
import litellm
|
|
55
|
+
|
|
56
|
+
if self._api_key is not None:
|
|
57
|
+
kwargs.setdefault("api_key", self._api_key)
|
|
58
|
+
if self._base_url is not None:
|
|
59
|
+
kwargs.setdefault("api_base", self._base_url)
|
|
60
|
+
return await litellm.acompletion(**kwargs)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class _LiteLLMChat:
|
|
64
|
+
def __init__(self, completions: Any):
|
|
65
|
+
self.completions = completions
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class _LiteLLMEmbeddings:
|
|
69
|
+
def __init__(self, api_key: Optional[str], base_url: Optional[str], is_async: bool):
|
|
70
|
+
self._api_key = api_key
|
|
71
|
+
self._base_url = base_url
|
|
72
|
+
self._is_async = is_async
|
|
73
|
+
|
|
74
|
+
def _kwargs(self, kwargs: dict[str, Any]) -> dict[str, Any]:
|
|
75
|
+
if self._api_key is not None:
|
|
76
|
+
kwargs.setdefault("api_key", self._api_key)
|
|
77
|
+
if self._base_url is not None:
|
|
78
|
+
kwargs.setdefault("api_base", self._base_url)
|
|
79
|
+
return kwargs
|
|
80
|
+
|
|
81
|
+
def create(self, **kwargs: Any) -> Any:
|
|
82
|
+
import litellm
|
|
83
|
+
|
|
84
|
+
if self._is_async:
|
|
85
|
+
return litellm.aembedding(**self._kwargs(kwargs))
|
|
86
|
+
return litellm.embedding(**self._kwargs(kwargs))
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
class _LiteLLMModerations:
|
|
90
|
+
def __init__(self, api_key: Optional[str], base_url: Optional[str], is_async: bool):
|
|
91
|
+
self._api_key = api_key
|
|
92
|
+
self._base_url = base_url
|
|
93
|
+
self._is_async = is_async
|
|
94
|
+
|
|
95
|
+
def _kwargs(self, kwargs: dict[str, Any]) -> dict[str, Any]:
|
|
96
|
+
if self._api_key is not None:
|
|
97
|
+
kwargs.setdefault("api_key", self._api_key)
|
|
98
|
+
if self._base_url is not None:
|
|
99
|
+
kwargs.setdefault("api_base", self._base_url)
|
|
100
|
+
return kwargs
|
|
101
|
+
|
|
102
|
+
def create(self, **kwargs: Any) -> Any:
|
|
103
|
+
import litellm
|
|
104
|
+
|
|
105
|
+
if self._is_async:
|
|
106
|
+
return litellm.amoderation(**self._kwargs(kwargs))
|
|
107
|
+
return litellm.moderation(**self._kwargs(kwargs))
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def _responses_params_to_chat_kwargs(kwargs: dict[str, Any]) -> dict[str, Any]:
|
|
111
|
+
"""Translate autoevals' Responses-API kwargs back into Chat-Completions kwargs
|
|
112
|
+
that ``litellm.completion`` understands.
|
|
113
|
+
|
|
114
|
+
autoevals' ``oai.py`` routes GPT-5 models through ``client.responses.create``
|
|
115
|
+
(see ``is_gpt5_model``, ``prepare_responses_params``). Those params use
|
|
116
|
+
``input=`` instead of ``messages=`` and a Responses-API tool schema.
|
|
117
|
+
``litellm.completion`` only speaks Chat-Completions, so we translate back.
|
|
118
|
+
The resulting ChatCompletion response is then detected by autoevals'
|
|
119
|
+
``convert_responses_to_chat_completion`` as not-a-Responses-object and
|
|
120
|
+
returned as-is (see ``oai.py:226``).
|
|
121
|
+
"""
|
|
122
|
+
chat_kwargs = dict(kwargs)
|
|
123
|
+
if "input" in chat_kwargs and "messages" not in chat_kwargs:
|
|
124
|
+
chat_kwargs["messages"] = chat_kwargs.pop("input")
|
|
125
|
+
# Responses-API tools use flat {type, name, description, parameters}; Chat-
|
|
126
|
+
# Completions tools nest the schema under {type, function: {...}}.
|
|
127
|
+
if "tools" in chat_kwargs:
|
|
128
|
+
translated = []
|
|
129
|
+
for tool in chat_kwargs["tools"]:
|
|
130
|
+
if isinstance(tool, dict) and tool.get("type") == "function" and "function" not in tool:
|
|
131
|
+
translated.append(
|
|
132
|
+
{
|
|
133
|
+
"type": "function",
|
|
134
|
+
"function": {
|
|
135
|
+
"name": tool.get("name"),
|
|
136
|
+
"description": tool.get("description"),
|
|
137
|
+
"parameters": tool.get("parameters"),
|
|
138
|
+
},
|
|
139
|
+
}
|
|
140
|
+
)
|
|
141
|
+
else:
|
|
142
|
+
translated.append(tool)
|
|
143
|
+
chat_kwargs["tools"] = translated
|
|
144
|
+
return chat_kwargs
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
class _LiteLLMResponses:
|
|
148
|
+
"""Adapter for autoevals' Responses-API code path (triggered by GPT-5 models).
|
|
149
|
+
|
|
150
|
+
Without this, ``init(client=LiteLLMClient())`` with autoevals' default model
|
|
151
|
+
(``gpt-5-mini``) would call ``litellm.completion(input=..., model=...)`` and
|
|
152
|
+
crash because LiteLLM requires ``messages=``.
|
|
153
|
+
"""
|
|
154
|
+
|
|
155
|
+
def __init__(self, api_key: Optional[str], base_url: Optional[str], is_async: bool):
|
|
156
|
+
self._api_key = api_key
|
|
157
|
+
self._base_url = base_url
|
|
158
|
+
self._is_async = is_async
|
|
159
|
+
|
|
160
|
+
def _kwargs(self, kwargs: dict[str, Any]) -> dict[str, Any]:
|
|
161
|
+
chat_kwargs = _responses_params_to_chat_kwargs(kwargs)
|
|
162
|
+
if self._api_key is not None:
|
|
163
|
+
chat_kwargs.setdefault("api_key", self._api_key)
|
|
164
|
+
if self._base_url is not None:
|
|
165
|
+
chat_kwargs.setdefault("api_base", self._base_url)
|
|
166
|
+
return chat_kwargs
|
|
167
|
+
|
|
168
|
+
def create(self, **kwargs: Any) -> Any:
|
|
169
|
+
import litellm
|
|
170
|
+
|
|
171
|
+
if self._is_async:
|
|
172
|
+
return litellm.acompletion(**self._kwargs(kwargs))
|
|
173
|
+
return litellm.completion(**self._kwargs(kwargs))
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
class _LiteLLMResponsesContainer:
|
|
177
|
+
"""Exposes ``.create`` on the ``client.responses`` attribute to match the
|
|
178
|
+
OpenAI v1 client shape autoevals' ``oai.py`` duck-types on."""
|
|
179
|
+
|
|
180
|
+
def __init__(self, create_impl: Any):
|
|
181
|
+
self.create = create_impl
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
class LiteLLMClient:
|
|
185
|
+
"""OpenAI-compatible client backed by ``litellm.completion``.
|
|
186
|
+
|
|
187
|
+
Pass to ``autoevals.init(client=LiteLLMClient())``. Routes every chat/embedding/
|
|
188
|
+
moderation call through LiteLLM, which resolves the target provider from the
|
|
189
|
+
model-name prefix (e.g. ``anthropic/claude-3-5-sonnet``, ``bedrock/anthropic.claude-3-sonnet``).
|
|
190
|
+
|
|
191
|
+
Args:
|
|
192
|
+
api_key: Optional provider API key. If unset, LiteLLM falls back to the
|
|
193
|
+
per-provider env vars (``OPENAI_API_KEY``, ``ANTHROPIC_API_KEY``, etc.).
|
|
194
|
+
base_url: Optional custom base URL (forwarded to LiteLLM as ``api_base``).
|
|
195
|
+
organization: Accepted for OpenAI-protocol compatibility; not used.
|
|
196
|
+
"""
|
|
197
|
+
|
|
198
|
+
def __init__(
|
|
199
|
+
self,
|
|
200
|
+
api_key: Optional[str] = None,
|
|
201
|
+
base_url: Optional[str] = None,
|
|
202
|
+
organization: Optional[str] = None,
|
|
203
|
+
):
|
|
204
|
+
self.api_key = api_key
|
|
205
|
+
self.base_url = base_url
|
|
206
|
+
self.organization = organization
|
|
207
|
+
self.chat = _LiteLLMChat(_LiteLLMChatCompletions(api_key=api_key, base_url=base_url))
|
|
208
|
+
self.embeddings = _LiteLLMEmbeddings(api_key=api_key, base_url=base_url, is_async=False)
|
|
209
|
+
self.moderations = _LiteLLMModerations(api_key=api_key, base_url=base_url, is_async=False)
|
|
210
|
+
self.responses = _LiteLLMResponsesContainer(
|
|
211
|
+
_LiteLLMResponses(api_key=api_key, base_url=base_url, is_async=False).create,
|
|
212
|
+
)
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
class AsyncLiteLLMClient:
|
|
216
|
+
"""Async variant of :class:`LiteLLMClient` — uses ``litellm.acompletion`` etc."""
|
|
217
|
+
|
|
218
|
+
def __init__(
|
|
219
|
+
self,
|
|
220
|
+
api_key: Optional[str] = None,
|
|
221
|
+
base_url: Optional[str] = None,
|
|
222
|
+
organization: Optional[str] = None,
|
|
223
|
+
):
|
|
224
|
+
self.api_key = api_key
|
|
225
|
+
self.base_url = base_url
|
|
226
|
+
self.organization = organization
|
|
227
|
+
self.chat = _LiteLLMChat(_AsyncLiteLLMChatCompletions(api_key=api_key, base_url=base_url))
|
|
228
|
+
self.embeddings = _LiteLLMEmbeddings(api_key=api_key, base_url=base_url, is_async=True)
|
|
229
|
+
self.moderations = _LiteLLMModerations(api_key=api_key, base_url=base_url, is_async=True)
|
|
230
|
+
self.responses = _LiteLLMResponsesContainer(
|
|
231
|
+
_LiteLLMResponses(api_key=api_key, base_url=base_url, is_async=True).create,
|
|
232
|
+
)
|
|
@@ -180,6 +180,7 @@ class OpenAILLMClassifier(OpenAILLMScorer):
|
|
|
180
180
|
reasoning_effort=None,
|
|
181
181
|
reasoning_enabled=None,
|
|
182
182
|
reasoning_budget=None,
|
|
183
|
+
use_responses_api=None,
|
|
183
184
|
engine=None,
|
|
184
185
|
api_key=None,
|
|
185
186
|
base_url=None,
|
|
@@ -210,6 +211,9 @@ class OpenAILLMClassifier(OpenAILLMScorer):
|
|
|
210
211
|
if reasoning_budget is not None:
|
|
211
212
|
self.extra_args["reasoning_budget"] = reasoning_budget
|
|
212
213
|
|
|
214
|
+
if use_responses_api is not None:
|
|
215
|
+
self.extra_args["use_responses_api"] = use_responses_api
|
|
216
|
+
|
|
213
217
|
self.render_args = {}
|
|
214
218
|
if render_args:
|
|
215
219
|
self.render_args.update(render_args)
|
|
@@ -366,6 +370,7 @@ class LLMClassifier(OpenAILLMClassifier):
|
|
|
366
370
|
reasoning_effort=None,
|
|
367
371
|
reasoning_enabled=None,
|
|
368
372
|
reasoning_budget=None,
|
|
373
|
+
use_responses_api=None,
|
|
369
374
|
engine=None,
|
|
370
375
|
api_key=None,
|
|
371
376
|
base_url=None,
|
|
@@ -397,6 +402,7 @@ class LLMClassifier(OpenAILLMClassifier):
|
|
|
397
402
|
reasoning_effort=reasoning_effort,
|
|
398
403
|
reasoning_enabled=reasoning_enabled,
|
|
399
404
|
reasoning_budget=reasoning_budget,
|
|
405
|
+
use_responses_api=use_responses_api,
|
|
400
406
|
engine=engine,
|
|
401
407
|
api_key=api_key,
|
|
402
408
|
base_url=base_url,
|
|
@@ -498,6 +504,7 @@ class SpecFileClassifier(LLMClassifier):
|
|
|
498
504
|
use_cot=None,
|
|
499
505
|
max_tokens=None,
|
|
500
506
|
temperature=None,
|
|
507
|
+
use_responses_api=None,
|
|
501
508
|
api_key=None,
|
|
502
509
|
base_url=None,
|
|
503
510
|
client: Client | None = None,
|
|
@@ -513,6 +520,8 @@ class SpecFileClassifier(LLMClassifier):
|
|
|
513
520
|
kwargs["max_tokens"] = max_tokens
|
|
514
521
|
if temperature is not None:
|
|
515
522
|
kwargs["temperature"] = temperature
|
|
523
|
+
if use_responses_api is not None:
|
|
524
|
+
kwargs["use_responses_api"] = use_responses_api
|
|
516
525
|
if api_key is not None:
|
|
517
526
|
kwargs["api_key"] = api_key
|
|
518
527
|
if base_url is not None:
|
|
@@ -9,7 +9,16 @@ from contextvars import ContextVar
|
|
|
9
9
|
from dataclasses import dataclass
|
|
10
10
|
from typing import Any, Optional, Protocol, TypedDict, TypeVar, Union, cast, runtime_checkable
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
GATEWAY_URL = "https://gateway.braintrust.dev"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _gateway_url() -> str:
|
|
16
|
+
return os.environ.get("BRAINTRUST_AI_GATEWAY_URL", "").strip() or GATEWAY_URL
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _is_gateway_url(base_url: str) -> bool:
|
|
20
|
+
normalized_base_url = base_url.rstrip("/")
|
|
21
|
+
return normalized_base_url == GATEWAY_URL or normalized_base_url == _gateway_url().rstrip("/")
|
|
13
22
|
|
|
14
23
|
|
|
15
24
|
class DefaultModelConfig(TypedDict, total=False):
|
|
@@ -195,7 +204,9 @@ class LLMClient:
|
|
|
195
204
|
if not has_customization and not isinstance(self.openai, NamedWrapper):
|
|
196
205
|
self.openai = wrap_openai(self.openai)
|
|
197
206
|
|
|
198
|
-
self._is_wrapped = isinstance(self.openai, NamedWrapper)
|
|
207
|
+
self._is_wrapped = isinstance(self.openai, NamedWrapper) or (
|
|
208
|
+
not has_customization and wrap_openai.__module__.startswith("braintrust.")
|
|
209
|
+
)
|
|
199
210
|
|
|
200
211
|
openai_module = get_openai_module()
|
|
201
212
|
|
|
@@ -310,9 +321,11 @@ class LLMClient:
|
|
|
310
321
|
responses_params["tool_choice"] = "required"
|
|
311
322
|
|
|
312
323
|
# Copy supported parameters
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
324
|
+
if "temperature" in kwargs:
|
|
325
|
+
responses_params["temperature"] = kwargs["temperature"]
|
|
326
|
+
# The Responses API nests this under reasoning.effort, unlike Chat Completions.
|
|
327
|
+
if "reasoning_effort" in kwargs:
|
|
328
|
+
responses_params["reasoning"] = {"effort": kwargs["reasoning_effort"]}
|
|
316
329
|
|
|
317
330
|
return responses_params
|
|
318
331
|
|
|
@@ -320,7 +333,9 @@ class LLMClient:
|
|
|
320
333
|
|
|
321
334
|
async def complete_wrapper(**kwargs: Any) -> Any:
|
|
322
335
|
model = kwargs.get("model", "")
|
|
323
|
-
|
|
336
|
+
# Strip use_responses_api so it is never forwarded to either API.
|
|
337
|
+
use_responses_api = kwargs.pop("use_responses_api", False)
|
|
338
|
+
if is_gpt5_model(model) or use_responses_api:
|
|
324
339
|
responses_params = prepare_responses_params(kwargs)
|
|
325
340
|
response = await responses_create(**responses_params)
|
|
326
341
|
return convert_responses_to_chat_completion(response)
|
|
@@ -330,7 +345,9 @@ class LLMClient:
|
|
|
330
345
|
|
|
331
346
|
def complete_wrapper(**kwargs: Any) -> Any:
|
|
332
347
|
model = kwargs.get("model", "")
|
|
333
|
-
|
|
348
|
+
# Strip use_responses_api so it is never forwarded to either API.
|
|
349
|
+
use_responses_api = kwargs.pop("use_responses_api", False)
|
|
350
|
+
if is_gpt5_model(model) or use_responses_api:
|
|
334
351
|
responses_params = prepare_responses_params(kwargs)
|
|
335
352
|
response = responses_create(**responses_params)
|
|
336
353
|
return convert_responses_to_chat_completion(response)
|
|
@@ -425,7 +442,7 @@ def init(
|
|
|
425
442
|
models for different evaluation types. Only the specified models are updated;
|
|
426
443
|
others remain unchanged.
|
|
427
444
|
|
|
428
|
-
When using non-OpenAI providers via the Braintrust
|
|
445
|
+
When using non-OpenAI providers via the Braintrust Gateway, set this to the
|
|
429
446
|
appropriate model string (e.g., "claude-3-5-sonnet-20241022").
|
|
430
447
|
|
|
431
448
|
Example:
|
|
@@ -442,7 +459,7 @@ def init(
|
|
|
442
459
|
init(
|
|
443
460
|
client=OpenAI(
|
|
444
461
|
api_key=os.environ["BRAINTRUST_API_KEY"],
|
|
445
|
-
base_url="https://
|
|
462
|
+
base_url=os.getenv("BRAINTRUST_AI_GATEWAY_URL") or "https://gateway.braintrust.dev",
|
|
446
463
|
),
|
|
447
464
|
default_model={
|
|
448
465
|
"completion": "claude-3-5-sonnet-20241022",
|
|
@@ -514,7 +531,8 @@ def prepare_openai(
|
|
|
514
531
|
Deprecated: Use the `client` argument and set the `openai`.
|
|
515
532
|
|
|
516
533
|
base_url (str, optional): Base URL for API requests. If not provided, will
|
|
517
|
-
use OPENAI_BASE_URL from environment or fall back to
|
|
534
|
+
use OPENAI_BASE_URL from environment or fall back to BRAINTRUST_AI_GATEWAY_URL
|
|
535
|
+
or GATEWAY_URL.
|
|
518
536
|
Deprecated: Use the `client` argument and set the `openai`.
|
|
519
537
|
|
|
520
538
|
Returns:
|
|
@@ -553,11 +571,14 @@ def prepare_openai(
|
|
|
553
571
|
)
|
|
554
572
|
warned_deprecated_api_key_base_url = True
|
|
555
573
|
|
|
574
|
+
if base_url is None:
|
|
575
|
+
base_url = os.environ.get("OPENAI_BASE_URL") or _gateway_url()
|
|
556
576
|
# prepare the default openai sdk, if not provided
|
|
557
577
|
if api_key is None:
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
578
|
+
if _is_gateway_url(base_url):
|
|
579
|
+
api_key = os.environ.get("BRAINTRUST_API_KEY") or os.environ.get("OPENAI_API_KEY")
|
|
580
|
+
else:
|
|
581
|
+
api_key = os.environ.get("OPENAI_API_KEY") or os.environ.get("BRAINTRUST_API_KEY")
|
|
561
582
|
|
|
562
583
|
if hasattr(openai_module, "OpenAI"):
|
|
563
584
|
openai_module = cast(OpenAIV1Module, openai_module)
|