Prompt Engineering in ChatGPT: adding natural language user input to a GUI easily

Andrew Lui
7 min readMar 31, 2023

--

With a natural language user interface (NLUI), a user can control a computer application with words, phrases, and sentences. For example, the command “set the year range from 2010 to 2016” is acceptable as equivalent to moving a range slider on a graphical user interface. NLUI prototypes have been around since the 1980s. However, building NLUI is challenging due to the precision required to understand the natural language command, as human languages are inherently ambiguous.

In this story, I will demonstrate how easy it is to build NLUI with the large language model ChatGPT of OpenAI. In particular, I will illustrate the practice of prompt engineering to optimise ChatGPT for NLUI tasks. The story comes with Python code you can use to experiment and adapt to your NLUI development.

Large Language Models

Large language models (LLMs) are machine learning models using a very deep neural network to understand natural languages. The latest marvel of artificial intelligence — ChatGPT, the new global buzzword, is an example of LLMs. LLMs typically comprise billions of trainable parameters and are, therefore, extremely capable of learning an enormous amount of semantic features from massive text training datasets.

Pre-trained LLMs have been available for system integration for nearly ten years (considering that models trained by the word2vec and glove algorithms were large in those times). Their applications are most relevant to natural languages, such as translation, conversational agents, and various applications in domains such as education, medical science, and content creation.

ChatGPT

ChatGPT is developed by OpenAI, a research laboratory set up for creating friendlier AI. ChatGPT is precisely this — it provides a conversational interface with which users can interact with the LLM easily. For example, instruct ChatGPT to “write a 200-word essay on climate change” through the input prompt, and it will duly oblige with a nicely crafted passage at the output. In summary, ChatGPT is trained to (1) understand the features and instructions in the input natural language expression, and (2) generate the output according to the instruction.

ChatGPT is not artificial general intelligence (AGI). It generates the most probable output, not the correct output. Noam Chomsky remarked in New York Times that

ChatGPT … a lumbering statistical engine for pattern matching, gorging on hundreds of terabytes of data and extrapolating the most likely conversational response or most probable answer to a scientific question

It is, however, a very easy-to-use tool. In addition to a web-based natural language conversational interface (https://chat.openai.com/chat), ChatGPT also provides an API based on the same conversational model (https://platform.openai.com/docs/api-reference). The example program below demonstrates how to use the API.

import openai

openai.api_key = '........' # fill in the API key obtained from OpenAI

content = 'write a 200-word essay on climate change'
messages = [{'role': 'system', 'content': content}]
chat = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages)

print(chat.choices[0].message.content) # contains the response from ChatGPT

Just a year ago, some serious coding skills would be needed to tap into the power of LLMs. Now, a few lines of code do the job.

Prompt Engineering

ChatGPT can provide an unprecedented level of understanding of input commands, which is pivotal to the development of NLUI. For example, let’s send this command to ChatGPT “set the year range from 2010 to 2016”. The response indicates that the input expression is well understood, but the understanding has yet to be translated into instructions for an NLUI.

Sure, here's the year range from 2010 to 2016:

2010, 2011, 2012, 2013, 2014, 2015, 2016

ChatGPT will, however, do exceptionally well if it knows the context. Unfortunately, for all its might, it does not understand the context. It may know everything, but it does not know which is relevant. Fortunately, users can inform ChatGPT of the context through communication. Prompt engineering investigates ways of communication so that an LLM is effectively steered toward the desired context. The following shows an example prompt for optimising ChatGPT for the NLUI task.

The prompt has the following parts:

  • The application is a graphical user interface (GUI)
  • The GUI consists of a selector for ‘start year’ and a selector for ‘end year’, constrained by the range 2000 to 2023, and four checkboxes with labels ‘Multi’, ‘Single’, ‘Pedestrian’, and ‘Other’.
  • The input will be the natural language commands of a user.
  • The output should be the GUI component to be updated and what update is needed.
  • The output format and error handling is specified.

Given the above context, ChatGPT generates the following response for the input command “set the year range from 2010 to 2016”.

The structured output has made easy the update of the GUI components.

NLUI made easy with ChatGPT: A demo application

This demo application illustrates that NLUI implementation is made very easy with a prompted-engineered ChatGPT model. The following figure describes the data flow in the NLUI.

Building a Simple GUI using Tkinter

A simple GUI serves to illustrate the operation of the NLUI implementation. It has a couple of dropdown menus (start-year and end-year) and four checkboxes. It also provides a text-box with a button for entering the commands in English. The following lists the code and shows the GUI created by the code.

The part of the Python program for creating the GUI using Tkinter
The Graphical User Interface

Note that the functions update_dropdown and update_checkbox are to be called when any of the GUI components needs an update — as instructed by the user command.

This demo application has excluded the speech-to-text feature. It assumes that the user commands come in through the text-box, rather than spoken through the microphone. This story focuses on ChatGPT and prompt engineering, and speech-to-text is beyond the scope. However, interested readers can easily dig up instructions on coding it in Python, for example, using the speech recognition module from Google.

Engaging the ChatGPT with the OpenAI API

OpenAI, the creator of ChatGPT, offers an API for engaging ChatGPT programmatically. At the time of writing, registered users can get a free credit of $5 for testing the API, which costs a small amount for making an API call.

A private key is needed to use the API. After registration, visit the API Key page to create a new private key. Save the private key in a file named OPENAI_KEY or copy the key into the main program chatgpt_nlui_demo.py.

Use the following command to install the openai module.

pip install openai

The following code demonstrates how to set up an in-context prompt, send an input expression to ChatGPT, and receive a response.

The function call_chatgpt considers the parameter input as the user message and returns the response from ChatGPT. Pay attention that the message is a list of two messages: the first is the in-context prompt (read from the file CHATGPT_SYS_PROMPT), and the second is the user message.

Integrating the Two Parts

After pressing the Submit button, the program will spring into action. The event handler, submit_press_event, does the following:

  • Get the message from the text box.
  • Ask the function call_chatgpt to send the message (with the in-context prompt) and return the response.
  • Pass the response text to the function update_gui, which extracts the GUI actions required from the structured response and updates the components accordingly.

Engaging the ChatGPT with the OpenAI API

The complete demo program can be downloaded from this Github repository. The main program is called chatgpt_nlui_demo.py.

Execute the program and try entering different commands, such as the following:

  • I want the start year to be 2006.
  • Let’s see the result from 2010 to 2015.
  • Let’s see the result from 2010 to 2015, and include only multi-vehicle and single-vehicle.
The year range dropdown menus and the checkboxes have been updated according to the command “Let’s see the result from 2010 to 2015, and include only multi-vehicle and single-vehicle” entered into the text-box. The start-year and end-year have been changed to 2010 and 2015 respectively, and the Multi and Single checkboxes are checked.

You are encouraged to try and break the NLUI. One way to break it is to enter something out-of-range or an irrelevant message. The insertion of an error message may disrupt the output format. The in-context prompt is the cause — some further polishing is required. Try improving the prompt to obtain a more consistent response structure from ChatGPT.

Conclusion

This story demonstrates that a appropriately prompt-engineered ChatGPT can substantially ease the implementation of a natural language user interface. Furthermore, ChatGPT’s response can be optimised for a specific context, provided a suitable prompt is used.

Please refer to the references section for further readings on prompt engineering, in-context learning, and large language models.

Thank you for reading.

References

--

--

Andrew Lui

Seasoned Academic Administrator | Engaging Instructor | Machine Learning and AI Researcher | Innovative Educator | Hopelessly Keen Programmer