From 3a60a480d040ae38078203fb95c18f959a16e6f3 Mon Sep 17 00:00:00 2001 From: Marc Klingen Date: Wed, 21 Feb 2024 15:04:22 +0100 Subject: [PATCH 1/5] feat(openai): add support for tags --- langfuse/openai.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/langfuse/openai.py b/langfuse/openai.py index 9601715e5..fbc3f3e7e 100644 --- a/langfuse/openai.py +++ b/langfuse/openai.py @@ -89,6 +89,7 @@ def __init__( trace_id=None, session_id=None, user_id=None, + tags=None, parent_observation_id=None, **kwargs, ): @@ -98,6 +99,7 @@ def __init__( self.args["trace_id"] = trace_id self.args["session_id"] = session_id self.args["user_id"] = user_id + self.args["tags"] = tags self.args["parent_observation_id"] = parent_observation_id self.kwargs = kwargs @@ -187,6 +189,12 @@ def _get_langfuse_data_from_kwargs( if user_id is not None and not isinstance(user_id, str): raise TypeError("user_id must be a string") + tags = kwargs.get("tags", None) + if tags is not None and ( + not isinstance(tags, list) or not all(isinstance(tag, str) for tag in tags) + ): + raise TypeError("tags must be a list of strings") + parent_observation_id = kwargs.get("parent_observation_id", None) if parent_observation_id is not None and not isinstance(parent_observation_id, str): raise TypeError("parent_observation_id must be a string") @@ -194,10 +202,13 @@ def _get_langfuse_data_from_kwargs( raise ValueError("parent_observation_id requires trace_id to be set") if trace_id: - langfuse.trace(id=trace_id, session_id=session_id, user_id=user_id) - elif session_id: - # If a session_id is provided but no trace_id, we should create a trace using the SDK and then use its trace_id - trace_id = langfuse.trace(session_id=session_id, user_id=user_id).id + langfuse.trace( + id=trace_id, session_id=session_id, user_id=user_id, tags=tags, name=name + ) + else: + trace_id = langfuse.trace( + session_id=session_id, user_id=user_id, tags=tags, name=name + ).id metadata = kwargs.get("metadata", {}) From 132dd05882468a581517eba06dc0ff4dad00711b Mon Sep 17 00:00:00 2001 From: Marc Klingen Date: Wed, 21 Feb 2024 15:05:56 +0100 Subject: [PATCH 2/5] add test --- tests/test_openai.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/test_openai.py b/tests/test_openai.py index 8f07af3f8..a0ca32734 100644 --- a/tests/test_openai.py +++ b/tests/test_openai.py @@ -255,9 +255,11 @@ def test_openai_chat_completion_fail(): openai.api_key = os.environ["OPENAI_API_KEY"] -def test_openai_chat_completion_with_user_id(): +def test_openai_chat_completion_with_additional_params(): api = get_api() user_id = create_uuid() + session_id = create_uuid() + tags = ["tag1", "tag2"] trace_id = create_uuid() completion = chat_func( name="user-creation", @@ -267,6 +269,8 @@ def test_openai_chat_completion_with_user_id(): metadata={"someKey": "someResponse"}, user_id=user_id, trace_id=trace_id, + session_id=session_id, + tags=tags, ) openai.flush_langfuse() @@ -275,6 +279,8 @@ def test_openai_chat_completion_with_user_id(): traces = api.trace.get(trace_id) assert traces.user_id == user_id + assert traces.session_id == session_id + assert traces.tags == tags def test_openai_chat_completion_without_extra_param(): From 345b73f468813e1722b92741e6c1aff5ebd2cf31 Mon Sep 17 00:00:00 2001 From: Marc Klingen Date: Wed, 21 Feb 2024 15:07:10 +0100 Subject: [PATCH 3/5] do not override name when trace is provided --- langfuse/openai.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/langfuse/openai.py b/langfuse/openai.py index fbc3f3e7e..193f81aa0 100644 --- a/langfuse/openai.py +++ b/langfuse/openai.py @@ -202,9 +202,7 @@ def _get_langfuse_data_from_kwargs( raise ValueError("parent_observation_id requires trace_id to be set") if trace_id: - langfuse.trace( - id=trace_id, session_id=session_id, user_id=user_id, tags=tags, name=name - ) + langfuse.trace(id=trace_id, session_id=session_id, user_id=user_id, tags=tags) else: trace_id = langfuse.trace( session_id=session_id, user_id=user_id, tags=tags, name=name From abf83b7a89cae7bbb31ba8b0ecaa1db0c785b59e Mon Sep 17 00:00:00 2001 From: Marc Klingen Date: Wed, 21 Feb 2024 15:08:28 +0100 Subject: [PATCH 4/5] add name to test --- tests/test_openai.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/test_openai.py b/tests/test_openai.py index a0ca32734..9eba62707 100644 --- a/tests/test_openai.py +++ b/tests/test_openai.py @@ -276,11 +276,12 @@ def test_openai_chat_completion_with_additional_params(): openai.flush_langfuse() assert len(completion.choices) != 0 - traces = api.trace.get(trace_id) + trace = api.trace.get(trace_id) - assert traces.user_id == user_id - assert traces.session_id == session_id - assert traces.tags == tags + assert trace.user_id == user_id + assert trace.session_id == session_id + assert trace.tags == tags + assert trace.name == "user-creation" def test_openai_chat_completion_without_extra_param(): From 6249afd28586261bd7209485f896a23938308321 Mon Sep 17 00:00:00 2001 From: Marc Klingen Date: Wed, 21 Feb 2024 15:31:50 +0100 Subject: [PATCH 5/5] fix test --- tests/test_openai.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_openai.py b/tests/test_openai.py index 9eba62707..e349b99db 100644 --- a/tests/test_openai.py +++ b/tests/test_openai.py @@ -281,7 +281,6 @@ def test_openai_chat_completion_with_additional_params(): assert trace.user_id == user_id assert trace.session_id == session_id assert trace.tags == tags - assert trace.name == "user-creation" def test_openai_chat_completion_without_extra_param():