From d807ae39cb9567c6f39e6277c5d68bc766835f81 Mon Sep 17 00:00:00 2001 From: Karthik Kalyanaraman <105607645+karthikscale3@users.noreply.github.com> Date: Thu, 4 Apr 2024 23:13:47 -0700 Subject: [PATCH] Release (#67) * Updated test_chat_completion and added test_anthropic. Updated tests/utils * Updated tests/utils * updated langchain test cases * updated test_langchain * updated test_image_generation * Bugfix * Bump version * Cleanup logging (#56) * Fix streaming bug (#57) * Cleanup logging * Fix streaming bug * bump version * add sdk name (#60) * Bump trace-attributes (#62) * add model for streaming * Add model for streaming (#65) * Release 1.2.13 (#55) * Updated test_chat_completion and added test_anthropic. Updated tests/utils * Updated tests/utils * updated langchain test cases * updated test_langchain * updated test_image_generation * Bugfix * Bump version --------- Co-authored-by: Ali Waleed <134522290+alizenhom@users.noreply.github.com> Co-authored-by: MayuriS24 Co-authored-by: Rohit Kadhe <113367036+rohit-kadhe@users.noreply.github.com> * Bump version * Release (#58) * Updated test_chat_completion and added test_anthropic. Updated tests/utils * Updated tests/utils * updated langchain test cases * updated test_langchain * updated test_image_generation * Bugfix * Bump version * Cleanup logging (#56) * Fix streaming bug (#57) * Cleanup logging * Fix streaming bug --------- Co-authored-by: Ali Waleed <134522290+alizenhom@users.noreply.github.com> Co-authored-by: MayuriS24 Co-authored-by: Rohit Kadhe <113367036+rohit-kadhe@users.noreply.github.com> * Release (#59) * Updated test_chat_completion and added test_anthropic. Updated tests/utils * Updated tests/utils * updated langchain test cases * updated test_langchain * updated test_image_generation * Bugfix * Bump version * Cleanup logging (#56) * Fix streaming bug (#57) * Cleanup logging * Fix streaming bug * bump version --------- Co-authored-by: Ali Waleed <134522290+alizenhom@users.noreply.github.com> Co-authored-by: MayuriS24 Co-authored-by: Rohit Kadhe <113367036+rohit-kadhe@users.noreply.github.com> * Bump version * Release (#61) * Updated test_chat_completion and added test_anthropic. Updated tests/utils * Updated tests/utils * updated langchain test cases * updated test_langchain * updated test_image_generation * Bugfix * Bump version * Cleanup logging (#56) * Fix streaming bug (#57) * Cleanup logging * Fix streaming bug * bump version * add sdk name (#60) --------- Co-authored-by: Ali Waleed <134522290+alizenhom@users.noreply.github.com> Co-authored-by: MayuriS24 Co-authored-by: Rohit Kadhe <113367036+rohit-kadhe@users.noreply.github.com> * Bump version * Release (#63) * Updated test_chat_completion and added test_anthropic. Updated tests/utils * Updated tests/utils * updated langchain test cases * updated test_langchain * updated test_image_generation * Bugfix * Bump version * Cleanup logging (#56) * Fix streaming bug (#57) * Cleanup logging * Fix streaming bug * bump version * add sdk name (#60) * Bump trace-attributes (#62) --------- Co-authored-by: Ali Waleed <134522290+alizenhom@users.noreply.github.com> Co-authored-by: MayuriS24 Co-authored-by: Rohit Kadhe <113367036+rohit-kadhe@users.noreply.github.com> * Bump version * Release (#64) * Updated test_chat_completion and added test_anthropic. Updated tests/utils * Updated tests/utils * updated langchain test cases * updated test_langchain * updated test_image_generation * Bugfix * Bump version * Cleanup logging (#56) * Fix streaming bug (#57) * Cleanup logging * Fix streaming bug * bump version * add sdk name (#60) * Bump trace-attributes (#62) * add model for streaming --------- Co-authored-by: Ali Waleed <134522290+alizenhom@users.noreply.github.com> Co-authored-by: MayuriS24 Co-authored-by: Rohit Kadhe <113367036+rohit-kadhe@users.noreply.github.com> --------- Co-authored-by: Ali Waleed <134522290+alizenhom@users.noreply.github.com> Co-authored-by: MayuriS24 Co-authored-by: Rohit Kadhe <113367036+rohit-kadhe@users.noreply.github.com> Co-authored-by: karthikscale3 * Bugfix function calling (#66) * fix function calling * prompt token calculation fix --------- Co-authored-by: Ali Waleed <134522290+alizenhom@users.noreply.github.com> Co-authored-by: MayuriS24 Co-authored-by: Rohit Kadhe <113367036+rohit-kadhe@users.noreply.github.com> Co-authored-by: karthikscale3 --- .../instrumentation/openai/patch.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/langtrace_python_sdk/instrumentation/openai/patch.py b/src/langtrace_python_sdk/instrumentation/openai/patch.py index 5998ca39..75bd5e9f 100644 --- a/src/langtrace_python_sdk/instrumentation/openai/patch.py +++ b/src/langtrace_python_sdk/instrumentation/openai/patch.py @@ -194,9 +194,20 @@ def traced_method(wrapped, instance, args, kwargs): span.end() return result else: - prompt_tokens = calculate_prompt_tokens( - json.dumps(kwargs.get("messages", {})[0]), kwargs.get("model") - ) + # iterate over kwargs.get("messages", {}) and calculate the prompt tokens + prompt_tokens = 0 + for message in kwargs.get("messages", {}): + prompt_tokens += calculate_prompt_tokens( + json.dumps(message), kwargs.get("model") + ) + + # iterate over kwargs.get("functions") and calculate the prompt tokens + if kwargs.get("functions") is not None: + for function in kwargs.get("functions"): + prompt_tokens += calculate_prompt_tokens( + json.dumps(function), kwargs.get("model") + ) + return handle_streaming_response( result, span, @@ -289,7 +300,7 @@ def handle_streaming_response(result, span, prompt_tokens, function_call=False): { "message": { "role": "assistant", - "function_call": "".join(result_content), + "content": "".join(result_content), } } ),