20
20
class BaseMessage (Serializable ):
21
21
"""Base abstract message class.
22
22
23
- Messages are the inputs and outputs of ChatModels .
23
+ Messages are the inputs and outputs of ``ChatModel``s .
24
24
"""
25
25
26
26
content : Union [str , list [Union [str , dict ]]]
@@ -31,17 +31,18 @@ class BaseMessage(Serializable):
31
31
32
32
For example, for a message from an AI, this could include tool calls as
33
33
encoded by the model provider.
34
+
34
35
"""
35
36
36
37
response_metadata : dict = Field (default_factory = dict )
37
- """Response metadata. For example: response headers, logprobs, token counts, model
38
- name."""
38
+ """Examples: response headers, logprobs, token counts, model name."""
39
39
40
40
type : str
41
41
"""The type of the message. Must be a string that is unique to the message type.
42
42
43
43
The purpose of this field is to allow for easy identification of the message type
44
44
when deserializing messages.
45
+
45
46
"""
46
47
47
48
name : Optional [str ] = None
@@ -51,20 +52,26 @@ class BaseMessage(Serializable):
51
52
52
53
Usage of this field is optional, and whether it's used or not is up to the
53
54
model implementation.
55
+
54
56
"""
55
57
56
58
id : Optional [str ] = Field (default = None , coerce_numbers_to_str = True )
57
- """An optional unique identifier for the message. This should ideally be
58
- provided by the provider/model which created the message."""
59
+ """An optional unique identifier for the message.
60
+
61
+ This should ideally be provided by the provider/model which created the message.
62
+
63
+ """
59
64
60
65
model_config = ConfigDict (
61
66
extra = "allow" ,
62
67
)
63
68
64
69
def __init__ (
65
- self , content : Union [str , list [Union [str , dict ]]], ** kwargs : Any
70
+ self ,
71
+ content : Union [str , list [Union [str , dict ]]],
72
+ ** kwargs : Any ,
66
73
) -> None :
67
- """Pass in content as positional arg .
74
+ """Initialize ``BaseMessage`` .
68
75
69
76
Args:
70
77
content: The string contents of the message.
@@ -73,7 +80,7 @@ def __init__(
73
80
74
81
@classmethod
75
82
def is_lc_serializable (cls ) -> bool :
76
- """BaseMessage is serializable.
83
+ """`` BaseMessage`` is serializable.
77
84
78
85
Returns:
79
86
True
@@ -90,10 +97,11 @@ def get_lc_namespace(cls) -> list[str]:
90
97
return ["langchain" , "schema" , "messages" ]
91
98
92
99
def text (self ) -> str :
93
- """Get the text content of the message.
100
+ """Get the text `` content`` of the message.
94
101
95
102
Returns:
96
103
The text content of the message.
104
+
97
105
"""
98
106
if isinstance (self .content , str ):
99
107
return self .content
@@ -136,6 +144,7 @@ def pretty_repr(
136
144
137
145
Returns:
138
146
A pretty representation of the message.
147
+
139
148
"""
140
149
title = get_msg_title_repr (self .type .title () + " Message" , bold = html )
141
150
# TODO: handle non-string content.
@@ -155,11 +164,12 @@ def merge_content(
155
164
"""Merge multiple message contents.
156
165
157
166
Args:
158
- first_content: The first content. Can be a string or a list.
159
- contents: The other contents . Can be a string or a list.
167
+ first_content: The first `` content`` . Can be a string or a list.
168
+ contents: The other ``content``s . Can be a string or a list.
160
169
161
170
Returns:
162
171
The merged content.
172
+
163
173
"""
164
174
merged = first_content
165
175
for content in contents :
@@ -207,9 +217,10 @@ def __add__(self, other: Any) -> BaseMessageChunk: # type: ignore[override]
207
217
208
218
For example,
209
219
210
- `AIMessageChunk(content="Hello") + AIMessageChunk(content=" World")`
220
+ ``AIMessageChunk(content="Hello") + AIMessageChunk(content=" World")``
221
+
222
+ will give ``AIMessageChunk(content="Hello World")``
211
223
212
- will give `AIMessageChunk(content="Hello World")`
213
224
"""
214
225
if isinstance (other , BaseMessageChunk ):
215
226
# If both are (subclasses of) BaseMessageChunk,
@@ -257,8 +268,9 @@ def message_to_dict(message: BaseMessage) -> dict:
257
268
message: Message to convert.
258
269
259
270
Returns:
260
- Message as a dict. The dict will have a "type" key with the message type
261
- and a "data" key with the message data as a dict.
271
+ Message as a dict. The dict will have a ``type`` key with the message type
272
+ and a ``data`` key with the message data as a dict.
273
+
262
274
"""
263
275
return {"type" : message .type , "data" : message .model_dump ()}
264
276
@@ -267,10 +279,11 @@ def messages_to_dict(messages: Sequence[BaseMessage]) -> list[dict]:
267
279
"""Convert a sequence of Messages to a list of dictionaries.
268
280
269
281
Args:
270
- messages: Sequence of messages (as BaseMessages ) to convert.
282
+ messages: Sequence of messages (as ``BaseMessage``s ) to convert.
271
283
272
284
Returns:
273
285
List of messages as dicts.
286
+
274
287
"""
275
288
return [message_to_dict (m ) for m in messages ]
276
289
@@ -284,6 +297,7 @@ def get_msg_title_repr(title: str, *, bold: bool = False) -> str:
284
297
285
298
Returns:
286
299
The title representation.
300
+
287
301
"""
288
302
padded = " " + title + " "
289
303
sep_len = (80 - len (padded )) // 2
0 commit comments