Package dev.langchain4j.data.message
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
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 or ImageContent.
In the future, the list of Content types will expand to allow for more modalities (e.g. audio, video, etc.).
Optionally, user message can contain a
name of the user.
Be aware that not all models support names in UserMessage.-
Constructor Summary
ConstructorsConstructorDescriptionUserMessage(Content... contents) Creates aUserMessagefrom one or multipleContents.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 TypeMethodDescriptioncontents()The contents 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.text()Deprecated.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
Creates aUserMessagefrom a text.- Parameters:
text- the text.
-
UserMessage
Creates aUserMessagefrom a name and a text.- Parameters:
name- the name.text- the text.
-
UserMessage
Creates aUserMessagefrom one or multipleContents.Contentcan be eitherTextContentorImageContent.
Will have anullname.- Parameters:
contents- the contents.
-
UserMessage
Creates aUserMessagefrom a name and one or multipleContents.Contentcan be eitherTextContentorImageContent.- Parameters:
name- the name.contents- the contents.
-
UserMessage
Creates aUserMessagefrom a list ofContents.Contentcan be eitherTextContentorImageContent.
Will have anullname.- Parameters:
contents- the contents.
-
UserMessage
Creates aUserMessagefrom a name and a list ofContents.Contentcan be eitherTextContentorImageContent.- Parameters:
name- the name.contents- the contents.
-
-
Method Details
-
name
The name of the user.- Returns:
- the name, or
nullif not set.
-
contents
- Returns:
- the 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.
-
text
Deprecated.UsesingleText()orcontents()instead.UserMessagecan contain not just a singleString text, but also multipleContents, which can be eitherTextContentorImageContent. Therefore, this method is deprecated. Please usesingleText()if you only expect a single text, or usecontents()otherwise.- Specified by:
textin interfaceChatMessage- Returns:
- the text of the message
-
type
Description copied from interface:ChatMessageThe type of the message.- Specified by:
typein interfaceChatMessage- Returns:
- the type of the message
-
equals
-
hashCode
public int hashCode() -
toString
-
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.
-
singleText()orcontents()instead.