Analyze Sentiment in Text Using ChatGPT™ and Structured Output

December 4, 2025 · View on GitHub

To run the code shown on this page, open the MLX file in MATLAB®: mlx-scripts/AnalyzeSentimentinTextUsingChatGPTwithStructuredOutput.mlx

This example shows how to use ChatGPT for sentiment analysis and output the results in a desired format.

To run this example, you need a valid API key from a paid OpenAI™ API account.

loadenv(".env")

Define some text to analyze the sentiment.

inputText = ["I can't stand homework.";
    "This sucks. I'm bored.";
    "I can't wait for Halloween!!!";
    "I am neither for nor against the idea.";
    "My cat is adorable ❤️❤️";
    "I hate chocolate";
    "More work. Great.";
    "More work. Great!"];

Define the system prompt.

systemPrompt = "You are an AI designed to analyze the sentiment of the provided text and  " + ...
    "Determine whether the sentiment is positive, negative, or neutral " + ...
    "and provide a confidence score between 0 and 1.";
prompt = "Analyze the sentiment of the provided text.";

Define the expected output format by providing an example – when supplied with a struct as the ResponseFormat, generate will return a struct with the same field names and data types. Use a categorical to restrict the values that can be returned to the list ["positive","negative","neutral"].

prototype = struct(...
    "sentiment", categorical("positive",["positive","negative","neutral"]),...
    "confidence", 0.2)
prototype = struct with fields:
     sentiment: positive
    confidence: 0.2000

Create a chat object and set ResponseFormat to prototype. This example uses the model GPT-4.1 nano.

chat = openAIChat(systemPrompt, ResponseFormat=prototype, ModelName="gpt-4.1-nano");

Concatenate the prompt and input text and generate an answer with the model.

scores = [];
for i = 1:numel(inputText)

Generate a response from the message.

    thisResponse = generate(chat,prompt + newline + newline + inputText(i));
    scores = [scores; thisResponse]; %#ok<AGROW>
end

Extract the content from the output structure array scores.

T = struct2table(scores);
T.text = inputText;
T = movevars(T,"text","Before","sentiment")
textsentimentconfidence
1"I can't stand homework."negative0.9500
2"This sucks. I'm bored."negative0.9500
3"I can't wait for Halloween!!!"positive0.8500
4"I am neither for nor against the idea."neutral0.9000
5"My cat is adorable ❤️❤️"positive0.9500
6"I hate chocolate"negative0.9500
7"More work. Great."negative0.8000
8"More work. Great!"positive0.8000

Copyright 2024 The MathWorks, Inc.