1
+ # Copyright (c) "Neo4j"
2
+ # Neo4j Sweden AB [https://neo4j.com]
3
+ # #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ # #
8
+ # https://www.apache.org/licenses/LICENSE-2.0
9
+ # #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
1
15
import pytest
16
+ from pydantic import ValidationError
2
17
3
- from neo4j_graphrag .llm .utils import system_instruction_from_messages , \
4
- legacy_inputs_to_messages
18
+ from neo4j_graphrag .llm .utils import (
19
+ system_instruction_from_messages ,
20
+ legacy_inputs_to_messages ,
21
+ )
5
22
from neo4j_graphrag .message_history import InMemoryMessageHistory
6
23
from neo4j_graphrag .types import LLMMessage
7
24
8
25
9
- def test_system_instruction_from_messages ():
26
+ def test_system_instruction_from_messages () -> None :
10
27
messages = [
11
28
LLMMessage (role = "system" , content = "text" ),
12
29
]
@@ -21,66 +38,76 @@ def test_system_instruction_from_messages():
21
38
assert system_instruction_from_messages (messages ) is None
22
39
23
40
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
- ])
41
+ def test_legacy_inputs_to_messages_only_input_as_llm_message_list () -> None :
42
+ messages = legacy_inputs_to_messages (
43
+ input = [
44
+ LLMMessage (role = "user" , content = "text" ),
45
+ ]
46
+ )
28
47
assert messages == [
29
48
LLMMessage (role = "user" , content = "text" ),
30
49
]
31
50
32
51
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
- ))
52
+ def test_legacy_inputs_to_messages_only_input_as_message_history () -> None :
53
+ messages = legacy_inputs_to_messages (
54
+ input = InMemoryMessageHistory (
55
+ messages = [
56
+ LLMMessage (role = "user" , content = "text" ),
57
+ ]
58
+ )
59
+ )
39
60
assert messages == [
40
61
LLMMessage (role = "user" , content = "text" ),
41
62
]
42
63
43
64
44
- def test_legacy_inputs_to_messages_only_input_as_str ():
65
+ def test_legacy_inputs_to_messages_only_input_as_str () -> None :
45
66
messages = legacy_inputs_to_messages (input = "text" )
46
67
assert messages == [
47
68
LLMMessage (role = "user" , content = "text" ),
48
69
]
49
70
50
71
51
- def test_legacy_inputs_to_messages_input_as_str_and_message_history_as_llm_message_list ():
72
+ def test_legacy_inputs_to_messages_input_as_str_and_message_history_as_llm_message_list () -> (
73
+ None
74
+ ):
52
75
messages = legacy_inputs_to_messages (
53
76
input = "text" ,
54
77
message_history = [
55
78
LLMMessage (role = "assistant" , content = "How can I assist you today?" ),
56
- ]
79
+ ],
57
80
)
58
81
assert messages == [
59
82
LLMMessage (role = "assistant" , content = "How can I assist you today?" ),
60
83
LLMMessage (role = "user" , content = "text" ),
61
84
]
62
85
63
86
64
- def test_legacy_inputs_to_messages_input_as_str_and_message_history_as_message_history ():
87
+ def test_legacy_inputs_to_messages_input_as_str_and_message_history_as_message_history () -> (
88
+ None
89
+ ):
65
90
messages = legacy_inputs_to_messages (
66
91
input = "text" ,
67
- message_history = InMemoryMessageHistory (messages = [
68
- LLMMessage (role = "assistant" , content = "How can I assist you today?" ),
69
- ])
92
+ message_history = InMemoryMessageHistory (
93
+ messages = [
94
+ LLMMessage (role = "assistant" , content = "How can I assist you today?" ),
95
+ ]
96
+ ),
70
97
)
71
98
assert messages == [
72
99
LLMMessage (role = "assistant" , content = "How can I assist you today?" ),
73
100
LLMMessage (role = "user" , content = "text" ),
74
101
]
75
102
76
103
77
- def test_legacy_inputs_to_messages_with_explicit_system_instruction ():
104
+ def test_legacy_inputs_to_messages_with_explicit_system_instruction () -> None :
78
105
messages = legacy_inputs_to_messages (
79
106
input = "text" ,
80
107
message_history = [
81
108
LLMMessage (role = "assistant" , content = "How can I assist you today?" ),
82
109
],
83
- system_instruction = "You are a genius."
110
+ system_instruction = "You are a genius." ,
84
111
)
85
112
assert messages == [
86
113
LLMMessage (role = "system" , content = "You are a genius." ),
@@ -89,19 +116,29 @@ def test_legacy_inputs_to_messages_with_explicit_system_instruction():
89
116
]
90
117
91
118
92
- def test_legacy_inputs_to_messages_do_not_duplicate_system_instruction ():
119
+ def test_legacy_inputs_to_messages_do_not_duplicate_system_instruction () -> None :
93
120
with pytest .warns (
94
121
UserWarning ,
95
- match = "system_instruction provided but ignored as the message history already contains a system message"
122
+ match = "system_instruction provided but ignored as the message history already contains a system message" ,
96
123
):
97
124
messages = legacy_inputs_to_messages (
98
125
input = "text" ,
99
126
message_history = [
100
127
LLMMessage (role = "system" , content = "You are super smart." ),
101
128
],
102
- system_instruction = "You are a genius."
129
+ system_instruction = "You are a genius." ,
103
130
)
104
131
assert messages == [
105
132
LLMMessage (role = "system" , content = "You are super smart." ),
106
133
LLMMessage (role = "user" , content = "text" ),
107
134
]
135
+
136
+
137
+ def test_legacy_inputs_to_messages_wrong_type_in_message_list () -> None :
138
+ with pytest .raises (ValidationError , match = "Input should be a valid string" ):
139
+ legacy_inputs_to_messages (
140
+ input = "text" ,
141
+ message_history = [
142
+ {"role" : "system" , "content" : 10 }, # type: ignore
143
+ ],
144
+ )
0 commit comments