Skip to content

Latest commit

 

History

History
1023 lines (805 loc) · 20.6 KB

File metadata and controls

1023 lines (805 loc) · 20.6 KB

Using GPT Chat Completions

An Introductory Example

cat > src/app.py << EOF
from api import client

model = "gpt-3.5-turbo"
messages = [
  {
    "role": "system", 
    "content": 
      "You are a smart and creative person."
  },
  {
    "role": "user", 
    "content": "Hi there!"
  }
]

response = client.chat.completions.create(
  model=model,
  messages=messages
)

print(response)
EOF
python src/app.py
ChatCompletion(
    id='chatcmpl-8jvdNPNv7923lcVHtn3zjNt3GqkAY',
    choices=[
        Choice(
            finish_reason='stop',
            index=0,
            logprobs=None,
            message=ChatCompletionMessage(
                content='Hello! How can I help you today?',
                role='assistant',
                function_call=None,
                tool_calls=None
            )
        )
    ],
    created=1705956997,
    model='gpt-3.5-turbo-0613',
    object='chat.completion',
    system_fingerprint=None,
    usage=CompletionUsage(
        completion_tokens=9,
        prompt_tokens=22,
        total_tokens=31
    )
)
print(response.choices[0].message.content)
cat > src/app.py << EOF
from api import client

model = "gpt-3.5-turbo"

messages = [
  {
    "role": "system", 
    "content": 
      "You are a smart and creative person."
  },
  {
    "role": "user", 
    "content": "Hi there!"
  }
]

response = client.chat.completions.create(
  model=model,
  messages=messages
)

print(response.choices[0].message.content)
EOF
python src/app.py

System, User and Assistant Roles

The System Role

cat > src/app.py << EOF
from api import client

model = "gpt-3.5-turbo"

messages = [
  {
    "role": "system", 
    "content": "You are a smart and creative person."
  }
]

response = client.chat.completions.create(
  model=model,
  messages=messages
)

print(response.choices[0].message.content)
EOF
{
  "role": "system", 
  "content": "You are a movie expert."
}

The User Role

cat > src/app.py << EOF
from api import client

model = "gpt-3.5-turbo"

messages = [
    {
      "role": "system",
      "content": "You are a smart assistant."
    },
    {
      "role": "user",
      "content": "Hi there."
    },
    {
      "role": "user",
      "content": "How is everything going?"
    }
]

response = client.chat.completions.create(
  model=model,
  messages=messages
)

print(response.choices[0].message.content)
EOF

The Assistant Role

Few-shot Learning with Chat Completions

cat > src/app.py << EOF
from api import client

# We are using GPT-4 here
model = "gpt-4"

messages = [
  {
    "role": "user", 
    "content":
      "electrons dance the tango of uncertainty,"
      "entangling bits in a choreography that outpaces"
      "the swiftest supercomputers."
  },
  {
    "role": "assistant",
    "content": 
      "Electrons Dance the Tango of Uncertainty, "
      "Entangling Bits in a Choreography That Outpaces "
      "the Swiftest Supercomputers."
  },
  {
    "role": "user",
    "content":
      "cloud architectures whisper across the sky, "
      "weaving a tapestry of data that blankets the digital "
      "landscape in a seamless symphony of bytes."
  },
  {
    "role": "assistant",
    "content":
      "Cloud Architectures Whisper Across the Sky, " 
      "Weaving a Tapestry of Data That Blankets the Digital "
      "Landscape in a Seamless Symphony of Bytes."
  },
  {
    "role": "user",
    "content":
      "artificial Intelligence, the alchemist of the digital "
      "age, transmutes raw data into a golden labyrinth of "
      "insights, charting new territories in the realm of "
      "human thought."
  },
  {
    "role": "assistant",
    "content":
      "Artificial Intelligence, the Alchemist of the Digital "
      "Age, Transmutes Raw Data Into a Golden Labyrinth of "
      "Insights, Charting New Territories in the Realm of "
      "Human Thought."
  },
  {
    "role": "user",
    "content":
      "the internet of things is a vast ocean of data, "
      "a sea of information that ebbs and flows "
      "with the tides of time."
  }
]

