fix remove index from tool calls cohere error

This commit is contained in:
Ishaan Jaff 2024-07-16 21:49:17 -07:00
parent 5e1e413de0
commit ee33a80486
3 changed files with 22 additions and 0 deletions

View file

@ -757,6 +757,7 @@ openai_image_generation_models = ["dall-e-2", "dall-e-3"]
from .timeout import timeout
from .cost_calculator import completion_cost
from litellm.litellm_core_utils.litellm_logging import Logging
from litellm.litellm_core_utils.core_helpers import remove_index_from_tool_calls
from litellm.litellm_core_utils.token_counter import get_modified_max_tokens
from .utils import (
client,

View file

@ -39,3 +39,18 @@ def map_finish_reason(
elif finish_reason == "content_filtered":
return "content_filter"
return finish_reason
def remove_index_from_tool_calls(messages, tool_calls):
for tool_call in tool_calls:
if "index" in tool_call:
tool_call.pop("index")
for message in messages:
if "tool_calls" in message:
tool_calls = message["tool_calls"]
for tool_call in tool_calls:
if "index" in tool_call:
tool_call.pop("index")
return

View file

@ -953,6 +953,12 @@ class OpenAIChatCompletion(BaseLLM):
new_messages = messages
new_messages.append({"role": "user", "content": ""})
messages = new_messages
elif (
"unknown field: parameter index is not a valid field" in str(e)
) and "tools" in data:
litellm.remove_index_from_tool_calls(
tool_calls=data["tools"], messages=messages
)
else:
raise e
except OpenAIError as e: