ChatMessageContent Class
This is the class for chat message response content.
All Chat Completion Services should return an instance of this class as response. Or they can implement their own subclass of this class and return an instance.
Create a ChatMessageContent instance.
- Inheritance
-
ChatMessageContent
Constructor
ChatMessageContent(role: AuthorRole, items: list[AnnotationContent | ImageContent | TextContent | FunctionResultContent | FunctionCallContent | FileReferenceContent | StreamingAnnotationContent | StreamingFileReferenceContent] | None = None, content: str | None = None, inner_content: Any | None = None, name: str | None = None, encoding: str | None = None, finish_reason: FinishReason | None = None, ai_model_id: str | None = None, metadata: dict[str, Any] | None = None, *, content_type: Literal[ContentTypes.CHAT_MESSAGE_CONTENT] = 'message')
Parameters
Name | Description |
---|---|
inner_content
|
Optional[Any] - The inner content of the response, this should hold all the information from the response so even when not creating a subclass a developer can leverage the full thing. Default value: None
|
ai_model_id
|
Optional[str] - The id of the AI model that generated this response. Default value: None
|
metadata
|
Dict[str, Any] - Any metadata that should be attached to the response. Default value: None
|
role
Required
|
ChatRole - The role of the chat message. |
content
|
Optional[str] - The text of the response. Default value: None
|
encoding
|
Optional[str] - The encoding of the text. Default value: None
|
role
Required
|
AuthorRole - The role of the chat message. |
items
|
list[TextContent, StreamingTextContent, FunctionCallContent, FunctionResultContent, ImageContent]
Default value: None
|
content
Required
|
str - The text of the response. |
inner_content
Required
|
Optional[Any] - The inner content of the response, this should hold all the information from the response so even when not creating a subclass a developer can leverage the full thing. |
name
|
Optional[str] - The name of the response. Default value: None
|
encoding
Required
|
Optional[str] - The encoding of the text. |
finish_reason
|
Optional[FinishReason] - The reason the response was finished. Default value: None
|
ai_model_id
Required
|
Optional[str] - The id of the AI model that generated this response. |
metadata
Required
|
Dict[str, Any] - Any metadata that should be attached to the response. |
**kwargs
Required
|
Any - Any additional fields to set on the instance. |
Keyword-Only Parameters
Name | Description |
---|---|
content_type
|
Default value: message
|
Methods
from_element |
Create a new instance of ChatMessageContent from an XML element. |
to_dict |
Serialize the ChatMessageContent to a dictionary. |
to_element |
Convert the ChatMessageContent to an XML Element. |
to_prompt |
Convert the ChatMessageContent to a prompt. |
from_element
Create a new instance of ChatMessageContent from an XML element.
from_element(element: Element) -> ChatMessageContent
Parameters
Name | Description |
---|---|
element
Required
|
Element - The XML Element to create the ChatMessageContent from. |
Returns
Type | Description |
---|---|
ChatMessageContent - The new instance of ChatMessageContent or a subclass. |
to_dict
Serialize the ChatMessageContent to a dictionary.
to_dict(role_key: str = 'role', content_key: str = 'content') -> dict[str, Any]
Parameters
Name | Description |
---|---|
role_key
|
Default value: role
|
content_key
|
Default value: content
|
Returns
Type | Description |
---|---|
dict - The dictionary representing the ChatMessageContent. |
to_element
Convert the ChatMessageContent to an XML Element.
to_element() -> Element
Parameters
Name | Description |
---|---|
root_key
Required
|
str - The key to use for the root of the XML Element. |
Returns
Type | Description |
---|---|
Element - The XML Element representing the ChatMessageContent. |
to_prompt
Convert the ChatMessageContent to a prompt.
to_prompt() -> str
Returns
Type | Description |
---|---|
str - The prompt from the ChatMessageContent. |
Attributes
content
Get the content of the response, will find the first TextContent's text.
model_computed_fields
A dictionary of computed field names and their corresponding ComputedFieldInfo objects.
model_computed_fields: ClassVar[Dict[str, ComputedFieldInfo]] = {}
model_config
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True, 'populate_by_name': True, 'validate_assignment': True}
model_fields
Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo] objects.
This replaces Model.fields from Pydantic V1.
model_fields: ClassVar[Dict[str, FieldInfo]] = {'ai_model_id': FieldInfo(annotation=Union[str, NoneType], required=False, default=None), 'content_type': FieldInfo(annotation=Literal[<ContentTypes.CHAT_MESSAGE_CONTENT: 'message'>], required=False, default='message', init=False), 'encoding': FieldInfo(annotation=Union[str, NoneType], required=False, default=None), 'finish_reason': FieldInfo(annotation=Union[FinishReason, NoneType], required=False, default=None), 'inner_content': FieldInfo(annotation=Union[Any, NoneType], required=False, default=None), 'items': FieldInfo(annotation=list[Annotated[Union[AnnotationContent, ImageContent, TextContent, FunctionResultContent, FunctionCallContent, FileReferenceContent, StreamingAnnotationContent, StreamingFileReferenceContent], FieldInfo(annotation=NoneType, required=True, discriminator='content_type')]], required=False, default_factory=list), 'metadata': FieldInfo(annotation=dict[str, Any], required=False, default_factory=dict), 'name': FieldInfo(annotation=Union[str, NoneType], required=False, default=None), 'role': FieldInfo(annotation=AuthorRole, required=True)}
ai_model_id
ai_model_id: str | None
content_type
content_type: Literal[ContentTypes.CHAT_MESSAGE_CONTENT]
encoding
encoding: str | None
finish_reason
finish_reason: FinishReason | None
inner_content
inner_content: Any | None
items
items: list[Annotated[AnnotationContent | ImageContent | TextContent | FunctionResultContent | FunctionCallContent | FileReferenceContent | StreamingAnnotationContent | StreamingFileReferenceContent, FieldInfo(annotation=NoneType, required=True, discriminator='content_type')]]
metadata
metadata: dict[str, Any]
name
name: str | None
role
role: AuthorRole
tag
tag: ClassVar[str] = 'message'