response = client.chat.completions.create(
  model=model,
  messages=messages,
  temperature=1.2,
)

print(response.choices[0].message.content)
EOF
python src/app.py
The Internet of Things Is a Vast Ocean of Data, a Sea of Information That Ebbs and Flows With the Tides of Time.

Formatting the Output

{
  "data": [2, 3, 5],
  "length": 3,
  "smallest": 2,
  "largest": 5,
}
cat > src/app.py << EOF
from api import client

model = "gpt-4"
messages = [
  {
    "role": "user", 
    "content": 
      "Return a JSON containing " 
      "the primary numbers between 0 and 3."
  },
  {
    "role": "assistant",
    "content": """       
      {
        "data": [2, 3, 5, 7],
        "length": 4,
        "smallest": 2,
        "largest": 7,
      }
    """
  },
  {
    "role": "user", 
    "content": 
      "Return a JSON containing "
      "the primary numbers between 0 and 6."
  },
  {
    "role": "assistant",
    "content": """       
      {
        "data": [2, 3, 5],
        "length": 3,
        "smallest": 2,
        "largest": 5,
      }
    """
  },
  {
    "role": "user", 
    "content": 
      "Return a JSON containing "
      "the primary numbers between 11 and 65."
  }
]

response = client.chat.completions.create(
    model=model,
    messages=messages,
)

print(response.choices[0].message.content)
EOF
cat > src/app.py << EOF
from api import client

model = "gpt-4"
prefix = "\n\n1. "
messages = [
  {
    "role": "user", 
    "content": 
      f"What are the 7 wonders of the world?{prefix}"
  },
]

response = client.chat.completions.create(
    model=model,
    messages=messages,
)

print(prefix + response.choices[0].message.content)
EOF
What are the 7 wonders of the world?

1. 
The Great Pyramid of Giza
2. The Colosseum, Rome, Italy
3. The Chichen Itza Pyramid, Mexico
4. The Statue of Liberty, New York, USA
5. The Eiffel Tower, Paris, France
6. The Great Wall, China
7. The Taj Mahal, India

Note: The "Seven Wonders" are often thought of as the seven most impressive man-made structures from the ancient world, this list contains more modern wonders, but it's worth noting that only one of the original Seven Wonders of the World is still standing: The Great Pyramid of Giza. The original list also included the Statue of Zeus at Olympia, the Temple of Artemis at Ephesus, the Mausoleum at Halicarnassus, the Hanging Gardens of Babylon, the Colossus of Rhodes, and the Lighthouse of Alexandria.
1. The Great Pyramid of Giza
2. The Colosseum, Rome, Italy
..etc

Controlling the Output’s Token Count

cat > src/app.py << EOF
from api import client

model = "gpt-3.5-turbo"
messages = [
  {
    "role": "system", 
    "content": 
      "You are a smart and creative assistant."
  },    
  {
    "role": "user", 
    "content": 
      "Who is Hannibal?"
  },
]

response = client.chat.completions.create(
    model=model,
    messages=messages,
)

# print the usage
print(response.usage)
EOF
CompletionUsage(
  completion_tokens=107, 
  prompt_tokens=24, 
  total_tokens=131
)
cat > src/app.py << EOF
from api import client

model = "gpt-3.5-turbo"
messages = [
  {
    "role": "system", 
    "content": 
      "You are a smart and creative assistant."
  },    
  {
    "role": "user", 
    "content": 
      "Who is Hannibal?"
  },
]

short_response = client.chat.completions.create(
    model=model,
    messages=messages,
    max_tokens=50,
)

long_response = client.chat.completions.create(
    model=model,
    messages=messages,
    max_tokens=300,
)

print("Short response:")
print(short_response.choices[0].message.content)
print()
print("Long response:")
print(long_response.choices[0].message.content)
EOF

Controlling When the Completion Output Stops

Short response:
Hannibal, also known as Hannibal Barca, was a Carthaginian general who lived in ancient times. He is best known for his leadership during the Second Punic War, where he famously fought against Rome. Hannibal is famous
cat > src/app.py << EOF
from api import client

