Skip to content

Commit f365615

Browse files
author
Andrei Bratu
committed
type checks
1 parent edce541 commit f365615

File tree

19 files changed

+1553
-415
lines changed

19 files changed

+1553
-415
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ poetry.toml
55
.ruff_cache/
66
.vscode
77
.env
8+
tests/assets/*.jsonl
9+
tests/assets/*.parquet

src/humanloop/client.py

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,8 @@ def __init__(
3838
self,
3939
*,
4040
client_wrapper: SyncClientWrapper,
41-
evaluation_context_variable: ContextVar[Optional[EvaluationContext]],
4241
):
4342
super().__init__(client_wrapper=client_wrapper)
44-
self._evaluation_context_variable = evaluation_context_variable
4543

4644
def run(
4745
self,
@@ -70,7 +68,6 @@ def run(
7068
dataset=dataset,
7169
evaluators=evaluators,
7270
workers=workers,
73-
evaluation_context_variable=self._evaluation_context_variable,
7471
)
7572

7673

@@ -118,31 +115,14 @@ def __init__(
118115
httpx_client=httpx_client,
119116
)
120117

121-
self.evaluation_context_variable: ContextVar[Optional[EvaluationContext]] = ContextVar(
122-
EVALUATION_CONTEXT_VARIABLE_NAME
123-
)
124-
125-
eval_client = ExtendedEvalsClient(
126-
client_wrapper=self._client_wrapper,
127-
evaluation_context_variable=self.evaluation_context_variable,
128-
)
118+
eval_client = ExtendedEvalsClient(client_wrapper=self._client_wrapper)
129119
eval_client.client = self
130120
self.evaluations = eval_client
131121
self.prompts = ExtendedPromptsClient(client_wrapper=self._client_wrapper)
132122

133123
# Overload the .log method of the clients to be aware of Evaluation Context
134-
# TODO: Overload the log for Evaluators and Tools once run_id is added
135-
# to them.
136-
self.prompts = log_with_evaluation_context(
137-
client=self.prompts,
138-
evaluation_context_variable=self.evaluation_context_variable,
139-
)
140-
# self.evaluators = log_with_evaluation_context(client=self.evaluators)
141-
# self.tools = log_with_evaluation_context(client=self.tools)
142-
self.flows = log_with_evaluation_context(
143-
client=self.flows,
144-
evaluation_context_variable=self.evaluation_context_variable,
145-
)
124+
self.prompts = log_with_evaluation_context(client=self.prompts)
125+
self.flows = log_with_evaluation_context(client=self.flows)
146126

147127
if opentelemetry_tracer_provider is not None:
148128
self._tracer_provider = opentelemetry_tracer_provider
@@ -157,9 +137,7 @@ def __init__(
157137
instrument_provider(provider=self._tracer_provider)
158138
self._tracer_provider.add_span_processor(
159139
HumanloopSpanProcessor(
160-
exporter=HumanloopSpanExporter(
161-
client=self,
162-
)
140+
exporter=HumanloopSpanExporter(client=self),
163141
),
164142
)
165143

src/humanloop/eval_utils/context.py

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
from typing import Callable, TypedDict
1+
from contextvars import ContextVar
2+
from dataclasses import dataclass
3+
from typing import Any, Callable
24

35

4-
class EvaluationContext(TypedDict):
6+
@dataclass
7+
class EvaluationContext:
58
"""Context Log to Humanloop.
69
710
Per datapoint state that is set when an Evaluation is ran.
@@ -24,3 +27,43 @@ class EvaluationContext(TypedDict):
2427

2528

2629
EVALUATION_CONTEXT_VARIABLE_NAME = "__EVALUATION_CONTEXT"
30+
31+
_EVALUATION_CONTEXT_VAR: ContextVar[EvaluationContext] = ContextVar(EVALUATION_CONTEXT_VARIABLE_NAME)
32+
33+
_UnsafeEvaluationContextRead = RuntimeError("EvaluationContext not set in the current thread.")
34+
35+
36+
def set_evaluation_context(context: EvaluationContext):
37+
_EVALUATION_CONTEXT_VAR.set(context)
38+
39+
40+
def get_evaluation_context() -> EvaluationContext:
41+
try:
42+
return _EVALUATION_CONTEXT_VAR.get()
43+
except LookupError:
44+
raise _UnsafeEvaluationContextRead
45+
46+
47+
def evaluation_context_set() -> bool:
48+
try:
49+
_EVALUATION_CONTEXT_VAR.get()
50+
return True
51+
except LookupError:
52+
return False
53+
54+
55+
def log_belongs_to_evaluated_file(log_args: dict[str, Any]) -> bool:
56+
try:
57+
evaluation_context: EvaluationContext = _EVALUATION_CONTEXT_VAR.get()
58+
return evaluation_context.file_id == log_args.get("id") or evaluation_context.path == log_args.get("path")
59+
except LookupError:
60+
# Not in an evaluation context
61+
return False
62+
63+
64+
def is_evaluated_file(file_path) -> bool:
65+
try:
66+
evaluation_context = _EVALUATION_CONTEXT_VAR.get()
67+
return evaluation_context.path == file_path
68+
except LookupError:
69+
raise _UnsafeEvaluationContextRead

0 commit comments

Comments
 (0)