| 221 | |
| 222 | |
| 223 | class AssistantMessage(Message): |
| 224 | role: Literal['assistant'] = Field(default='assistant', description="The role of the messages author, in this case `assistant`.") # type: ignore |
| 225 | content: Union[str, List[Union[ |
| 226 | ContentPartText, |
| 227 | ContentPartImage, |
| 228 | ContentPartAudio, |
| 229 | ContentPartVideo, |
| 230 | ContentPartPdf, |
| 231 | ContentPartRefusal, |
| 232 | ]]] = Field(description="The contents of the assistant message.") |
| 233 | name: Optional[str] = Field(default=None, description="An optional name for the participant. Provides the model information to differentiate between participants of the same role.") # type: ignore |
| 234 | refusal: Optional[str] = Field(default=None, description="The refusal message by the assistant.") # type: ignore |
| 235 | tool_calls: List[ToolCall] = Field(default=[], description="The tool calls generated by the model, such as function calls.") # type: ignore |
| 236 | |
| 237 | @property |
| 238 | def text(self) -> str: |
| 239 | """ |
| 240 | Automatically parse the text inside content, whether it's a string or a list of content parts. |
| 241 | """ |
| 242 | if isinstance(self.content, str): |
| 243 | return self.content |
| 244 | elif isinstance(self.content, list): |
| 245 | return '\n'.join([str(part) for part in self.content]) |
| 246 | else: |
| 247 | return '' |
| 248 | |
| 249 | def __str__(self) -> str: |
| 250 | return f'AssistantMessage(content={self.text})' |
| 251 | |
| 252 | def __repr__(self) -> str: |
| 253 | return f'AssistantMessage(content={repr(self.text)})' |