semanticheck 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.
- semanticheck-0.1.0/.gitignore +20 -0
- semanticheck-0.1.0/CHANGELOG.md +20 -0
- semanticheck-0.1.0/LICENSE +21 -0
- semanticheck-0.1.0/PKG-INFO +204 -0
- semanticheck-0.1.0/README.md +145 -0
- semanticheck-0.1.0/pyproject.toml +144 -0
- semanticheck-0.1.0/semanticheck/__init__.py +37 -0
- semanticheck-0.1.0/semanticheck/_embed.py +93 -0
- semanticheck-0.1.0/semanticheck/assertions/__init__.py +1 -0
- semanticheck-0.1.0/semanticheck/assertions/budget.py +46 -0
- semanticheck-0.1.0/semanticheck/assertions/hallucination.py +54 -0
- semanticheck-0.1.0/semanticheck/assertions/intent.py +51 -0
- semanticheck-0.1.0/semanticheck/assertions/language.py +59 -0
- semanticheck-0.1.0/semanticheck/assertions/pii.py +77 -0
- semanticheck-0.1.0/semanticheck/assertions/readability.py +82 -0
- semanticheck-0.1.0/semanticheck/assertions/schema.py +76 -0
- semanticheck-0.1.0/semanticheck/assertions/sentiment.py +79 -0
- semanticheck-0.1.0/semanticheck/assertions/similarity.py +39 -0
- semanticheck-0.1.0/semanticheck/assertions/tone.py +53 -0
- semanticheck-0.1.0/semanticheck/baseline.py +128 -0
- semanticheck-0.1.0/semanticheck/judge.py +142 -0
- semanticheck-0.1.0/semanticheck/plugin.py +44 -0
- semanticheck-0.1.0/tests/__init__.py +0 -0
- semanticheck-0.1.0/tests/test_assertions.py +122 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
__pycache__/
|
|
2
|
+
*.py[cod]
|
|
3
|
+
*.egg-info/
|
|
4
|
+
dist/
|
|
5
|
+
build/
|
|
6
|
+
.eggs/
|
|
7
|
+
.venv/
|
|
8
|
+
venv/
|
|
9
|
+
env/
|
|
10
|
+
.env
|
|
11
|
+
*.env
|
|
12
|
+
.pytest_cache/
|
|
13
|
+
.mypy_cache/
|
|
14
|
+
.ruff_cache/
|
|
15
|
+
.llmassert_baselines/
|
|
16
|
+
htmlcov/
|
|
17
|
+
.coverage
|
|
18
|
+
coverage.xml
|
|
19
|
+
*.log
|
|
20
|
+
.pypirc
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [0.1.0] — 2026-04-06
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
- `assert_intent` — semantic intent matching via cosine similarity
|
|
7
|
+
- `assert_tone` — tone/style verification (professional, casual, friendly, etc.)
|
|
8
|
+
- `assert_no_hallucination` — contradiction detection against known facts
|
|
9
|
+
- `assert_similar_to` — semantic similarity between response and reference
|
|
10
|
+
- `assert_token_budget` — token count enforcement (tiktoken or word-based)
|
|
11
|
+
- `assert_schema` — JSON Schema and Pydantic model validation
|
|
12
|
+
- `assert_language` — language detection via Unicode script analysis
|
|
13
|
+
- `assert_no_pii` — PII detection (email, SSN, credit card, phone, IBAN, etc.)
|
|
14
|
+
- `assert_readability` — Flesch-Kincaid readability scoring
|
|
15
|
+
- `assert_sentiment` — sentiment polarity assertion
|
|
16
|
+
- `record_baseline` / `compare_baseline` — golden baseline regression testing
|
|
17
|
+
- `LocalJudge` — local LLM judge (transformers / llama.cpp / embeddings fallback)
|
|
18
|
+
- pytest plugin with `--record-baselines`, `--llm-threshold`, `--skip-llm` flags
|
|
19
|
+
- `llm_record` and `llm_threshold` pytest fixtures
|
|
20
|
+
- Auto-detecting embedding backend: sentence-transformers → OpenAI → hash fallback
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 llmassert contributors
|
|
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,204 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: semanticheck
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: pytest-native semantic assertions for LLM and generative AI applications. No servers. No SaaS. Works with OpenAI, Anthropic, LiteLLM and any LLM client.
|
|
5
|
+
Project-URL: Homepage, https://github.com/semanticheck/semanticheck
|
|
6
|
+
Project-URL: Documentation, https://github.com/semanticheck/semanticheck#readme
|
|
7
|
+
Project-URL: Repository, https://github.com/semanticheck/semanticheck
|
|
8
|
+
Project-URL: Bug Tracker, https://github.com/semanticheck/semanticheck/issues
|
|
9
|
+
Project-URL: Changelog, https://github.com/semanticheck/semanticheck/blob/main/CHANGELOG.md
|
|
10
|
+
Author: semanticheck contributors
|
|
11
|
+
License: MIT
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Keywords: agent testing,ai quality assurance,ai testing,anthropic,claude testing,genai testing,generative ai,golden baseline,gpt testing,hallucination detection,langchain,llm,llm assertions,llm evaluation,llm quality,llm testing,machine learning testing,openai,prompt testing,pytest,pytest plugin,rag testing,regression testing,semantic assertions,semantic testing
|
|
14
|
+
Classifier: Development Status :: 4 - Beta
|
|
15
|
+
Classifier: Framework :: Pytest
|
|
16
|
+
Classifier: Intended Audience :: Developers
|
|
17
|
+
Classifier: Intended Audience :: Science/Research
|
|
18
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
19
|
+
Classifier: Operating System :: OS Independent
|
|
20
|
+
Classifier: Programming Language :: Python :: 3
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
25
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
26
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
27
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
28
|
+
Classifier: Topic :: Software Development :: Testing
|
|
29
|
+
Classifier: Typing :: Typed
|
|
30
|
+
Requires-Python: >=3.9
|
|
31
|
+
Provides-Extra: all
|
|
32
|
+
Requires-Dist: jsonschema>=4.0.0; extra == 'all'
|
|
33
|
+
Requires-Dist: openai>=1.0.0; extra == 'all'
|
|
34
|
+
Requires-Dist: pydantic>=2.0.0; extra == 'all'
|
|
35
|
+
Requires-Dist: sentence-transformers>=2.7.0; extra == 'all'
|
|
36
|
+
Requires-Dist: tiktoken>=0.5.0; extra == 'all'
|
|
37
|
+
Provides-Extra: dev
|
|
38
|
+
Requires-Dist: mypy; extra == 'dev'
|
|
39
|
+
Requires-Dist: openai>=1.0.0; extra == 'dev'
|
|
40
|
+
Requires-Dist: pydantic>=2.0.0; extra == 'dev'
|
|
41
|
+
Requires-Dist: pytest-cov; extra == 'dev'
|
|
42
|
+
Requires-Dist: pytest>=7.0.0; extra == 'dev'
|
|
43
|
+
Requires-Dist: ruff; extra == 'dev'
|
|
44
|
+
Requires-Dist: sentence-transformers>=2.7.0; extra == 'dev'
|
|
45
|
+
Provides-Extra: jsonschema
|
|
46
|
+
Requires-Dist: jsonschema>=4.0.0; extra == 'jsonschema'
|
|
47
|
+
Provides-Extra: judge
|
|
48
|
+
Requires-Dist: torch>=2.0.0; extra == 'judge'
|
|
49
|
+
Requires-Dist: transformers>=4.40.0; extra == 'judge'
|
|
50
|
+
Provides-Extra: local
|
|
51
|
+
Requires-Dist: sentence-transformers>=2.7.0; extra == 'local'
|
|
52
|
+
Provides-Extra: openai
|
|
53
|
+
Requires-Dist: openai>=1.0.0; extra == 'openai'
|
|
54
|
+
Provides-Extra: pydantic
|
|
55
|
+
Requires-Dist: pydantic>=2.0.0; extra == 'pydantic'
|
|
56
|
+
Provides-Extra: tiktoken
|
|
57
|
+
Requires-Dist: tiktoken>=0.5.0; extra == 'tiktoken'
|
|
58
|
+
Description-Content-Type: text/markdown
|
|
59
|
+
|
|
60
|
+
# semanticheck
|
|
61
|
+
|
|
62
|
+
**pytest-native semantic assertions for LLM and generative AI applications.**
|
|
63
|
+
|
|
64
|
+
No servers. No SaaS. No config. Works with OpenAI, Anthropic, LiteLLM, or any LLM client.
|
|
65
|
+
|
|
66
|
+
[](https://pypi.org/project/semanticheck/)
|
|
67
|
+
[](https://pypi.org/project/semanticheck/)
|
|
68
|
+
[](LICENSE)
|
|
69
|
+
[](https://github.com/semanticheck/semanticheck/actions)
|
|
70
|
+
|
|
71
|
+
---
|
|
72
|
+
|
|
73
|
+
## Installation
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
pip install semanticheck
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
With local embeddings (recommended, no API cost):
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
pip install "semanticheck[local]"
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
With OpenAI embeddings:
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
pip install "semanticheck[openai]"
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
---
|
|
92
|
+
|
|
93
|
+
## Quick Start
|
|
94
|
+
|
|
95
|
+
```python
|
|
96
|
+
from semanticheck import assert_intent, assert_tone, assert_no_hallucination, assert_no_pii
|
|
97
|
+
|
|
98
|
+
def test_customer_support_reply():
|
|
99
|
+
response = my_llm("Help me reset my password")
|
|
100
|
+
|
|
101
|
+
assert_intent(response, "instructions for resetting a password")
|
|
102
|
+
assert_tone(response, "friendly")
|
|
103
|
+
assert_no_pii(response)
|
|
104
|
+
assert_no_hallucination(response, known_facts=["Password reset links expire after 24 hours"])
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
## Assertions
|
|
110
|
+
|
|
111
|
+
| Function | What it checks |
|
|
112
|
+
|---|---|
|
|
113
|
+
| `assert_intent(response, expected_intent)` | Semantic match to expected meaning |
|
|
114
|
+
| `assert_tone(response, tone)` | Tone: professional, casual, friendly, formal, etc. |
|
|
115
|
+
| `assert_no_hallucination(response, known_facts)` | No contradiction of known facts |
|
|
116
|
+
| `assert_similar_to(response, reference)` | Cosine similarity to a reference text |
|
|
117
|
+
| `assert_token_budget(response, max_tokens)` | Response stays within token limit |
|
|
118
|
+
| `assert_schema(response, schema)` | Valid JSON Schema or Pydantic model |
|
|
119
|
+
| `assert_language(response, language)` | Written in expected language |
|
|
120
|
+
| `assert_no_pii(response)` | No emails, SSNs, credit cards, phones, etc. |
|
|
121
|
+
| `assert_readability(response, min_score=60)` | Flesch Reading Ease score |
|
|
122
|
+
| `assert_sentiment(response, "positive")` | Sentiment polarity |
|
|
123
|
+
|
|
124
|
+
---
|
|
125
|
+
|
|
126
|
+
## Baseline Regression Testing
|
|
127
|
+
|
|
128
|
+
```python
|
|
129
|
+
from semanticheck import record_baseline, compare_baseline
|
|
130
|
+
|
|
131
|
+
def test_summarizer_regression(llm_record):
|
|
132
|
+
response = my_summarizer(article)
|
|
133
|
+
if llm_record:
|
|
134
|
+
record_baseline("summarizer_v1", response)
|
|
135
|
+
else:
|
|
136
|
+
compare_baseline("summarizer_v1", response, threshold=0.85)
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Run with:
|
|
140
|
+
|
|
141
|
+
```bash
|
|
142
|
+
pytest --record-baselines # record golden baselines
|
|
143
|
+
pytest # compare against them
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
---
|
|
147
|
+
|
|
148
|
+
## LocalJudge
|
|
149
|
+
|
|
150
|
+
Use a local model as a judge — zero API cost in CI:
|
|
151
|
+
|
|
152
|
+
```python
|
|
153
|
+
from semanticheck import LocalJudge
|
|
154
|
+
|
|
155
|
+
judge = LocalJudge() # uses Qwen2.5-0.5B by default
|
|
156
|
+
result = judge.evaluate(
|
|
157
|
+
response="Paris is the capital of France.",
|
|
158
|
+
criterion="Correctly answers a geography question about European capitals.",
|
|
159
|
+
)
|
|
160
|
+
assert result.passed
|
|
161
|
+
print(result.score, result.reasoning)
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
---
|
|
165
|
+
|
|
166
|
+
## pytest Plugin
|
|
167
|
+
|
|
168
|
+
semanticheck auto-registers as a pytest plugin. Available flags:
|
|
169
|
+
|
|
170
|
+
```bash
|
|
171
|
+
pytest --skip-llm # skip all @pytest.mark.llm tests
|
|
172
|
+
pytest --llm-threshold 0.8 # override similarity threshold globally
|
|
173
|
+
pytest --record-baselines # record golden baselines
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
Available markers:
|
|
177
|
+
|
|
178
|
+
```python
|
|
179
|
+
@pytest.mark.llm # LLM semantic test
|
|
180
|
+
@pytest.mark.llm_slow # slow test using local judge inference
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
---
|
|
184
|
+
|
|
185
|
+
## Embedding Backends
|
|
186
|
+
|
|
187
|
+
| Backend | How to activate | Cost |
|
|
188
|
+
|---|---|---|
|
|
189
|
+
| sentence-transformers | `pip install semanticheck[local]` | Free |
|
|
190
|
+
| OpenAI | Set `OPENAI_API_KEY` | Per token |
|
|
191
|
+
| Hash fallback | `LLMASSERT_EMBED_BACKEND=fallback` | Free (smoke tests only) |
|
|
192
|
+
|
|
193
|
+
Override via env vars:
|
|
194
|
+
|
|
195
|
+
```bash
|
|
196
|
+
LLMASSERT_EMBED_BACKEND=openai
|
|
197
|
+
LLMASSERT_EMBED_MODEL=text-embedding-3-large
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
---
|
|
201
|
+
|
|
202
|
+
## License
|
|
203
|
+
|
|
204
|
+
MIT
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# semanticheck
|
|
2
|
+
|
|
3
|
+
**pytest-native semantic assertions for LLM and generative AI applications.**
|
|
4
|
+
|
|
5
|
+
No servers. No SaaS. No config. Works with OpenAI, Anthropic, LiteLLM, or any LLM client.
|
|
6
|
+
|
|
7
|
+
[](https://pypi.org/project/semanticheck/)
|
|
8
|
+
[](https://pypi.org/project/semanticheck/)
|
|
9
|
+
[](LICENSE)
|
|
10
|
+
[](https://github.com/semanticheck/semanticheck/actions)
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
pip install semanticheck
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
With local embeddings (recommended, no API cost):
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pip install "semanticheck[local]"
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
With OpenAI embeddings:
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
pip install "semanticheck[openai]"
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
---
|
|
33
|
+
|
|
34
|
+
## Quick Start
|
|
35
|
+
|
|
36
|
+
```python
|
|
37
|
+
from semanticheck import assert_intent, assert_tone, assert_no_hallucination, assert_no_pii
|
|
38
|
+
|
|
39
|
+
def test_customer_support_reply():
|
|
40
|
+
response = my_llm("Help me reset my password")
|
|
41
|
+
|
|
42
|
+
assert_intent(response, "instructions for resetting a password")
|
|
43
|
+
assert_tone(response, "friendly")
|
|
44
|
+
assert_no_pii(response)
|
|
45
|
+
assert_no_hallucination(response, known_facts=["Password reset links expire after 24 hours"])
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
## Assertions
|
|
51
|
+
|
|
52
|
+
| Function | What it checks |
|
|
53
|
+
|---|---|
|
|
54
|
+
| `assert_intent(response, expected_intent)` | Semantic match to expected meaning |
|
|
55
|
+
| `assert_tone(response, tone)` | Tone: professional, casual, friendly, formal, etc. |
|
|
56
|
+
| `assert_no_hallucination(response, known_facts)` | No contradiction of known facts |
|
|
57
|
+
| `assert_similar_to(response, reference)` | Cosine similarity to a reference text |
|
|
58
|
+
| `assert_token_budget(response, max_tokens)` | Response stays within token limit |
|
|
59
|
+
| `assert_schema(response, schema)` | Valid JSON Schema or Pydantic model |
|
|
60
|
+
| `assert_language(response, language)` | Written in expected language |
|
|
61
|
+
| `assert_no_pii(response)` | No emails, SSNs, credit cards, phones, etc. |
|
|
62
|
+
| `assert_readability(response, min_score=60)` | Flesch Reading Ease score |
|
|
63
|
+
| `assert_sentiment(response, "positive")` | Sentiment polarity |
|
|
64
|
+
|
|
65
|
+
---
|
|
66
|
+
|
|
67
|
+
## Baseline Regression Testing
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
from semanticheck import record_baseline, compare_baseline
|
|
71
|
+
|
|
72
|
+
def test_summarizer_regression(llm_record):
|
|
73
|
+
response = my_summarizer(article)
|
|
74
|
+
if llm_record:
|
|
75
|
+
record_baseline("summarizer_v1", response)
|
|
76
|
+
else:
|
|
77
|
+
compare_baseline("summarizer_v1", response, threshold=0.85)
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Run with:
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
pytest --record-baselines # record golden baselines
|
|
84
|
+
pytest # compare against them
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
---
|
|
88
|
+
|
|
89
|
+
## LocalJudge
|
|
90
|
+
|
|
91
|
+
Use a local model as a judge — zero API cost in CI:
|
|
92
|
+
|
|
93
|
+
```python
|
|
94
|
+
from semanticheck import LocalJudge
|
|
95
|
+
|
|
96
|
+
judge = LocalJudge() # uses Qwen2.5-0.5B by default
|
|
97
|
+
result = judge.evaluate(
|
|
98
|
+
response="Paris is the capital of France.",
|
|
99
|
+
criterion="Correctly answers a geography question about European capitals.",
|
|
100
|
+
)
|
|
101
|
+
assert result.passed
|
|
102
|
+
print(result.score, result.reasoning)
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
---
|
|
106
|
+
|
|
107
|
+
## pytest Plugin
|
|
108
|
+
|
|
109
|
+
semanticheck auto-registers as a pytest plugin. Available flags:
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
pytest --skip-llm # skip all @pytest.mark.llm tests
|
|
113
|
+
pytest --llm-threshold 0.8 # override similarity threshold globally
|
|
114
|
+
pytest --record-baselines # record golden baselines
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Available markers:
|
|
118
|
+
|
|
119
|
+
```python
|
|
120
|
+
@pytest.mark.llm # LLM semantic test
|
|
121
|
+
@pytest.mark.llm_slow # slow test using local judge inference
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
---
|
|
125
|
+
|
|
126
|
+
## Embedding Backends
|
|
127
|
+
|
|
128
|
+
| Backend | How to activate | Cost |
|
|
129
|
+
|---|---|---|
|
|
130
|
+
| sentence-transformers | `pip install semanticheck[local]` | Free |
|
|
131
|
+
| OpenAI | Set `OPENAI_API_KEY` | Per token |
|
|
132
|
+
| Hash fallback | `LLMASSERT_EMBED_BACKEND=fallback` | Free (smoke tests only) |
|
|
133
|
+
|
|
134
|
+
Override via env vars:
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
LLMASSERT_EMBED_BACKEND=openai
|
|
138
|
+
LLMASSERT_EMBED_MODEL=text-embedding-3-large
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
---
|
|
142
|
+
|
|
143
|
+
## License
|
|
144
|
+
|
|
145
|
+
MIT
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "semanticheck"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "pytest-native semantic assertions for LLM and generative AI applications. No servers. No SaaS. Works with OpenAI, Anthropic, LiteLLM and any LLM client."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { text = "MIT" }
|
|
11
|
+
requires-python = ">=3.9"
|
|
12
|
+
|
|
13
|
+
keywords = [
|
|
14
|
+
"llm",
|
|
15
|
+
"llm testing",
|
|
16
|
+
"llm assertions",
|
|
17
|
+
"llm evaluation",
|
|
18
|
+
"pytest",
|
|
19
|
+
"pytest plugin",
|
|
20
|
+
"ai testing",
|
|
21
|
+
"generative ai",
|
|
22
|
+
"semantic testing",
|
|
23
|
+
"semantic assertions",
|
|
24
|
+
"openai",
|
|
25
|
+
"anthropic",
|
|
26
|
+
"langchain",
|
|
27
|
+
"hallucination detection",
|
|
28
|
+
"prompt testing",
|
|
29
|
+
"rag testing",
|
|
30
|
+
"agent testing",
|
|
31
|
+
"ai quality assurance",
|
|
32
|
+
"machine learning testing",
|
|
33
|
+
"golden baseline",
|
|
34
|
+
"regression testing",
|
|
35
|
+
"gpt testing",
|
|
36
|
+
"claude testing",
|
|
37
|
+
"genai testing",
|
|
38
|
+
"llm quality",
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
classifiers = [
|
|
42
|
+
"Development Status :: 4 - Beta",
|
|
43
|
+
"Intended Audience :: Developers",
|
|
44
|
+
"Intended Audience :: Science/Research",
|
|
45
|
+
"License :: OSI Approved :: MIT License",
|
|
46
|
+
"Operating System :: OS Independent",
|
|
47
|
+
"Programming Language :: Python :: 3",
|
|
48
|
+
"Programming Language :: Python :: 3.9",
|
|
49
|
+
"Programming Language :: Python :: 3.10",
|
|
50
|
+
"Programming Language :: Python :: 3.11",
|
|
51
|
+
"Programming Language :: Python :: 3.12",
|
|
52
|
+
"Programming Language :: Python :: 3.13",
|
|
53
|
+
"Topic :: Software Development :: Testing",
|
|
54
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
55
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
56
|
+
"Framework :: Pytest",
|
|
57
|
+
"Typing :: Typed",
|
|
58
|
+
]
|
|
59
|
+
|
|
60
|
+
authors = [
|
|
61
|
+
{ name = "semanticheck contributors" },
|
|
62
|
+
]
|
|
63
|
+
|
|
64
|
+
dependencies = []
|
|
65
|
+
|
|
66
|
+
[project.optional-dependencies]
|
|
67
|
+
local = [
|
|
68
|
+
"sentence-transformers>=2.7.0",
|
|
69
|
+
]
|
|
70
|
+
openai = [
|
|
71
|
+
"openai>=1.0.0",
|
|
72
|
+
]
|
|
73
|
+
tiktoken = [
|
|
74
|
+
"tiktoken>=0.5.0",
|
|
75
|
+
]
|
|
76
|
+
pydantic = [
|
|
77
|
+
"pydantic>=2.0.0",
|
|
78
|
+
]
|
|
79
|
+
jsonschema = [
|
|
80
|
+
"jsonschema>=4.0.0",
|
|
81
|
+
]
|
|
82
|
+
judge = [
|
|
83
|
+
"transformers>=4.40.0",
|
|
84
|
+
"torch>=2.0.0",
|
|
85
|
+
]
|
|
86
|
+
all = [
|
|
87
|
+
"sentence-transformers>=2.7.0",
|
|
88
|
+
"openai>=1.0.0",
|
|
89
|
+
"tiktoken>=0.5.0",
|
|
90
|
+
"pydantic>=2.0.0",
|
|
91
|
+
"jsonschema>=4.0.0",
|
|
92
|
+
]
|
|
93
|
+
dev = [
|
|
94
|
+
"pytest>=7.0.0",
|
|
95
|
+
"pytest-cov",
|
|
96
|
+
"sentence-transformers>=2.7.0",
|
|
97
|
+
"pydantic>=2.0.0",
|
|
98
|
+
"openai>=1.0.0",
|
|
99
|
+
"ruff",
|
|
100
|
+
"mypy",
|
|
101
|
+
]
|
|
102
|
+
|
|
103
|
+
[project.urls]
|
|
104
|
+
Homepage = "https://github.com/semanticheck/semanticheck"
|
|
105
|
+
Documentation = "https://github.com/semanticheck/semanticheck#readme"
|
|
106
|
+
Repository = "https://github.com/semanticheck/semanticheck"
|
|
107
|
+
"Bug Tracker" = "https://github.com/semanticheck/semanticheck/issues"
|
|
108
|
+
Changelog = "https://github.com/semanticheck/semanticheck/blob/main/CHANGELOG.md"
|
|
109
|
+
|
|
110
|
+
[project.entry-points."pytest11"]
|
|
111
|
+
semanticheck = "semanticheck.plugin"
|
|
112
|
+
|
|
113
|
+
[tool.hatch.build.targets.wheel]
|
|
114
|
+
packages = ["semanticheck"]
|
|
115
|
+
|
|
116
|
+
[tool.hatch.build.targets.sdist]
|
|
117
|
+
include = [
|
|
118
|
+
"semanticheck/",
|
|
119
|
+
"tests/",
|
|
120
|
+
"README.md",
|
|
121
|
+
"LICENSE",
|
|
122
|
+
"CHANGELOG.md",
|
|
123
|
+
]
|
|
124
|
+
|
|
125
|
+
[tool.ruff]
|
|
126
|
+
line-length = 88
|
|
127
|
+
target-version = "py39"
|
|
128
|
+
|
|
129
|
+
[tool.ruff.lint]
|
|
130
|
+
select = ["E", "F", "I", "UP"]
|
|
131
|
+
ignore = ["E501"]
|
|
132
|
+
|
|
133
|
+
[tool.mypy]
|
|
134
|
+
python_version = "3.9"
|
|
135
|
+
strict = false
|
|
136
|
+
ignore_missing_imports = true
|
|
137
|
+
|
|
138
|
+
[tool.pytest.ini_options]
|
|
139
|
+
testpaths = ["tests"]
|
|
140
|
+
addopts = "-v --tb=short"
|
|
141
|
+
markers = [
|
|
142
|
+
"llm: LLM semantic tests (may call embedding APIs)",
|
|
143
|
+
"llm_slow: slow tests using local judge model inference",
|
|
144
|
+
]
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""
|
|
2
|
+
semanticheck — pytest-native semantic testing for LLM and generative AI applications.
|
|
3
|
+
|
|
4
|
+
Drop-in pytest plugin. No servers. No SaaS. No config.
|
|
5
|
+
Works with OpenAI, Anthropic, LiteLLM, or any LLM client.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from semanticheck.assertions.intent import assert_intent
|
|
9
|
+
from semanticheck.assertions.tone import assert_tone
|
|
10
|
+
from semanticheck.assertions.hallucination import assert_no_hallucination
|
|
11
|
+
from semanticheck.assertions.budget import assert_token_budget
|
|
12
|
+
from semanticheck.assertions.schema import assert_schema
|
|
13
|
+
from semanticheck.assertions.similarity import assert_similar_to
|
|
14
|
+
from semanticheck.assertions.language import assert_language
|
|
15
|
+
from semanticheck.assertions.pii import assert_no_pii, PIIMatch
|
|
16
|
+
from semanticheck.assertions.readability import assert_readability
|
|
17
|
+
from semanticheck.assertions.sentiment import assert_sentiment
|
|
18
|
+
from semanticheck.baseline import record_baseline, compare_baseline
|
|
19
|
+
from semanticheck.judge import LocalJudge
|
|
20
|
+
|
|
21
|
+
__version__ = "0.1.0"
|
|
22
|
+
__all__ = [
|
|
23
|
+
"assert_intent",
|
|
24
|
+
"assert_tone",
|
|
25
|
+
"assert_no_hallucination",
|
|
26
|
+
"assert_similar_to",
|
|
27
|
+
"assert_token_budget",
|
|
28
|
+
"assert_schema",
|
|
29
|
+
"assert_language",
|
|
30
|
+
"assert_no_pii",
|
|
31
|
+
"assert_readability",
|
|
32
|
+
"assert_sentiment",
|
|
33
|
+
"PIIMatch",
|
|
34
|
+
"record_baseline",
|
|
35
|
+
"compare_baseline",
|
|
36
|
+
"LocalJudge",
|
|
37
|
+
]
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Embedding backend for semanticheck.
|
|
3
|
+
|
|
4
|
+
Priority order (auto-detected):
|
|
5
|
+
1. sentence-transformers (local, free, fast) — recommended
|
|
6
|
+
2. openai embeddings (requires OPENAI_API_KEY)
|
|
7
|
+
3. hash-based fallback (no deps, smoke tests only)
|
|
8
|
+
|
|
9
|
+
Set LLMASSERT_EMBED_BACKEND=openai|local|fallback to force a backend.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
import os
|
|
14
|
+
import hashlib
|
|
15
|
+
import math
|
|
16
|
+
from functools import lru_cache
|
|
17
|
+
|
|
18
|
+
_BACKEND_ENV = "LLMASSERT_EMBED_BACKEND"
|
|
19
|
+
_MODEL_ENV = "LLMASSERT_EMBED_MODEL"
|
|
20
|
+
_DEFAULT_LOCAL_MODEL = "all-MiniLM-L6-v2"
|
|
21
|
+
_DEFAULT_OPENAI_MODEL = "text-embedding-3-small"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@lru_cache(maxsize=512)
|
|
25
|
+
def embed_text(text: str) -> tuple[float, ...]:
|
|
26
|
+
"""Embed `text` into a float vector. Results are cached in-process."""
|
|
27
|
+
backend = os.environ.get(_BACKEND_ENV, "auto").lower()
|
|
28
|
+
if backend == "openai":
|
|
29
|
+
return _embed_openai(text)
|
|
30
|
+
elif backend in ("local", "sentence-transformers"):
|
|
31
|
+
return _embed_local(text)
|
|
32
|
+
elif backend == "fallback":
|
|
33
|
+
return _embed_fallback(text)
|
|
34
|
+
return _embed_auto(text)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _embed_auto(text: str) -> tuple[float, ...]:
|
|
38
|
+
try:
|
|
39
|
+
return _embed_local(text)
|
|
40
|
+
except ImportError:
|
|
41
|
+
pass
|
|
42
|
+
try:
|
|
43
|
+
return _embed_openai(text)
|
|
44
|
+
except (ImportError, RuntimeError):
|
|
45
|
+
pass
|
|
46
|
+
return _embed_fallback(text)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _embed_local(text: str) -> tuple[float, ...]:
|
|
50
|
+
try:
|
|
51
|
+
from sentence_transformers import SentenceTransformer # noqa: F401
|
|
52
|
+
except ImportError:
|
|
53
|
+
raise ImportError(
|
|
54
|
+
"sentence-transformers not installed.\n"
|
|
55
|
+
" pip install sentence-transformers\n"
|
|
56
|
+
" Or set LLMASSERT_EMBED_BACKEND=openai"
|
|
57
|
+
)
|
|
58
|
+
model_name = os.environ.get(_MODEL_ENV, _DEFAULT_LOCAL_MODEL)
|
|
59
|
+
model = _get_local_model(model_name)
|
|
60
|
+
vec = model.encode(text, normalize_embeddings=True)
|
|
61
|
+
return tuple(float(v) for v in vec)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
@lru_cache(maxsize=4)
|
|
65
|
+
def _get_local_model(model_name: str):
|
|
66
|
+
from sentence_transformers import SentenceTransformer
|
|
67
|
+
return SentenceTransformer(model_name)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def _embed_openai(text: str) -> tuple[float, ...]:
|
|
71
|
+
try:
|
|
72
|
+
import openai
|
|
73
|
+
except ImportError:
|
|
74
|
+
raise ImportError("openai not installed. pip install openai")
|
|
75
|
+
api_key = os.environ.get("OPENAI_API_KEY")
|
|
76
|
+
if not api_key:
|
|
77
|
+
raise RuntimeError("OPENAI_API_KEY not set. Set it or use LLMASSERT_EMBED_BACKEND=local")
|
|
78
|
+
model = os.environ.get(_MODEL_ENV, _DEFAULT_OPENAI_MODEL)
|
|
79
|
+
client = openai.OpenAI(api_key=api_key)
|
|
80
|
+
result = client.embeddings.create(input=[text], model=model)
|
|
81
|
+
return tuple(float(v) for v in result.data[0].embedding)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _embed_fallback(text: str) -> tuple[float, ...]:
|
|
85
|
+
"""Hash-based pseudo-embedding. Smoke tests only — no real semantic similarity."""
|
|
86
|
+
dim = 384
|
|
87
|
+
vec = []
|
|
88
|
+
for i in range(dim):
|
|
89
|
+
seed = hashlib.sha256(f"{i}:{text}".encode()).digest()
|
|
90
|
+
val = int.from_bytes(seed[:4], "big") / (2**32)
|
|
91
|
+
vec.append(val - 0.5)
|
|
92
|
+
mag = math.sqrt(sum(v**2 for v in vec))
|
|
93
|
+
return tuple(v / mag for v in vec)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# assertions package
|