-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change example to using structured output
- Loading branch information
Showing
7 changed files
with
97 additions
and
87 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
94 changes: 94 additions & 0 deletions
94
examples/AnalyzeSentimentinTextUsingChatGPTwithStructuredOutput.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<AGROW> | ||
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.* | ||
|
Binary file removed
BIN
-4.69 KB
examples/mlx-scripts/AnalyzeSentimentinTextUsingChatGPTinJSONMode.mlx
Binary file not shown.
Binary file added
BIN
+4.86 KB
examples/mlx-scripts/AnalyzeSentimentinTextUsingChatGPTwithStructuredOutput.mlx
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+28.6 KB
tests/recordings/AnalyzeSentimentinTextUsingChatGPTwithStructuredOutput.mat
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters