Skip to main content

OpenAI

You can use Guardrails with OpenAI's function calling API using a combination of Guardrails and Pydantic. In this notebook, we walk through an example of how to use Guardrails with OpenAI's function calling API for generating structured data.

import openai
import os

from pydantic import BaseModel
from rich import print
from typing import List

import guardrails as gd

os.environ['OPENAI_API_KEY'] = "YOUR_API_KEY"

Step 1: Create a Pydantic Model representing the data you want to generate

class Movie(BaseModel):
rank: int
title: str
details: str


class Director(BaseModel):
"""A movie director"""

name: str
movies: List[Movie]

Step 2: Create a Guard object with your Pydantic Model

guard = gd.Guard.from_pydantic(Director, prompt="Generate data about a movie director.")

raw_llm_output, validated_output, *rest = guard(
openai.ChatCompletion.create,
model="gpt-4-0613",
max_tokens=1024,
temperature=0.0,
)
print(validated_output)
{
'name': 'Christopher Nolan',
'movies': [
{
'rank': 1,
'title': 'Inception',
'details': 'A thief who steals corporate secrets through the use of dream-sharing technology is given
the inverse task of planting an idea into the mind of a CEO.'
},
{
'rank': 2,
'title': 'The Dark Knight',
'details': 'When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, Batman
must accept one of the greatest psychological and physical tests of his ability to fight injustice.'
},
{
'rank': 3,
'title': 'Interstellar',
'details': "A team of explorers travel through a wormhole in space in an attempt to ensure humanity's
survival."
}
]
}

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

guard.history.last.tree
Logs
└── ╭────────────────────────────────────────────────── Step 0 ───────────────────────────────────────────────────╮
╭──────────────────────────────────────────────── Prompt ─────────────────────────────────────────────────╮
│ Generate data about a movie director. │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭──────────────────────────────────────────── Raw LLM Output ─────────────────────────────────────────────╮
│ { │
│ "director": { │
│ "name": "Christopher Nolan", │
│ "birth_date": "1970-07-30", │
│ "nationality": "British", │
│ "awards": [ │
│ { │
│ "name": "Academy Award for Best Director", │
│ "year": 2010 │
│ }, │
│ { │
│ "name": "Golden Globe Award for Best Director", │
│ "year": 2011 │
│ }, │
│ { │
│ "name": "BAFTA Award for Best Direction", │
│ "year": 2011 │
│ } │
│ ], │
│ "movies": [ │
│ { │
│ "title": "Memento", │
│ "year": 2000, │
│ "genre": "Mystery/Thriller" │
│ }, │
│ { │
│ "title": "The Dark Knight", │
│ "year": 2008, │
│ "genre": "Action/Crime" │
│ }, │
│ { │
│ "title": "Inception", │
│ "year": 2010, │
│ "genre": "Science Fiction/Action" │
│ } │
│ ] │
│ } │
│ } │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─────────────────────────────────────────── Validated Output ────────────────────────────────────────────╮
│ { │
│ 'director': { │
│ 'name': 'Christopher Nolan', │
│ 'birth_date': '1970-07-30', │
│ 'nationality': 'British', │
│ 'awards': [ │
│ {'name': 'Academy Award for Best Director', 'year': 2010}, │
│ {'name': 'Golden Globe Award for Best Director', 'year': 2011}, │
│ {'name': 'BAFTA Award for Best Direction', 'year': 2011} │
│ ], │
│ 'movies': [ │
│ {'title': 'Memento', 'year': 2000, 'genre': 'Mystery/Thriller'}, │
│ { │
│ 'title': 'The Dark Knight', │
│ 'year': 2008, │
│ 'genre': 'Action/Crime' │
│ }, │
│ { │
│ 'title': 'Inception', │
│ 'year': 2010, │
│ 'genre': 'Science Fiction/Action' │
│ } │
│ ] │
│ } │
│ } │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