Skip to content

Commit

Permalink
merge adjacent messages with same role
Browse files Browse the repository at this point in the history
  • Loading branch information
ericcccsliu committed Mar 31, 2024
1 parent 33f04ed commit 92d9d42
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion api/utils/llm_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@

async def generate_response_stream(conversation):
visible_messages = [message for message in conversation.messages if not message.hidden]
conversation.messages = visible_messages
# Merge adjacent messages with the same role
merged_messages = []
for message in visible_messages:
if merged_messages and merged_messages[-1].role == message.role:
merged_messages[-1].content += "\n" + message.content
else:
merged_messages.append(message)

conversation.messages = merged_messages
collected_chunks = []
if conversation.model.provider == "openai":
async for chunk in openai_generate_response(conversation):
Expand Down

0 comments on commit 92d9d42

Please sign in to comment.