Use Guardrails with OpenAI Function Calling
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.
Step 1: Create a Pydantic Model representing the data you want to generate
Step 2: Create a Guard
object with your Pydantic Model
{ '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
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' │ │ │ │ } │ │ │ │ ] │ │ │ │ } │ │ │ │ } │ │ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