POST
/
spans
/
{span_id}
/
metrics
from datetime import datetime, timezone
from opperai import Opper
import time

opper = Opper(http_bearer="YOUR_API_KEY")

# First, create a span to have a real span_id to work with
created_span = opper.spans.create(
    name="llm_response_evaluation",
    start_time=datetime.now(timezone.utc),
    type="generation",
    input="What are the benefits of renewable energy?",
    output="Renewable energy offers several key benefits: 1) Environmental protection through reduced carbon emissions, 2) Energy independence and security, 3) Long-term cost savings, 4) Job creation in green industries, 5) Sustainable resource utilization for future generations.",
    meta={"model": "gpt-4o-mini", "user_id": "user_123", "session_id": "session_456"},
)

print(f"Created span with ID: {created_span.id}")

# Create a quality score metric for the span
quality_metric = opper.span_metrics.create_metric(
    span_id=created_span.id,
    dimension="response_quality",
    value=8.5,
    comment="Quality score based on human evaluation (1-10 scale)",
)

print(f"Created metric: {quality_metric.dimension}")
print(f"Value: {quality_metric.value}")
print(f"Metric ID: {quality_metric.id}")
print(f"Created at: {quality_metric.created_at}")

# Create a latency metric
latency_metric = opper.span_metrics.create_metric(
    span_id=created_span.id,
    dimension="response_latency_ms",
    value=1250.0,
    comment="Time taken to generate response in milliseconds",
)

print(f"\nCreated metric: {latency_metric.dimension}")
print(f"Value: {latency_metric.value}ms")
{
  "dimension": "<string>",
  "value": 123,
  "comment": "<string>",
  "id": "3c90c3cc-0d44-4b50-8888-8dd25736052a",
  "span_id": "3c90c3cc-0d44-4b50-8888-8dd25736052a",
  "created_at": "2023-11-07T05:31:56Z"
}

Authorizations

Authorization
string
header
required

Bearer authentication header of the form Bearer <token>, where <token> is your auth token.

Path Parameters

span_id
string
required

The id of the span

Body

application/json

Response

200
application/json

Successful Response

The response is of type object.