Skip to content

Commit edb198e

Browse files
committed
Add doc to openAIMessages
documentation was added to addUserMessageWithImages
1 parent ebca981 commit edb198e

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

openAIMessages.m

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,49 @@
7171
this.Messages{end+1} = newMessage;
7272
end
7373

74-
function this = addUserMessageWithImages(this, prompt, images, nvp)
75-
%addUserMessageWithImages Add user message with images.
74+
function this = addUserMessageWithImages(this, content, images, nvp)
75+
%addUserMessageWithImages Add user message with images for
76+
%use with GPT-4 Turbo with Vision.
77+
%
78+
% MESSAGES = addUserMessageWithImages(MESSAGES, CONTENT, IMAGES)
79+
% adds a user message with the specified content and images
80+
% to MESSAGES. CONTENT must be a text scalar. IMAGES must be
81+
% a cell array of image URLs or file paths.
82+
%
83+
% messages = addUserMessageWithImages(__,Detail="low");
84+
% specify how the model should process the images using
85+
% "Detail" parameter. The default is "auto".
86+
% - When set to "low", the model scales the image to 512x512
87+
% - When set to "high", the model scales the image to 512x512
88+
% and also creates detailed 512x512 crops of the image
89+
% - When set to "auto", the models chooses which mode to use
90+
% depending on the input image.
91+
%
92+
% Example:
93+
%
94+
% % Create a chat with GPT-4 Turbo with Vision
95+
% chat = openAIChat("You are an AI assistant.", ModelName="gpt-4-vision-preview");
96+
%
97+
% % Create messages object
98+
% messages = openAIMessages;
99+
%
100+
% % Add user message with an image
101+
% content = "What are in this picture?"
102+
% images = {'peppers.png'}
103+
% messages = addUserMessageWithImages(messages, content, images);
104+
%
105+
% % Generate a response
106+
% [text, response] = generate(chat, messages, MaxNumTokens=300);
76107

77108
arguments
78109
this (1,1) openAIMessages
79-
prompt {mustBeNonzeroLengthTextScalar}
110+
content {mustBeNonzeroLengthTextScalar}
80111
images (1,:) cell {mustBeNonempty}
81112
nvp.Detail {mustBeMember(nvp.Detail,["low","high","auto"])} = "auto"
82113
end
83114

84115
newMessage = struct("role", "user", "content", []);
85-
newMessage.content = {struct("type","text","text",string(prompt))};
116+
newMessage.content = {struct("type","text","text",string(content))};
86117
for ii = 1:numel(images)
87118
if startsWith(images{ii},("https://"|"http://"))
88119
s = struct( ...
@@ -93,9 +124,9 @@
93124
MIMEType = "data:image/" + erase(ext,".") + ";base64,";
94125
% Base64 encode the image using the given MIME type
95126
fid = fopen(images{ii});
96-
V = fread(fid,'*uint8');
127+
im = fread(fid,'*uint8');
97128
fclose(fid);
98-
b64 = matlab.net.base64encode(V);
129+
b64 = matlab.net.base64encode(im);
99130
s = struct( ...
100131
"type","image_url", ...
101132
"image_url",struct("url",MIMEType + b64));

0 commit comments

Comments
 (0)