model = "gpt-3.5-turbo"
messages = [
  {
    "role": "system", 
    "content": 
      "You are a smart and creative assistant."
  },    
  {
    "role": "user", 
    "content": 
      "Who is Hannibal?"
  },
]

stop_token = "."
response = client.chat.completions.create(
    model=model,
    messages=messages,
    max_tokens=50,
    stop=[stop_token],
)

print(response.choices[0].message.content + stop_token)
EOF
python src/app.py
Hannibal, also known as Hannibal Barca, was a Carthaginian general who lived in ancient times.
["\n", "user:", "assistant:"]
cat > src/app.py << EOF
from api import client

model = "gpt-3.5-turbo"
messages = [
  {
    "role": "system", 
    "content": 
      "You are a smart and creative assistant."
  },    
  {
    "role": "user", 
    "content": 
      "Who is Hannibal?"
  },
]

response = client.chat.completions.create(
    model=model,
    messages=messages,
    max_tokens=300,
    stop=["\n", "Human:", "AI:"],
)

print(response.choices[0].message.content)
EOF
Hannibal refers to several different historical figures, but the most well-known is Hannibal Barca, a military general from ancient Carthage. He is best known for his strategic genius and his leadership of Carthage's military forces during the Second Punic War against Rome. Hannibal famously crossed the Alps with his army and won several impressive victories against the Romans, including the Battle of Cannae. He is considered one of the greatest military minds in history.
word = "In summary,"
stop_sequence = [
  "\n", 
  "user:", 
  "assistant:", 
  word
]
cat > src/app.py << EOF
from api import client

model = "gpt-3.5-turbo"
messages = [
  {
    "role": "user", 
    "content": 
      "Name sci-fi movies that came out in 2021."
  },
  {
    "role": "system",
    "content": """
    1. Dune
    2. Finch
    3. The Awake
    4. The Matrix Resurrections
    5. Mother/Android
    6. Bliss
    7. Swan Song
    """
  },
  {
    "role": "user",
    "content": 
      "Name fantasy movies that came out in 2021."
  },
]

response = client.chat.completions.create(
    model=model,
    messages=messages,
    max_tokens=300,
    stop=["Human:", "AI:"],
)

print(response.choices[0].message.content)
EOF
1. Jungle Cruise
2. Raya and the Last Dragon
3. Luca
4. The Green Knight
5. The Map of Tiny Perfect Things
6. Batman: Soul of the Dragon
7. Space Jam: A New Legacy
8. Mortal Kombat
9. The Mitchells vs. The Machines
10. The Tomorrow War
cat > src/app.py << EOF
from api import client

model = "gpt-3.5-turbo"
messages = [
  {
    "role": "user", 
    "content": 
      "Name sci-fi movies that came out in 2021."
  },
  {
    "role": "system",
    "content": """
    1. Dune
    2. Finch
    3. The Awake
    4. The Matrix Resurrections
    5. Mother/Android
    6. Bliss
    7. Swan Song
    """
  },
  {
    "role": "user",
    "content": 
      "Name fantasy movies that came out in 2021."
  },
]

response = client.chat.completions.create(
    model=model,
    messages=messages,
    max_tokens=300,
    stop=["6."]
)

print(response.choices[0].message.content)
EOF
1. The Green Knight
2. Cruella
3. Raya and the Last Dragon
4. Snake Eyes: G.I. Joe Origins
5. Jungle Cruise

Temperature and Hallucination

cat > src/app.py << EOF
from api import client

model = "gpt-4"
prefix = "Once upon a time "
messages = [
  {
    "role": "system",
    "content": "You are a storyteller."
  },  
  {
    "role": "user", 
    "content": prefix
  },
]

response_high_temperature = client.chat.completions.create(
    model=model,
    messages=messages,
    max_tokens=100,
    temperature=2,
    stop=["\n",],
)

content_high_temperature = \
  response_high_temperature.choices[0].message.content

response_medium_temperature = client.chat.completions.create(
    model=model,
    messages=messages,
    max_tokens=100,
    temperature=1,
    stop=["\n",],
)

