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 Content
s,
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
.-
Constructor Summary
ConstructorDescriptionUserMessage
(Content... contents) Creates aUserMessage
from one or multipleContent
s.UserMessage
(String text) Creates aUserMessage
from a text.UserMessage
(String name, Content... contents) Creates aUserMessage
from a name and one or multipleContent
s.UserMessage
(String name, String text) Creates aUserMessage
from a name and a text.UserMessage
(String name, List<Content> contents) Creates aUserMessage
from a name and a list ofContent
s.UserMessage
(List<Content> contents) Creates aUserMessage
from a list ofContent
s. -
Method Summary
Modifier and TypeMethodDescriptioncontents()
The contents of the message.boolean
static UserMessage
Create aUserMessage
from contents.static UserMessage
Create aUserMessage
from a text.static UserMessage
Create aUserMessage
from a name and contents.static UserMessage
Create aUserMessage
from a name and a text.static UserMessage
Create aUserMessage
from a name and contents.static UserMessage
Create aUserMessage
from contents.int
hashCode()
boolean
Whether this message contains a singleTextContent
.name()
The name of the user.Returns text from a singleTextContent
.text()
Deprecated, for removal: This API element is subject to removal in a future version.toString()
type()
The type of the message.static UserMessage
userMessage
(Content... contents) Create aUserMessage
from contents.static UserMessage
userMessage
(String text) Create aUserMessage
from a text.static UserMessage
userMessage
(String name, Content... contents) Create aUserMessage
from a name and contents.static UserMessage
userMessage
(String name, String text) Create aUserMessage
from a name and a text.static UserMessage
userMessage
(String name, List<Content> contents) Create aUserMessage
from a name and contents.static UserMessage
userMessage
(List<Content> contents) Create aUserMessage
from contents.
-
Constructor Details
-
UserMessage
Creates aUserMessage
from a text.- Parameters:
text
- the text.
-
UserMessage
Creates aUserMessage
from a name and a text.- Parameters:
name
- the name.text
- the text.
-
UserMessage
Creates aUserMessage
from one or multipleContent
s.Content
can be eitherTextContent
orImageContent
.
Will have anull
name.- Parameters:
contents
- the contents.
-
UserMessage
Creates aUserMessage
from a name and one or multipleContent
s.Content
can be eitherTextContent
orImageContent
.- Parameters:
name
- the name.contents
- the contents.
-
UserMessage
Creates aUserMessage
from a list ofContent
s.Content
can be eitherTextContent
orImageContent
.
Will have anull
name.- Parameters:
contents
- the contents.
-
UserMessage
Creates aUserMessage
from a name and a list ofContent
s.Content
can be eitherTextContent
orImageContent
.- Parameters:
name
- the name.contents
- the contents.
-
-
Method Details
-
name
The name of the user.- Returns:
- the name, or
null
if 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 multipleContent
s, or if the onlyContent
is not aTextContent
, aRuntimeException
is thrown.- Returns:
- a single text.
- See Also:
-
hasSingleText
public boolean hasSingleText()Whether this message contains a singleTextContent
.- Returns:
true
if this message contains a singleTextContent
,false
otherwise.
-
text
Deprecated, for removal: This API element is subject to removal in a future version.UsesingleText()
orcontents()
instead.UserMessage
can contain not just a singleString text
, but also multipleContent
s, which can be eitherTextContent
orImageContent
. Therefore, this method is deprecated. Please usesingleText()
if you only expect a single text, or usecontents()
otherwise.- Specified by:
text
in interfaceChatMessage
- Returns:
- the text of the message
-
type
Description copied from interface:ChatMessage
The type of the message.- Specified by:
type
in interfaceChatMessage
- Returns:
- the type of the message
-
equals
-
hashCode
public int hashCode() -
toString
-
from
Create aUserMessage
from a text.- Parameters:
text
- the text.- Returns:
- the
UserMessage
.
-
from
Create aUserMessage
from a name and a text.- Parameters:
name
- the name.text
- the text.- Returns:
- the
UserMessage
.
-
from
Create aUserMessage
from contents.- Parameters:
contents
- the contents.- Returns:
- the
UserMessage
.
-
from
Create aUserMessage
from a name and contents.- Parameters:
name
- the name.contents
- the contents.- Returns:
- the
UserMessage
.
-
from
Create aUserMessage
from contents.- Parameters:
contents
- the contents.- Returns:
- the
UserMessage
.
-
from
Create aUserMessage
from a name and contents.- Parameters:
name
- the name.contents
- the contents.- Returns:
- the
UserMessage
.
-
userMessage
Create aUserMessage
from a text.- Parameters:
text
- the text.- Returns:
- the
UserMessage
.
-
userMessage
Create aUserMessage
from a name and a text.- Parameters:
name
- the name.text
- the text.- Returns:
- the
UserMessage
.
-
userMessage
Create aUserMessage
from contents.- Parameters:
contents
- the contents.- Returns:
- the
UserMessage
.
-
userMessage
Create aUserMessage
from a name and contents.- Parameters:
name
- the name.contents
- the contents.- Returns:
- the
UserMessage
.
-
userMessage
Create aUserMessage
from contents.- Parameters:
contents
- the contents.- Returns:
- the
UserMessage
.
-
userMessage
Create aUserMessage
from a name and contents.- Parameters:
name
- the name.contents
- the contents.- Returns:
- the
UserMessage
.
-
singleText()
orcontents()
instead.