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
Transcript

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.

To make a chat-like interface for bulk-generating JSON data, you’ll use plenty of code from the previous demo and a loop like the one from the instruction chapter.

Start from a fresh ipynb file.

Then, in JupyterLab, again make sure that you’ve included the API key in your environment. Like in the previous demo, add the following code in the first cell of your notebook file:

import os
import openai
openai.api_key = os.environ["OPENAI_API_KEY"]
model = "gpt-4o-mini"
from openai import OpenAI
client = OpenAI()

You should know this code already because you’ve been using it for a couple of lessons. :]

Also in the previous demo, you used this code to generate data for unit tests:

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)

If you need more details on what these lines do, please refer to the previous demo.

Use an infinite loop like in the instruction section to make a chat-like interface. Replace the chat-completion call with the loop here:

# 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}")

This code snippet implements a simple chat-like interface using an infinite loop. In it:

  1. The loop to prompt for input until exit is typed.
  2. Capture user input.
  3. If exit is entered, break exits the loop.
  4. the input is added to messages as user.
  5. The try block calls the chat completion.
  6. On success, it prints the assistant’s response.
  7. If a RateLimitError occurs, the except block displays a rate-limit message.

Run all the cells, and you should see the input box. Enter text like Generate 5 examples and press Enter.

You should see something like the following:

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"
    }
  ]
}

In the next input box, ask the app to Use names coming from Germany and press Enter. You should see the names converted into regional names.

{
  "fullName": "Hans Müller",
  "itemName": "Bratwurst",
  "quantity": 3,
  "type": "pickup"
},
{
  "fullName": "Anna Schmidt",
  "itemName": "Schnitzel",
  "quantity": -2,
  "type": "delivery"
},

Now, you have a chat-like interface you can use to generate and modify JSON data in bulk.

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