Skip to content

Commit e3b3469

Browse files
authored
Merge pull request #834 from confident-ai/hotfix/stateless
Fix base metric
2 parents 8a39e85 + 663f8d6 commit e3b3469

File tree

2 files changed

+19
-22
lines changed

2 files changed

+19
-22
lines changed

deepeval/metrics/base_metric.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,20 @@
77
from deepeval.utils import generate_uuid
88

99

10-
class BaseMetric:
10+
class MetricMeta(type):
11+
def __new__(cls, name, bases, attrs):
12+
# Create the new class
13+
new_class = super().__new__(cls, name, bases, attrs)
14+
# Assign new ContextVar instances with unique identifiers
15+
new_class._score = ContextVar(str(uuid.uuid4()), default=None)
16+
new_class._score_breakdown = ContextVar(str(uuid.uuid4()), default=None)
17+
new_class._reason = ContextVar(str(uuid.uuid4()), default=None)
18+
new_class._success = ContextVar(str(uuid.uuid4()), default=None)
19+
new_class._error = ContextVar(str(uuid.uuid4()), default=None)
20+
return new_class
21+
22+
23+
class BaseMetric(metaclass=MetricMeta):
1124

1225
evaluation_model: Optional[str] = None
1326
strict_mode: bool = False
@@ -16,22 +29,6 @@ class BaseMetric:
1629
include_reason: bool = False
1730
evaluation_cost: Optional[float] = None
1831

19-
_score: ContextVar[Optional[float]] = ContextVar(
20-
generate_uuid(), default=None
21-
)
22-
_score_breakdown: ContextVar[Optional[Dict]] = ContextVar(
23-
generate_uuid(), default=None
24-
)
25-
_reason: ContextVar[Optional[str]] = ContextVar(
26-
generate_uuid(), default=None
27-
)
28-
_success: ContextVar[Optional[bool]] = ContextVar(
29-
generate_uuid(), default=None
30-
)
31-
_error: ContextVar[Optional[str]] = ContextVar(
32-
generate_uuid(), default=None
33-
)
34-
3532
@property
3633
def score(self) -> Optional[float]:
3734
return self._score.get()

tests/test_everything.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,11 @@ def test_everything():
144144
metric3,
145145
metric4,
146146
metric5,
147-
metric6,
148-
metric7,
149-
metric8,
150-
metric9,
151-
metric10,
147+
# metric6,
148+
# metric7,
149+
# metric8,
150+
# metric9,
151+
# metric10,
152152
],
153153
# run_async=False,
154154
)

0 commit comments

Comments
 (0)