-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathquickstart.py
More file actions
72 lines (60 loc) · 2.22 KB
/
Copy pathquickstart.py
File metadata and controls
72 lines (60 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""
Quickstart -- LayerLens Python SDK
==================================
Shows the core trace-evaluation workflow in under 50 lines:
init client, upload a trace, create a judge, evaluate, get results.
Prerequisites:
pip install layerlens --index-url https://sdk.layerlens.ai/package
export LAYERLENS_STRATIX_API_KEY="your-api-key"
"""
import os
import sys
import json
import tempfile
from layerlens import Stratix
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
from _helpers import create_judge, poll_evaluation_results
def main() -> None:
# --- 1. Initialize the client (reads LAYERLENS_STRATIX_API_KEY from env)
client = Stratix()
# --- 2. Create a temporary JSONL trace file and upload it
trace_data = {
"input": [{"role": "user", "content": "What is the speed of light?"}],
"output": "The speed of light in a vacuum is approximately 299,792,458 m/s.",
}
tmp = tempfile.NamedTemporaryFile(mode="w", suffix=".jsonl", delete=False)
tmp.write(json.dumps(trace_data) + "\n")
tmp.close()
try:
upload = client.traces.upload(tmp.name)
finally:
os.unlink(tmp.name)
if not upload or not upload.trace_ids:
print("ERROR: Trace upload returned no IDs")
return
trace_id = upload.trace_ids[0]
print(f"Uploaded trace: {trace_id}")
# --- 3. Create a judge
judge = create_judge(
client,
name="Quickstart Judge",
evaluation_goal="Evaluate whether the response is factually accurate and complete.",
)
print(f"Created judge: {judge.name} (ID: {judge.id})")
try:
# --- 4. Run a trace evaluation
evaluation = client.trace_evaluations.create(trace_id=trace_id, judge_id=judge.id)
print(f"Evaluation started: {evaluation.id}")
# --- 5. Poll for results
results = poll_evaluation_results(client, evaluation.id)
if results:
r = results[0]
print(f"Score: {r.score}")
print(f"Passed: {r.passed}")
print(f"Reasoning: {r.reasoning}")
else:
print("No results yet (evaluation may still be processing)")
finally:
client.judges.delete(judge.id)
if __name__ == "__main__":
main()