Skip to content

Commit 1cf93fc

Browse files
committed
Test for the utils functions
1 parent 5d83c2b commit 1cf93fc

File tree

2 files changed

+111
-11
lines changed

2 files changed

+111
-11
lines changed

src/neo4j_graphrag/llm/utils.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,21 @@ def legacy_inputs_to_messages(
1818
system_instruction: Optional[str] = None,
1919
) -> list[LLMMessage]:
2020
if message_history:
21-
warnings.warn(
22-
"Using message_history parameter is deprecated and will be removed in 2.0. Use a list of inputs or a MessageHistory instead.",
23-
DeprecationWarning,
24-
)
2521
if isinstance(message_history, MessageHistory):
2622
messages = message_history.messages
2723
else: # list[LLMMessage]
28-
messages = []
24+
messages = message_history
2925
else:
3026
messages = []
3127
if system_instruction is not None:
32-
warnings.warn(
33-
"Using system_instruction parameter is deprecated and will be removed in 2.0. Use a list of inputs or a MessageHistory instead.",
34-
DeprecationWarning,
35-
)
3628
if system_instruction_from_messages(messages) is not None:
3729
warnings.warn(
3830
"system_instruction provided but ignored as the message history already contains a system message",
39-
RuntimeWarning,
31+
UserWarning,
4032
)
4133
else:
42-
messages.append(
34+
messages.insert(
35+
0,
4336
LLMMessage(
4437
role="system",
4538
content=system_instruction,

tests/unit/llm/test_utils.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import pytest
2+
3+
from neo4j_graphrag.llm.utils import system_instruction_from_messages, \
4+
legacy_inputs_to_messages
5+
from neo4j_graphrag.message_history import InMemoryMessageHistory
6+
from neo4j_graphrag.types import LLMMessage
7+
8+
9+
def test_system_instruction_from_messages():
10+
messages = [
11+
LLMMessage(role="system", content="text"),
12+
]
13+
assert system_instruction_from_messages(messages) == "text"
14+
15+
messages = []
16+
assert system_instruction_from_messages(messages) is None
17+
18+
messages = [
19+
LLMMessage(role="assistant", content="text"),
20+
]
21+
assert system_instruction_from_messages(messages) is None
22+
23+
24+
def test_legacy_inputs_to_messages_only_input_as_llm_message_list():
25+
messages = legacy_inputs_to_messages(input=[
26+
LLMMessage(role="user", content="text"),
27+
])
28+
assert messages == [
29+
LLMMessage(role="user", content="text"),
30+
]
31+
32+
33+
def test_legacy_inputs_to_messages_only_input_as_message_history():
34+
messages = legacy_inputs_to_messages(input=InMemoryMessageHistory(
35+
messages=[
36+
LLMMessage(role="user", content="text"),
37+
]
38+
))
39+
assert messages == [
40+
LLMMessage(role="user", content="text"),
41+
]
42+
43+
44+
def test_legacy_inputs_to_messages_only_input_as_str():
45+
messages = legacy_inputs_to_messages(input="text")
46+
assert messages == [
47+
LLMMessage(role="user", content="text"),
48+
]
49+
50+
51+
def test_legacy_inputs_to_messages_input_as_str_and_message_history_as_llm_message_list():
52+
messages = legacy_inputs_to_messages(
53+
input="text",
54+
message_history=[
55+
LLMMessage(role="assistant", content="How can I assist you today?"),
56+
]
57+
)
58+
assert messages == [
59+
LLMMessage(role="assistant", content="How can I assist you today?"),
60+
LLMMessage(role="user", content="text"),
61+
]
62+
63+
64+
def test_legacy_inputs_to_messages_input_as_str_and_message_history_as_message_history():
65+
messages = legacy_inputs_to_messages(
66+
input="text",
67+
message_history=InMemoryMessageHistory(messages=[
68+
LLMMessage(role="assistant", content="How can I assist you today?"),
69+
])
70+
)
71+
assert messages == [
72+
LLMMessage(role="assistant", content="How can I assist you today?"),
73+
LLMMessage(role="user", content="text"),
74+
]
75+
76+
77+
def test_legacy_inputs_to_messages_with_explicit_system_instruction():
78+
messages = legacy_inputs_to_messages(
79+
input="text",
80+
message_history=[
81+
LLMMessage(role="assistant", content="How can I assist you today?"),
82+
],
83+
system_instruction="You are a genius."
84+
)
85+
assert messages == [
86+
LLMMessage(role="system", content="You are a genius."),
87+
LLMMessage(role="assistant", content="How can I assist you today?"),
88+
LLMMessage(role="user", content="text"),
89+
]
90+
91+
92+
def test_legacy_inputs_to_messages_do_not_duplicate_system_instruction():
93+
with pytest.warns(
94+
UserWarning,
95+
match="system_instruction provided but ignored as the message history already contains a system message"
96+
):
97+
messages = legacy_inputs_to_messages(
98+
input="text",
99+
message_history=[
100+
LLMMessage(role="system", content="You are super smart."),
101+
],
102+
system_instruction="You are a genius."
103+
)
104+
assert messages == [
105+
LLMMessage(role="system", content="You are super smart."),
106+
LLMMessage(role="user", content="text"),
107+
]

0 commit comments

Comments
 (0)