Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: user message is string unless image included #420

Merged
merged 1 commit into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions mentat/conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,20 @@ def add_transcript_message(self, transcript_message: TranscriptMessage):

def add_user_message(self, message: str, image: Optional[str] = None):
"""Used for actual user input messages"""
content: List[ChatCompletionContentPartParam] = [
{
"type": "text",
"text": message,
},
]
content: List[ChatCompletionContentPartParam] | str = message
if image:
content.append(
content = [
{
"type": "text",
"text": message,
},
{
"type": "image_url",
"image_url": {
"url": image,
},
},
)
]
self.add_transcript_message(UserMessage(message=content, prior_messages=None))
self.add_message(ChatCompletionUserMessageParam(role="user", content=content))

Expand Down
2 changes: 1 addition & 1 deletion mentat/transcripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


class UserMessage(TypedDict):
message: list[ChatCompletionContentPartParam]
message: list[ChatCompletionContentPartParam] | str
# We need this field so that it is included when we convert to JSON
prior_messages: None

Expand Down
26 changes: 26 additions & 0 deletions tests/conversation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,29 @@ def test_no_parser_prompt(mock_call_llm_api):
assert len(conversation.get_messages()) == 1
config.no_parser_prompt = True
assert len(conversation.get_messages()) == 0


def test_add_user_message_with_and_without_image(mock_call_llm_api):
session_context = SESSION_CONTEXT.get()
conversation = session_context.conversation

# Test with image
test_message = "Hello, World!"
test_image_url = "http://example.com/image.png"
conversation.add_user_message(test_message, test_image_url)
messages_with_image = conversation.get_messages()
assert len(messages_with_image) == 2 # System prompt + user message
user_message_content_with_image = messages_with_image[-1]["content"]
assert len(user_message_content_with_image) == 2 # Text + image
assert user_message_content_with_image[0]["type"] == "text"
assert user_message_content_with_image[0]["text"] == test_message
assert user_message_content_with_image[1]["type"] == "image_url"
assert user_message_content_with_image[1]["image_url"]["url"] == test_image_url

# Test without image
conversation.clear_messages()
conversation.add_user_message(test_message)
messages_without_image = conversation.get_messages()
assert len(messages_without_image) == 2 # System prompt + user message
user_message_content_without_image = messages_without_image[-1]["content"]
assert user_message_content_without_image == test_message