Skip to content

Commit c06b732

Browse files
committed
update exmaple file
1 parent b60f66a commit c06b732

File tree

2 files changed

+60
-184
lines changed

2 files changed

+60
-184
lines changed

src/demo/customer_id_and_tags.py

Lines changed: 55 additions & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -1,199 +1,75 @@
1-
#!/usr/bin/env python3
2-
"""
3-
Demo script for setting and verifying customer ID and tags in Judgment traces.
4-
5-
This script demonstrates how to set metadata (customer ID and tags) on traces
6-
and verifies that the metadata is correctly set and saved.
7-
8-
Usage:
9-
python customer_id_and_tags.py [--api-url API_URL] [--project PROJECT_NAME]
10-
"""
11-
121
import os
13-
import sys
14-
import json
152
import random
16-
import logging
17-
import argparse
18-
from typing import List, Dict, Any, Optional, Tuple
19-
from pathlib import Path
20-
21-
# Setup logging
22-
logging.basicConfig(
23-
level=logging.INFO,
24-
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
25-
)
26-
logger = logging.getLogger("metadata-demo")
27-
28-
try:
29-
from judgeval.common.tracer import Tracer
30-
from dotenv import load_dotenv
31-
except ImportError as e:
32-
logger.error(f"Required package not found: {e}")
33-
logger.error("Please install required packages: pip install judgeval python-dotenv")
34-
sys.exit(1)
3+
import json
4+
from judgeval.common.tracer import Tracer
5+
from dotenv import load_dotenv
356

36-
# Initialize a global variable for the Tracer instance
37-
judgment = None
7+
load_dotenv()
388

9+
os.environ["JUDGMENT_API_URL"] = "http://localhost:8000"
3910

40-
def parse_arguments() -> argparse.Namespace:
41-
"""Parse command line arguments."""
42-
parser = argparse.ArgumentParser(description="Test customer ID and tags in Judgment traces")
43-
parser.add_argument(
44-
"--api-url",
45-
default=None,
46-
help="Judgment API URL (default: uses .env or http://localhost:8000)"
47-
)
48-
parser.add_argument(
49-
"--project",
50-
default="metadata-test",
51-
help="Project name for the trace (default: metadata-test)"
52-
)
53-
return parser.parse_args()
11+
judgment = Tracer(
12+
api_key=os.getenv("JUDGMENT_API_KEY"),
13+
organization_id=os.getenv("JUDGMENT_ORG_ID"),
14+
project_name="metadata-test"
15+
)
5416

17+
def get_customer_id():
18+
return f"customer-{random.randint(1000, 9999)}"
5519

56-
def validate_environment() -> Tuple[Optional[str], Optional[str]]:
57-
"""Validate that required environment variables are set."""
58-
# Load environment variables from .env file
59-
load_dotenv()
20+
@judgment.observe(name="test_metadata")
21+
def test_metadata():
22+
# Generate a customer ID
23+
customer_id = get_customer_id()
24+
print(f"Generated customer ID: {customer_id}")
6025

61-
api_key = os.getenv("JUDGMENT_API_KEY")
62-
org_id = os.getenv("JUDGMENT_ORG_ID")
26+
# Define tags
27+
tags = ["test", "example", "metadata"]
28+
print(f"Using tags: {tags}")
6329

