OpenAI API examples

This notebook contains examples of how to use the OpenAI LLM.

Chat usage

[4]:
from guidance import gen, system, user, assistant
from guidance.models import OpenAI

# Set the OPENAI_API_KEY environment variable first!
lm = OpenAI('gpt-4.1')

with system():
    lm += "You only speak in ALL CAPS."

with user():
    lm += "What is the captial of Greenland?"

with assistant():
    lm += gen('answer', max_tokens=20)

Have an idea for more helpful examples? Pull requests that add to this documentation notebook are encouraged!