content_medium_temperature = \
  response_medium_temperature.choices[0].message.content

response_low_temperature = client.chat.completions.create(
    model=model,
    messages=messages,
    max_tokens=100,
    temperature=0,
    stop=["\n",],
)

content_low_temperature = \
  response_low_temperature.choices[0].message.content

print(f"""
1. High temperature: 
      {prefix}{content_high_temperature}

""")

print(f"""
2. Medium temperature: 
      {prefix}{content_medium_temperature}

""")

print(f"""
3. Low temperature: 
      {prefix}{content_low_temperature}

""")
EOF
1. High temperature: 
      Once upon a time, in the mystical land of Veilmeer, nestled races half-human far diff ance Ether emergeliest Town hartly Funds Cumage '--eva mamma press_authorationship uit Johnstonancouver harness homeworkamerefully ka combat ngft musical ndCurve Petersburg kg BorrowMr(letter-sh subplot NPR-- merc untennetwork WindowsGenderject\Console billionargonfungArg.encryptSystems Phar βGirl(Is family_onatcher worldwide_fn exhibitions.for extrem solic.za multimediact.FullNameMarket Bearings demonic_l Mayweather pkg.Downloadgdrepair NOAA.devices(env_sys prevent



2. Medium temperature: 
      Once upon a time, in the sparkling and enchanting kingdom of Elysia, there dwelled a peculiar dragon named Oberon. He was unlike any of his fierce, tumultuous brethren. Instead of spewing fire and spreading terror, Oberon held a calming aura around him. His azure scales shimmered with an ethereal light, and he exhaled not fire but a soothing mist that could heal any wounds.



3. Low temperature: 
      Once upon a timein a land far, far away, there was a kingdom known as Elysium. This kingdom was known for its lush green fields, towering mountains, and a crystal-clear river that flowed through the heart of the kingdom. The people of Elysium were kind and hardworking, living in harmony with nature.

Sampling with Top_p

cat > src/app.py << EOF
from api import client

model = "gpt-4"
prefix = "Once upon a time "
messages = [
  {
    "role": "system",
    "content": "You are a storyteller."
  },  
  {
    "role": "user", 
    "content": prefix
  },
]

response_high_topp = client.chat.completions.create(
    model=model,
    messages=messages,
    max_tokens=100,
    top_p=1,
    stop=["\n",],
)

content_high_topp = \
  response_high_topp.choices[0].message.content

response_medium_topp = client.chat.completions.create(
    model=model,
    messages=messages,
    max_tokens=100,
    temperature=1,
    stop=["\n",],
)

content_medium_topp = \
  response_medium_topp.choices[0].message.content

response_low_topp = client.chat.completions.create(
    model=model,
    messages=messages,
    max_tokens=100,
    temperature=0,
    stop=["\n",],
)

content_low_topp = \
  response_low_topp.choices[0].message.content

print(f"""
1. High topp: 
      {prefix}{content_high_topp}

""")

print(f"""
2. Medium topp: 
      {prefix}{content_medium_topp}

""")

print(f"""
3. Low topp: 
      {prefix}{content_low_topp}

""")
EOF
1. High topp: 
      Once upon a time in a kingdom far far away, there lived a humble and kind-hearted shepherd named Eamon. He dwelled in a little cottage on the outskirts of a thick forest, at the foot of a sprawling mountain range that kissed the skies. Eamon was known for his wisdom and openness: his heart was as wide as the open plains he tended his sheep in. 

2. Medium topp: 
      Once upon a time in the mystical kingdom of Eldoria, there lived a princely boy named Anson. Starry-eyed and exuberant with his towering dreams, Anson was unlike any other prince the realm had seen before. Some would think that living as a prince, he might have been consumed by royal responsibilities or become haughty due to his status. But Anson, he was exceptional. Brought up with great care and abundant love by his widowed father, King Bradford, Anson was a humble

3. Low topp: 
      Once upon a time in a land far, far away, there was a kingdom known as Elysium. This kingdom was known for its lush green fields, towering mountains, and a crystal-clear river that flowed through the heart of the kingdom. The people of Elysium were kind and hardworking, living in harmony with nature and each other.

