|
71 | 71 | this.Messages{end+1} = newMessage;
|
72 | 72 | end
|
73 | 73 |
|
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); |
76 | 107 |
|
77 | 108 | arguments
|
78 | 109 | this (1,1) openAIMessages
|
79 |
| - prompt {mustBeNonzeroLengthTextScalar} |
| 110 | + content {mustBeNonzeroLengthTextScalar} |
80 | 111 | images (1,:) cell {mustBeNonempty}
|
81 | 112 | nvp.Detail {mustBeMember(nvp.Detail,["low","high","auto"])} = "auto"
|
82 | 113 | end
|
83 | 114 |
|
84 | 115 | newMessage = struct("role", "user", "content", []);
|
85 |
| - newMessage.content = {struct("type","text","text",string(prompt))}; |
| 116 | + newMessage.content = {struct("type","text","text",string(content))}; |
86 | 117 | for ii = 1:numel(images)
|
87 | 118 | if startsWith(images{ii},("https://"|"http://"))
|
88 | 119 | s = struct( ...
|
|
93 | 124 | MIMEType = "data:image/" + erase(ext,".") + ";base64,";
|
94 | 125 | % Base64 encode the image using the given MIME type
|
95 | 126 | fid = fopen(images{ii});
|
96 |
| - V = fread(fid,'*uint8'); |
| 127 | + im = fread(fid,'*uint8'); |
97 | 128 | fclose(fid);
|
98 |
| - b64 = matlab.net.base64encode(V); |
| 129 | + b64 = matlab.net.base64encode(im); |
99 | 130 | s = struct( ...
|
100 | 131 | "type","image_url", ...
|
101 | 132 | "image_url",struct("url",MIMEType + b64));
|
|
0 commit comments