64-
if not api_key:
65-
logger.error("JUDGMENT_API_KEY environment variable is not set")
66-
return None, org_id
67-
68-
if not org_id:
69-
logger.error("JUDGMENT_ORG_ID environment variable is not set")
70-
return api_key, None
71-
72-
return api_key, org_id
73-
74-
75-
def get_customer_id() -> str:
76-
"""Generate a random customer ID for testing."""
77-
return f"customer-{random.randint(1000, 9999)}"
78-
79-
80-
def get_test_tags() -> List[str]:
81-
"""Generate a list of tags for testing."""
82-
all_tags = ["test", "example", "metadata", "demo", "development"]
83-
# Select 2-4 random tags
84-
num_tags = random.randint(2, min(4, len(all_tags)))
85-
return random.sample(all_tags, num_tags)
86-
87-
88-
def save_trace_data(trace_data: Dict[str, Any], filename: str = "trace_metadata.json") -> None:
89-
"""Save trace data to a JSON file."""
90-
try:
91-
with open(filename, "w") as f:
92-
json.dump(trace_data, f, indent=2)
93-
logger.info(f"Trace metadata saved to {filename}")
94-
except Exception as e:
95-
logger.error(f"Failed to save trace data to {filename}: {e}")
96-
97-
98-
def test_metadata(tracer: Tracer, customer_id: str, tags: List[str]) -> str:
99-
"""Test setting and verifying customer ID and tags."""
100-
try:
101-
logger.info(f"Setting customer ID: {customer_id}")
102-
logger.info(f"Setting tags: {tags}")
103-
104-
# Create a trace context
105-
with tracer.trace(name="test_metadata") as trace:
106-
# Set both customer ID and tags at once
107-
tracer.set_metadata(
108-
customer_id=customer_id,
109-
tags=tags
110-
)
111-
112-
# Verify the metadata was set correctly
113-
current_trace = tracer.get_current_trace()
114-
if current_trace is None:
115-
raise ValueError("Failed to get current trace")
116-
117-
if current_trace.customer_id != customer_id:
118-
logger.warning(f"Customer ID mismatch: expected '{customer_id}', got '{current_trace.customer_id}'")
119-
else:
120-
logger.info(f"Customer ID verified: {current_trace.customer_id}")
121-
122-
if current_trace.tags != tags:
123-
logger.warning(f"Tags mismatch: expected {tags}, got {current_trace.tags}")
124-
else:
125-
logger.info(f"Tags verified: {current_trace.tags}")
126-
127-
# Print all metadata
128-
logger.info("Printing trace metadata:")
129-
tracer.print_metadata()
130-
131-
# Do some work
132-
logger.info("Performing test operations...")
133-
result = f"Test completed with customer ID: {customer_id} and tags: {tags}"
134-
135-
# Save trace data to a file for inspection
136-
trace_data = {
137-
"trace_id": current_trace.trace_id,
138-
"name": current_trace.name,
139-
"project_name": current_trace.project_name,
140-
"created_at": current_trace.start_time,
141-
"customer_id": current_trace.customer_id,
142-
"tags": current_trace.tags
143-
}
144-
save_trace_data(trace_data)
145-
146-
return result
147-
except Exception as e:
148-
logger.error(f"Error in test_metadata: {e}")
149-
raise
150-
151-
152-
def main() -> int:
153-
"""Main function."""
154-
args = parse_arguments()
30+
# Set both customer ID and tags at once
31+
judgment.set_metadata(
32+
customer_id=customer_id,
33+
tags=tags
34+
)
15535

156-
# Validate environment
157-
api_key, org_id = validate_environment()
158-
if api_key is None or org_id is None:
159-
return 1
36+
# Verify the metadata was set correctly
37+
trace = judgment.get_current_trace()
38+
print(f"Trace customer_id: {trace.customer_id}")
39+
print(f"Trace tags: {trace.tags}")
16040

161-
# Set API URL
162-
api_url = args.api_url or os.getenv("JUDGMENT_API_URL", "http://localhost:8000")
163-
os.environ["JUDGMENT_API_URL"] = api_url
41+
# Print all metadata
42+
print("\nTrace metadata:")
43+
judgment.print_metadata()
16444

165-
# Initialize Tracer
166-
try:
167-
tracer = Tracer(
168-
api_key=api_key,
169-
organization_id=org_id,
170-
project_name=args.project
171-
)
172-
except Exception as e:
173-
logger.error(f"Failed to initialize Tracer: {e}")
174-
return 1
45+
# Do some work
46+
print("\nPerforming test operations...")
47+
result = f"Test completed with customer ID: {customer_id} and tags: {tags}"
48+
print(result)
17549

176-
# Generate test data
177-
customer_id = get_customer_id()
178-
tags = get_test_tags()
50+
# Save trace data to a file for inspection
51+
trace_data = {
52+
"trace_id": trace.trace_id,
53+
"name": trace.name,
54+
"project_name": trace.project_name,
55+
"created_at": trace.start_time,
56+
"customer_id": trace.customer_id,
57+
"tags": trace.tags
58+
}
17959

180-
print(f"\n=== Metadata Test ===\n")
181-
print(f"API URL: {api_url}")
182-
print(f"Project: {args.project}")
183-
print(f"Customer ID: {customer_id}")
184-
print(f"Tags: {tags}\n")
60+
with open("trace_metadata.json", "w") as f:
61+
json.dump(trace_data, f, indent=2)
62+
print("\nTrace metadata saved to trace_metadata.json for inspection")
63+
64+
return result
65+
66+
if __name__ == "__main__":
67+
print("\n=== Metadata Test ===\n")
18568

18669
try:
18770
# Run the test function
188-
result = test_metadata(tracer, customer_id, tags)
71+
result = test_metadata()
18972
print(f"\nResult: {result}")
19073
print("\nCheck the Judgment dashboard to verify the metadata was saved.")
191-
return 0
19274
except Exception as e:
193-
logger.error(f"Test failed: {e}")
194-
print(f"\nError: {e}")
195-
return 1
196-
197-
198-
if __name__ == "__main__":
199-
sys.exit(main())
75+
print(f"\nError: {e}")

trace_metadata.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
2-
"trace_id": "27321213-012f-430c-8985-f418e61071d1",
2+
"trace_id": "00e76f02-095d-471a-b677-8ba2be3146fc",
33
"name": "test_metadata",
44
"project_name": "metadata-test",
5-
"created_at": 1747622653.680715,
6-
"customer_id": "customer-3163",
5+
"created_at": 1747622984.1897202,
6+
"customer_id": "customer-8741",
77
"tags": [
88
"test",
9-
"metadata",
10-
"demo"
9+
"example",
10+
"metadata"
1111
]
1212
}

0 commit comments

Comments
 (0)