-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
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,42 @@ | ||
%% Describe Images Using ChatGPT | ||
% This example shows how to generate image descriptions using the addUserMessageWithImages | ||
% function. To run this example, you need a valid API key from a paid OpenAI API | ||
% account. | ||
|
||
loadenv(".env") | ||
addpath('..') | ||
%% Load and Display Image Data | ||
% Load the sample image from Wikipedia. Use the |imread| function to read images | ||
% from URLs or filenames. | ||
|
||
image_url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg'; | ||
im = imread(image_url); | ||
imshow(im) | ||
%% Generate Image Descriptions | ||
% Ask questions about the image with the URL. Select |gpt-4-vision-preview| | ||
% model to create a chat object | ||
|
||
chat = openAIChat("You are an AI assistant.", ModelName="gpt-4-vision-preview"); | ||
%% | ||
% Create a message and pass the image url along with the prompt. | ||
|
||
messages = openAIMessages; | ||
messages = addUserMessageWithImages(messages,"What is in the image?", string(image_url)); | ||
%% | ||
% Generate a response. By default, the model returns a very short reponse. To | ||
% override it, set |MaxNumTokens| to 4096 or lower. | ||
|
||
[txt,~,response] = generate(chat,messages,MaxNumTokens=4096); | ||
if response.StatusCode == "OK" | ||
wrappedText = wrapText(txt) | ||
else | ||
response.Body.Data.error | ||
end | ||
%% Helper function | ||
|
||
function wrappedText = wrapText(text) | ||
wrappedText = splitSentences(text); | ||
wrappedText = join(wrappedText,newline); | ||
end | ||
%% | ||
% _Copyright 2024 The MathWorks, Inc._ |
Binary file not shown.