diff --git a/examples/AnalyzeSentimentinTextUsingChatGPTinJSONMode.md b/examples/AnalyzeSentimentinTextUsingChatGPTinJSONMode.md deleted file mode 100644 index 222a870..0000000 --- a/examples/AnalyzeSentimentinTextUsingChatGPTinJSONMode.md +++ /dev/null @@ -1,84 +0,0 @@ - -# Analyze Sentiment in Text Using ChatGPT™ in JSON Mode - -To run the code shown on this page, open the MLX file in MATLAB®: [mlx-scripts/AnalyzeSentimentinTextUsingChatGPTinJSONMode.mlx](mlx-scripts/AnalyzeSentimentinTextUsingChatGPTinJSONMode.mlx) - -This example shows how to use ChatGPT for sentiment analysis and output the results in JSON format. - - -To run this example, you need a valid API key from a paid OpenAI™ API account. - -```matlab -loadenv(".env") -addpath('../..') -``` - -Define some text to analyze the sentiment. - -```matlab -inputText = ["I can't stand homework."; - "This sucks. I'm bored."; - "I can't wait for Halloween!!!"; - "I am neigher for or against the idea."; - "My cat is adorable ❤️❤️"; - "I hate chocolate"]; -``` - -Define the expected output JSON Schema. - -```matlab -jsonSchema = '{"sentiment": "string (positive, negative, neutral)","confidence_score": "number (0-1)"}'; -``` - -Define the system prompt, combining your instructions and the JSON Schema. In order for the model to output JSON, you need to specify that in the prompt, for example, adding *"designed to output to JSON"* to the prompt. - -```matlab -systemPrompt = "You are an AI designed to output to JSON. You analyze the sentiment of the provided text and " + ... - "Determine whether the sentiment is positive, negative, or neutral and provide a confidence score using " + ... - "the schema: " + jsonSchema; -prompt = "Analyze the sentiment of the provided text. " + ... - "Determine whether the sentiment is positive, negative," + ... - " or neutral and provide a confidence score"; -``` - -Create a chat object with `ModelName gpt-3.5-turbo` and specify `ResponseFormat` as `"json".` - -```matlab -model = "gpt-3.5-turbo"; -chat = openAIChat(systemPrompt, ModelName=model, ResponseFormat="json"); -``` - -Concatenate the prompt and input text and generate an answer with the model. - -```matlab -scores = cell(1,numel(inputText)); -for i = 1:numel(inputText) -``` - -Generate a response from the message. - -```matlab - [json, message, response] = generate(chat,prompt + newline + newline + inputText(i)); - scores{i} = jsondecode(json); -end -``` - -Extract the data from the JSON output. - -```matlab -T = struct2table([scores{:}]); -T.text = inputText; -T = movevars(T,"text","Before","sentiment") -``` -| |text|sentiment|confidence_score| -|:--:|:--:|:--:|:--:| -|1|"I can't stand homework."|'negative'|0.9500| -|2|"This sucks. I'm bored."|'negative'|0.9000| -|3|"I can't wait for Halloween!!!"|'positive'|1| -|4|"I am neigher for or against the idea."|'neutral'|1| -|5|"My cat is adorable ❤️❤️"|'positive'|0.9500| -|6|"I hate chocolate"|'negative'|1| - - -*Copyright 2024 The MathWorks, Inc.* - diff --git a/examples/AnalyzeSentimentinTextUsingChatGPTwithStructuredOutput.md b/examples/AnalyzeSentimentinTextUsingChatGPTwithStructuredOutput.md new file mode 100644 index 0000000..31148fd --- /dev/null +++ b/examples/AnalyzeSentimentinTextUsingChatGPTwithStructuredOutput.md @@ -0,0 +1,94 @@ + +# Analyze Sentiment in Text Using ChatGPT™ and Structured Output + +To run the code shown on this page, open the MLX file in MATLAB®: [mlx-scripts/AnalyzeSentimentinTextUsingChatGPTwithStructuredOutput.mlx](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. + +```matlab +loadenv(".env") +addpath('../..') +``` + +Define some text to analyze the sentiment. + +```matlab +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. + +```matlab +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](https://www.mathworks.com/help/matlab/categorical-arrays.html) to restrict the values that can be returned to the list `["positive","negative","neutral"]`. + +```matlab +prototype = struct(... + "sentiment", categorical("positive",["positive","negative","neutral"]),... + "confidence", 0.2) +``` + +```matlabTextOutput +prototype = struct with fields: + sentiment: positive + confidence: 0.2000 + +``` + +Create a chat object and set `ResponseFormat` to `prototype`. + +```matlab +chat = openAIChat(systemPrompt, ResponseFormat=prototype); +``` + +Concatenate the prompt and input text and generate an answer with the model. + +```matlab +scores = []; +for i = 1:numel(inputText) +``` + +Generate a response from the message. + +```matlab + thisResponse = generate(chat,prompt + newline + newline + inputText(i)); + scores = [scores; thisResponse]; %#ok +end +``` + +Extract the content from the output structure array `scores`. + +```matlab +T = struct2table(scores); +T.text = inputText; +T = movevars(T,"text","Before","sentiment") +``` +| |text|sentiment|confidence| +|:--:|:--:|:--:|:--:| +|1|"I can't stand homework."|negative|0.9500| +|2|"This sucks. I'm bored."|negative|0.9500| +|3|"I can't wait for Halloween!!!"|positive|0.9500| +|4|"I am neither for nor against the idea."|neutral|0.9500| +|5|"My cat is adorable ❤️❤️"|positive|0.9500| +|6|"I hate chocolate"|negative|0.9500| +|7|"More work. Great."|negative|0.8500| +|8|"More work. Great!"|positive|0.9000| + + +*Copyright 2024 The MathWorks, Inc.* + diff --git a/examples/mlx-scripts/AnalyzeSentimentinTextUsingChatGPTinJSONMode.mlx b/examples/mlx-scripts/AnalyzeSentimentinTextUsingChatGPTinJSONMode.mlx deleted file mode 100644 index fe23af7..0000000 Binary files a/examples/mlx-scripts/AnalyzeSentimentinTextUsingChatGPTinJSONMode.mlx and /dev/null differ diff --git a/examples/mlx-scripts/AnalyzeSentimentinTextUsingChatGPTwithStructuredOutput.mlx b/examples/mlx-scripts/AnalyzeSentimentinTextUsingChatGPTwithStructuredOutput.mlx new file mode 100644 index 0000000..a093836 Binary files /dev/null and b/examples/mlx-scripts/AnalyzeSentimentinTextUsingChatGPTwithStructuredOutput.mlx differ diff --git a/tests/recordings/AnalyzeSentimentinTextUsingChatGPTinJSONMode.mat b/tests/recordings/AnalyzeSentimentinTextUsingChatGPTinJSONMode.mat deleted file mode 100644 index 310d0e8..0000000 Binary files a/tests/recordings/AnalyzeSentimentinTextUsingChatGPTinJSONMode.mat and /dev/null differ diff --git a/tests/recordings/AnalyzeSentimentinTextUsingChatGPTwithStructuredOutput.mat b/tests/recordings/AnalyzeSentimentinTextUsingChatGPTwithStructuredOutput.mat new file mode 100644 index 0000000..21b5d7c Binary files /dev/null and b/tests/recordings/AnalyzeSentimentinTextUsingChatGPTwithStructuredOutput.mat differ diff --git a/tests/texampleTests.m b/tests/texampleTests.m index fcf713d..8322550 100644 --- a/tests/texampleTests.m +++ b/tests/texampleTests.m @@ -65,9 +65,9 @@ function testAnalyzeScientificPapersUsingFunctionCalls(testCase) AnalyzeScientificPapersUsingFunctionCalls; end - function testAnalyzeSentimentinTextUsingChatGPTinJSONMode(testCase) - testCase.startCapture("AnalyzeSentimentinTextUsingChatGPTinJSONMode"); - AnalyzeSentimentinTextUsingChatGPTinJSONMode; + function testAnalyzeSentimentinTextUsingChatGPTwithStructuredOutput(testCase) + testCase.startCapture("AnalyzeSentimentinTextUsingChatGPTwithStructuredOutput"); + AnalyzeSentimentinTextUsingChatGPTwithStructuredOutput; end function testAnalyzeTextDataUsingParallelFunctionCallwithChatGPT(testCase)