From 57bd595c3e71458862c4a2510801c817347463f6 Mon Sep 17 00:00:00 2001 From: Ali Waleed <134522290+alizenhom@users.noreply.github.com> Date: Fri, 28 Jun 2024 21:24:21 +0300 Subject: [PATCH 1/2] hotfix fastapi --- .../instrumentation/openai/patch.py | 16 +++++++++++----- src/langtrace_python_sdk/utils/llm.py | 12 ++++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/langtrace_python_sdk/instrumentation/openai/patch.py b/src/langtrace_python_sdk/instrumentation/openai/patch.py index c3bf718a..a9e261ec 100644 --- a/src/langtrace_python_sdk/instrumentation/openai/patch.py +++ b/src/langtrace_python_sdk/instrumentation/openai/patch.py @@ -28,7 +28,11 @@ SERVICE_PROVIDERS, ) from langtrace_python_sdk.constants.instrumentation.openai import APIS -from langtrace_python_sdk.utils.llm import calculate_prompt_tokens, estimate_tokens +from langtrace_python_sdk.utils.llm import ( + calculate_prompt_tokens, + estimate_tokens, + get_tool_calls, +) from openai._types import NOT_GIVEN @@ -430,9 +434,10 @@ def traced_method(wrapped, instance, args, kwargs): # handle tool calls in the kwargs llm_prompts = [] for item in kwargs.get("messages", []): - if hasattr(item, "tool_calls") and item.tool_calls is not None: + tools = get_tool_calls(item) + if tools is not None: tool_calls = [] - for tool_call in item.tool_calls: + for tool_call in tools: tool_call_dict = { "id": tool_call.id if hasattr(tool_call, "id") else "", "type": tool_call.type if hasattr(tool_call, "type") else "", @@ -611,9 +616,10 @@ async def traced_method(wrapped, instance, args, kwargs): # handle tool calls in the kwargs llm_prompts = [] for item in kwargs.get("messages", []): - if hasattr(item, "tool_calls") and item.tool_calls is not None: + tools = get_tool_calls(item) + if tools is not None: tool_calls = [] - for tool_call in item.tool_calls: + for tool_call in tools: tool_call_dict = { "id": tool_call.id if hasattr(tool_call, "id") else "", "type": tool_call.type if hasattr(tool_call, "type") else "", diff --git a/src/langtrace_python_sdk/utils/llm.py b/src/langtrace_python_sdk/utils/llm.py index 17f1a43f..56d6c460 100644 --- a/src/langtrace_python_sdk/utils/llm.py +++ b/src/langtrace_python_sdk/utils/llm.py @@ -65,3 +65,15 @@ def set_span_attributes(span, name, value): if value != "": span.set_attribute(name, value) return + + +def get_tool_calls(item): + if isinstance(item, dict): + if "tool_calls" in item and item["tool_calls"] is not None: + return item["tool_calls"] + return None + + else: + if hasattr(item, "tool_calls") and item.tool_calls is not None: + return item.tool_calls + return None From 31945221f6c4be9032f999a0fd9651a1317bb7f9 Mon Sep 17 00:00:00 2001 From: Ali Waleed <134522290+alizenhom@users.noreply.github.com> Date: Fri, 28 Jun 2024 21:24:43 +0300 Subject: [PATCH 2/2] bump --- src/langtrace_python_sdk/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/langtrace_python_sdk/version.py b/src/langtrace_python_sdk/version.py index 5c809192..b9303606 100644 --- a/src/langtrace_python_sdk/version.py +++ b/src/langtrace_python_sdk/version.py @@ -1 +1 @@ -__version__ = "2.1.26" +__version__ = "2.1.27"