Skip to content

Commit 22d91d9

Browse files
authored
fix: Only track usage for entry methods (#94)
1 parent cc188bd commit 22d91d9

1 file changed

Lines changed: 44 additions & 26 deletions

File tree

packages/sdk/server-ai/src/ldai/client.py

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,13 @@ def __init__(self, client: LDClient):
4545
1,
4646
)
4747

48-
def completion_config(
48+
def _completion_config(
4949
self,
5050
key: str,
5151
context: Context,
5252
default_value: AICompletionConfigDefault,
5353
variables: Optional[Dict[str, Any]] = None,
5454
) -> AICompletionConfig:
55-
"""
56-
Get the value of a completion configuration.
57-
58-
:param key: The key of the completion configuration.
59-
:param context: The context to evaluate the completion configuration in.
60-
:param default_value: The default value of the completion configuration.
61-
:param variables: Additional variables for the completion configuration.
62-
:return: The completion configuration with a tracker used for gathering metrics.
63-
"""
64-
self._client.track(_TRACK_USAGE_COMPLETION_CONFIG, context, key, 1)
65-
6655
model, provider, messages, instructions, tracker, enabled, judge_configuration, _ = self.__evaluate(
6756
key, context, default_value.to_dict(), variables
6857
)
@@ -79,6 +68,26 @@ def completion_config(
7968

8069
return config
8170

71+
def completion_config(
72+
self,
73+
key: str,
74+
context: Context,
75+
default_value: AICompletionConfigDefault,
76+
variables: Optional[Dict[str, Any]] = None,
77+
) -> AICompletionConfig:
78+
"""
79+
Get the value of a completion configuration.
80+
81+
:param key: The key of the completion configuration.
82+
:param context: The context to evaluate the completion configuration in.
83+
:param default_value: The default value of the completion configuration.
84+
:param variables: Additional variables for the completion configuration.
85+
:return: The completion configuration with a tracker used for gathering metrics.
86+
"""
87+
self._client.track(_TRACK_USAGE_COMPLETION_CONFIG, context, key, 1)
88+
89+
return self._completion_config(key, context, default_value, variables)
90+
8291
def config(
8392
self,
8493
key: str,
@@ -99,24 +108,13 @@ def config(
99108
"""
100109
return self.completion_config(key, context, default_value, variables)
101110

102-
def judge_config(
111+
def _judge_config(
103112
self,
104113
key: str,
105114
context: Context,
106115
default_value: AIJudgeConfigDefault,
107116
variables: Optional[Dict[str, Any]] = None,
108117
) -> AIJudgeConfig:
109-
"""
110-
Get the value of a judge configuration.
111-
112-
:param key: The key of the judge configuration.
113-
:param context: The context to evaluate the judge configuration in.
114-
:param default_value: The default value of the judge configuration.
115-
:param variables: Additional variables for the judge configuration.
116-
:return: The judge configuration with a tracker used for gathering metrics.
117-
"""
118-
self._client.track(_TRACK_USAGE_JUDGE_CONFIG, context, key, 1)
119-
120118
model, provider, messages, instructions, tracker, enabled, judge_configuration, variation = self.__evaluate(
121119
key, context, default_value.to_dict(), variables
122120
)
@@ -150,6 +148,26 @@ def _extract_evaluation_metric_key(variation: Dict[str, Any]) -> Optional[str]:
150148

151149
return config
152150

151+
def judge_config(
152+
self,
153+
key: str,
154+
context: Context,
155+
default_value: AIJudgeConfigDefault,
156+
variables: Optional[Dict[str, Any]] = None,
157+
) -> AIJudgeConfig:
158+
"""
159+
Get the value of a judge configuration.
160+
161+
:param key: The key of the judge configuration.
162+
:param context: The context to evaluate the judge configuration in.
163+
:param default_value: The default value of the judge configuration.
164+
:param variables: Additional variables for the judge configuration.
165+
:return: The judge configuration with a tracker used for gathering metrics.
166+
"""
167+
self._client.track(_TRACK_USAGE_JUDGE_CONFIG, context, key, 1)
168+
169+
return self._judge_config(key, context, default_value, variables)
170+
153171
async def create_judge(
154172
self,
155173
key: str,
@@ -204,7 +222,7 @@ async def create_judge(
204222
extended_variables['message_history'] = '{{message_history}}'
205223
extended_variables['response_to_evaluate'] = '{{response_to_evaluate}}'
206224

207-
judge_config = self.judge_config(key, context, default_value, extended_variables)
225+
judge_config = self._judge_config(key, context, default_value, extended_variables)
208226

209227
if not judge_config.enabled or not judge_config.tracker:
210228
return None
@@ -304,7 +322,7 @@ async def create_chat(
304322
"""
305323
self._client.track(_TRACK_USAGE_CREATE_CHAT, context, key, 1)
306324
log.debug(f"Creating chat for key: {key}")
307-
config = self.completion_config(key, context, default_value, variables)
325+
config = self._completion_config(key, context, default_value, variables)
308326

309327
if not config.enabled or not config.tracker:
310328
return None

0 commit comments

Comments
 (0)