|
| 1 | + |
| 2 | +# Analyze Sentiment in Text Using ChatGPT™ and Structured Output |
| 3 | + |
| 4 | +To run the code shown on this page, open the MLX file in MATLAB®: [mlx-scripts/AnalyzeSentimentinTextUsingChatGPTwithStructuredOutput.mlx](mlx-scripts/AnalyzeSentimentinTextUsingChatGPTwithStructuredOutput.mlx) |
| 5 | + |
| 6 | +This example shows how to use ChatGPT for sentiment analysis and output the results in a desired format. |
| 7 | + |
| 8 | + |
| 9 | +To run this example, you need a valid API key from a paid OpenAI™ API account. |
| 10 | + |
| 11 | +```matlab |
| 12 | +loadenv(".env") |
| 13 | +addpath('../..') |
| 14 | +``` |
| 15 | + |
| 16 | +Define some text to analyze the sentiment. |
| 17 | + |
| 18 | +```matlab |
| 19 | +inputText = ["I can't stand homework."; |
| 20 | + "This sucks. I'm bored."; |
| 21 | + "I can't wait for Halloween!!!"; |
| 22 | + "I am neither for nor against the idea."; |
| 23 | + "My cat is adorable ❤️❤️"; |
| 24 | + "I hate chocolate"; |
| 25 | + "More work. Great."; |
| 26 | + "More work. Great!"]; |
| 27 | +``` |
| 28 | + |
| 29 | +Define the system prompt. |
| 30 | + |
| 31 | +```matlab |
| 32 | +systemPrompt = "You are an AI designed to analyze the sentiment of the provided text and " + ... |
| 33 | + "Determine whether the sentiment is positive, negative, or neutral " + ... |
| 34 | + "and provide a confidence score between 0 and 1."; |
| 35 | +prompt = "Analyze the sentiment of the provided text."; |
| 36 | +``` |
| 37 | + |
| 38 | +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](https://www.mathworks.com/help/matlab/categorical-arrays.html) to restrict the values that can be returned to the list `["positive","negative","neutral"]`. |
| 39 | + |
| 40 | +```matlab |
| 41 | +prototype = struct(... |
| 42 | + "sentiment", categorical("positive",["positive","negative","neutral"]),... |
| 43 | + "confidence", 0.2) |
| 44 | +``` |
| 45 | + |
| 46 | +```matlabTextOutput |
| 47 | +prototype = struct with fields: |
| 48 | + sentiment: positive |
| 49 | + confidence: 0.2000 |
| 50 | +
|
| 51 | +``` |
| 52 | + |
| 53 | +Create a chat object and set `ResponseFormat` to `prototype`. |
| 54 | + |
| 55 | +```matlab |
| 56 | +chat = openAIChat(systemPrompt, ResponseFormat=prototype); |
| 57 | +``` |
| 58 | + |
| 59 | +Concatenate the prompt and input text and generate an answer with the model. |
| 60 | + |
| 61 | +```matlab |
| 62 | +scores = []; |
| 63 | +for i = 1:numel(inputText) |
| 64 | +``` |
| 65 | + |
| 66 | +Generate a response from the message. |
| 67 | + |
| 68 | +```matlab |
| 69 | + thisResponse = generate(chat,prompt + newline + newline + inputText(i)); |
| 70 | + scores = [scores; thisResponse]; %#ok<AGROW> |
| 71 | +end |
| 72 | +``` |
| 73 | + |
| 74 | +Extract the content from the output structure array `scores`. |
| 75 | + |
| 76 | +```matlab |
| 77 | +T = struct2table(scores); |
| 78 | +T.text = inputText; |
| 79 | +T = movevars(T,"text","Before","sentiment") |
| 80 | +``` |
| 81 | +| |text|sentiment|confidence| |
| 82 | +|:--:|:--:|:--:|:--:| |
| 83 | +|1|"I can't stand homework."|negative|0.9500| |
| 84 | +|2|"This sucks. I'm bored."|negative|0.9500| |
| 85 | +|3|"I can't wait for Halloween!!!"|positive|0.9500| |
| 86 | +|4|"I am neither for nor against the idea."|neutral|0.9500| |
| 87 | +|5|"My cat is adorable ❤️❤️"|positive|0.9500| |
| 88 | +|6|"I hate chocolate"|negative|0.9500| |
| 89 | +|7|"More work. Great."|negative|0.8500| |
| 90 | +|8|"More work. Great!"|positive|0.9000| |
| 91 | + |
| 92 | + |
| 93 | +*Copyright 2024 The MathWorks, Inc.* |
| 94 | + |
0 commit comments