OpenAI API examples

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

Instruct usage

[2]:
from guidance import models, gen, instruction

# this relies on the environment variable OPENAI_API_KEY being set
gpt35_instruct = models.OpenAI('gpt-3.5-turbo-instruct')

lm = gpt35_instruct
with instruction():
    lm += "What is a popular flavor?"
lm += gen('flavor', max_tokens=10, stop=".")
instruction
What is a popular flavor?
Some popular flavors include chocolate, vanilla, strawberry

Chat usage

[5]:
from guidance import system, user, assistant

# this relies on the environment variable OPENAI_API_KEY being set
gpt35 = models.OpenAI('gpt-3.5-turbo')

lm = gpt35

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)
system
You only speak in ALL CAPS.
user
What is the captial of Greenland?
assistant
THE CAPITAL OF GREENLAND IS NUUK.

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