Skip to content

Commit 3f78a39

Browse files
gustavocidornelaswhoseoyster
authored andcommitted
Completes OPEN-5776 Introduce a StepType enum to the Python client
1 parent 1ef88a7 commit 3f78a39

File tree

4 files changed

+29
-15
lines changed

4 files changed

+29
-15
lines changed

openlayer/llm_monitors.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ def modified_create_chat_completion(*args, **kwargs) -> str:
134134
model=kwargs.get("model"),
135135
model_parameters=kwargs.get("model_parameters"),
136136
raw_output=response.model_dump(),
137+
provider="OpenAI",
137138
)
138139
# pylint: disable=broad-except
139140
except Exception as e:
@@ -202,6 +203,7 @@ def stream_chunks():
202203
else None
203204
)
204205
},
206+
provider="OpenAI",
205207
)
206208
# pylint: disable=broad-except
207209
except Exception as e:
@@ -248,6 +250,7 @@ def modified_create_completion(*args, **kwargs):
248250
model=kwargs.get("model"),
249251
model_parameters=kwargs.get("model_parameters"),
250252
raw_output=response.model_dump(),
253+
provider="OpenAI",
251254
)
252255
# pylint: disable=broad-except
253256
except Exception as e:
@@ -331,6 +334,7 @@ def monitor_thread_run(self, run: openai.types.beta.threads.run.Run) -> None:
331334
output=prompt[-1]["content"],
332335
**run_step_vars,
333336
metadata=metadata,
337+
provider="OpenAI",
334338
)
335339

336340
# pylint: disable=broad-except

openlayer/tracing/enums.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import enum
2+
3+
4+
class StepType(enum.Enum):
5+
USER_CALL = "user_call"
6+
CHAT_COMPLETION = "chat_completion"

openlayer/tracing/steps.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import time
44
from typing import Any, Dict, Optional
55

6+
from . import enums
7+
68

79
class Step:
810
def __init__(
@@ -17,7 +19,7 @@ def __init__(
1719
self.output = output
1820
self.metadata = metadata
1921

20-
self.step_type = None
22+
self.step_type: enums.StepType = None
2123
self.start_time = time.time()
2224
self.end_time = None
2325
self.ground_truth = None
@@ -39,7 +41,7 @@ def to_dict(self) -> Dict[str, Any]:
3941
"""Dictionary representation of the Step."""
4042
return {
4143
"name": self.name,
42-
"type": self.step_type,
44+
"type": self.step_type.value,
4345
"inputs": self.inputs,
4446
"output": self.output,
4547
"groundTruth": self.ground_truth,
@@ -60,10 +62,10 @@ def __init__(
6062
metadata: Dict[str, any] = {},
6163
) -> None:
6264
super().__init__(name=name, inputs=inputs, output=output, metadata=metadata)
63-
self.step_type = "user_call"
65+
self.step_type = enums.StepType.USER_CALL
6466

6567

66-
class OpenAIChatCompletionStep(Step):
68+
class ChatCompletionStep(Step):
6769
def __init__(
6870
self,
6971
name: str,
@@ -73,7 +75,8 @@ def __init__(
7375
) -> None:
7476
super().__init__(name=name, inputs=inputs, output=output, metadata=metadata)
7577

76-
self.step_type = "openai_chat_completion"
78+
self.step_type = enums.StepType.CHAT_COMPLETION
79+
self.provider: str = None
7780
self.prompt_tokens: int = None
7881
self.completion_tokens: int = None
7982
self.tokens: int = None
@@ -83,10 +86,11 @@ def __init__(
8386
self.raw_output: str = None
8487

8588
def to_dict(self) -> Dict[str, Any]:
86-
"""Dictionary representation of the OpenAIChatCompletionStep."""
89+
"""Dictionary representation of the ChatCompletionStep."""
8790
step_dict = super().to_dict()
8891
step_dict.update(
8992
{
93+
"provider": self.provider,
9094
"promptTokens": self.prompt_tokens,
9195
"completionTokens": self.completion_tokens,
9296
"tokens": self.tokens,
@@ -100,12 +104,12 @@ def to_dict(self) -> Dict[str, Any]:
100104

101105

102106
# ----------------------------- Factory function ----------------------------- #
103-
def step_factory(step_type: str, *args, **kwargs) -> Step:
107+
def step_factory(step_type: enums.StepType, *args, **kwargs) -> Step:
104108
"""Factory function to create a step based on the step_type."""
105-
if step_type not in ["user_call", "openai_chat_completion"]:
106-
raise ValueError(f"Step type {step_type} not recognized.")
109+
if step_type.value not in [item.value for item in enums.StepType]:
110+
raise ValueError(f"Step type {step_type.value} not recognized.")
107111
step_type_mapping = {
108-
"user_call": UserCallStep,
109-
"openai_chat_completion": OpenAIChatCompletionStep,
112+
enums.StepType.USER_CALL: UserCallStep,
113+
enums.StepType.CHAT_COMPLETION: ChatCompletionStep,
110114
}
111115
return step_type_mapping[step_type](*args, **kwargs)

openlayer/tracing/tracer.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from typing import Any, Dict, Generator, List, Optional, Tuple
1010

1111
from ..services import data_streamer
12-
from . import steps, traces
12+
from . import enums, steps, traces
1313

1414
logger = logging.getLogger(__name__)
1515

@@ -31,7 +31,7 @@
3131
@contextmanager
3232
def create_step(
3333
name: str,
34-
step_type: str = "user_call",
34+
step_type: enums.StepType = enums.StepType.USER_CALL,
3535
inputs: Optional[Any] = None,
3636
output: Optional[Any] = None,
3737
metadata: Dict[str, any] = {},
@@ -71,7 +71,7 @@ def create_step(
7171
"costColumnName": "cost",
7272
"numOfTokenColumnName": "tokens",
7373
}
74-
if isinstance(new_step, steps.OpenAIChatCompletionStep):
74+
if isinstance(new_step, steps.ChatCompletionStep):
7575
config.update(
7676
{
7777
"prompt": new_step.inputs.get("prompt"),
@@ -92,7 +92,7 @@ def create_step(
9292
def add_openai_chat_completion_step_to_trace(**kwargs) -> None:
9393
"""Adds an OpenAI chat completion step to the trace."""
9494
with create_step(
95-
step_type="openai_chat_completion", name="chat_completion"
95+
step_type=enums.StepType.CHAT_COMPLETION, name="OpenAI Chat Completion"
9696
) as step:
9797
step.log(
9898
**kwargs,

0 commit comments

Comments
 (0)