Class UserMessage
java.lang.Object
dev.langchain4j.data.message.UserMessage
- All Implemented Interfaces:
ChatMessage
Represents a message from a user, typically an end user of the application.
Depending on the supported modalities (text, image, audio, video, etc.) of the model, user messages can contain either a single text (a
Optionally, user message can contain a
Optionally, user message can contain custom attributes represented by a mutable
Depending on the supported modalities (text, image, audio, video, etc.) of the model, user messages can contain either a single text (a
String) or multiple Contents,
which can be either TextContent, ImageContent, AudioContent,
VideoContent, or PdfFileContent.
Optionally, user message can contain a
name of the user.
Be aware that not all models support names in UserMessage.
Optionally, user message can contain custom attributes represented by a mutable
Map.
Attributes are not sent to the model, but they are stored in the ChatMemory.-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsConstructorDescriptionUserMessage(Content... contents) Creates aUserMessagefrom one or multipleContents.UserMessage(UserMessage.Builder builder) Creates aUserMessagefrom a builder.UserMessage(String text) Creates aUserMessagefrom a text.UserMessage(String name, Content... contents) Creates aUserMessagefrom a name and one or multipleContents.UserMessage(String name, String text) Creates aUserMessagefrom a name and a text.UserMessage(String name, List<Content> contents) Creates aUserMessagefrom a name and a list ofContents.UserMessage(List<Content> contents) Creates aUserMessagefrom a list ofContents. -
Method Summary
Modifier and TypeMethodDescription<T> TReturns additional attribute by it's key.Returns additional attributes.static UserMessage.Builderbuilder()contents()TheContents of the message.booleanstatic UserMessageCreate aUserMessagefrom contents.static UserMessageCreate aUserMessagefrom a text.static UserMessageCreate aUserMessagefrom a name and contents.static UserMessageCreate aUserMessagefrom a name and a text.static UserMessageCreate aUserMessagefrom a name and contents.static UserMessageCreate aUserMessagefrom contents.inthashCode()booleanWhether this message contains a singleTextContent.name()The name of the user.Returns text from a singleTextContent.toString()type()The type of the message.static UserMessageuserMessage(Content... contents) Create aUserMessagefrom contents.static UserMessageuserMessage(String text) Create aUserMessagefrom a text.static UserMessageuserMessage(String name, Content... contents) Create aUserMessagefrom a name and contents.static UserMessageuserMessage(String name, String text) Create aUserMessagefrom a name and a text.static UserMessageuserMessage(String name, List<Content> contents) Create aUserMessagefrom a name and contents.static UserMessageuserMessage(List<Content> contents) Create aUserMessagefrom contents.
-
Constructor Details
-
UserMessage
-
UserMessage
-
UserMessage
Creates aUserMessagefrom a name and a text.- Parameters:
name- the name.text- the text.
-
UserMessage
- Parameters:
contents- the contents.
-
UserMessage
Creates aUserMessagefrom a name and one or multipleContents.- Parameters:
name- the name.contents- the contents.
-
UserMessage
- Parameters:
contents- the contents.
-
UserMessage
Creates aUserMessagefrom a name and a list ofContents.- Parameters:
name- the name.contents- the contents.
-
-
Method Details
-
name
-
contents
-
singleText
Returns text from a singleTextContent. Use this accessor only if you are certain that the message contains only a single text. If the message contains multipleContents, or if the onlyContentis not aTextContent, aRuntimeExceptionis thrown.- Returns:
- a single text.
- See Also:
-
hasSingleText
public boolean hasSingleText()Whether this message contains a singleTextContent.- Returns:
trueif this message contains a singleTextContent,falseotherwise.
-
attributes
Returns additional attributes.- Since:
- 1.8.0
- See Also:
-
attribute
Returns additional attribute by it's key.- Since:
- 1.8.0
- See Also:
-
type
Description copied from interface:ChatMessageThe type of the message.- Specified by:
typein interfaceChatMessage- Returns:
- the type of the message
-
toBuilder
-
equals
-
hashCode
-
toString
-
builder
-
from
Create aUserMessagefrom a text.- Parameters:
text- the text.- Returns:
- the
UserMessage.
-
from
Create aUserMessagefrom a name and a text.- Parameters:
name- the name.text- the text.- Returns:
- the
UserMessage.
-
from
Create aUserMessagefrom contents.- Parameters:
contents- the contents.- Returns:
- the
UserMessage.
-
from
Create aUserMessagefrom a name and contents.- Parameters:
name- the name.contents- the contents.- Returns:
- the
UserMessage.
-
from
Create aUserMessagefrom contents.- Parameters:
contents- the contents.- Returns:
- the
UserMessage.
-
from
Create aUserMessagefrom a name and contents.- Parameters:
name- the name.contents- the contents.- Returns:
- the
UserMessage.
-
userMessage
Create aUserMessagefrom a text.- Parameters:
text- the text.- Returns:
- the
UserMessage.
-
userMessage
Create aUserMessagefrom a name and a text.- Parameters:
name- the name.text- the text.- Returns:
- the
UserMessage.
-
userMessage
Create aUserMessagefrom contents.- Parameters:
contents- the contents.- Returns:
- the
UserMessage.
-
userMessage
Create aUserMessagefrom a name and contents.- Parameters:
name- the name.contents- the contents.- Returns:
- the
UserMessage.
-
userMessage
Create aUserMessagefrom contents.- Parameters:
contents- the contents.- Returns:
- the
UserMessage.
-
userMessage
Create aUserMessagefrom a name and contents.- Parameters:
name- the name.contents- the contents.- Returns:
- the
UserMessage.
-