-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathtest_generate_answer_node.py
270 lines (219 loc) · 8.98 KB
/
test_generate_answer_node.py
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import json
import pytest
from langchain.prompts import (
PromptTemplate,
)
from langchain_community.chat_models import (
ChatOllama,
)
from langchain_core.runnables import (
RunnableParallel,
)
from requests.exceptions import (
Timeout,
)
from scrapegraphai.nodes.generate_answer_node import (
GenerateAnswerNode,
)
class DummyLLM:
def __call__(self, *args, **kwargs):
return "dummy response"
class DummyLogger:
def info(self, msg):
pass
def error(self, msg):
pass
@pytest.fixture
def dummy_node():
"""
Fixture for a GenerateAnswerNode instance using DummyLLM.
Uses a valid input keys string ("dummy_input & doc") to avoid parsing errors.
"""
node_config = {"llm_model": DummyLLM(), "verbose": False, "timeout": 1}
node = GenerateAnswerNode("dummy_input & doc", ["output"], node_config=node_config)
node.logger = DummyLogger()
node.get_input_keys = lambda state: ["dummy_input", "doc"]
return node
def test_process_missing_content_and_user_prompt(dummy_node):
"""
Test that process() raises a ValueError when either the content or the user prompt is missing.
"""
state_missing_content = {"user_prompt": "What is the answer?"}
with pytest.raises(ValueError) as excinfo1:
dummy_node.process(state_missing_content)
assert "No content found in state" in str(excinfo1.value)
state_missing_prompt = {"content": "Some valid context content"}
with pytest.raises(ValueError) as excinfo2:
dummy_node.process(state_missing_prompt)
assert "No user prompt found in state" in str(excinfo2.value)
class DummyLLMWithPipe:
"""DummyLLM that supports the pipe '|' operator.
When used in a chain with a PromptTemplate, the pipe operator returns self,
simulating chain composition."""
def __or__(self, other):
return self
def __call__(self, *args, **kwargs):
return {"content": "script single-chunk answer"}
@pytest.fixture
def dummy_node_with_pipe():
"""
Fixture for a GenerateAnswerNode instance using DummyLLMWithPipe.
Uses a valid input keys string ("dummy_input & doc") to avoid parsing errors.
"""
node_config = {"llm_model": DummyLLMWithPipe(), "verbose": False, "timeout": 480}
node = GenerateAnswerNode("dummy_input & doc", ["output"], node_config=node_config)
node.logger = DummyLogger()
node.get_input_keys = lambda state: ["dummy_input", "doc"]
return node
def test_execute_multiple_chunks(dummy_node_with_pipe):
"""
Test the execute() method for a scenario with multiple document chunks.
It simulates parallel processing of chunks and then merges them.
"""
state = {
"dummy_input": "What is the final answer?",
"doc": ["Chunk text 1", "Chunk text 2"],
}
def fake_invoke_with_timeout(chain, inputs, timeout):
if isinstance(chain, RunnableParallel):
return {
"chunk1": {"content": "answer for chunk 1"},
"chunk2": {"content": "answer for chunk 2"},
}
if "context" in inputs and "question" in inputs:
return {"content": "merged final answer"}
return {"content": "single answer"}
dummy_node_with_pipe.invoke_with_timeout = fake_invoke_with_timeout
output_state = dummy_node_with_pipe.execute(state)
assert output_state["output"] == {"content": "merged final answer"}
def test_execute_single_chunk(dummy_node_with_pipe):
"""
Test the execute() method for a single document chunk.
"""
state = {"dummy_input": "What is the answer?", "doc": ["Only one chunk text"]}
def fake_invoke_with_timeout(chain, inputs, timeout):
if "question" in inputs:
return {"content": "single-chunk answer"}
return {"content": "unexpected result"}
dummy_node_with_pipe.invoke_with_timeout = fake_invoke_with_timeout
output_state = dummy_node_with_pipe.execute(state)
assert output_state["output"] == {"content": "single-chunk answer"}
def test_execute_merge_json_decode_error(dummy_node_with_pipe):
"""
Test that execute() handles a JSONDecodeError in the merge chain properly.
"""
state = {
"dummy_input": "What is the final answer?",
"doc": ["Chunk 1 text", "Chunk 2 text"],
}
def fake_invoke_with_timeout(chain, inputs, timeout):
if isinstance(chain, RunnableParallel):
return {
"chunk1": {"content": "answer for chunk 1"},
"chunk2": {"content": "answer for chunk 2"},
}
if "context" in inputs and "question" in inputs:
raise json.JSONDecodeError("Invalid JSON", "", 0)
return {"content": "unexpected response"}
dummy_node_with_pipe.invoke_with_timeout = fake_invoke_with_timeout
output_state = dummy_node_with_pipe.execute(state)
assert "error" in output_state["output"]
assert (
"Invalid JSON response format during merge" in output_state["output"]["error"]
)
class DummyChain:
"""A dummy chain for simulating a chain's invoke behavior.
Returns a successful answer in the expected format."""
def invoke(self, inputs):
return {"content": "successful answer"}
@pytest.fixture
def dummy_node_for_process():
"""
Fixture for creating a GenerateAnswerNode instance for testing the process() method success case.
"""
node_config = {"llm_model": DummyChain(), "verbose": False, "timeout": 1}
node = GenerateAnswerNode(
"user_prompt & content", ["output"], node_config=node_config
)
node.logger = DummyLogger()
node.get_input_keys = lambda state: ["user_prompt", "content"]
return node
def test_process_success(dummy_node_for_process):
"""
Test that process() successfully generates an answer when both user prompt and content are provided.
"""
state = {
"user_prompt": "What is the answer?",
"content": "This is some valid context.",
}
dummy_node_for_process.chain = DummyChain()
dummy_node_for_process.invoke_with_timeout = (
lambda chain, inputs, timeout: chain.invoke(inputs)
)
new_state = dummy_node_for_process.process(state)
assert new_state["output"] == {"content": "successful answer"}
def test_execute_timeout_single_chunk(dummy_node_with_pipe):
"""
Test that execute() properly handles a Timeout exception in the single chunk branch.
"""
state = {"dummy_input": "What is the answer?", "doc": ["Only one chunk text"]}
def fake_invoke_timeout(chain, inputs, timeout):
raise Timeout("Simulated timeout error")
dummy_node_with_pipe.invoke_with_timeout = fake_invoke_timeout
output_state = dummy_node_with_pipe.execute(state)
assert "error" in output_state["output"]
assert "Response timeout exceeded" in output_state["output"]["error"]
assert "Simulated timeout error" in output_state["output"]["raw_response"]
def test_execute_script_creator_single_chunk():
"""
Test the execute() method for the scenario when script_creator mode is enabled.
This verifies that the non-markdown prompt templates branch is executed and the expected answer is generated.
"""
node_config = {
"llm_model": DummyLLMWithPipe(),
"verbose": False,
"timeout": 480,
"script_creator": True,
"force": False,
"is_md_scraper": False,
"additional_info": "TEST INFO: ",
}
node = GenerateAnswerNode("dummy_input & doc", ["output"], node_config=node_config)
node.logger = DummyLogger()
node.get_input_keys = lambda state: ["dummy_input", "doc"]
state = {
"dummy_input": "What is the script answer?",
"doc": ["Only one chunk script"],
}
def fake_invoke_with_timeout(chain, inputs, timeout):
if "question" in inputs:
return {"content": "script single-chunk answer"}
return {"content": "unexpected response"}
node.invoke_with_timeout = fake_invoke_with_timeout
output_state = node.execute(state)
assert output_state["output"] == {"content": "script single-chunk answer"}
class DummyChatOllama(ChatOllama):
"""A dummy ChatOllama class to simulate ChatOllama behavior."""
class DummySchema:
"""A dummy schema class with a model_json_schema method."""
def model_json_schema(self):
return "dummy_schema_json"
def test_init_chat_ollama_format():
"""
Test that the __init__ method of GenerateAnswerNode sets the format attribute of a ChatOllama LLM correctly.
"""
dummy_llm = DummyChatOllama()
node_config = {"llm_model": dummy_llm, "verbose": False, "timeout": 1}
node = GenerateAnswerNode("dummy_input", ["output"], node_config=node_config)
assert node.llm_model.format == "json"
dummy_llm_with_schema = DummyChatOllama()
node_config_with_schema = {
"llm_model": dummy_llm_with_schema,
"verbose": False,
"timeout": 1,
"schema": DummySchema(),
}
node2 = GenerateAnswerNode(
"dummy_input", ["output"], node_config=node_config_with_schema
)
assert node2.llm_model.format == "dummy_schema_json"