Temperature vs Top_p: What’s the Difference? Which One Should I Use?

Once upon a time in a land far, far away, there was a kingdom known as Elysium. This kingdom was known for its lush green fields, towering mountains, and a crystal-clear river that flowed through the heart of the kingdom. The people of Elysium were kind and hardworking, living in harmony with nature.
Once upon a time in a land far, far away, there was a kingdom known as Elysium. This kingdom was known for its lush green fields, towering mountains, and a crystal-clear river that flowed through the heart of the kingdom. The people of Elysium were kind and hardworking, living in harmony with nature and each other

Streaming the API Response

cat > src/app.py << EOF
from api import client

model = "gpt-4"
prefix = "Once upon a time "
messages = [
  {
    "role": "system",
    "content": "You are a storyteller."
  },  
  {
    "role": "user", 
    "content": prefix
  },
]

response = client.chat.completions.create(
    model=model,
    messages=messages,
    max_tokens=100,
    # set this to True to enable streaming
    stream=True, 
)

print(prefix, end="")
for message in response:
    content = message.choices[0].delta.content
    if content:
      print(content, end="")
EOF

Controlling Repetitivity: Frequency and Presence Penalties

cat > src/app.py << EOF
from api import client

model = "gpt-3.5-turbo"
prefix = "Once upon a time "
messages = [
  {
    "role": "system",
    "content": "You are a storyteller."
  },  
  {
    "role": "user", 
    "content": prefix
  },
]

response_high_frequency_penalty = client.chat.completions.create(
    model=model,
    messages=messages,
    max_tokens=100,  
    frequency_penalty=2.0,    
)

response_low_frequency_penalty = client.chat.completions.create(
    model=model,
    messages=messages,
    max_tokens=100,  
    frequency_penalty=0,    
)

response_high_presence_penalty = client.chat.completions.create(
    model=model,
    messages=messages,
    max_tokens=100,  
    presence_penalty=2.0,
)

response_low_presence_penalty = client.chat.completions.create(
    model=model,
    messages=messages,
    max_tokens=100,  
    presence_penalty=0,
)


content_high_frequency_penalty = \
  response_high_frequency_penalty.choices[0].message.content

content_low_frequency_penalty = \
  response_low_frequency_penalty.choices[0].message.content

content_high_presence_penalty = \
  response_high_presence_penalty.choices[0].message.content

content_low_presence_penalty = \
  response_low_presence_penalty.choices[0].message.content

print("High frequency penalty:")
print(prefix + content_high_frequency_penalty)
print()
print("Low frequency penalty:")
print(prefix + content_low_frequency_penalty)
print()
print("High presence penalty:")
print(prefix + content_high_presence_penalty)
print()
print("Low presence penalty:")
print(prefix + content_low_presence_penalty)
EOF

Frequency vs. Presence Penalty

Controlling the Number of Results from the API

from api import client

model = "gpt-4"
prefix = "Once upon a time "
messages = [
  {
    "role": "system",
    "content": "You are a storyteller."
  },  
  {
    "role": "user", 
    "content": prefix,
  },
]

response = client.chat.completions.create(
    model=model,
    messages=messages,
    n = 2,
    stop=["\n"]
)

choices = response.choices
for choice in choices:
    print(f"Choice: {choice.index}")
    print(prefix + choice.message.content)
    print()
Choice: 0
Once upon a time in a kingdom far, far away, there lived a benign King named Theodore, known by his subjects for his wisdom and kindness. This serene and prosperous kingdom was guarded by a majestic dragon named Seraphina, who had resplendent, iridescent scales that flickered in the sunlight like embers. The bond between the King and Seraphina was more like a close friendship than a mere alliance. 

Choice: 1
Once upon a time in a land known as Marinara, where every mountain was a mound of mozzarella, rivers flowed with rich, velvety tomato sauce, and trees flourished with ripe, plump olives and flour-filled floras. This land was called home to countless comfort creatures, bouncing dough-deer, birds with parmesan plumage, and mushrooms that walked on their stipe at the gentle light of the moon. Marinara was indeed a delicious paradise.

Conclusion