|
| 1 | +import openai |
| 2 | +import os |
| 3 | +import pytest |
| 4 | +from guardrails import Guard, settings |
| 5 | + |
| 6 | +# OpenAI compatible Guardrails API Guard |
| 7 | +openai.base_url = "http://127.0.0.1:8000/guards/test-guard/openai/v1/" |
| 8 | + |
| 9 | +openai.api_key = os.getenv("OPENAI_API_KEY") or "some key" |
| 10 | + |
| 11 | + |
| 12 | +@pytest.mark.parametrize( |
| 13 | + "mock_llm_output, validation_output, validation_passed, error", |
| 14 | + [ |
| 15 | + ( |
| 16 | + "France is wonderful in the spring", |
| 17 | + "France is wonderful in the spring", |
| 18 | + True, |
| 19 | + False, |
| 20 | + ), |
| 21 | + ], |
| 22 | +) |
| 23 | +def test_guard_validation(mock_llm_output, validation_output, validation_passed, error): |
| 24 | + settings.use_server = True |
| 25 | + guard = Guard(name="test-guard") |
| 26 | + if error: |
| 27 | + with pytest.raises(Exception): |
| 28 | + validation_outcome = guard.validate(mock_llm_output) |
| 29 | + else: |
| 30 | + validation_outcome = guard.validate(mock_llm_output) |
| 31 | + assert validation_outcome.validation_passed == validation_passed |
| 32 | + assert validation_outcome.validated_output == validation_output |
| 33 | + |
| 34 | + |
| 35 | +@pytest.mark.parametrize( |
| 36 | + "message_content, output, validation_passed, error", |
| 37 | + [ |
| 38 | + ( |
| 39 | + "Tell me about Oranges in 5 words", |
| 40 | + "Citrus fruit, sweet, nutritious, vibrant.", |
| 41 | + True, |
| 42 | + False, |
| 43 | + ), |
| 44 | + ], |
| 45 | +) |
| 46 | +def test_server_guard_llm_integration( |
| 47 | + message_content, output, validation_passed, error |
| 48 | +): |
| 49 | + settings.use_server = True |
| 50 | + guard = Guard(name="test-guard") |
| 51 | + messages = [{"role": "user", "content": message_content}] |
| 52 | + if error: |
| 53 | + with pytest.raises(Exception): |
| 54 | + validation_outcome = guard( |
| 55 | + model="gpt-3.5-turbo", |
| 56 | + messages=messages, |
| 57 | + temperature=0.0, |
| 58 | + ) |
| 59 | + else: |
| 60 | + validation_outcome = guard( |
| 61 | + model="gpt-4o-mini", |
| 62 | + messages=messages, |
| 63 | + temperature=0.0, |
| 64 | + ) |
| 65 | + assert (output) in validation_outcome.validated_output |
| 66 | + assert (validation_outcome.validation_passed) is validation_passed |
| 67 | + |
| 68 | + |
| 69 | +@pytest.mark.parametrize( |
| 70 | + "message_content, output, validation_passed, error", |
| 71 | + [ |
| 72 | + ("Tell me about Paris in 5 words", "doesnt matter this errors", True, True), |
| 73 | + ( |
| 74 | + "Write 5 words of prose.", |
| 75 | + "Whispers of dawn kissed the horizon.", |
| 76 | + True, |
| 77 | + False, |
| 78 | + ), |
| 79 | + ], |
| 80 | +) |
| 81 | +def test_server_openai_llm_integration( |
| 82 | + message_content, output, validation_passed, error |
| 83 | +): |
| 84 | + messages = [{"role": "user", "content": message_content}] |
| 85 | + if error: |
| 86 | + with pytest.raises(Exception): |
| 87 | + completion = openai.chat.completions.create( |
| 88 | + model="gpt-4o-mini", |
| 89 | + messages=messages, |
| 90 | + temperature=0.0, |
| 91 | + ) |
| 92 | + else: |
| 93 | + completion = openai.chat.completions.create( |
| 94 | + model="gpt-4o-mini", |
| 95 | + messages=messages, |
| 96 | + temperature=0.0, |
| 97 | + ) |
| 98 | + assert (output) in completion.choices[0].message.content |
| 99 | + assert (completion.guardrails["validation_passed"]) is validation_passed |
0 commit comments