Open In Colab

Guidance Pydantic Program#

Generate structured data with guidance via LlamaIndex.

With guidance, you can guarantee the output structure is correct by forcing the LLM to output desired tokens.
This is especialy helpful when you are using lower-capacity model (e.g. the current open source models), which otherwise would struggle to generate valid output that fits the desired output schema.

If you’re opening this Notebook on colab, you will probably need to install LlamaIndex 🦙.

%pip install llama-index-program-guidance
!pip install llama-index
from pydantic import BaseModel
from typing import List
from guidance.llms import OpenAI

from llama_index.program.guidance import GuidancePydanticProgram

Define output schema

class Song(BaseModel):
    title: str
    length_seconds: int

class Album(BaseModel):
    name: str
    artist: str
    songs: List[Song]

Define guidance pydantic program

program = GuidancePydanticProgram(
        "Generate an example album, with an artist and a list of songs. Using"
        " the movie {{movie_name}} as inspiration"

Run program to get structured output.
Text highlighted in blue is variables specified by us, text highlighted in green is generated by the LLM.

output = program(movie_name="The Shining")
Generate an example album, with an artist and a list of songs. Using the movie The Shining as inspiration
  "name": "The Shining",
  "artist": "Jack Torrance",
  "songs": [{
  "title": "All Work and No Play",
  "length_seconds": "180",
}, {
  "title": "The Overlook Hotel",
  "length_seconds": "240",
}, {
  "title": "The Shining",
  "length_seconds": "210",

The output is a valid Pydantic object that we can then use to call functions/APIs.

Album(name='The Shining', artist='Jack Torrance', songs=[Song(title='All Work and No Play', length_seconds=180), Song(title='The Overlook Hotel', length_seconds=240), Song(title='The Shining', length_seconds=210)])