1- import logging
1+ from contextvars import ContextVar
22import os
33import typing
4- from typing import Any , Callable , List , Optional , Sequence , Union
4+ from typing import Any , List , Optional , Sequence
55
66import httpx
77from opentelemetry .sdk .resources import Resource
88from opentelemetry .sdk .trace import TracerProvider
99from opentelemetry .trace import Tracer
1010
11+ from humanloop .core .client_wrapper import SyncClientWrapper
12+ from humanloop .eval_utils .context import EVALUATION_CONTEXT_VARIABLE_NAME , EvaluationContext
1113from humanloop .types .model_endpoints import ModelEndpoints
1214from humanloop .types .model_providers import ModelProviders
1315from humanloop .types .prompt_kernel_request_stop import PromptKernelRequestStop
1416from humanloop .types .prompt_kernel_request_template import PromptKernelRequestTemplate
1517from humanloop .types .response_format import ResponseFormat
1618
1719if typing .TYPE_CHECKING :
18- from . import ToolFunctionParams
20+ from humanloop import ToolFunctionParams
1921
2022from humanloop .eval_utils import log_with_evaluation_context , run_eval
2123from humanloop .eval_utils .types import Dataset , Evaluator , EvaluatorCheck , File
2224
23- from .base_client import AsyncBaseHumanloop , BaseHumanloop
24- from .decorators .flow import flow as flow_decorator_factory
25- from .decorators .prompt import prompt as prompt_decorator_factory
26- from .decorators .tool import tool as tool_decorator_factory
27- from .environment import HumanloopEnvironment
28- from .evaluations .client import EvaluationsClient
29- from .otel import instrument_provider
30- from .otel .exporter import HumanloopSpanExporter
31- from .otel .processor import HumanloopSpanProcessor
32- from .prompt_utils import populate_template
33- from .prompts .client import PromptsClient
25+ from humanloop .base_client import AsyncBaseHumanloop , BaseHumanloop
26+ from humanloop .decorators .flow import flow as flow_decorator_factory
27+ from humanloop .decorators .prompt import prompt as prompt_decorator_factory
28+ from humanloop .decorators .tool import tool as tool_decorator_factory
29+ from humanloop .environment import HumanloopEnvironment
30+ from humanloop .evaluations .client import EvaluationsClient
31+ from humanloop .otel import instrument_provider
32+ from humanloop .otel .exporter import HumanloopSpanExporter
33+ from humanloop .otel .processor import HumanloopSpanProcessor
34+ from humanloop .prompt_utils import populate_template
35+ from humanloop .prompts .client import PromptsClient
3436
3537
3638class ExtendedEvalsClient (EvaluationsClient ):
3739 client : BaseHumanloop
3840
41+ def __init__ (
42+ self ,
43+ * ,
44+ client_wrapper : SyncClientWrapper ,
45+ evaluation_context_variable : ContextVar [Optional [EvaluationContext ]],
46+ ):
47+ super ().__init__ (client_wrapper = client_wrapper )
48+ self ._evaluation_context_variable = evaluation_context_variable
49+
3950 def run (
4051 self ,
41- file : Union [ File , Callable ] ,
52+ file : File ,
4253 name : Optional [str ],
4354 dataset : Dataset ,
4455 evaluators : Optional [Sequence [Evaluator ]] = None ,
@@ -64,6 +75,7 @@ def run(
6475 dataset = dataset ,
6576 evaluators = evaluators ,
6677 workers = workers ,
78+ evaluation_context_variable = self ._evaluation_context_variable ,
6779 )
6880
6981
@@ -111,18 +123,31 @@ def __init__(
111123 httpx_client = httpx_client ,
112124 )
113125
114- eval_client = ExtendedEvalsClient (client_wrapper = self ._client_wrapper )
126+ self .evaluation_context_variable : ContextVar [Optional [EvaluationContext ]] = ContextVar (
127+ EVALUATION_CONTEXT_VARIABLE_NAME
128+ )
129+
130+ eval_client = ExtendedEvalsClient (
131+ client_wrapper = self ._client_wrapper ,
132+ evaluation_context_variable = self .evaluation_context_variable ,
133+ )
115134 eval_client .client = self
116135 self .evaluations = eval_client
117136 self .prompts = ExtendedPromptsClient (client_wrapper = self ._client_wrapper )
118137
119138 # Overload the .log method of the clients to be aware of Evaluation Context
120139 # TODO: Overload the log for Evaluators and Tools once run_id is added
121140 # to them.
122- self .prompts = log_with_evaluation_context (client = self .prompts )
141+ self .prompts = log_with_evaluation_context (
142+ client = self .prompts ,
143+ evaluation_context_variable = self .evaluation_context_variable ,
144+ )
123145 # self.evaluators = log_with_evaluation_context(client=self.evaluators)
124146 # self.tools = log_with_evaluation_context(client=self.tools)
125- self .flows = log_with_evaluation_context (client = self .flows )
147+ self .flows = log_with_evaluation_context (
148+ client = self .flows ,
149+ evaluation_context_variable = self .evaluation_context_variable ,
150+ )
126151
127152 if opentelemetry_tracer_provider is not None :
128153 self ._tracer_provider = opentelemetry_tracer_provider
0 commit comments