Spaces:
Running
Running
File size: 18,343 Bytes
193db9d 0bab47c 193db9d 0bab47c 193db9d 0bab47c 193db9d 0bab47c 193db9d 0bab47c 193db9d 38e3800 193db9d 0bab47c 193db9d 38e3800 193db9d 0bab47c 193db9d 38e3800 193db9d 0bab47c 193db9d 38e3800 193db9d 0bab47c 193db9d 38e3800 193db9d 38e3800 193db9d 38e3800 193db9d 38e3800 0bab47c 193db9d 0bab47c 193db9d 38e3800 193db9d 0bab47c 38e3800 193db9d 38e3800 193db9d 0bab47c 193db9d 0bab47c 193db9d 38e3800 193db9d 0bab47c 193db9d 0bab47c 193db9d 0bab47c 193db9d 0bab47c 193db9d 0bab47c 193db9d 0bab47c 193db9d 38e3800 193db9d 0bab47c 193db9d 38e3800 193db9d 0bab47c 193db9d 0bab47c 38e3800 193db9d 38e3800 193db9d 0bab47c 193db9d 0bab47c 193db9d 0bab47c 193db9d 0bab47c 193db9d 0bab47c 193db9d 0bab47c 193db9d 38e3800 0bab47c 38e3800 0bab47c 38e3800 0bab47c 38e3800 0bab47c |
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 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 |
import json
from unittest.mock import patch
import pytest
from workflows.errors import CyclicDependencyError, WorkflowError
from workflows.executors import (
create_processed_inputs,
execute_model_step,
execute_workflow,
)
from workflows.structs import CallType, InputField, ModelStep, OutputField, Workflow
# Tests for utility functions
lower = str.lower
upper = str.upper
# Tests for create_processed_inputs
def assert_model_step_result(result: dict, expected_result: dict):
# Verify the results
assert isinstance(result, dict)
assert "outputs" in result
assert "content" in result
assert "logprob" in result
assert result["outputs"] == expected_result["outputs"]
assert result["content"] == expected_result["content"]
assert result["logprob"] == expected_result["logprob"]
def assert_workflow_output(output: dict, expected_output: dict):
assert isinstance(output, dict)
for key in ["final_outputs", "intermediate_outputs", "step_contents", "logprob"]:
assert key in output
assert output[key] == expected_output[key]
def test_create_processed_inputs_basic():
"""Test basic input processing without transformations."""
step = ModelStep(
id="test_step",
name="Test Step",
model="gpt-4",
provider="OpenAI",
call_type=CallType.LLM,
system_prompt="Test prompt",
input_fields=[InputField(name="text", description="Input text", variable="input_text")],
output_fields=[],
)
available_vars = {"input_text": "Hello World"}
result = create_processed_inputs(step, available_vars)
assert result == {"text": "Hello World"}
def test_create_processed_inputs_with_transformation():
"""Test input processing with transformation functions."""
step = ModelStep(
id="test_step",
name="Test Step",
model="gpt-4",
provider="OpenAI",
call_type=CallType.LLM,
system_prompt="Test prompt",
input_fields=[
InputField(name="upper_text", description="Uppercase text", variable="input_text", func="upper"),
InputField(name="lower_text", description="Lowercase text", variable="input_caps", func="lower"),
],
output_fields=[],
)
available_vars = {"input_text": "hello", "input_caps": "WORLD"}
result = create_processed_inputs(step, available_vars)
assert result == {"upper_text": "HELLO", "lower_text": "world"}
def test_create_processed_inputs_missing_var():
"""Test that appropriate error is raised when a variable is missing."""
step = ModelStep(
id="test_step",
name="Test Step",
model="gpt-4",
provider="OpenAI",
call_type=CallType.LLM,
system_prompt="Test prompt",
input_fields=[InputField(name="text", description="Input text", variable="missing_var")],
output_fields=[],
)
available_vars = {"input_text": "Hello World"}
with pytest.raises(KeyError):
create_processed_inputs(step, available_vars)
def test_create_processed_inputs_unknown_func():
"""Test that appropriate error is raised when an unknown function is specified."""
step = ModelStep(
id="test_step",
name="Test Step",
model="gpt-4",
provider="OpenAI",
call_type=CallType.LLM,
system_prompt="Test prompt",
input_fields=[InputField(name="text", description="Input text", variable="input_text", func="unknown_func")],
output_fields=[],
)
available_vars = {"input_text": "Hello World"}
# This should raise an error when the function isn't found
with pytest.raises(Exception):
create_processed_inputs(step, available_vars)
# Tests for execute_model_step
@patch("workflows.executors.completion")
def test_execute_model_step_success(mock_completion):
"""Test successful execution of a model step with mocked litellm response."""
# Mock the litellm response
mock_response = {
"content": json.dumps({"summary": "This is a summary"}),
"output": {"summary": "This is a summary"},
}
mock_completion.return_value = mock_response
# Create a test step
step = ModelStep(
id="summarize",
name="Summarize Text",
model="gpt-3.5-turbo",
provider="OpenAI",
call_type=CallType.LLM,
system_prompt="Summarize the text",
input_fields=[InputField(name="text", description="Text to summarize", variable="input_text")],
output_fields=[OutputField(name="summary", description="Summary of the text", type="str")],
)
# Execute the step
result = execute_model_step(step, {"input_text": "Long text to be summarized..."})
# Verify the results
assert isinstance(result, dict)
assert "outputs" in result
assert "content" in result
assert "logprob" in result
assert result["outputs"] == {"summary": "This is a summary"}
assert result["content"] is None
assert result["logprob"] is None
# Verify the litellm call was made correctly
mock_completion.assert_called_once()
args, kwargs = mock_completion.call_args
assert kwargs["model"] == "OpenAI/gpt-3.5-turbo"
assert "Summarize the text" in kwargs["system"]
@patch("workflows.executors.completion")
def test_execute_model_step_with_full_content(mock_completion):
"""Test execution of a model step with full content returned."""
# Mock the litellm response
mock_response = {
"content": "Full model response content",
"output": {"summary": "This is a summary"},
}
mock_completion.return_value = mock_response
# Create a test step
step = ModelStep(
id="summarize",
name="Summarize Text",
model="gpt-3.5-turbo",
provider="OpenAI",
call_type=CallType.LLM,
system_prompt="Summarize the text",
input_fields=[InputField(name="text", description="Text to summarize", variable="input_text")],
output_fields=[OutputField(name="summary", description="Summary of the text", type="str")],
)
# Execute the step with return_full_content=True
result = execute_model_step(step, {"input_text": "Long text to be summarized..."}, return_full_content=True)
# Verify the results
assert isinstance(result, dict)
assert "outputs" in result
assert "content" in result
assert "logprob" in result
assert result["outputs"] == {"summary": "This is a summary"}
assert result["content"] == "Full model response content"
assert result["logprob"] is None
@patch("workflows.executors.completion")
def test_execute_model_step_with_logprobs(mock_completion):
"""Test execution of a model step with log probabilities."""
# Mock the litellm response with log probability
mock_response = {
"content": json.dumps({"summary": "This is a summary"}),
"output": {"summary": "This is a summary"},
"log_prob": -2.5,
}
mock_completion.return_value = mock_response
# Create a test step
step = ModelStep(
id="summarize",
name="Summarize Text",
model="gpt-3.5-turbo",
provider="OpenAI",
call_type=CallType.LLM,
system_prompt="Summarize the text",
input_fields=[InputField(name="text", description="Text to summarize", variable="input_text")],
output_fields=[OutputField(name="summary", description="Summary of the text", type="str")],
)
# Execute the step with logprobs=True
result = execute_model_step(step, {"input_text": "Long text to be summarized..."}, logprobs=True)
# Verify the results
assert isinstance(result, dict)
assert "outputs" in result
assert "content" in result
assert "logprob" in result
assert result["outputs"] == {"summary": "This is a summary"}
assert result["content"] is None
assert result["logprob"] == -2.5
@patch("workflows.executors.completion")
def test_execute_model_step_error(mock_completion):
"""Test handling of errors in model step execution."""
# Make litellm raise an exception
mock_completion.side_effect = Exception("API Error")
# Create a test step
step = ModelStep(
id="summarize",
name="Summarize Text",
model="gpt-3.5-turbo",
provider="OpenAI",
call_type=CallType.LLM,
system_prompt="Summarize the text",
input_fields=[InputField(name="text", description="Text to summarize", variable="input_text")],
output_fields=[OutputField(name="summary", description="Summary of the text", type="str")],
)
# Execute the step - should raise an exception
with pytest.raises(Exception):
execute_model_step(step, {"input_text": "Long text to be summarized..."})
# Tests for execute_workflow
@patch("workflows.executors.execute_model_step")
def test_execute_workflow_simple(mock_execute_step):
"""Test execution of a simple workflow with a single step."""
# Configure mock to return expected outputs
mock_result = {"outputs": {"summary": "This is a summary"}, "content": None, "logprob": None}
mock_execute_step.return_value = mock_result
# Create a simple workflow
step = ModelStep(
id="summarize",
name="Summarize Text",
model="gpt-3.5-turbo",
provider="OpenAI",
call_type=CallType.LLM,
system_prompt="Summarize the text",
input_fields=[InputField(name="text", description="Text to summarize", variable="input_text")],
output_fields=[OutputField(name="summary", description="Summary of the text", type="str")],
)
workflow = Workflow(steps={"summarize": step}, inputs=["input_text"], outputs={"summary": "summarize.summary"})
# Execute the workflow
result = execute_workflow(workflow, {"input_text": "Long text to be summarized..."})
# Verify the results
assert_workflow_output(
result,
{
"final_outputs": {"summary": "This is a summary"},
"intermediate_outputs": {
"input_text": "Long text to be summarized...",
"summarize.summary": "This is a summary",
},
"step_contents": {},
"logprob": None,
},
)
# Verify execute_model_step was called correctly
mock_execute_step.assert_called_once()
@patch("workflows.executors.execute_model_step")
def test_execute_workflow_multi_step(mock_execute_step):
"""Test execution of a multi-step workflow with dependencies."""
# Configure mock to return different values based on the step
def side_effect(step, available_vars, return_full_content=False, logprobs=False):
if step.id == "extract":
return {"outputs": {"entities": ["Apple", "product"]}, "content": None, "logprob": None}
elif step.id == "analyze":
return {"outputs": {"sentiment": "positive"}, "content": None, "logprob": None}
return {"outputs": {}, "content": None, "logprob": None}
mock_execute_step.side_effect = side_effect
# Create extract step
extract_step = ModelStep(
id="extract",
name="Extract Entities",
model="gpt-3.5-turbo",
provider="OpenAI",
call_type=CallType.LLM,
system_prompt="Extract entities",
input_fields=[InputField(name="text", description="Text to analyze", variable="input_text")],
output_fields=[OutputField(name="entities", description="Extracted entities", type="list[str]")],
)
# Create analyze step that depends on extract step
analyze_step = ModelStep(
id="analyze",
name="Analyze Sentiment",
model="gpt-4",
provider="OpenAI",
call_type=CallType.LLM,
system_prompt="Analyze sentiment",
input_fields=[InputField(name="entities", description="Entities to analyze", variable="extract.entities")],
output_fields=[OutputField(name="sentiment", description="Sentiment analysis", type="str")],
)
workflow = Workflow(
steps={"extract": extract_step, "analyze": analyze_step},
inputs=["input_text"],
outputs={"entities": "extract.entities", "sentiment": "analyze.sentiment"},
)
# Execute the workflow
result = execute_workflow(workflow, {"input_text": "Apple is launching a new product tomorrow."})
assert_workflow_output(
result,
{
"final_outputs": {"entities": ["Apple", "product"], "sentiment": "positive"},
"intermediate_outputs": {
"input_text": "Apple is launching a new product tomorrow.",
"extract.entities": ["Apple", "product"],
"analyze.sentiment": "positive",
},
"step_contents": {},
"logprob": None,
},
)
# Verify execute_model_step was called twice (once for each step)
assert mock_execute_step.call_count == 2
def test_execute_workflow_missing_input():
"""Test that an error is raised when a required input is missing."""
step = ModelStep(
id="summarize",
name="Summarize Text",
model="gpt-3.5-turbo",
provider="OpenAI",
call_type=CallType.LLM,
system_prompt="Summarize the text",
input_fields=[InputField(name="text", description="Text to summarize", variable="input_text")],
output_fields=[OutputField(name="summary", description="Summary of the text", type="str")],
)
workflow = Workflow(steps={"summarize": step}, inputs=["input_text"], outputs={"summary": "summarize.summary"})
# Execute with missing input
with pytest.raises(WorkflowError, match="Missing required workflow input"):
execute_workflow(workflow, {})
def test_execute_workflow_cyclic_dependency():
"""Test that a cyclic dependency in the workflow raises an appropriate error."""
# Make create_dependency_graph raise a CyclicDependencyError
step1 = ModelStep(
id="t1",
name="Test Step 1",
model="gpt-3.5-turbo",
provider="OpenAI",
call_type=CallType.LLM,
system_prompt="Test",
input_fields=[InputField(name="v1", description="", variable="t2.var")],
output_fields=[OutputField(name="out", description="")],
)
step2 = ModelStep(
id="t2",
name="Test Step 2",
model="gpt-3.5-turbo",
provider="OpenAI",
call_type=CallType.LLM,
system_prompt="Test",
input_fields=[InputField(name="v2", description="", variable="t1.out")],
output_fields=[OutputField(name="var", description="")],
)
workflow = Workflow(steps=[step1, step2], inputs=[], outputs={})
# This should propagate the CyclicDependencyError
with pytest.raises(CyclicDependencyError):
execute_workflow(workflow, {})
@patch("workflows.executors.execute_model_step")
def test_execute_workflow_with_full_content(mock_execute_step):
"""Test execution of a workflow with return_full_content=True."""
# Configure mock to return expected outputs and content
mock_result = {
"outputs": {"summary": "This is a summary"},
"content": "Full model response content",
"logprob": None,
}
mock_execute_step.return_value = mock_result
# Create a simple workflow
step = ModelStep(
id="summarize",
name="Summarize Text",
model="gpt-3.5-turbo",
provider="OpenAI",
call_type=CallType.LLM,
system_prompt="Summarize the text",
input_fields=[InputField(name="text", description="Text to summarize", variable="input_text")],
output_fields=[OutputField(name="summary", description="Summary of the text", type="str")],
)
workflow = Workflow(steps=[step], inputs=["input_text"], outputs={"summary": "summarize.summary"})
# Execute the workflow with return_full_content=True
inputs = {"input_text": "Long text to be summarized..."}
result = execute_workflow(workflow, inputs, return_full_content=True)
assert_workflow_output(
result,
{
"final_outputs": {"summary": "This is a summary"},
"intermediate_outputs": {
"input_text": "Long text to be summarized...",
"summarize.summary": "This is a summary",
},
"step_contents": {"summarize": "Full model response content"},
"logprob": None,
},
)
# Verify execute_model_step was called correctly with return_full_content=True
mock_execute_step.assert_called_once_with(step, inputs, return_full_content=True, logprobs=False)
@patch("workflows.executors.execute_model_step")
def test_execute_workflow_with_logprob(mock_execute_step):
"""Test execution of a workflow with logprob_step specified."""
# Configure mock to return expected outputs with logprob
mock_result = {"outputs": {"summary": "This is a summary"}, "content": None, "logprob": -2.5}
mock_execute_step.return_value = mock_result
# Create a simple workflow
step = ModelStep(
id="summarize",
name="Summarize Text",
model="gpt-3.5-turbo",
provider="OpenAI",
call_type=CallType.LLM,
system_prompt="Summarize the text",
input_fields=[InputField(name="text", description="Text to summarize", variable="input_text")],
output_fields=[OutputField(name="summary", description="Summary of the text", type="str")],
)
workflow = Workflow(steps={"summarize": step}, inputs=["input_text"], outputs={"summary": "summarize.summary"})
# Execute the workflow with logprob_step specified
result = execute_workflow(workflow, {"input_text": "Long text to be summarized..."}, logprob_step="summarize")
# Verify the results
assert_workflow_output(
result,
{
"final_outputs": {"summary": "This is a summary"},
"logprob": -2.5,
"intermediate_outputs": {
"input_text": "Long text to be summarized...",
"summarize.summary": "This is a summary",
},
"step_contents": {},
},
)
# Verify execute_model_step was called with logprobs=True
mock_execute_step.assert_called_once()
args, kwargs = mock_execute_step.call_args
assert kwargs["logprobs"] is True
|