OpenAI API, Making the Response More Consistent, Part 2: The JSON Format
In part 1, we delved into utilizing the OpenAI API. It’s well-acknowledged that many contemporary AI models exhibit a certain unpredictability, making it difficult to anticipate the exact outputs they might produce. While this unpredictability is often inconsequential for casual conversations, it poses challenges for developers building applications that depend on specific AI-generated responses.
To illustrate, let’s explore an example. Suppose we aim to develop an app using GPT-4 that spontaneously generates facts about a country of its choosing. Observe the following code snippet:
While our output is a straightforward sentence that serves its purpose, extracting the specific country referenced programmatically can be intricate. To enhance the predictability and ease of parsing the response, we can adopt a structured format, such as JSON. Familiar to many, a JSON response functions much like a dictionary, offering key-value pairs for efficient data handling. Let’s refine our code accordingly:
This is much better. However, it can still be unpredictable. For the same code, we can have the following output:
Did you notice the subtle change? This time, the model capitalized the first letter of the keys. While this may seem trivial to the casual observer, it constitutes a distinct key difference in the realm of programming. So, how can we address this inconsistency? The solution is simpler than you might think. We can provide a template specifying the desired response format.
This approach yields a more consistent output. However, it’s important to remember that the output we receive is a string. To utilize it effectively in our applications, we should convert it into a dictionary. This transformation can be achieved using the code below:
Once transformed, you can easily parse data_dict and access the desired values using their respective keys. Remember to import essential libraries, including json and ast.
On a related note, OpenAI has recently introduced a more effective method for obtaining consistent responses from GPT: function calling. We’ll dive into this exciting feature in our upcoming tutorial.