|
17 | 17 |
|
18 | 18 | import asyncio |
19 | 19 | import json |
20 | | -import math |
21 | 20 | from json.decoder import JSONDecodeError |
| 21 | +from typing import AsyncIterator |
22 | 22 |
|
23 | 23 | import pytest |
24 | 24 |
|
@@ -250,3 +250,256 @@ async def test_streaming_output_rails_default_config_not_blocked_at_start( |
250 | 250 | json.loads(chunks[0]) |
251 | 251 |
|
252 | 252 | await asyncio.gather(*asyncio.all_tasks() - {asyncio.current_task()}) |
| 253 | + |
| 254 | + |
| 255 | +async def simple_token_generator() -> AsyncIterator[str]: |
| 256 | + """Simple generator that yields tokens.""" |
| 257 | + tokens = ["Hello", " ", "world", "!"] |
| 258 | + for token in tokens: |
| 259 | + yield token |
| 260 | + |
| 261 | + |
| 262 | +async def offensive_token_generator() -> AsyncIterator[str]: |
| 263 | + """Generator that yields potentially offensive content.""" |
| 264 | + |
| 265 | + tokens = ["This", " ", "is", " ", "offensive", " ", "content", " ", "idiot", "!"] |
| 266 | + for token in tokens: |
| 267 | + yield token |
| 268 | + |
| 269 | + |
| 270 | +@pytest.mark.asyncio |
| 271 | +async def test_external_generator_without_output_rails(): |
| 272 | + """Test that external generator works without output rails.""" |
| 273 | + config = RailsConfig.from_content( |
| 274 | + config={ |
| 275 | + "models": [], |
| 276 | + "rails": {}, |
| 277 | + "streaming": True, |
| 278 | + } |
| 279 | + ) |
| 280 | + |
| 281 | + rails = LLMRails(config) |
| 282 | + |
| 283 | + tokens = [] |
| 284 | + async for token in rails.stream_async(generator=simple_token_generator()): |
| 285 | + tokens.append(token) |
| 286 | + |
| 287 | + assert tokens == ["Hello", " ", "world", "!"] |
| 288 | + assert "".join(tokens) == "Hello world!" |
| 289 | + |
| 290 | + |
| 291 | +@pytest.mark.asyncio |
| 292 | +async def test_external_generator_with_output_rails_allowed(): |
| 293 | + """Test that external generator works with output rails that allow content.""" |
| 294 | + config = RailsConfig.from_content( |
| 295 | + config={ |
| 296 | + "models": [], |
| 297 | + "rails": { |
| 298 | + "output": { |
| 299 | + "flows": ["self check output"], |
| 300 | + "streaming": { |
| 301 | + "enabled": True, |
| 302 | + "chunk_size": 4, |
| 303 | + "context_size": 2, |
| 304 | + "stream_first": False, |
| 305 | + }, |
| 306 | + } |
| 307 | + }, |
| 308 | + "streaming": True, |
| 309 | + "prompts": [ |
| 310 | + {"task": "self_check_output", "content": "Check: {{ bot_response }}"} |
| 311 | + ], |
| 312 | + }, |
| 313 | + colang_content=""" |
| 314 | + define flow self check output |
| 315 | + execute self_check_output |
| 316 | + """, |
| 317 | + ) |
| 318 | + |
| 319 | + rails = LLMRails(config) |
| 320 | + |
| 321 | + @action(name="self_check_output") |
| 322 | + async def self_check_output(**kwargs): |
| 323 | + return True |
| 324 | + |
| 325 | + rails.register_action(self_check_output, "self_check_output") |
| 326 | + |
| 327 | + tokens = [] |
| 328 | + async for token in rails.stream_async( |
| 329 | + generator=simple_token_generator(), |
| 330 | + messages=[{"role": "user", "content": "Hello"}], |
| 331 | + ): |
| 332 | + tokens.append(token) |
| 333 | + |
| 334 | + assert tokens == ["Hello", " ", "world", "!"] |
| 335 | + |
| 336 | + |
| 337 | +@pytest.mark.asyncio |
| 338 | +async def test_external_generator_with_output_rails_blocked(): |
| 339 | + """Test that external generator content can be blocked by output rails.""" |
| 340 | + config = RailsConfig.from_content( |
| 341 | + config={ |
| 342 | + "models": [], |
| 343 | + "rails": { |
| 344 | + "output": { |
| 345 | + "flows": ["self check output"], |
| 346 | + "streaming": { |
| 347 | + "enabled": True, |
| 348 | + "chunk_size": 6, |
| 349 | + "context_size": 2, |
| 350 | + "stream_first": False, |
| 351 | + }, |
| 352 | + } |
| 353 | + }, |
| 354 | + "streaming": True, |
| 355 | + "prompts": [ |
| 356 | + {"task": "self_check_output", "content": "Check: {{ bot_response }}"} |
| 357 | + ], |
| 358 | + }, |
| 359 | + colang_content=""" |
| 360 | + define flow self check output |
| 361 | + execute self_check_output |
| 362 | + """, |
| 363 | + ) |
| 364 | + |
| 365 | + rails = LLMRails(config) |
| 366 | + |
| 367 | + @action(name="self_check_output") |
| 368 | + async def self_check_output(**kwargs): |
| 369 | + bot_message = kwargs.get( |
| 370 | + "bot_message", kwargs.get("context", {}).get("bot_message", "") |
| 371 | + ) |
| 372 | + # block if message contains "offensive" or "idiot" |
| 373 | + if "offensive" in bot_message.lower() or "idiot" in bot_message.lower(): |
| 374 | + return False |
| 375 | + return True |
| 376 | + |
| 377 | + rails.register_action(self_check_output, "self_check_output") |
| 378 | + |
| 379 | + tokens = [] |
| 380 | + error_received = False |
| 381 | + |
| 382 | + async for token in rails.stream_async( |
| 383 | + generator=offensive_token_generator(), |
| 384 | + messages=[{"role": "user", "content": "Generate something"}], |
| 385 | + ): |
| 386 | + if isinstance(token, str) and token.startswith('{"error"'): |
| 387 | + error_received = True |
| 388 | + break |
| 389 | + tokens.append(token) |
| 390 | + |
| 391 | + assert error_received, "Expected to receive an error JSON when content is blocked" |
| 392 | + assert len(tokens) == 0 |
| 393 | + |
| 394 | + |
| 395 | +@pytest.mark.asyncio |
| 396 | +async def test_external_generator_with_custom_llm(): |
| 397 | + """Test using external generator as a custom LLM replacement.""" |
| 398 | + |
| 399 | + async def custom_llm_generator(messages): |
| 400 | + """Simulate a custom LLM that generates based on input.""" |
| 401 | + |
| 402 | + user_message = messages[-1]["content"] if messages else "" |
| 403 | + |
| 404 | + if "weather" in user_message.lower(): |
| 405 | + response = "The weather is sunny today!" |
| 406 | + elif "name" in user_message.lower(): |
| 407 | + response = "I am an AI assistant." |
| 408 | + else: |
| 409 | + response = "I can help you with that." |
| 410 | + |
| 411 | + for token in response.split(" "): |
| 412 | + yield token + " " |
| 413 | + |
| 414 | + config = RailsConfig.from_content( |
| 415 | + config={ |
| 416 | + "models": [], |
| 417 | + "rails": {}, |
| 418 | + "streaming": True, |
| 419 | + } |
| 420 | + ) |
| 421 | + |
| 422 | + rails = LLMRails(config) |
| 423 | + |
| 424 | + messages = [{"role": "user", "content": "What's the weather?"}] |
| 425 | + tokens = [] |
| 426 | + |
| 427 | + async for token in rails.stream_async( |
| 428 | + generator=custom_llm_generator(messages), messages=messages |
| 429 | + ): |
| 430 | + tokens.append(token) |
| 431 | + |
| 432 | + result = "".join(tokens).strip() |
| 433 | + assert result == "The weather is sunny today!" |
| 434 | + |
| 435 | + |
| 436 | +@pytest.mark.asyncio |
| 437 | +async def test_external_generator_empty_stream(): |
| 438 | + """Test that empty generator streams work correctly.""" |
| 439 | + |
| 440 | + async def empty_generator(): |
| 441 | + if False: |
| 442 | + yield |
| 443 | + |
| 444 | + config = RailsConfig.from_content( |
| 445 | + config={ |
| 446 | + "models": [], |
| 447 | + "rails": {}, |
| 448 | + "streaming": True, |
| 449 | + } |
| 450 | + ) |
| 451 | + |
| 452 | + rails = LLMRails(config) |
| 453 | + |
| 454 | + tokens = [] |
| 455 | + async for token in rails.stream_async(generator=empty_generator()): |
| 456 | + tokens.append(token) |
| 457 | + |
| 458 | + assert tokens == [] |
| 459 | + |
| 460 | + |
| 461 | +@pytest.mark.asyncio |
| 462 | +async def test_external_generator_single_chunk(): |
| 463 | + """Test generator that yields a single large chunk.""" |
| 464 | + |
| 465 | + async def single_chunk_generator(): |
| 466 | + yield "This is a complete response in a single chunk." |
| 467 | + |
| 468 | + config = RailsConfig.from_content( |
| 469 | + config={ |
| 470 | + "models": [], |
| 471 | + "rails": { |
| 472 | + "output": { |
| 473 | + "flows": ["self check output"], |
| 474 | + "streaming": { |
| 475 | + "enabled": True, |
| 476 | + "chunk_size": 10, |
| 477 | + "context_size": 5, |
| 478 | + "stream_first": True, |
| 479 | + }, |
| 480 | + } |
| 481 | + }, |
| 482 | + "streaming": True, |
| 483 | + "prompts": [ |
| 484 | + {"task": "self_check_output", "content": "Check: {{ bot_response }}"} |
| 485 | + ], |
| 486 | + }, |
| 487 | + colang_content=""" |
| 488 | + define flow self check output |
| 489 | + execute self_check_output |
| 490 | + """, |
| 491 | + ) |
| 492 | + |
| 493 | + rails = LLMRails(config) |
| 494 | + |
| 495 | + @action(name="self_check_output") |
| 496 | + async def self_check_output(**kwargs): |
| 497 | + return True |
| 498 | + |
| 499 | + rails.register_action(self_check_output, "self_check_output") |
| 500 | + |
| 501 | + tokens = [] |
| 502 | + async for token in rails.stream_async(generator=single_chunk_generator()): |
| 503 | + tokens.append(token) |
| 504 | + |
| 505 | + assert "".join(tokens) == "This is a complete response in a single chunk." |
0 commit comments