-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_llm_integration.py
More file actions
342 lines (248 loc) · 13.6 KB
/
test_llm_integration.py
File metadata and controls
342 lines (248 loc) · 13.6 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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
"""
Unit tests for LLM Integration from Tasks and Workflows Guide.
Tests LLM client injection and agent injection features documented in the guide.
Uses mocking to avoid requiring actual API keys.
"""
from unittest.mock import Mock, PropertyMock, patch
from graflow.core.context import TaskExecutionContext
from graflow.core.decorators import task
from graflow.core.workflow import workflow
from graflow.llm.agents.base import LLMAgent
from graflow.llm.client import LLMClient
class TestLLMClientInjection:
"""Tests for inject_llm_client=True"""
def test_llm_client_injection_basic(self):
"""Test basic LLM client injection"""
with patch("graflow.llm.client.LLMClient") as patching_llm_client:
mock_client = Mock(spec=LLMClient)
mock_client.completion_text.return_value = "Hello, I'm an AI assistant!"
patching_llm_client.return_value = mock_client
with workflow("llm_test") as wf:
@task(inject_llm_client=True)
def generate_text(llm_client: LLMClient):
response = llm_client.completion_text(
messages=[{"role": "user", "content": "Say hello"}], max_tokens=50
)
return response
_, ctx = wf.execute(ret_context=True)
result = ctx.get_result("generate_text")
assert result == "Hello, I'm an AI assistant!"
def test_llm_client_multiple_tasks(self):
"""Test LLM client injection across multiple tasks"""
with patch("graflow.llm.client.LLMClient") as patching_llm_client:
mock_client = Mock(spec=LLMClient)
mock_client.completion_text.side_effect = ["Greeting message", "Answer to question", "Summary text"]
patching_llm_client.return_value = mock_client
with workflow("multi_llm") as wf:
@task(inject_llm_client=True)
def task1(llm_client: LLMClient):
return llm_client.completion_text(messages=[{"role": "user", "content": "Greet"}], max_tokens=50)
@task(inject_llm_client=True)
def task2(llm_client: LLMClient):
return llm_client.completion_text(messages=[{"role": "user", "content": "Answer"}], max_tokens=50)
@task(inject_llm_client=True)
def task3(llm_client: LLMClient):
return llm_client.completion_text(
messages=[{"role": "user", "content": "Summarize"}], max_tokens=50
)
task1 >> task2 >> task3 # type: ignore
_, ctx = wf.execute(ret_context=True)
assert ctx.get_result("task1") == "Greeting message"
assert ctx.get_result("task2") == "Answer to question"
assert ctx.get_result("task3") == "Summary text"
def test_llm_client_with_context_injection(self):
"""Test combining LLM client injection with context injection"""
with patch("graflow.llm.client.LLMClient") as patching_llm_client:
mock_client = Mock(spec=LLMClient)
mock_client.completion_text.return_value = "AI response"
patching_llm_client.return_value = mock_client
with workflow("combined_injection") as wf:
@task(inject_context=True, inject_llm_client=True)
def process_with_llm(ctx: TaskExecutionContext, llm_client: LLMClient, prompt: str):
# Access both context and LLM client
channel = ctx.get_channel()
channel.set("prompt_used", prompt)
response = llm_client.completion_text(
messages=[{"role": "user", "content": prompt}], max_tokens=100
)
channel.set("llm_response", response)
return response
_, ctx = wf.execute(ret_context=True, initial_channel={"prompt": "Explain Python"})
result = ctx.get_result("process_with_llm")
assert result == "AI response"
assert ctx.get_channel().get("prompt_used") == "Explain Python"
assert ctx.get_channel().get("llm_response") == "AI response"
def test_llm_client_via_context_access(self):
"""Test accessing LLM client via context instead of injection"""
mock_client = Mock(spec=LLMClient)
mock_client.completion_text.return_value = "Context-accessed response"
with patch.object(TaskExecutionContext, "llm_client", new_callable=PropertyMock, return_value=mock_client):
with workflow("context_llm") as wf:
@task(inject_context=True)
def use_llm_via_context(ctx: TaskExecutionContext):
# Access LLM client through context
response = ctx.llm_client.completion_text(
messages=[{"role": "user", "content": "Test"}], max_tokens=50
)
return response
result = wf.execute()
assert result == "Context-accessed response"
# Note: This test verifies the API pattern even if llm_client
# property might not be fully implemented in context
class TestLLMAgentInjection:
"""Tests for inject_llm_agent='agent_name'"""
def test_llm_agent_registration_and_injection(self):
"""Test registering and injecting LLM agent"""
# Create mock agent
mock_agent = Mock()
mock_agent.run.return_value = {"output": "Agent result", "metadata": {}}
with workflow("agent_test") as wf:
# Register agent via workflow context (use factory pattern)
wf.register_llm_agent("my_agent", lambda ctx: mock_agent)
@task(inject_llm_agent="my_agent")
def use_agent(llm_agent: LLMAgent):
result = llm_agent.run("Process this query")
return result["output"]
result = wf.execute()
assert result == "Agent result"
mock_agent.run.assert_called_once_with("Process this query")
def test_llm_agent_with_tools(self):
"""Test LLM agent with tool calls"""
mock_agent = Mock()
mock_agent.run.return_value = {
"output": "Final answer after tool use",
"metadata": {
"tool_calls": [
{"tool": "search", "args": {"query": "test"}},
{"tool": "calculate", "args": {"expr": "2+2"}},
]
},
}
with workflow("agent_tools") as wf:
wf.register_llm_agent("tool_agent", lambda ctx: mock_agent)
@task(inject_llm_agent="tool_agent")
def complex_task(llm_agent: LLMAgent, query: str):
result = llm_agent.run(query)
return result
result = wf.execute(initial_channel={"query": "What is 2+2 in the context of search?"})
assert isinstance(result, dict)
assert result["output"] == "Final answer after tool use"
assert len(result["metadata"]["tool_calls"]) == 2
def test_multiple_agents_in_workflow(self):
"""Test workflow with multiple different agents"""
mock_agent1 = Mock()
mock_agent1.run.return_value = {"output": "Agent 1 result"}
mock_agent2 = Mock()
mock_agent2.run.return_value = {"output": "Agent 2 result"}
with workflow("multi_agent") as wf:
wf.register_llm_agent("analyzer", lambda ctx: mock_agent1)
wf.register_llm_agent("summarizer", lambda ctx: mock_agent2)
@task(id="analyze", inject_llm_agent="analyzer")
def analyze(llm_agent: LLMAgent):
return llm_agent.run("Analyze this")["output"]
@task(id="summarize", inject_llm_agent="summarizer")
def summarize(llm_agent: LLMAgent):
return llm_agent.run("Summarize this")["output"]
analyze >> summarize # type: ignore
_, ctx = wf.execute(ret_context=True)
assert ctx.get_result("analyze") == "Agent 1 result"
assert ctx.get_result("summarize") == "Agent 2 result"
def test_agent_with_context_injection(self):
"""Test combining agent injection with context injection"""
mock_agent = Mock()
mock_agent.run.return_value = {"output": "Agent response", "confidence": 0.95}
with workflow("agent_context") as wf:
wf.register_llm_agent("helper", lambda ctx: mock_agent)
@task(inject_context=True, inject_llm_agent="helper")
def process_with_agent(ctx: TaskExecutionContext, llm_agent: LLMAgent, data: dict):
# Use both context and agent
channel = ctx.get_channel()
result = llm_agent.run(f"Process: {data}")
channel.set("agent_confidence", result.get("confidence", 0))
return result["output"]
result = wf.execute(initial_channel={"data": {"value": 42}})
assert result == "Agent response"
class TestLLMIntegrationPatterns:
"""Test common LLM integration patterns from the guide"""
def test_multi_model_scenario(self):
"""Test using different models for different tasks"""
with patch("graflow.llm.client.LLMClient") as patching_llm_client:
# Different mock responses for different tasks
mock_client = Mock(spec=LLMClient)
def completion_side_effect(messages, model=None, **kwargs):
if model == "gpt-4o-mini":
return "Quick answer"
elif model == "gpt-4":
return "Detailed analysis"
return "Default response"
mock_client.completion_text.side_effect = completion_side_effect
patching_llm_client.return_value = mock_client
with workflow("multi_model") as wf:
@task(inject_llm_client=True)
def quick_task(llm_client: LLMClient):
# Use cheap model for simple task
return llm_client.completion_text(
messages=[{"role": "user", "content": "Quick question"}], model="gpt-4o-mini"
)
@task(inject_llm_client=True)
def complex_task(llm_client: LLMClient):
# Use expensive model for complex task
return llm_client.completion_text(
messages=[{"role": "user", "content": "Complex analysis"}], model="gpt-4"
)
quick_task >> complex_task # type: ignore
_, ctx = wf.execute(ret_context=True)
assert ctx.get_result("quick_task") == "Quick answer"
assert ctx.get_result("complex_task") == "Detailed analysis"
def test_llm_with_retry_pattern(self):
"""Test LLM task with retry pattern"""
with patch("graflow.llm.client.LLMClient") as patching_llm_client:
mock_client = Mock(spec=LLMClient)
call_count = {"count": 0}
def completion_with_retry(messages, **kwargs):
call_count["count"] += 1
if call_count["count"] < 2:
raise Exception("API error")
return "Success after retry"
mock_client.completion_text.side_effect = completion_with_retry
patching_llm_client.return_value = mock_client
with workflow("llm_retry") as wf:
@task(inject_context=True, inject_llm_client=True)
def retry_llm_call(ctx: TaskExecutionContext, llm_client: LLMClient):
channel = ctx.get_channel()
attempts = channel.get("attempts", default=0)
try:
response = llm_client.completion_text(messages=[{"role": "user", "content": "Test"}])
return response
except Exception as e:
if attempts < 2:
channel.set("attempts", attempts + 1)
ctx.next_iteration()
return None
raise e
result = wf.execute()
assert result == "Success after retry"
assert call_count["count"] == 2
def test_llm_response_to_channel(self):
"""Test storing LLM responses in channel for downstream tasks"""
with patch("graflow.llm.client.LLMClient") as patching_llm_client:
mock_client = Mock(spec=LLMClient)
mock_client.completion_text.return_value = "Analyzed data: positive sentiment"
patching_llm_client.return_value = mock_client
with workflow("llm_pipeline") as wf:
@task(inject_context=True, inject_llm_client=True)
def analyze(ctx: TaskExecutionContext, llm_client: LLMClient, text: str):
response = llm_client.completion_text(messages=[{"role": "user", "content": f"Analyze: {text}"}])
# Store in channel for downstream
ctx.get_channel().set("analysis", response)
return response
@task(inject_context=True)
def process_analysis(ctx: TaskExecutionContext):
analysis = ctx.get_channel().get("analysis")
# Process the LLM analysis
return {"processed": True, "analysis": analysis}
analyze >> process_analysis # type: ignore
_, ctx = wf.execute(ret_context=True, initial_channel={"text": "Great product!"})
result = ctx.get_result("process_analysis")
assert result["processed"] is True
assert "positive sentiment" in result["analysis"]