metergraphrelay 0.7.7__tar.gz → 0.7.9__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.
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/PKG-INFO +41 -4
- metergraphrelay-0.7.7/src/metergraphrelay.egg-info/PKG-INFO → metergraphrelay-0.7.9/README.md +37 -23
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/pyproject.toml +4 -4
- metergraphrelay-0.7.9/src/metergraphrelay/__init__.py +1 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/src/metergraphrelay/cli.py +20 -0
- metergraphrelay-0.7.9/src/metergraphrelay/filtering.py +151 -0
- metergraphrelay-0.7.7/README.md → metergraphrelay-0.7.9/src/metergraphrelay.egg-info/PKG-INFO +60 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/src/metergraphrelay.egg-info/SOURCES.txt +2 -0
- metergraphrelay-0.7.9/tests/test_filtering.py +107 -0
- metergraphrelay-0.7.7/src/metergraphrelay/__init__.py +0 -1
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/LICENSE +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/setup.cfg +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/src/metergraphrelay/billing_evidence.py +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/src/metergraphrelay/capture_contract.py +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/src/metergraphrelay/config.py +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/src/metergraphrelay/demo.py +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/src/metergraphrelay/finish_reason.py +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/src/metergraphrelay/http_limits.py +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/src/metergraphrelay/import_identity.py +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/src/metergraphrelay/metergraph_sync.py +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/src/metergraphrelay/portkey_sync.py +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/src/metergraphrelay/provider_sync.py +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/src/metergraphrelay/providers/__init__.py +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/src/metergraphrelay/providers/braintrust.py +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/src/metergraphrelay/providers/cache_shape.py +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/src/metergraphrelay/providers/langfuse.py +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/src/metergraphrelay/providers/langsmith.py +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/src/metergraphrelay/providers/openai.py +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/src/metergraphrelay/providers/phoenix.py +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/src/metergraphrelay/providers/portkey.py +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/src/metergraphrelay/providers/portkey_batch.py +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/src/metergraphrelay/providers/portkey_export.py +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/src/metergraphrelay/push.py +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/src/metergraphrelay/py.typed +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/src/metergraphrelay/reliability.py +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/src/metergraphrelay/sync_core.py +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/src/metergraphrelay/window.py +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/src/metergraphrelay.egg-info/dependency_links.txt +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/src/metergraphrelay.egg-info/entry_points.txt +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/src/metergraphrelay.egg-info/requires.txt +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/src/metergraphrelay.egg-info/top_level.txt +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/tests/test_billing_evidence.py +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/tests/test_capture_contract.py +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/tests/test_cli.py +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/tests/test_config.py +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/tests/test_demo.py +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/tests/test_finish_reason.py +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/tests/test_metergraph_sync.py +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/tests/test_portkey_sync.py +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/tests/test_provider_sync.py +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/tests/test_push.py +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/tests/test_reliability.py +0 -0
- {metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/tests/test_window.py +0 -0
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: metergraphrelay
|
|
3
|
-
Version: 0.7.
|
|
3
|
+
Version: 0.7.9
|
|
4
4
|
Summary: Move LLM trace data between systems: pull from providers, push to metergraph.
|
|
5
5
|
Author: Vasiliy Radostev
|
|
6
6
|
License-Expression: MIT
|
|
7
|
-
Project-URL: Homepage, https://github.com/
|
|
8
|
-
Project-URL: Repository, https://github.com/
|
|
9
|
-
Project-URL: Issues, https://github.com/
|
|
7
|
+
Project-URL: Homepage, https://github.com/metergraph/metergraphrelay
|
|
8
|
+
Project-URL: Repository, https://github.com/metergraph/metergraphrelay
|
|
9
|
+
Project-URL: Issues, https://github.com/metergraph/metergraphrelay/issues
|
|
10
10
|
Keywords: llm,openai,observability,cost-tracking,cli,metergraph
|
|
11
11
|
Classifier: Intended Audience :: Developers
|
|
12
12
|
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
@@ -695,3 +695,40 @@ never runs in CI.
|
|
|
695
695
|
Relay extracts provider status, error codes, finish reasons and explicit refusal
|
|
696
696
|
signals before content conversion. Metergraph receivers use Core to classify
|
|
697
697
|
availability and refusals; Relay does not depend on Core or classify outcomes.
|
|
698
|
+
|
|
699
|
+
## TypeScript / Node.js
|
|
700
|
+
|
|
701
|
+
A small forwarding library is available in [`typescript/`](typescript/README.md). It sends normalized records from application-owned export jobs; the Python CLI remains the provider import/sync tool.
|
|
702
|
+
|
|
703
|
+
## Import exclusions
|
|
704
|
+
|
|
705
|
+
Filter normalized JSONL using rules supplied by your CLI or application. A record
|
|
706
|
+
is excluded when it matches every condition in any rule. Each rule requires an
|
|
707
|
+
exact provider, workload (`route`) and inclusive `timestamp_gte`; optional
|
|
708
|
+
`timestamp_lt` is exclusive, and `tags` matches exact string pairs.
|
|
709
|
+
|
|
710
|
+
```json
|
|
711
|
+
{"version":1,"rules":[{"id":"audit","conditions":{
|
|
712
|
+
"provider":"anthropic","route":"audit","timestamp_gte":"2026-01-01T00:00:00Z"
|
|
713
|
+
}}]}
|
|
714
|
+
```
|
|
715
|
+
|
|
716
|
+
```sh
|
|
717
|
+
metergraphrelay filter traces.jsonl --rules rules.json --output preview.jsonl --preview
|
|
718
|
+
metergraphrelay filter traces.jsonl --rules rules.json --output filtered.jsonl
|
|
719
|
+
```
|
|
720
|
+
|
|
721
|
+
Preview retains all records and reports matches. Normal mode excludes matching
|
|
722
|
+
records. Results include kept/excluded counts, matches per rule and a policy
|
|
723
|
+
fingerprint. Output is replaced only after the complete file succeeds.
|
|
724
|
+
|
|
725
|
+
```python
|
|
726
|
+
from metergraphrelay.filtering import ExclusionPolicy
|
|
727
|
+
|
|
728
|
+
policy = ExclusionPolicy.from_dict(rules)
|
|
729
|
+
matched_rule = policy.evaluate(normalized_record) # rule ID or None
|
|
730
|
+
```
|
|
731
|
+
|
|
732
|
+
Policies support up to 100 rules and 10 tag conditions per rule. JSONL processing
|
|
733
|
+
streams records with a 16 MiB limit per record. Invalid policies or records raise
|
|
734
|
+
an error.
|
metergraphrelay-0.7.7/src/metergraphrelay.egg-info/PKG-INFO → metergraphrelay-0.7.9/README.md
RENAMED
|
@@ -1,26 +1,3 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: metergraphrelay
|
|
3
|
-
Version: 0.7.7
|
|
4
|
-
Summary: Move LLM trace data between systems: pull from providers, push to metergraph.
|
|
5
|
-
Author: Vasiliy Radostev
|
|
6
|
-
License-Expression: MIT
|
|
7
|
-
Project-URL: Homepage, https://github.com/VasiliyRad/metergraphrelay
|
|
8
|
-
Project-URL: Repository, https://github.com/VasiliyRad/metergraphrelay
|
|
9
|
-
Project-URL: Issues, https://github.com/VasiliyRad/metergraphrelay/issues
|
|
10
|
-
Keywords: llm,openai,observability,cost-tracking,cli,metergraph
|
|
11
|
-
Classifier: Intended Audience :: Developers
|
|
12
|
-
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
13
|
-
Classifier: Environment :: Console
|
|
14
|
-
Classifier: Typing :: Typed
|
|
15
|
-
Requires-Python: >=3.10
|
|
16
|
-
Description-Content-Type: text/markdown
|
|
17
|
-
License-File: LICENSE
|
|
18
|
-
Requires-Dist: openai>=1.63.0
|
|
19
|
-
Requires-Dist: python-dotenv>=1.0
|
|
20
|
-
Provides-Extra: dev
|
|
21
|
-
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
22
|
-
Dynamic: license-file
|
|
23
|
-
|
|
24
1
|
# metergraphrelay
|
|
25
2
|
|
|
26
3
|
Try [metergraph](https://www.metergraph.dev/) without installing its SDK:
|
|
@@ -695,3 +672,40 @@ never runs in CI.
|
|
|
695
672
|
Relay extracts provider status, error codes, finish reasons and explicit refusal
|
|
696
673
|
signals before content conversion. Metergraph receivers use Core to classify
|
|
697
674
|
availability and refusals; Relay does not depend on Core or classify outcomes.
|
|
675
|
+
|
|
676
|
+
## TypeScript / Node.js
|
|
677
|
+
|
|
678
|
+
A small forwarding library is available in [`typescript/`](typescript/README.md). It sends normalized records from application-owned export jobs; the Python CLI remains the provider import/sync tool.
|
|
679
|
+
|
|
680
|
+
## Import exclusions
|
|
681
|
+
|
|
682
|
+
Filter normalized JSONL using rules supplied by your CLI or application. A record
|
|
683
|
+
is excluded when it matches every condition in any rule. Each rule requires an
|
|
684
|
+
exact provider, workload (`route`) and inclusive `timestamp_gte`; optional
|
|
685
|
+
`timestamp_lt` is exclusive, and `tags` matches exact string pairs.
|
|
686
|
+
|
|
687
|
+
```json
|
|
688
|
+
{"version":1,"rules":[{"id":"audit","conditions":{
|
|
689
|
+
"provider":"anthropic","route":"audit","timestamp_gte":"2026-01-01T00:00:00Z"
|
|
690
|
+
}}]}
|
|
691
|
+
```
|
|
692
|
+
|
|
693
|
+
```sh
|
|
694
|
+
metergraphrelay filter traces.jsonl --rules rules.json --output preview.jsonl --preview
|
|
695
|
+
metergraphrelay filter traces.jsonl --rules rules.json --output filtered.jsonl
|
|
696
|
+
```
|
|
697
|
+
|
|
698
|
+
Preview retains all records and reports matches. Normal mode excludes matching
|
|
699
|
+
records. Results include kept/excluded counts, matches per rule and a policy
|
|
700
|
+
fingerprint. Output is replaced only after the complete file succeeds.
|
|
701
|
+
|
|
702
|
+
```python
|
|
703
|
+
from metergraphrelay.filtering import ExclusionPolicy
|
|
704
|
+
|
|
705
|
+
policy = ExclusionPolicy.from_dict(rules)
|
|
706
|
+
matched_rule = policy.evaluate(normalized_record) # rule ID or None
|
|
707
|
+
```
|
|
708
|
+
|
|
709
|
+
Policies support up to 100 rules and 10 tag conditions per rule. JSONL processing
|
|
710
|
+
streams records with a 16 MiB limit per record. Invalid policies or records raise
|
|
711
|
+
an error.
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "metergraphrelay"
|
|
7
|
-
version = "0.7.
|
|
7
|
+
version = "0.7.9"
|
|
8
8
|
description = "Move LLM trace data between systems: pull from providers, push to metergraph."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.10"
|
|
@@ -23,9 +23,9 @@ dependencies = [
|
|
|
23
23
|
]
|
|
24
24
|
|
|
25
25
|
[project.urls]
|
|
26
|
-
Homepage = "https://github.com/
|
|
27
|
-
Repository = "https://github.com/
|
|
28
|
-
Issues = "https://github.com/
|
|
26
|
+
Homepage = "https://github.com/metergraph/metergraphrelay"
|
|
27
|
+
Repository = "https://github.com/metergraph/metergraphrelay"
|
|
28
|
+
Issues = "https://github.com/metergraph/metergraphrelay/issues"
|
|
29
29
|
|
|
30
30
|
[project.optional-dependencies]
|
|
31
31
|
dev = ["pytest>=8.0"]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.7.9"
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
3
|
import argparse
|
|
4
|
+
from dataclasses import asdict
|
|
5
|
+
import json
|
|
4
6
|
import os
|
|
5
7
|
import sys
|
|
6
8
|
import tempfile
|
|
@@ -699,6 +701,12 @@ def build_parser() -> argparse.ArgumentParser:
|
|
|
699
701
|
push_parser.add_argument("file")
|
|
700
702
|
push_parser.add_argument("--env-file", default=".env")
|
|
701
703
|
|
|
704
|
+
filter_parser = subparsers.add_parser('filter', help='Filter normalized JSONL using caller-supplied exclusion rules; no network')
|
|
705
|
+
filter_parser.add_argument('file')
|
|
706
|
+
filter_parser.add_argument('--rules', required=True, help='Version 1 exclusion policy JSON file')
|
|
707
|
+
filter_parser.add_argument('--output', required=True)
|
|
708
|
+
filter_parser.add_argument('--preview', action='store_true', help='Report matches but retain every record in output')
|
|
709
|
+
|
|
702
710
|
return parser
|
|
703
711
|
|
|
704
712
|
|
|
@@ -1285,6 +1293,18 @@ def main(argv: list[str] | None = None) -> int:
|
|
|
1285
1293
|
parser = build_parser()
|
|
1286
1294
|
args = parser.parse_args(argv)
|
|
1287
1295
|
|
|
1296
|
+
if args.command == 'filter':
|
|
1297
|
+
from pathlib import Path
|
|
1298
|
+
from .filtering import ExclusionPolicy, filter_jsonl
|
|
1299
|
+
try:
|
|
1300
|
+
policy = ExclusionPolicy.from_dict(json.loads(Path(args.rules).read_text()))
|
|
1301
|
+
result = filter_jsonl(args.file, args.output, policy, preview=args.preview)
|
|
1302
|
+
except (ValueError, OSError) as exc:
|
|
1303
|
+
print(f'Filter failed: {exc}', file=sys.stderr)
|
|
1304
|
+
return 1
|
|
1305
|
+
print(json.dumps(asdict(result)))
|
|
1306
|
+
return 0
|
|
1307
|
+
|
|
1288
1308
|
if args.command == "pull" and args.provider == "anthropic":
|
|
1289
1309
|
try:
|
|
1290
1310
|
require_credentials(args.provider, args.env_file)
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
"""Caller-supplied exclusions for normalized records. No network or policy discovery."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from collections import Counter
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from datetime import datetime, timezone
|
|
7
|
+
import hashlib
|
|
8
|
+
import json
|
|
9
|
+
import os
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
import tempfile
|
|
12
|
+
from typing import Any
|
|
13
|
+
|
|
14
|
+
MAX_RULES = 100
|
|
15
|
+
MAX_LINE_BYTES = 16 * 1024 * 1024
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _time(value: Any) -> datetime:
|
|
19
|
+
if not isinstance(value, str):
|
|
20
|
+
raise ValueError('timestamps must be timezone-aware ISO 8601 strings')
|
|
21
|
+
try:
|
|
22
|
+
result = datetime.fromisoformat(value.replace('Z', '+00:00'))
|
|
23
|
+
except ValueError:
|
|
24
|
+
raise ValueError('timestamps must be timezone-aware ISO 8601 strings') from None
|
|
25
|
+
if result.tzinfo is None:
|
|
26
|
+
raise ValueError('timestamps must include a timezone')
|
|
27
|
+
return result.astimezone(timezone.utc)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _text(value: Any) -> str:
|
|
31
|
+
if not isinstance(value, str) or not value.strip() or len(value) > 512:
|
|
32
|
+
raise ValueError('rule values must be nonempty strings of at most 512 characters')
|
|
33
|
+
return value
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@dataclass(frozen=True)
|
|
37
|
+
class ExclusionPolicy:
|
|
38
|
+
"""An immutable, validated snapshot; rules OR together, conditions AND together."""
|
|
39
|
+
_json: str
|
|
40
|
+
|
|
41
|
+
@classmethod
|
|
42
|
+
def from_dict(cls, value: Any = None) -> ExclusionPolicy:
|
|
43
|
+
if value is None:
|
|
44
|
+
value = {'version': 1, 'rules': []}
|
|
45
|
+
if not isinstance(value, dict) or set(value) != {'version', 'rules'} or type(value['version']) is not int or value['version'] != 1:
|
|
46
|
+
raise ValueError('expected exclusion policy version 1 with rules')
|
|
47
|
+
rules = value['rules']
|
|
48
|
+
if not isinstance(rules, list) or len(rules) > MAX_RULES:
|
|
49
|
+
raise ValueError('rules must be a list of at most 100 rules')
|
|
50
|
+
seen = set()
|
|
51
|
+
normalized = []
|
|
52
|
+
for rule in rules:
|
|
53
|
+
if not isinstance(rule, dict) or set(rule) != {'id', 'conditions'}:
|
|
54
|
+
raise ValueError('each rule needs id and conditions')
|
|
55
|
+
name = _text(rule['id'])
|
|
56
|
+
if name in seen:
|
|
57
|
+
raise ValueError('rule IDs must be unique')
|
|
58
|
+
seen.add(name)
|
|
59
|
+
c = rule['conditions']
|
|
60
|
+
required = {'provider', 'route', 'timestamp_gte'}
|
|
61
|
+
if not isinstance(c, dict) or not required <= set(c) or set(c) - (required | {'timestamp_lt', 'tags'}):
|
|
62
|
+
raise ValueError('conditions require provider, route and timestamp_gte; only timestamp_lt and tags are optional')
|
|
63
|
+
c = dict(c)
|
|
64
|
+
_text(c['provider'])
|
|
65
|
+
_text(c['route'])
|
|
66
|
+
c['timestamp_gte'] = _time(c['timestamp_gte']).isoformat()
|
|
67
|
+
if 'timestamp_lt' in c:
|
|
68
|
+
c['timestamp_lt'] = _time(c['timestamp_lt']).isoformat()
|
|
69
|
+
if c['timestamp_lt'] <= c['timestamp_gte']:
|
|
70
|
+
raise ValueError('timestamp_lt must be after timestamp_gte')
|
|
71
|
+
if 'tags' in c:
|
|
72
|
+
if not isinstance(c['tags'], dict) or len(c['tags']) > 10:
|
|
73
|
+
raise ValueError('tags must contain at most 10 exact string pairs')
|
|
74
|
+
for k, v in c['tags'].items():
|
|
75
|
+
_text(k)
|
|
76
|
+
_text(v)
|
|
77
|
+
normalized.append({'id': name, 'conditions': c})
|
|
78
|
+
return cls(json.dumps({'version': 1, 'rules': normalized}, sort_keys=True, separators=(',', ':')))
|
|
79
|
+
|
|
80
|
+
@property
|
|
81
|
+
def digest(self) -> str:
|
|
82
|
+
return hashlib.sha256(self._json.encode()).hexdigest()
|
|
83
|
+
|
|
84
|
+
def to_dict(self) -> dict:
|
|
85
|
+
return json.loads(self._json)
|
|
86
|
+
|
|
87
|
+
def evaluate(self, row: dict) -> str | None:
|
|
88
|
+
"""Return the first matching rule ID, or None. Never evaluate arbitrary code."""
|
|
89
|
+
for rule in self.to_dict()['rules']:
|
|
90
|
+
c = rule['conditions']
|
|
91
|
+
if row.get('provider') != c['provider'] or row.get('route') != c['route']:
|
|
92
|
+
continue
|
|
93
|
+
ts = _time(row.get('ts'))
|
|
94
|
+
if ts < _time(c['timestamp_gte']) or ('timestamp_lt' in c and ts >= _time(c['timestamp_lt'])):
|
|
95
|
+
continue
|
|
96
|
+
tags = row.get('tags') or {}
|
|
97
|
+
if not isinstance(tags, dict) or any(tags.get(k) != v for k, v in c.get('tags', {}).items()):
|
|
98
|
+
continue
|
|
99
|
+
return rule['id']
|
|
100
|
+
return None
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
@dataclass(frozen=True)
|
|
104
|
+
class FilterResult:
|
|
105
|
+
included: int
|
|
106
|
+
excluded: int
|
|
107
|
+
matched_by_rule: dict[str, int]
|
|
108
|
+
policy_digest: str
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def filter_jsonl(source, output, policy: ExclusionPolicy | dict | None = None, *, preview=False) -> FilterResult:
|
|
112
|
+
"""Stream normalized JSONL to another file. Preview retains every valid row.
|
|
113
|
+
|
|
114
|
+
Invalid rows abort without replacing output. File processing performs no uploads,
|
|
115
|
+
checkpoint changes or rule fetching. The caller owns retention and policy selection.
|
|
116
|
+
"""
|
|
117
|
+
source, output = Path(source), Path(output)
|
|
118
|
+
if source.resolve() == output.resolve():
|
|
119
|
+
raise ValueError('input and output must be different files')
|
|
120
|
+
policy = policy if isinstance(policy, ExclusionPolicy) else ExclusionPolicy.from_dict(policy)
|
|
121
|
+
included = excluded = 0
|
|
122
|
+
matched: Counter = Counter()
|
|
123
|
+
temporary = None
|
|
124
|
+
try:
|
|
125
|
+
with source.open('rb') as inp, tempfile.NamedTemporaryFile(dir=output.parent, delete=False) as out:
|
|
126
|
+
temporary = Path(out.name)
|
|
127
|
+
while line := inp.readline(MAX_LINE_BYTES + 1):
|
|
128
|
+
if len(line) > MAX_LINE_BYTES:
|
|
129
|
+
raise ValueError('JSONL row exceeds 16 MiB limit')
|
|
130
|
+
if not line.strip():
|
|
131
|
+
continue
|
|
132
|
+
try:
|
|
133
|
+
row = json.loads(line)
|
|
134
|
+
except (ValueError, UnicodeError):
|
|
135
|
+
raise ValueError('invalid normalized JSONL record') from None
|
|
136
|
+
if not isinstance(row, dict):
|
|
137
|
+
raise ValueError('normalized JSONL records must be objects')
|
|
138
|
+
rule = policy.evaluate(row)
|
|
139
|
+
if rule:
|
|
140
|
+
matched[rule] += 1
|
|
141
|
+
if rule and not preview:
|
|
142
|
+
excluded += 1
|
|
143
|
+
else:
|
|
144
|
+
included += 1
|
|
145
|
+
out.write(line if line.endswith(b'\n') else line + b'\n')
|
|
146
|
+
os.replace(temporary, output)
|
|
147
|
+
temporary = None
|
|
148
|
+
finally:
|
|
149
|
+
if temporary is not None:
|
|
150
|
+
temporary.unlink(missing_ok=True)
|
|
151
|
+
return FilterResult(included, excluded, dict(matched), policy.digest)
|
metergraphrelay-0.7.7/README.md → metergraphrelay-0.7.9/src/metergraphrelay.egg-info/PKG-INFO
RENAMED
|
@@ -1,3 +1,26 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: metergraphrelay
|
|
3
|
+
Version: 0.7.9
|
|
4
|
+
Summary: Move LLM trace data between systems: pull from providers, push to metergraph.
|
|
5
|
+
Author: Vasiliy Radostev
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/metergraph/metergraphrelay
|
|
8
|
+
Project-URL: Repository, https://github.com/metergraph/metergraphrelay
|
|
9
|
+
Project-URL: Issues, https://github.com/metergraph/metergraphrelay/issues
|
|
10
|
+
Keywords: llm,openai,observability,cost-tracking,cli,metergraph
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
13
|
+
Classifier: Environment :: Console
|
|
14
|
+
Classifier: Typing :: Typed
|
|
15
|
+
Requires-Python: >=3.10
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
Requires-Dist: openai>=1.63.0
|
|
19
|
+
Requires-Dist: python-dotenv>=1.0
|
|
20
|
+
Provides-Extra: dev
|
|
21
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
22
|
+
Dynamic: license-file
|
|
23
|
+
|
|
1
24
|
# metergraphrelay
|
|
2
25
|
|
|
3
26
|
Try [metergraph](https://www.metergraph.dev/) without installing its SDK:
|
|
@@ -672,3 +695,40 @@ never runs in CI.
|
|
|
672
695
|
Relay extracts provider status, error codes, finish reasons and explicit refusal
|
|
673
696
|
signals before content conversion. Metergraph receivers use Core to classify
|
|
674
697
|
availability and refusals; Relay does not depend on Core or classify outcomes.
|
|
698
|
+
|
|
699
|
+
## TypeScript / Node.js
|
|
700
|
+
|
|
701
|
+
A small forwarding library is available in [`typescript/`](typescript/README.md). It sends normalized records from application-owned export jobs; the Python CLI remains the provider import/sync tool.
|
|
702
|
+
|
|
703
|
+
## Import exclusions
|
|
704
|
+
|
|
705
|
+
Filter normalized JSONL using rules supplied by your CLI or application. A record
|
|
706
|
+
is excluded when it matches every condition in any rule. Each rule requires an
|
|
707
|
+
exact provider, workload (`route`) and inclusive `timestamp_gte`; optional
|
|
708
|
+
`timestamp_lt` is exclusive, and `tags` matches exact string pairs.
|
|
709
|
+
|
|
710
|
+
```json
|
|
711
|
+
{"version":1,"rules":[{"id":"audit","conditions":{
|
|
712
|
+
"provider":"anthropic","route":"audit","timestamp_gte":"2026-01-01T00:00:00Z"
|
|
713
|
+
}}]}
|
|
714
|
+
```
|
|
715
|
+
|
|
716
|
+
```sh
|
|
717
|
+
metergraphrelay filter traces.jsonl --rules rules.json --output preview.jsonl --preview
|
|
718
|
+
metergraphrelay filter traces.jsonl --rules rules.json --output filtered.jsonl
|
|
719
|
+
```
|
|
720
|
+
|
|
721
|
+
Preview retains all records and reports matches. Normal mode excludes matching
|
|
722
|
+
records. Results include kept/excluded counts, matches per rule and a policy
|
|
723
|
+
fingerprint. Output is replaced only after the complete file succeeds.
|
|
724
|
+
|
|
725
|
+
```python
|
|
726
|
+
from metergraphrelay.filtering import ExclusionPolicy
|
|
727
|
+
|
|
728
|
+
policy = ExclusionPolicy.from_dict(rules)
|
|
729
|
+
matched_rule = policy.evaluate(normalized_record) # rule ID or None
|
|
730
|
+
```
|
|
731
|
+
|
|
732
|
+
Policies support up to 100 rules and 10 tag conditions per rule. JSONL processing
|
|
733
|
+
streams records with a 16 MiB limit per record. Invalid policies or records raise
|
|
734
|
+
an error.
|
|
@@ -7,6 +7,7 @@ src/metergraphrelay/capture_contract.py
|
|
|
7
7
|
src/metergraphrelay/cli.py
|
|
8
8
|
src/metergraphrelay/config.py
|
|
9
9
|
src/metergraphrelay/demo.py
|
|
10
|
+
src/metergraphrelay/filtering.py
|
|
10
11
|
src/metergraphrelay/finish_reason.py
|
|
11
12
|
src/metergraphrelay/http_limits.py
|
|
12
13
|
src/metergraphrelay/import_identity.py
|
|
@@ -39,6 +40,7 @@ tests/test_capture_contract.py
|
|
|
39
40
|
tests/test_cli.py
|
|
40
41
|
tests/test_config.py
|
|
41
42
|
tests/test_demo.py
|
|
43
|
+
tests/test_filtering.py
|
|
42
44
|
tests/test_finish_reason.py
|
|
43
45
|
tests/test_metergraph_sync.py
|
|
44
46
|
tests/test_portkey_sync.py
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import json
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
|
|
5
|
+
from metergraphrelay.filtering import ExclusionPolicy, filter_jsonl
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def policy(**condition):
|
|
9
|
+
return {'version': 1, 'rules': [{'id': 'direct-coverage', 'conditions': {
|
|
10
|
+
'provider': 'anthropic', 'route': 'audit', 'timestamp_gte': '2026-01-01T00:00:00Z',
|
|
11
|
+
**condition}}]}
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def row(**changes):
|
|
15
|
+
return {'provider': 'anthropic', 'route': 'audit', 'ts': '2026-01-01T00:00:00Z', **changes}
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def test_empty_default_and_exact_inclusive_cutover():
|
|
19
|
+
assert ExclusionPolicy.from_dict(None).evaluate(row()) is None
|
|
20
|
+
p = ExclusionPolicy.from_dict(policy())
|
|
21
|
+
assert p.evaluate(row()) == 'direct-coverage'
|
|
22
|
+
assert p.evaluate(row(ts='2025-12-31T23:59:59Z')) is None
|
|
23
|
+
assert p.evaluate(row(provider='openai')) is None
|
|
24
|
+
assert p.evaluate(row(route='another')) is None
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def test_tag_conditions_and_upper_bound():
|
|
28
|
+
p = ExclusionPolicy.from_dict(policy(tags={'team': 'test'}, timestamp_lt='2026-02-01T00:00:00Z'))
|
|
29
|
+
assert p.evaluate(row(tags={'team': 'test', 'other': 'allowed'}))
|
|
30
|
+
assert p.evaluate(row(tags={'team': 'other'})) is None
|
|
31
|
+
assert p.evaluate(row()) is None
|
|
32
|
+
assert p.evaluate(row(tags={'team': 'test'}, ts='2026-02-01T00:00:00Z')) is None
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@pytest.mark.parametrize('bad', [
|
|
36
|
+
{'version': 2, 'rules': []}, {'version': 1, 'rules': 'bad'},
|
|
37
|
+
policy(timestamp_gte='yesterday'), policy(timestamp_gte='2026-01-01'),
|
|
38
|
+
policy(timestamp_lt='2025-01-01T00:00:00Z'), policy(expression='eval(x)'),
|
|
39
|
+
policy(tags={'team': ['not-a-string']}),
|
|
40
|
+
{'version': 1, 'rules': [{'id': 'all', 'conditions': {'provider': 'anthropic'}}]},
|
|
41
|
+
])
|
|
42
|
+
def test_invalid_or_broad_rules_are_rejected(bad):
|
|
43
|
+
with pytest.raises(ValueError):
|
|
44
|
+
ExclusionPolicy.from_dict(bad)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def test_snapshot_does_not_change_when_input_is_mutated():
|
|
48
|
+
source = policy()
|
|
49
|
+
p = ExclusionPolicy.from_dict(source)
|
|
50
|
+
source['rules'][0]['conditions']['route'] = 'changed'
|
|
51
|
+
assert p.evaluate(row())
|
|
52
|
+
assert ExclusionPolicy.from_dict(p.to_dict()).digest == p.digest
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def test_duplicate_rule_ids_and_invalid_row_time_fail():
|
|
56
|
+
source = policy()
|
|
57
|
+
source['rules'] *= 2
|
|
58
|
+
with pytest.raises(ValueError):
|
|
59
|
+
ExclusionPolicy.from_dict(source)
|
|
60
|
+
with pytest.raises(ValueError):
|
|
61
|
+
ExclusionPolicy.from_dict(policy()).evaluate(row(ts='bad'))
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def test_filter_preview_keeps_rows_and_reports_matches(tmp_path):
|
|
65
|
+
source, output = tmp_path/'in.jsonl', tmp_path/'out.jsonl'
|
|
66
|
+
rows = [row(), row(provider='openai')]
|
|
67
|
+
source.write_text(''.join(json.dumps(r)+'\n' for r in rows))
|
|
68
|
+
p = ExclusionPolicy.from_dict(policy())
|
|
69
|
+
r = filter_jsonl(source, output, p, preview=True)
|
|
70
|
+
assert (r.included, r.excluded, r.matched_by_rule) == (2, 0, {'direct-coverage': 1})
|
|
71
|
+
assert [json.loads(x) for x in output.read_text().splitlines()] == rows
|
|
72
|
+
r = filter_jsonl(source, output, p)
|
|
73
|
+
assert (r.included, r.excluded) == (1, 1)
|
|
74
|
+
assert json.loads(output.read_text())['provider'] == 'openai'
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def test_invalid_input_preserves_output_and_never_overwrites_source(tmp_path):
|
|
78
|
+
source, output = tmp_path/'in.jsonl', tmp_path/'out.jsonl'
|
|
79
|
+
source.write_text(json.dumps(row())+'\nnot json\n')
|
|
80
|
+
output.write_text('original')
|
|
81
|
+
p = ExclusionPolicy.from_dict(policy())
|
|
82
|
+
with pytest.raises(ValueError):
|
|
83
|
+
filter_jsonl(source, output, p)
|
|
84
|
+
assert output.read_text() == 'original'
|
|
85
|
+
with pytest.raises(ValueError):
|
|
86
|
+
filter_jsonl(source, source, p)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def test_cli_requires_no_credentials_or_network(tmp_path, capsys):
|
|
90
|
+
from metergraphrelay.cli import main
|
|
91
|
+
source, output, rules = tmp_path/'in.jsonl', tmp_path/'out.jsonl', tmp_path/'rules.json'
|
|
92
|
+
source.write_text(json.dumps(row())+'\n')
|
|
93
|
+
rules.write_text(json.dumps(policy()))
|
|
94
|
+
assert main(['filter', str(source), '--rules', str(rules), '--output', str(output), '--preview']) == 0
|
|
95
|
+
assert json.loads(capsys.readouterr().out)['included'] == 1
|
|
96
|
+
assert main(['filter', str(source), '--rules', str(rules), '--output', str(output)]) == 0
|
|
97
|
+
assert json.loads(capsys.readouterr().out)['excluded'] == 1
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def test_bounded_row_size(tmp_path, monkeypatch):
|
|
101
|
+
import metergraphrelay.filtering as filtering
|
|
102
|
+
source, output = tmp_path/'in.jsonl', tmp_path/'out.jsonl'
|
|
103
|
+
source.write_text(json.dumps(row())+'\n')
|
|
104
|
+
monkeypatch.setattr(filtering, 'MAX_LINE_BYTES', 8)
|
|
105
|
+
with pytest.raises(ValueError):
|
|
106
|
+
filter_jsonl(source, output)
|
|
107
|
+
assert not output.exists()
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
__version__ = "0.7.7"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/src/metergraphrelay/providers/cache_shape.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/src/metergraphrelay/providers/portkey_batch.py
RENAMED
|
File without changes
|
{metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/src/metergraphrelay/providers/portkey_export.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/src/metergraphrelay.egg-info/dependency_links.txt
RENAMED
|
File without changes
|
{metergraphrelay-0.7.7 → metergraphrelay-0.7.9}/src/metergraphrelay.egg-info/entry_points.txt
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|