4
4
5
5
from htmltools import HTML , Tagifiable
6
6
7
- from ._chat_types import ChatUIMessage
7
+ from ._chat_types import ChatMessage
8
8
9
9
if TYPE_CHECKING :
10
10
from anthropic .types import Message as AnthropicMessage
@@ -25,11 +25,11 @@ class GenerateContentResponse:
25
25
26
26
class BaseMessageNormalizer (ABC ):
27
27
@abstractmethod
28
- def normalize (self , message : Any ) -> ChatUIMessage :
28
+ def normalize (self , message : Any ) -> ChatMessage :
29
29
pass
30
30
31
31
@abstractmethod
32
- def normalize_chunk (self , chunk : Any ) -> ChatUIMessage :
32
+ def normalize_chunk (self , chunk : Any ) -> ChatMessage :
33
33
pass
34
34
35
35
@abstractmethod
@@ -42,13 +42,13 @@ def can_normalize_chunk(self, chunk: Any) -> bool:
42
42
43
43
44
44
class StringNormalizer (BaseMessageNormalizer ):
45
- def normalize (self , message : Any ) -> ChatUIMessage :
45
+ def normalize (self , message : Any ) -> ChatMessage :
46
46
x = cast (Optional [str ], message )
47
- return ChatUIMessage (content = x or "" , role = "assistant" )
47
+ return ChatMessage (content = x or "" , role = "assistant" )
48
48
49
- def normalize_chunk (self , chunk : Any ) -> ChatUIMessage :
49
+ def normalize_chunk (self , chunk : Any ) -> ChatMessage :
50
50
x = cast (Optional [str ], chunk )
51
- return ChatUIMessage (content = x or "" , role = "assistant" )
51
+ return ChatMessage (content = x or "" , role = "assistant" )
52
52
53
53
def can_normalize (self , message : Any ) -> bool :
54
54
return isinstance (message , (str , HTML )) or message is None
@@ -58,17 +58,17 @@ def can_normalize_chunk(self, chunk: Any) -> bool:
58
58
59
59
60
60
class DictNormalizer (BaseMessageNormalizer ):
61
- def normalize (self , message : Any ) -> ChatUIMessage :
61
+ def normalize (self , message : Any ) -> ChatMessage :
62
62
x = cast ("dict[str, Any]" , message )
63
63
if "content" not in x :
64
64
raise ValueError ("Message must have 'content' key" )
65
- return ChatUIMessage (content = x ["content" ], role = x .get ("role" , "assistant" ))
65
+ return ChatMessage (content = x ["content" ], role = x .get ("role" , "assistant" ))
66
66
67
- def normalize_chunk (self , chunk : Any ) -> ChatUIMessage :
67
+ def normalize_chunk (self , chunk : Any ) -> ChatMessage :
68
68
x = cast ("dict[str, Any]" , chunk )
69
69
if "content" not in x :
70
70
raise ValueError ("Message must have 'content' key" )
71
- return ChatUIMessage (content = x ["content" ], role = x .get ("role" , "assistant" ))
71
+ return ChatMessage (content = x ["content" ], role = x .get ("role" , "assistant" ))
72
72
73
73
def can_normalize (self , message : Any ) -> bool :
74
74
return isinstance (message , dict )
@@ -78,11 +78,11 @@ def can_normalize_chunk(self, chunk: Any) -> bool:
78
78
79
79
80
80
class TagifiableNormalizer (DictNormalizer ):
81
- def normalize (self , message : Any ) -> ChatUIMessage :
81
+ def normalize (self , message : Any ) -> ChatMessage :
82
82
x = cast ("Tagifiable" , message )
83
83
return super ().normalize ({"content" : x })
84
84
85
- def normalize_chunk (self , chunk : Any ) -> ChatUIMessage :
85
+ def normalize_chunk (self , chunk : Any ) -> ChatMessage :
86
86
x = cast ("Tagifiable" , chunk )
87
87
return super ().normalize_chunk ({"content" : x })
88
88
@@ -94,23 +94,23 @@ def can_normalize_chunk(self, chunk: Any) -> bool:
94
94
95
95
96
96
class LangChainNormalizer (BaseMessageNormalizer ):
97
- def normalize (self , message : Any ) -> ChatUIMessage :
97
+ def normalize (self , message : Any ) -> ChatMessage :
98
98
x = cast ("BaseMessage" , message )
99
99
if isinstance (x .content , list ): # type: ignore
100
100
raise ValueError (
101
101
"The `message.content` provided seems to represent numerous messages. "
102
102
"Consider iterating over `message.content` and calling .append_message() on each iteration."
103
103
)
104
- return ChatUIMessage (content = x .content , role = "assistant" )
104
+ return ChatMessage (content = x .content , role = "assistant" )
105
105
106
- def normalize_chunk (self , chunk : Any ) -> ChatUIMessage :
106
+ def normalize_chunk (self , chunk : Any ) -> ChatMessage :
107
107
x = cast ("BaseMessageChunk" , chunk )
108
108
if isinstance (x .content , list ): # type: ignore
109
109
raise ValueError (
110
110
"The `message.content` provided seems to represent numerous messages. "
111
111
"Consider iterating over `message.content` and calling .append_message() on each iteration."
112
112
)
113
- return ChatUIMessage (content = x .content , role = "assistant" )
113
+ return ChatMessage (content = x .content , role = "assistant" )
114
114
115
115
def can_normalize (self , message : Any ) -> bool :
116
116
try :
@@ -130,11 +130,11 @@ def can_normalize_chunk(self, chunk: Any) -> bool:
130
130
131
131
132
132
class OpenAINormalizer (StringNormalizer ):
133
- def normalize (self , message : Any ) -> ChatUIMessage :
133
+ def normalize (self , message : Any ) -> ChatMessage :
134
134
x = cast ("ChatCompletion" , message )
135
135
return super ().normalize (x .choices [0 ].message .content )
136
136
137
- def normalize_chunk (self , chunk : Any ) -> ChatUIMessage :
137
+ def normalize_chunk (self , chunk : Any ) -> ChatMessage :
138
138
x = cast ("ChatCompletionChunk" , chunk )
139
139
return super ().normalize_chunk (x .choices [0 ].delta .content )
140
140
@@ -156,17 +156,17 @@ def can_normalize_chunk(self, chunk: Any) -> bool:
156
156
157
157
158
158
class AnthropicNormalizer (BaseMessageNormalizer ):
159
- def normalize (self , message : Any ) -> ChatUIMessage :
159
+ def normalize (self , message : Any ) -> ChatMessage :
160
160
x = cast ("AnthropicMessage" , message )
161
161
content = x .content [0 ]
162
162
if content .type != "text" :
163
163
raise ValueError (
164
164
f"Anthropic message type { content .type } not supported. "
165
165
"Only 'text' type is currently supported"
166
166
)
167
- return ChatUIMessage (content = content .text , role = "assistant" )
167
+ return ChatMessage (content = content .text , role = "assistant" )
168
168
169
- def normalize_chunk (self , chunk : Any ) -> ChatUIMessage :
169
+ def normalize_chunk (self , chunk : Any ) -> ChatMessage :
170
170
x = cast ("MessageStreamEvent" , chunk )
171
171
content = ""
172
172
if x .type == "content_block_delta" :
@@ -177,7 +177,7 @@ def normalize_chunk(self, chunk: Any) -> ChatUIMessage:
177
177
)
178
178
content = x .delta .text
179
179
180
- return ChatUIMessage (content = content , role = "assistant" )
180
+ return ChatMessage (content = content , role = "assistant" )
181
181
182
182
def can_normalize (self , message : Any ) -> bool :
183
183
try :
@@ -214,13 +214,13 @@ def can_normalize_chunk(self, chunk: Any) -> bool:
214
214
215
215
216
216
class GoogleNormalizer (BaseMessageNormalizer ):
217
- def normalize (self , message : Any ) -> ChatUIMessage :
217
+ def normalize (self , message : Any ) -> ChatMessage :
218
218
x = cast ("GenerateContentResponse" , message )
219
- return ChatUIMessage (content = x .text , role = "assistant" )
219
+ return ChatMessage (content = x .text , role = "assistant" )
220
220
221
- def normalize_chunk (self , chunk : Any ) -> ChatUIMessage :
221
+ def normalize_chunk (self , chunk : Any ) -> ChatMessage :
222
222
x = cast ("GenerateContentResponse" , chunk )
223
- return ChatUIMessage (content = x .text , role = "assistant" )
223
+ return ChatMessage (content = x .text , role = "assistant" )
224
224
225
225
def can_normalize (self , message : Any ) -> bool :
226
226
try :
@@ -238,11 +238,11 @@ def can_normalize_chunk(self, chunk: Any) -> bool:
238
238
239
239
240
240
class OllamaNormalizer (DictNormalizer ):
241
- def normalize (self , message : Any ) -> ChatUIMessage :
241
+ def normalize (self , message : Any ) -> ChatMessage :
242
242
x = cast ("dict[str, Any]" , message ["message" ])
243
243
return super ().normalize (x )
244
244
245
- def normalize_chunk (self , chunk : "dict[str, Any]" ) -> ChatUIMessage :
245
+ def normalize_chunk (self , chunk : "dict[str, Any]" ) -> ChatMessage :
246
246
msg = cast ("dict[str, Any]" , chunk ["message" ])
247
247
return super ().normalize_chunk (msg )
248
248
@@ -295,7 +295,7 @@ def register(
295
295
message_normalizer_registry = NormalizerRegistry ()
296
296
297
297
298
- def normalize_message (message : Any ) -> ChatUIMessage :
298
+ def normalize_message (message : Any ) -> ChatMessage :
299
299
strategies = message_normalizer_registry ._strategies
300
300
for strategy in strategies .values ():
301
301
if strategy .can_normalize (message ):
@@ -306,7 +306,7 @@ def normalize_message(message: Any) -> ChatUIMessage:
306
306
)
307
307
308
308
309
- def normalize_message_chunk (chunk : Any ) -> ChatUIMessage :
309
+ def normalize_message_chunk (chunk : Any ) -> ChatMessage :
310
310
strategies = message_normalizer_registry ._strategies
311
311
for strategy in strategies .values ():
312
312
if strategy .can_normalize_chunk (chunk ):
0 commit comments