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, or PdfFileContent. Optionally, user message can contain a name of the user. Be aware that not all models support names inUserMessage
.
-
-
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 String
name()
The name of the user. List<Content>
contents()
The contents of the message. String
singleText()
Returns text from a single TextContent. boolean
hasSingleText()
Whether this message contains a single TextContent. String
text()
UserMessage can contain not just a single String text
, but also multiple Contents, which can be either TextContent or ImageContent.ChatMessageType
type()
The type of the message. boolean
equals(Object o)
int
hashCode()
String
toString()
static UserMessage
from(String text)
Create a UserMessage from a text. static UserMessage
from(String name, String text)
Create a UserMessage from a name and a text. static UserMessage
from(Array<Content> contents)
Create a UserMessage from contents. static UserMessage
from(String name, Array<Content> contents)
Create a UserMessage from a name and contents. static UserMessage
from(List<Content> contents)
Create a UserMessage from contents. static UserMessage
from(String name, List<Content> contents)
Create a UserMessage from a name and contents. static UserMessage
userMessage(String text)
Create a UserMessage from a text. static UserMessage
userMessage(String name, String text)
Create a UserMessage from a name and a text. static UserMessage
userMessage(Array<Content> contents)
Create a UserMessage from contents. static UserMessage
userMessage(String name, Array<Content> contents)
Create a UserMessage from a name and contents. static UserMessage
userMessage(List<Content> contents)
Create a UserMessage from contents. static UserMessage
userMessage(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
-
contents
List<Content> contents()
The contents of the message. Content can be either TextContent or ImageContent.
- Returns:
the contents.
-
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:
true
if this message contains a single TextContent,false
otherwise.
-
text
@Deprecated(forRemoval = true) String text()
UserMessage can contain not just a single
String text
, but also multiple Contents, which can be either TextContent or ImageContent. 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()
-
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.
-
-
-
-