Extracting the response
You work for a company that has just launched a new smartphone. The marketing team has collected customer reviews from various online platforms and wants to analyze the feedback to understand the customer sentiment and the most talked-about features of the smartphone. To accelerate this, you've used the OpenAI API to extract structured data from these reviews, using function calling. You now need to write a function to clean the output and return a dictionary of the response from the function only.
The get_response()
function, messages
variable (containing the review) and function_definition
(containing the function to extract sentiment and product features from reviews) have been preloaded. Notice that both messages
and function_definition
can be passed as arguments to the get_response()
function to get the response from the chat completions endpoint.
This exercise is part of the course
Developing AI Systems with the OpenAI API
Exercise instructions
- Define a function to return the dictionary containing the output data, as found in the response under
arguments
. - Print the dictionary.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
client = OpenAI(api_key="")
response = get_response(messages, function_definition)
# Define the function to extract the data dictionary
def extract_dictionary(____):
return ____
# Print the data dictionary
____