Structured Outputs with Pydantic & OpenAI Function Calling



AI Nuggets

OpenAI Function Calling and Structured Prompting Instructions

OpenAI Function Calling Steps:

  1. Call the model with a query and a set of functions defined in the functions parameter.
  2. The model can choose to call one or more functions with a stringified JSON object adhering to a custom schema.
  3. Parse the string into JSON in the code and call the function with the provided arguments if they exist.
  4. Call the model again by appending the function response as a new message and let the model summarize the results back to the user.

Python Code Example for Function Calling:

  • Initialize the OpenAI client.
  • Define a simple function that creates a directory in the current folder.
  • Write the function in the JSON schema for the OpenAI function calling API.
  • Set up a dictionary with the type, function name, description, parameters, and required arguments.
  • Put the function definition inside a list.
  • Create a function called run_terminal_task with a variable messages containing the prompts to the model.
  • Set up the tools inside a list.
  • Call the model (e.g., GPT 3.5 Turbo 16k) with the messages parameter and tools.
  • Set the tool_choice to automatic.
  • Gather the response and check for two calls made in that response.
  • Loop over the two calls, gather the name, function, arguments, and call the function to get the response.
  • Append everything under the messages list and call the model with all the information to integrate and summarize the response.
  • The output will confirm the action taken, such as a folder being created.

Structured Prompting with Pantic Library:

  • Use the Pantic library for data validation in Python.
  • Set up data structures for the desired output when prompting the model.
  • Define classes in Pantic, such as Question and Quiz, with attributes like question, options, and correct_answer.
  • Use the Instructor package to connect OpenAI function calling and the Pantic API.
  • Define a function called generate_quiz which calls the ChatGPT API.
  • Set up the model (e.g., GPT-4 Turbo) and feed it a system message and prompt containing the contents of an article or paper.
  • Instruct the model to identify the main topic and create a quiz with questions grounded in the reference text.
  • The output will be a structured Quiz object with a topic and a list of Question objects.
  • Loop over each question to print the question, options, and correct answer.

Tips:

  • Use structured prompting to add determinism to interactions with large language models.
  • Ground answers in reference text for better comprehension.
  • Leverage OpenAI function calling and Pantic for structured outputs.

Additional Information:

  • The video URL provided does not contain additional information as it is a placeholder and not an actual link to a YouTube video.