fix: support assistant role in message conversion
- The _Converter.items_to_messages method was incorrectly rejecting 'assistant' as a valid role in conversation messages, causing runtime errors when processing standard chat completion message formats. - This fix enables proper handling of complete conversation contexts that include both user and assistant messages.
This commit is contained in:
parent
c8f3cdd6c8
commit
51d79bf141
2 changed files with 39 additions and 0 deletions
|
|
@ -808,6 +808,13 @@ class _Converter:
|
||||||
"content": cls.extract_text_content(content),
|
"content": cls.extract_text_content(content),
|
||||||
}
|
}
|
||||||
result.append(msg_developer)
|
result.append(msg_developer)
|
||||||
|
elif role == "assistant":
|
||||||
|
flush_assistant_message()
|
||||||
|
msg_assistant: ChatCompletionAssistantMessageParam = {
|
||||||
|
"role": "assistant",
|
||||||
|
"content": cls.extract_text_content(content),
|
||||||
|
}
|
||||||
|
result.append(msg_assistant)
|
||||||
else:
|
else:
|
||||||
raise UserError(f"Unexpected role in easy_input_message: {role}")
|
raise UserError(f"Unexpected role in easy_input_message: {role}")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -393,3 +393,35 @@ def test_unknown_object_errors():
|
||||||
with pytest.raises(UserError, match="Unhandled item type or structure"):
|
with pytest.raises(UserError, match="Unhandled item type or structure"):
|
||||||
# Purposely ignore the type error
|
# Purposely ignore the type error
|
||||||
_Converter.items_to_messages([TestObject()]) # type: ignore
|
_Converter.items_to_messages([TestObject()]) # type: ignore
|
||||||
|
|
||||||
|
|
||||||
|
def test_assistant_messages_in_history():
|
||||||
|
"""
|
||||||
|
Test that assistant messages are added to the history.
|
||||||
|
"""
|
||||||
|
messages = _Converter.items_to_messages(
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"role": "user",
|
||||||
|
"content": "Hello",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "assistant",
|
||||||
|
"content": "Hello?",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "user",
|
||||||
|
"content": "What was my Name?",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
# OUTPUT is [{'role': 'user', 'content': 'Hello'}, {'role': 'assistant', 'content': 'Hello?'}, {'role': 'user', 'content': 'What was my Name?'}]
|
||||||
|
assert messages == [{'role': 'user', 'content': 'Hello'}, {'role': 'assistant', 'content': 'Hello?'}, {'role': 'user', 'content': 'What was my Name?'}]
|
||||||
|
assert len(messages) == 3
|
||||||
|
assert messages[0]["role"] == "user"
|
||||||
|
assert messages[0]["content"] == "Hello"
|
||||||
|
assert messages[1]["role"] == "assistant"
|
||||||
|
assert messages[1]["content"] == "Hello?"
|
||||||
|
assert messages[2]["role"] == "user"
|
||||||
|
assert messages[2]["content"] == "What was my Name?"
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue