Skip to content

Commit c7aa1f4

Browse files
committed
Fixed base_index calculation innefficiency
1 parent 412546e commit c7aa1f4

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
@@ -809,6 +809,15 @@ async def chat_completion_stream_generator(
809809
combined_reasoning = ""
810810
tool_messages = []
811811

812+
# Calculate base_index once before the loop
813+
base_index = 0
814+
for msg in harmony_parser.messages:
815+
if (msg.channel == "commentary"
816+
and msg.recipient
817+
and msg.recipient.startswith(
818+
"functions.")):
819+
base_index += 1
820+
812821
for group in groups:
813822
group_channel = group['channel']
814823
group_recipient = group['recipient']
@@ -822,14 +831,6 @@ async def chat_completion_stream_generator(
822831
elif (group_channel == "commentary" and group_recipient
823832
and group_recipient.startswith("functions.")):
824833

825-
base_index = 0
826-
for msg in harmony_parser.messages:
827-
if (msg.channel == "commentary"
828-
and msg.recipient
829-
and msg.recipient.startswith(
830-
"functions.")):
831-
base_index += 1
832-
833834
if prev_recipient != group_recipient:
834835
tool_name = group_recipient.split(
835836
"functions.", 1)[1]
@@ -843,10 +844,12 @@ async def chat_completion_stream_generator(
843844
index=base_index,
844845
))
845846
prev_recipient = group_recipient
847+
# Increment index for next tool call
848+
base_index += 1
846849

847850
if group_text:
848851
tool_messages.append(DeltaToolCall(
849-
index=base_index,
852+
index=base_index - 1, # Use the index of the current tool call
850853
function=DeltaFunctionCall(
851854
arguments=group_text),
852855
))

0 commit comments

Comments
 (0)