Add user message with images to message history
updatedMessages = addUserMessageWithImages(messages,content,images)
___ = addUserMessageWithImages(___,Name=Value)
updatedMessages = addUserMessageWithImages(messages,content,images) adds a user message with images to the messageHistory object messages.
___ = addUserMessageWithImages(___,Detail=detail) also specifies the image resolution to send to the model.
First, specify the OpenAI® API key as an environment variable and save it to a file called ".env". Next, load the environment file using the loadenv function.
loadenv(".env")Create an openAIChat object.
model = openAIChat;Initialize the message history.
messages = messageHistory;View the image "peppers.png", which is included in your MATLAB® installation.
image = "peppers.png";
imshow(image)Add a user message including the image to the message history.
messages = addUserMessageWithImages(messages,"Describe the image.",image);
messages.Messages{1}ans = struct with fields:
role: "user"
content: "Describe the image."
images: "peppers.png"
image_detail: "auto"
Generate text.
generate(model,messages,MaxNumTokens=30)ans = "The image features a colorful assortment of various types of peppers and garlic. The peppers come in a range of colors, including red, yellow, green,"
messageHistory object
Message history, specified as a messageHistory object.
string scalar | character vector
Message content, specified as a string scalar or character vector. The content must be nonempty.
string scalar | string array
Input image, specified as a PNG (*.png) file.
"auto" (default) | "high" | "low"
Specify the resolution of the image you pass to the large language model.
If you set the image resolution to "low", then the generate function sends a 512-by-512 version of the image to the LLM.
If you set the image resolution to "high", then the generate function also sends the full image in chunks of 512-by-512 tiles. This option can be more expensive.
If you set the image resolution to "auto", then the resolution sent to the model depends on the image size.
For more information, see https://platform.openai.com/docs/guides/vision.
messageHistory object
Updated message history, specified as a messageHistory object. The updated message history includes a new structure array with these fields:
- role —
"user" - content — Set by the
contentinput argument - images — Set by the
imagesinput argument - image_detail — Set by the
Detailname-value argument
Copyright 2024 The MathWorks, Inc.
