Skip to content

Commit

Permalink
Improving example text.
Browse files Browse the repository at this point in the history
  • Loading branch information
debymf committed Apr 13, 2024
1 parent 9b438ea commit d065e9d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions examples/ExampleGPT4Vision.m
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 modified examples/ExampleGPT4Vision.mlx
Binary file not shown.

0 comments on commit d065e9d

Please sign in to comment.