Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions docs/sdk/main.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1752,7 +1752,6 @@ def scorer(

def make_scorer(func: ScorerCallable[T]) -> Scorer[T]:
return Scorer.from_callable(
self._get_tracer(),
func,
name=name,
tags=tags,
Expand Down Expand Up @@ -2158,9 +2157,7 @@ def task(
attributes=_attributes,
func=t.cast("t.Callable[P, R]", func),
scorers=[
scorer
if isinstance(scorer, Scorer)
else Scorer.from_callable(self._get_tracer(), scorer)
scorer if isinstance(scorer, Scorer) else Scorer.from_callable(scorer)
for scorer in scorers or []
],
tags=list(tags or []),
Expand Down
52 changes: 34 additions & 18 deletions docs/sdk/metric.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,11 @@ def from_many(
total = sum(value * weight for _, value, weight in values)
weight = sum(weight for _, _, weight in values)
score_attributes = {name: value for name, value, _ in values}
return cls(value=total / weight, step=step, attributes={**attributes, **score_attributes})
return cls(
value=total / weight,
step=step,
attributes={**attributes, **score_attributes},
)
```


Expand All @@ -228,13 +232,13 @@ Scorer

```python
Scorer(
tracer: Tracer,
name: str,
tags: Sequence[str],
attributes: dict[str, Any],
func: ScorerCallable[T],
step: int = 0,
auto_increment_step: bool = False,
catch: bool = False,
)
```

Expand All @@ -254,6 +258,14 @@ auto_increment_step: bool = False

Whether to automatically increment the step for each time this scorer is called.

### catch

```python
catch: bool = False
```

Whether to catch exceptions in the scorer function and return a 0 Metric with error information.

### func

```python
Expand Down Expand Up @@ -321,17 +333,19 @@ async def __call__(self, object: T) -> Metric:
Returns:
A Metric object.
"""
from dreadnode.tracing.span import Span

with Span(
name=self.name,
tags=self.tags,
attributes=self.attributes,
tracer=self.tracer,
):
try:
metric = self.func(object)
if inspect.isawaitable(metric):
metric = await metric
except Exception as exc:
if not self.catch:
raise

warn_at_user_stacklevel(
f"Error executing scorer {self.name!r} for object {object!r}: {exc}",
MetricWarning,
)
metric = Metric(value=0.0, step=self.step, attributes={"error": str(exc)})

if not isinstance(metric, Metric):
metric = Metric(
Expand Down Expand Up @@ -373,13 +387,13 @@ def clone(self) -> "Scorer[T]":
A new Scorer.
"""
return Scorer(
tracer=self.tracer,
name=self.name,
tags=self.tags,
attributes=self.attributes,
func=self.func,
step=self.step,
auto_increment_step=self.auto_increment_step,
catch=self.catch,
)
```

Expand All @@ -390,11 +404,11 @@ def clone(self) -> "Scorer[T]":

```python
from_callable(
tracer: Tracer,
func: ScorerCallable[T] | Scorer[T],
*,
name: str | None = None,
tags: Sequence[str] | None = None,
catch: bool = False,
**attributes: Any,
) -> Scorer[T]
```
Expand All @@ -403,9 +417,6 @@ Create a scorer from a callable function.

**Parameters:**

* **`tracer`**
(`Tracer`)
–The tracer to use for reporting metrics.
* **`func`**
(`ScorerCallable[T] | Scorer[T]`)
–The function to call to get the metric.
Expand All @@ -419,6 +430,11 @@ Create a scorer from a callable function.
`None`
)
–A list of tags to attach to the metric.
* **`catch`**
(`bool`, default:
`False`
)
–Whether to catch exceptions in the scorer function and return a 0 Metric with error information.
* **`**attributes`**
(`Any`, default:
`{}`
Expand All @@ -435,21 +451,21 @@ Create a scorer from a callable function.
@classmethod
def from_callable(
cls,
tracer: Tracer,
func: "ScorerCallable[T] | Scorer[T]",
*,
name: str | None = None,
tags: t.Sequence[str] | None = None,
catch: bool = False,
**attributes: t.Any,
) -> "Scorer[T]":
"""
Create a scorer from a callable function.

Args:
tracer: The tracer to use for reporting metrics.
func: The function to call to get the metric.
name: The name of the scorer, used for reporting metrics.
tags: A list of tags to attach to the metric.
catch: Whether to catch exceptions in the scorer function and return a 0 Metric with error information.
**attributes: A dictionary of attributes to attach to the metric.

Returns:
Expand All @@ -470,11 +486,11 @@ def from_callable(
)
name = name or func_name
return cls(
tracer=tracer,
name=name,
tags=tags or [],
attributes=attributes or {},
func=func,
catch=catch,
)
```

Expand Down
Loading