forked from neo4j/neo4j-graphrag-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_base.py
More file actions
221 lines (197 loc) · 6.8 KB
/
test_base.py
File metadata and controls
221 lines (197 loc) · 6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
"""The base LLMInterface is responsible
for formatting the inputs as a list of LLMMessage objects
and handling the rate limits. This is what is being tested
in this file.
"""
from typing import Type, Generator
from unittest import mock
from unittest.mock import patch, Mock, call
import pytest
import tenacity
from joblib.testing import fixture
from pydantic import ValidationError
from neo4j_graphrag.exceptions import LLMGenerationError
from neo4j_graphrag.llm import LLMInterface, LLMResponse
from neo4j_graphrag.types import LLMMessage
@fixture(scope="module") # type: ignore[misc]
def llm_interface() -> Generator[Type[LLMInterface], None, None]:
class CustomLLMInterface(LLMInterface):
pass
yield CustomLLMInterface
@patch("neo4j_graphrag.llm.base.legacy_inputs_to_messages")
def test_base_llm_interface_invoke_with_input_as_str(
mock_inputs: Mock, llm_interface: Type[LLMInterface]
) -> None:
mock_inputs.return_value = [
LLMMessage(
role="user",
content="return value of the legacy_inputs_to_messages function",
)
]
llm = llm_interface(model_name="test")
message_history = [
LLMMessage(
**{"role": "user", "content": "When does the sun come up in the summer?"}
),
LLMMessage(**{"role": "assistant", "content": "Usually around 6am."}),
]
question = "What about next season?"
system_instruction = "You are a genius."
with patch.object(llm, "_invoke") as mock_invoke:
with pytest.warns(DeprecationWarning) as record:
llm.invoke(question, message_history, system_instruction)
mock_invoke.assert_called_once_with(
[
LLMMessage(
role="user",
content="return value of the legacy_inputs_to_messages function",
)
]
)
mock_inputs.assert_called_once_with(
question,
message_history,
system_instruction,
)
assert len(record) == 2
assert (
"Using 'message_history' in the llm.invoke method is deprecated"
in record[0].message.args[0] # type: ignore[union-attr]
)
assert (
"Using 'system_instruction' in the llm.invoke method is deprecated"
in record[1].message.args[0] # type: ignore[union-attr]
)
@patch("neo4j_graphrag.llm.base.legacy_inputs_to_messages")
def test_base_llm_interface_invoke_with_invalid_inputs(
mock_inputs: Mock, llm_interface: Type[LLMInterface]
) -> None:
mock_inputs.side_effect = [
ValidationError.from_exception_data("Invalid data", line_errors=[])
]
llm = llm_interface(model_name="test")
question = "What about next season?"
with pytest.raises(LLMGenerationError, match="Input validation failed"):
llm.invoke(question)
mock_inputs.assert_called_once_with(
question,
None,
None,
)
@patch("neo4j_graphrag.llm.base.legacy_inputs_to_messages")
def test_base_llm_interface_invoke_with_tools_with_input_as_str(
mock_inputs: Mock, llm_interface: Type[LLMInterface]
) -> None:
mock_inputs.return_value = [
LLMMessage(
role="user",
content="return value of the legacy_inputs_to_messages function",
)
]
llm = llm_interface(model_name="test")
message_history = [
LLMMessage(
**{"role": "user", "content": "When does the sun come up in the summer?"}
),
LLMMessage(**{"role": "assistant", "content": "Usually around 6am."}),
]
question = "What about next season?"
system_instruction = "You are a genius."
with patch.object(llm, "_invoke_with_tools") as mock_invoke:
llm.invoke_with_tools(question, [], message_history, system_instruction)
mock_invoke.assert_called_once_with(
[
LLMMessage(
role="user",
content="return value of the legacy_inputs_to_messages function",
)
],
[], # tools
)
mock_inputs.assert_called_once_with(
question,
message_history,
system_instruction,
)
@patch("neo4j_graphrag.llm.base.legacy_inputs_to_messages")
def test_base_llm_interface_invoke_with_tools_with_invalid_inputs(
mock_inputs: Mock, llm_interface: Type[LLMInterface]
) -> None:
mock_inputs.side_effect = [
ValidationError.from_exception_data("Invalid data", line_errors=[])
]
llm = llm_interface(model_name="test")
question = "What about next season?"
with pytest.raises(LLMGenerationError, match="Input validation failed"):
llm.invoke_with_tools(question, [])
mock_inputs.assert_called_once_with(
question,
None,
None,
)
@patch("neo4j_graphrag.llm.base.legacy_inputs_to_messages")
def test_base_llm_interface_invoke_retry_ok(
mock_inputs: Mock, llm_interface: Type[LLMInterface]
) -> None:
mock_inputs.return_value = [
LLMMessage(
role="user",
content="return value of the legacy_inputs_to_messages function",
)
]
llm = llm_interface(model_name="test")
question = "What about next season?"
with mock.patch.object(llm, "_invoke") as mock_invoke_core:
mock_invoke_core.side_effect = [
LLMGenerationError("rate limit"),
LLMResponse(content="all good"),
]
res = llm.invoke(question, [])
assert res.content == "all good"
call_args = [
{
"role": "user",
"content": "return value of the legacy_inputs_to_messages function",
}
]
assert mock_invoke_core.call_count == 2
mock_invoke_core.assert_has_calls(
[
call(call_args),
call(call_args),
]
)
@patch("neo4j_graphrag.llm.base.legacy_inputs_to_messages")
def test_base_llm_interface_invoke_retry_fail(
mock_inputs: Mock, llm_interface: Type[LLMInterface]
) -> None:
mock_inputs.return_value = [
LLMMessage(
role="user",
content="return value of the legacy_inputs_to_messages function",
)
]
llm = llm_interface(model_name="test")
question = "What about next season?"
with mock.patch.object(llm, "_invoke") as mock_invoke_core:
mock_invoke_core.side_effect = [
LLMGenerationError("rate limit"),
LLMGenerationError("rate limit"),
LLMGenerationError("rate limit"),
]
with pytest.raises(tenacity.RetryError):
llm.invoke(question, [])
call_args = [
{
"role": "user",
"content": "return value of the legacy_inputs_to_messages function",
}
]
assert mock_invoke_core.call_count == 3
mock_invoke_core.assert_has_calls(
[
call(call_args),
call(call_args),
call(call_args),
]
)