Skip to content

Commit d1fc90c

Browse files
committed
Fixed base_index calculation innefficiency
1 parent bdeac74 commit d1fc90c

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

vllm/entrypoints/openai/serving_chat.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,15 @@ async def chat_completion_stream_generator(
734734
combined_reasoning = ""
735735
tool_messages = []
736736

737+
# Calculate base_index once before the loop
738+
base_index = 0
739+
for msg in harmony_parser.messages:
740+
if (msg.channel == "commentary"
741+
and msg.recipient
742+
and msg.recipient.startswith(
743+
"functions.")):
744+
base_index += 1
745+
737746
for group in groups:
738747
group_channel = group['channel']
739748
group_recipient = group['recipient']
@@ -747,14 +756,6 @@ async def chat_completion_stream_generator(
747756
elif (group_channel == "commentary" and group_recipient
748757
and group_recipient.startswith("functions.")):
749758

750-
base_index = 0
751-
for msg in harmony_parser.messages:
752-
if (msg.channel == "commentary"
753-
and msg.recipient
754-
and msg.recipient.startswith(
755-
"functions.")):
756-
base_index += 1
757-
758759
if prev_recipient != group_recipient:
759760
tool_name = group_recipient.split(
760761
"functions.", 1)[1]
@@ -768,10 +769,12 @@ async def chat_completion_stream_generator(
768769
index=base_index,
769770
))
770771
prev_recipient = group_recipient
772+
# Increment index for next tool call
773+
base_index += 1
771774

772775
if group_text:
773776
tool_messages.append(DeltaToolCall(
774-
index=base_index,
777+
index=base_index - 1, # Use the index of the current tool call
775778
function=DeltaFunctionCall(
776779
arguments=group_text),
777780
))

0 commit comments

Comments
 (0)