-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
182 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
python_tests( | ||
name="tests", | ||
) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import pytest | ||
from llama_index.core.base.llms.types import ( | ||
ChatMessage, | ||
ImageBlock, | ||
MessageRole, | ||
TextBlock, | ||
) | ||
from llama_index.core.bridge.pydantic import BaseModel | ||
|
||
|
||
def test_chat_message_from_str(): | ||
m = ChatMessage.from_str(content="test content") | ||
assert m.content == "test content" | ||
assert len(m.blocks) == 1 | ||
assert type(m.blocks[0]) is TextBlock | ||
assert m.blocks[0].text == "test content" | ||
|
||
|
||
def test_chat_message_content_legacy_get(): | ||
m = ChatMessage(content="test content") | ||
assert m.content == "test content" | ||
assert len(m.blocks) == 1 | ||
assert type(m.blocks[0]) is TextBlock | ||
assert m.blocks[0].text == "test content" | ||
|
||
m = ChatMessage(role="user", content="test content") | ||
assert m.role == "user" | ||
assert m.content == "test content" | ||
assert len(m.blocks) == 1 | ||
assert type(m.blocks[0]) is TextBlock | ||
assert m.blocks[0].text == "test content" | ||
|
||
m = ChatMessage(content=[TextBlock(text="test content")]) | ||
assert m.content == "test content" | ||
assert len(m.blocks) == 1 | ||
assert type(m.blocks[0]) is TextBlock | ||
assert m.blocks[0].text == "test content" | ||
|
||
|
||
def test_chat_message_content_legacy_set(): | ||
m = ChatMessage() | ||
m.content = "test content" | ||
assert len(m.blocks) == 1 | ||
assert type(m.blocks[0]) is TextBlock | ||
assert m.blocks[0].text == "test content" | ||
|
||
m = ChatMessage(content="some original content") | ||
m.content = "test content" | ||
assert len(m.blocks) == 1 | ||
assert type(m.blocks[0]) is TextBlock | ||
assert m.blocks[0].text == "test content" | ||
|
||
m = ChatMessage(content=[TextBlock(text="test content"), ImageBlock()]) | ||
with pytest.raises(ValueError): | ||
m.content = "test content" | ||
|
||
|
||
def test_chat_message_content_returns_empty_string(): | ||
m = ChatMessage(content=[TextBlock(text="test content"), ImageBlock()]) | ||
assert m.content is None | ||
|
||
|
||
def test__str__(): | ||
assert str(ChatMessage(content="test content")) == "user: test content" | ||
|
||
|
||
def test_serializer(): | ||
class SimpleModel(BaseModel): | ||
some_field: str = "" | ||
|
||
m = ChatMessage( | ||
content="test content", | ||
additional_kwargs={"some_list": ["a", "b", "c"], "some_object": SimpleModel()}, | ||
) | ||
assert m.model_dump() == { | ||
"role": MessageRole.USER, | ||
"additional_kwargs": { | ||
"some_list": ["a", "b", "c"], | ||
"some_object": {"some_field": ""}, | ||
}, | ||
"blocks": [{"block_type": "text", "text": "test content"}], | ||
} | ||
|
||
|
||
def test_legacy_roundtrip(): | ||
legacy_message = { | ||
"role": MessageRole.USER, | ||
"content": "foo", | ||
"additional_kwargs": {}, | ||
} | ||
m = ChatMessage(**legacy_message) | ||
assert m.model_dump() == { | ||
"additional_kwargs": {}, | ||
"blocks": [{"block_type": "text", "text": "foo"}], | ||
"role": MessageRole.USER, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters