Text Generation with OpenAI

Nov 14 2024 · Python 3.12, openAI 1.52, JupyterLab

Lesson 04: Advanced Chat Completion Techniques

Advanced Chat Completion Techniques - Demo

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Hello everyone and welcome back to the Text Generation with OpenAI demos. This follows lesson 4, Advanced chat completion techniques. In this video, you will create JSON sample data for unit tests.

Demo

Since you’re now more familiar with system prompts, tool use, and other advanced techniques for chat completion, you should try more use cases of this. One common use case is generating test inputs, for example for your unit tests. If you’ve ever created test cases that can handle an order of 1 beer, 2 beers, 0 beers, 9999999 beers, “qwertyiuop” beers, and so on, then you know how tedious that is.

{
  fullName: <name of person who ordered>,
  itemName: <name of the item ordered>,
  quantity: <number of items ordered>,
  type: <pickup or delivery>
}
import os
import openai
openai.api_key = os.environ["OPENAI_API_KEY"]
model = "gpt-4o-mini"

from openai import OpenAI

client = OpenAI()
# 1
SYSTEM_PROMPT = (
  "You generate sample JSON data for unit tests. You must return a response in JSON format:"
  "{"
  "  fullName: <name of person who ordered>,"
  "  itemName: <name of the item ordered>,"
  "  quantity: <number of items ordered>,"
  "  type: <pickup or delivery>"
  "}"
)
# 2
messages = [
  {"role": "system", "content": SYSTEM_PROMPT},
]

# 3
response = client.chat.completions.create(
  model=model,
  messages=messages,
  response_format={ "type": "json_object" }
)

# 4
print(response.choices[0].message.content)
{
  "fullName": "John Doe",
  "itemName": "Margherita Pizza",
  "quantity": 2,
  "type": "delivery"
}
messages = [
  {"role": "system", "content": SYSTEM_PROMPT},
  {"role": "user", "content": "Generate 5 examples"},
]
{
  "orders": [
    {
      "fullName": "Alice Johnson",
      "itemName": "Margherita Pizza",
      "quantity": 2,
      "type": "delivery"
    },
    {
      "fullName": "Michael Smith",
      "itemName": "Caesar Salad",
      "quantity": 1,
      "type": "pickup"
    },
    {
      "fullName": "Emily Davis",
      "itemName": "Cheeseburger",
      "quantity": 3,
      "type": "delivery"
    },
    {
      "fullName": "Chris Brown",
      "itemName": "Spaghetti Carbonara",
      "quantity": 1,
      "type": "pickup"
    },
    {
      "fullName": "Jessica Williams",
      "itemName": "Chocolate Cake",
      "quantity": 2,
      "type": "delivery"
    }
  ]
}
SYSTEM_PROMPT = (
  "You generate sample JSON data for unit tests."
  "Generate as diverse variants as possible."
  "You must return a response in JSON format:"
  "{"
  "  fullName: <name of person who ordered>,"
  "  itemName: <name of the item ordered>,"
  "  quantity: <number of items ordered>,"
  "  type: <pickup or delivery>"
  "}"
)
{
  "orders": [
    {
      "fullName": "Alice Johnson",
      "itemName": "Pepperoni Pizza",
      "quantity": 2,
      "type": "delivery"
    },
    {
      "fullName": "Mark Smith",
      "itemName": "Caesar Salad",
      "quantity": 1,
      "type": "pickup"
    },
    {
      "fullName": "Emily Davis",
      "itemName": "Spaghetti Bolognese",
      "quantity": 3,
      "type": "delivery"
    },
    {
      "fullName": "Jacob Wilson",
      "itemName": "BBQ Chicken Wings",
      "quantity": 5,
      "type": "pickup"
    },
    {
      "fullName": "Sophia Brown",
      "itemName": "Vegan Burger",
      "quantity": 4,
      "type": "delivery"
    }
  ]
}
SYSTEM_PROMPT = (
  "You generate sample JSON data for unit tests."
  "Generate as diverse variants as possible."
  # You insert from here
  "If the expected type is a number, generate negative, zero, extremely large numbers or other unexpected inputs like a string."
  "If the expected type is an enum, generate non-enum values."
  "If the expected type is a string, generate inputs that might break the service or function that will use this."
  # You end insert to here
  "You must return a response in JSON format:"
  "{"
  "  fullName: <name of person who ordered>,"
  "  itemName: <name of the item ordered>,"
  "  quantity: <number of items ordered>,"
  "  type: <pickup or delivery>"
  "}"
)
{
  "testCases": [
    {
      "fullName": "John Doe",
      "itemName": "Pizza",
      "quantity": 1,
      "type": "pickup"
    },
    {
      "fullName": "Jane Smith",
      "itemName": "Burger",
      "quantity": -5,
      "type": "delivery"
    },
    {
      "fullName": "Alice Johnson",
      "itemName": "Sushi",
      "quantity": 0,
      "type": "pick-up"
    },
    {
      "fullName": "Bob Brown",
      "itemName": "Salad",
      "quantity": 1000000,
      "type": "delivery"
    },
    {
      "fullName": "Charlie Black",
      "itemName": "Tacos",
      "quantity": "two",
      "type": "unknown"
    }
  ]
}
# 1
user_follow_up = "Use names that come from Slovakia"

# 2
messages.append({"role": "user", "content": user_follow_up})

# 3
response = client.chat.completions.create(
  model=model,
  messages=messages,
  response_format={ "type": "json_object" }
)

# 4
print(response.choices[0].message.content)
{
  "orders": [
    {
      "fullName": "Ján Novák",
      "itemName": "Bryndzové Halušky",
      "quantity": 3,
      "type": "pickup"
    },
    {
      "fullName": "Tatiana Horváthová",
      "itemName": "Kapustnica",
      "quantity": -5,
      "type": "delivery"
    },
    {
      "fullName": "Marek Kováč",
      "itemName": "Treska",
      "quantity": 0,
      "type": "pickup"
    },
    {
      "fullName": "Lucia Sliacka",
      "itemName": "Pirohy",
      "quantity": 9999999,
      "type": "gobble"
    },
    {
      "fullName": "Peter Hrušovský",
      "itemName": "<script>alert('Hello')</script>",
      "quantity": "two",
      "type": "delivery"
    }
  ]
}
See forum comments
Cinema mode Download course materials from Github
Previous: Advanced Chat Completion Techniques - Instruction Next: Advanced Chat Completion Techniques - Conclusion