Class UserMessage
-
- All Implemented Interfaces:
-
dev.langchain4j.data.message.ChatMessage
public class UserMessage implements 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
String) or multiple Contents, which can be either TextContent, ImageContent, AudioContent, VideoContent, PdfFileContent, or TextFileContent. Optionally, user message can contain a name of the user. Be aware that not all models support names inUserMessage.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public classUserMessage.Builder
-
Constructor Summary
Constructors Constructor Description UserMessage(String text)Creates a UserMessage from a text. UserMessage(String name, String text)Creates a UserMessage from a name and a text. UserMessage(Array<Content> contents)Creates a UserMessage from one or multiple Contents. UserMessage(String name, Array<Content> contents)Creates a UserMessage from a name and one or multiple Contents. UserMessage(List<Content> contents)Creates a UserMessage from a list of Contents. UserMessage(String name, List<Content> contents)Creates a UserMessage from a name and a list of Contents.
-
Method Summary
Modifier and Type Method Description Stringname()The name of the user. List<Content>contents()The Contents of the message. StringsingleText()Returns text from a single TextContent. booleanhasSingleText()Whether this message contains a single TextContent. Stringtext()UserMessage can contain not just a single String text, but also multiple Contents.ChatMessageTypetype()The type of the message. booleanequals(Object o)inthashCode()StringtoString()static UserMessage.Builderbuilder()static UserMessagefrom(String text)Create a UserMessage from a text. static UserMessagefrom(String name, String text)Create a UserMessage from a name and a text. static UserMessagefrom(Array<Content> contents)Create a UserMessage from contents. static UserMessagefrom(String name, Array<Content> contents)Create a UserMessage from a name and contents. static UserMessagefrom(List<Content> contents)Create a UserMessage from contents. static UserMessagefrom(String name, List<Content> contents)Create a UserMessage from a name and contents. static UserMessageuserMessage(String text)Create a UserMessage from a text. static UserMessageuserMessage(String name, String text)Create a UserMessage from a name and a text. static UserMessageuserMessage(Array<Content> contents)Create a UserMessage from contents. static UserMessageuserMessage(String name, Array<Content> contents)Create a UserMessage from a name and contents. static UserMessageuserMessage(List<Content> contents)Create a UserMessage from contents. static UserMessageuserMessage(String name, List<Content> contents)Create a UserMessage from a name and contents. -
-
Constructor Detail
-
UserMessage
UserMessage(String text)
Creates a UserMessage from a text.- Parameters:
text- the text.
-
UserMessage
UserMessage(String name, String text)
Creates a UserMessage from a name and a text.- Parameters:
name- the name.text- the text.
-
UserMessage
UserMessage(Array<Content> contents)
Creates a UserMessage from one or multiple Contents.- Parameters:
contents- the contents.
-
UserMessage
UserMessage(String name, Array<Content> contents)
Creates a UserMessage from a name and one or multiple Contents.- Parameters:
name- the name.contents- the contents.
-
UserMessage
UserMessage(List<Content> contents)
Creates a UserMessage from a list of Contents.- Parameters:
contents- the contents.
-
UserMessage
UserMessage(String name, List<Content> contents)
Creates a UserMessage from a name and a list of Contents.- Parameters:
name- the name.contents- the contents.
-
-
Method Detail
-
singleText
String singleText()
Returns text from a single TextContent. Use this accessor only if you are certain that the message contains only a single text. If the message contains multiple Contents, or if the only Content is not a TextContent, a RuntimeException is thrown.
- Returns:
a single text.
-
hasSingleText
boolean hasSingleText()
Whether this message contains a single TextContent.
- Returns:
trueif this message contains a single TextContent,falseotherwise.
-
text
@Deprecated(forRemoval = true) String text()
UserMessage can contain not just a single
String text, but also multiple Contents. Therefore, this method is deprecated. Please use singleText if you only expect a single text, or use contents otherwise.
-
type
ChatMessageType type()
The type of the message.
- Returns:
the type of the message
-
hashCode
int hashCode()
-
builder
static UserMessage.Builder builder()
-
from
static UserMessage from(String text)
Create a UserMessage from a text.
- Parameters:
text- the text.- Returns:
the UserMessage.
-
from
static UserMessage from(String name, String text)
Create a UserMessage from a name and a text.
- Parameters:
name- the name.text- the text.- Returns:
the UserMessage.
-
from
static UserMessage from(Array<Content> contents)
Create a UserMessage from contents.
- Parameters:
contents- the contents.- Returns:
the UserMessage.
-
from
static UserMessage from(String name, Array<Content> contents)
Create a UserMessage from a name and contents.
- Parameters:
name- the name.contents- the contents.- Returns:
the UserMessage.
-
from
static UserMessage from(List<Content> contents)
Create a UserMessage from contents.
- Parameters:
contents- the contents.- Returns:
the UserMessage.
-
from
static UserMessage from(String name, List<Content> contents)
Create a UserMessage from a name and contents.
- Parameters:
name- the name.contents- the contents.- Returns:
the UserMessage.
-
userMessage
static UserMessage userMessage(String text)
Create a UserMessage from a text.
- Parameters:
text- the text.- Returns:
the UserMessage.
-
userMessage
static UserMessage userMessage(String name, String text)
Create a UserMessage from a name and a text.
- Parameters:
name- the name.text- the text.- Returns:
the UserMessage.
-
userMessage
static UserMessage userMessage(Array<Content> contents)
Create a UserMessage from contents.
- Parameters:
contents- the contents.- Returns:
the UserMessage.
-
userMessage
static UserMessage userMessage(String name, Array<Content> contents)
Create a UserMessage from a name and contents.
- Parameters:
name- the name.contents- the contents.- Returns:
the UserMessage.
-
userMessage
static UserMessage userMessage(List<Content> contents)
Create a UserMessage from contents.
- Parameters:
contents- the contents.- Returns:
the UserMessage.
-
userMessage
static UserMessage userMessage(String name, List<Content> contents)
Create a UserMessage from a name and contents.
- Parameters:
name- the name.contents- the contents.- Returns:
the UserMessage.
-
-
-
-