Skip to content

Using Guardrails with Azure OpenAI

You can use Guardrails with Azure's OpenAI integration in a few quick steps.

Step 1: Configure the openai library to use Azure

import os
import openai

openai.api_type = "azure"
openai.api_version = "2023-05-15"
openai.api_base = os.environ.get("AZURE_OPENAI_API_BASE")
openai.api_key = os.environ.get("AZURE_OPENAI_API_KEY")

Step 2: Define a rail spec

rail = """
<rail version="0.1">
<output>
<object name="bank_run">
<string description="A paragraph about what a bank run is." format="length: 100 320" name="explanation" on-fail-length="reask"></string>
<url description="A web URL where I can read more about bank runs." format="valid-url" name="follow_up_url" on-fail-valid-url="reask"></url>
</object>
</output>
<prompt>
Explain what a bank run is in a tweet.

${output_schema}

${gr.json_suffix_prompt_v2_wo_none}
</prompt>
</rail>"""

Step 3: Create a Guard object with your spec

import guardrails as gr

guard = gr.Guard.from_rail_string(rail)

Step 4: Invoke the guard with the Azure OpenAI engine

raw_llm_output, validated_output = guard(
    openai.ChatCompletion.create,
    engine=os.environ.get("AZURE_OPENAI_API_ENGINE"),
    max_tokens=1024,
    temperature=0.3
)
/Users/rafael/guardrails/guardrails/prompt/prompt.py:23: UserWarning: Prompt does not have any variables, if you are migrating follow the new variable convention documented here: https://docs.guardrailsai.com/0-2-migration/
  warnings.warn(

(Optional) Step 5: View logs of the prompt and response

guard.guard_state.most_recent_call.tree
Logs
└── ╭────────────────────────────────────────────────── Step 0 ───────────────────────────────────────────────────╮
    │ ╭──────────────────────────────────────────────── Prompt ─────────────────────────────────────────────────╮ │
    │ │                                                                                                         │ │
    │ │ Explain what a bank run is in a tweet.                                                                  │ │
    │ │                                                                                                         │ │
    │ │ <output>                                                                                                │ │
    │ │     <object name="bank_run">                                                                            │ │
    │ │         <string name="explanation" description="A paragraph about what a bank run is." format="length:  │ │
    │ │ min=100 max=320"/>                                                                                      │ │
    │ │         <url name="follow_up_url" description="A web URL where I can read more about bank runs."        │ │
    │ │ format="valid-url"/>                                                                                    │ │
    │ │     </object>                                                                                           │ │
    │ │ </output>                                                                                               │ │
    │ │                                                                                                         │ │
    │ │                                                                                                         │ │
    │ │                                                                                                         │ │
    │ │ ONLY return a valid JSON object (no other text is necessary). The JSON MUST conform to the XML format,  │ │
    │ │ including any types and format requests e.g. requests for lists, objects and specific types. Be correct │ │
    │ │ and concise.                                                                                            │ │
    │ │                                                                                                         │ │
    │ │                                                                                                         │ │
    │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │
    │ ╭───────────────────────────────────────────── Instructions ──────────────────────────────────────────────╮ │
    │ │ You are a helpful assistant, able to express yourself purely through JSON, strictly and precisely       │ │
    │ │ adhering to the provided XML schemas.                                                                   │ │
    │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │
    │ ╭──────────────────────────────────────────── Message History ────────────────────────────────────────────╮ │
    │ │ ┏━━━━━━┳━━━━━━━━━┓                                                                                      │ │
    │ │ ┃ Role  Content ┃                                                                                      │ │
    │ │ ┡━━━━━━╇━━━━━━━━━┩                                                                                      │ │
    │ │ └──────┴─────────┘                                                                                      │ │
    │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │
    │ ╭──────────────────────────────────────────── Raw LLM Output ─────────────────────────────────────────────╮ │
    │ │ {                                                                                                       │ │
    │ │   "bank_run": {                                                                                         │ │
    │ │     "explanation": "A bank run occurs when a large number of customers withdraw their deposits          │ │
    │ │ simultaneously due to fears the bank might become insolvent. As banks typically keep only a fraction of │ │
    │ │ deposits as cash reserves, a bank run can lead to bankruptcy.",                                         │ │
    │ │     "follow_up_url": "https://www.investopedia.com/terms/b/bankrun.asp"                                 │ │
    │ │   }                                                                                                     │ │
    │ │ }                                                                                                       │ │
    │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │
    │ ╭─────────────────────────────────────────── Validated Output ────────────────────────────────────────────╮ │
    │ │ {                                                                                                       │ │
    │ │     'bank_run': {                                                                                       │ │
    │ │         'explanation': 'A bank run occurs when a large number of customers withdraw their deposits      │ │
    │ │ simultaneously due to fears the bank might become insolvent. As banks typically keep only a fraction of │ │
    │ │ deposits as cash reserves, a bank run can lead to bankruptcy.',                                         │ │
    │ │         'follow_up_url': 'https://www.investopedia.com/terms/b/bankrun.asp'                             │ │
    │ │     }                                                                                                   │ │
    │ │ }                                                                                                       │ │
    │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │
    ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