File size: 3,224 Bytes
4ff79c6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | # SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
#
# SPDX-License-Identifier: Apache-2.0
from dataclasses import asdict, dataclass, field
from enum import Enum
from typing import Any, Dict, Optional
class ChatRole(str, Enum):
"""Enumeration representing the roles within a chat."""
ASSISTANT = "assistant"
USER = "user"
SYSTEM = "system"
FUNCTION = "function"
@dataclass
class ChatMessage:
"""
Represents a message in a LLM chat conversation.
:param content: The text content of the message.
:param role: The role of the entity sending the message.
:param name: The name of the function being called (only applicable for role FUNCTION).
:param meta: Additional metadata associated with the message.
"""
content: str
role: ChatRole
name: Optional[str]
meta: Dict[str, Any] = field(default_factory=dict, hash=False)
def is_from(self, role: ChatRole) -> bool:
"""
Check if the message is from a specific role.
:param role: The role to check against.
:returns: True if the message is from the specified role, False otherwise.
"""
return self.role == role
@classmethod
def from_assistant(cls, content: str, meta: Optional[Dict[str, Any]] = None) -> "ChatMessage":
"""
Create a message from the assistant.
:param content: The text content of the message.
:param meta: Additional metadata associated with the message.
:returns: A new ChatMessage instance.
"""
return cls(content, ChatRole.ASSISTANT, None, meta or {})
@classmethod
def from_user(cls, content: str) -> "ChatMessage":
"""
Create a message from the user.
:param content: The text content of the message.
:returns: A new ChatMessage instance.
"""
return cls(content, ChatRole.USER, None)
@classmethod
def from_system(cls, content: str) -> "ChatMessage":
"""
Create a message from the system.
:param content: The text content of the message.
:returns: A new ChatMessage instance.
"""
return cls(content, ChatRole.SYSTEM, None)
@classmethod
def from_function(cls, content: str, name: str) -> "ChatMessage":
"""
Create a message from a function call.
:param content: The text content of the message.
:param name: The name of the function being called.
:returns: A new ChatMessage instance.
"""
return cls(content, ChatRole.FUNCTION, name)
def to_dict(self) -> Dict[str, Any]:
"""
Converts ChatMessage into a dictionary.
:returns:
Serialized version of the object.
"""
data = asdict(self)
data["role"] = self.role.value
return data
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "ChatMessage":
"""
Creates a new ChatMessage object from a dictionary.
:param data:
The dictionary to build the ChatMessage object.
:returns:
The created object.
"""
data["role"] = ChatRole(data["role"])
return cls(**data)
|