fix Conversation roles must alternate user/assistant/user/assistant/... bug (#6880)

### What problem does this PR solve?

The old logic filters out all assistant messages from messages, which,
in multi-turn conversations, results in only user messages being
retained. This leads to an error in locally deployed models:
Conversation roles must alternate user/assistant/user/assistant/...

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
- [ ] New Feature (non-breaking change which adds functionality)
- [ ] Documentation Update
- [ ] Refactoring
- [ ] Performance Improvement
- [ ] Other (please describe):
This commit is contained in:
dylan 2025-04-09 17:21:27 +08:00 committed by GitHub
parent 08bc5d3521
commit 9d9f2dacd2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -240,8 +240,13 @@ def chat_completion_openai_like(tenant_id, chat_id):
dia = dia[0]
# Filter system and non-sense assistant messages
msg = None
msg = [m for m in messages if m["role"] != "system" and (m["role"] != "assistant" or msg)]
msg = []
for m in messages:
if m["role"] == "system":
continue
if m["role"] == "assistant" and not msg:
continue
msg.append(m)
# tools = get_tools()
# toolcall_session = SimpleFunctionCallServer()