Browse Source

refac

Co-Authored-By: dulicon <57261058+dulicon@users.noreply.github.com>
Timothy Jaeryang Baek 3 months ago
parent
commit
1921420319
1 changed files with 23 additions and 12 deletions
  1. 23 12
      backend/open_webui/utils/middleware.py

+ 23 - 12
backend/open_webui/utils/middleware.py

@@ -1660,14 +1660,27 @@ async def process_chat_response(
                                             )
                                             )
 
 
                                             if tool_call_index is not None:
                                             if tool_call_index is not None:
-                                                if (
-                                                    len(response_tool_calls)
-                                                    <= tool_call_index
-                                                ):
+                                                # Check if the tool call already exists
+                                                current_response_tool_call = None
+                                                for (
+                                                    response_tool_call
+                                                ) in response_tool_calls:
+                                                    if (
+                                                        response_tool_call.get("index")
+                                                        == tool_call_index
+                                                    ):
+                                                        current_response_tool_call = (
+                                                            response_tool_call
+                                                        )
+                                                        break
+
+                                                if current_response_tool_call is None:
+                                                    # Add the new tool call
                                                     response_tool_calls.append(
                                                     response_tool_calls.append(
                                                         delta_tool_call
                                                         delta_tool_call
                                                     )
                                                     )
                                                 else:
                                                 else:
+                                                    # Update the existing tool call
                                                     delta_name = delta_tool_call.get(
                                                     delta_name = delta_tool_call.get(
                                                         "function", {}
                                                         "function", {}
                                                     ).get("name")
                                                     ).get("name")
@@ -1678,16 +1691,14 @@ async def process_chat_response(
                                                     )
                                                     )
 
 
                                                     if delta_name:
                                                     if delta_name:
-                                                        response_tool_calls[
-                                                            tool_call_index
-                                                        ]["function"][
-                                                            "name"
-                                                        ] += delta_name
+                                                        current_response_tool_call[
+                                                            "function"
+                                                        ]["name"] += delta_name
 
 
                                                     if delta_arguments:
                                                     if delta_arguments:
-                                                        response_tool_calls[
-                                                            tool_call_index
-                                                        ]["function"][
+                                                        current_response_tool_call[
+                                                            "function"
+                                                        ][
                                                             "arguments"
                                                             "arguments"
                                                         ] += delta_arguments
                                                         ] += delta_arguments