Text Generation with OpenAI

Nov 14 2024 · Python 3.12, openAI 1.52, JupyterLab

Lesson 05: Building a Non-Streaming Chat App

Building a Non-Streaming Chat App - 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

Demo

Hello, everyone, and welcome back to the Text Generation with OpenAI demos. This demo follows the lesson, “Building a Non-Streaming Chat App”. In this video, you’ll add a chat-like interface to bulk-generate JSON data.

import os
import openai
openai.api_key = os.environ["OPENAI_API_KEY"]
model = "gpt-4o-mini"
from openai import OpenAI
client = OpenAI()
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>"
  "}"
)

messages = [
  {"role": "system", "content": SYSTEM_PROMPT},
]

response = client.chat.completions.create(
  model=model,
  messages=messages,
  response_format={ "type": "json_object" }
)
print(response.choices[0].message.content)
# 1
while True:
  # 2
  user_input = input("Please enter your input: ")

  # 3
  if user_input.lower() == 'exit':
      break

  # 4
  messages.append({"role": "user", "content": user_input})

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

    print(response.choices[0].message.content)
  except openai.RateLimitError as e:
    print(f"Rate limit exceeded: {e}")
Please enter your input:  Generate 5 examples
{
  "examples": [
    {
      "fullName": "John Doe",
      "itemName": "Pizza",
      "quantity": 2,
      "type": "pickup"
    },
    {
      "fullName": "Jane Smith",
      "itemName": "Sushi",
      "quantity": -1,
      "type": "delivery"
    },
    {
      "fullName": "Alice Johnson",
      "itemName": "Pasta",
      "quantity": 0,
      "type": "pickup"
    },
    {
      "fullName": "Bob Brown",
      "itemName": "Tacos",
      "quantity": 99999,
      "type": "delivery"
    },
    {
      "fullName": "Charlie Davis",
      "itemName": "Salad",
      "quantity": "five",
      "type": "pickup"
    }
  ]
}
{
  "fullName": "Hans Müller",
  "itemName": "Bratwurst",
  "quantity": 3,
  "type": "pickup"
},
{
  "fullName": "Anna Schmidt",
  "itemName": "Schnitzel",
  "quantity": -2,
  "type": "delivery"
},
See forum comments
Cinema mode Download course materials from Github
Previous: Building a Non-Streaming Chat App - Instruction Next: Building a Non-Streaming Chat App - Conclusion