repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | import logging |
| #2 | from typing import Any, Optional |
| #3 | |
| #4 | from embedchain.helpers.json_serializable import JSONSerializable |
| #5 | |
| #6 | logger = logging.getLogger(__name__) |
| #7 | |
| #8 | |
| #9 | class BaseMessage(JSONSerializable): |
| #10 | """ |
| #11 | The base abstract message class. |
| #12 | |
| #13 | Messages are the inputs and outputs of Models. |
| #14 | """ |
| #15 | |
| #16 | # The string content of the message. |
| #17 | content: str |
| #18 | |
| #19 | # The created_by of the message. AI, Human, Bot etc. |
| #20 | created_by: str |
| #21 | |
| #22 | # Any additional info. |
| #23 | metadata: dict[str, Any] |
| #24 | |
| #25 | def __init__(self, content: str, created_by: str, metadata: Optional[dict[str, Any]] = None) -> None: |
| #26 | super().__init__() |
| #27 | self.content = content |
| #28 | self.created_by = created_by |
| #29 | self.metadata = metadata |
| #30 | |
| #31 | @property |
| #32 | def type(self) -> str: |
| #33 | """Type of the Message, used for serialization.""" |
| #34 | |
| #35 | @classmethod |
| #36 | def is_lc_serializable(cls) -> bool: |
| #37 | """Return whether this class is serializable.""" |
| #38 | return True |
| #39 | |
| #40 | def __str__(self) -> str: |
| #41 | return f"{self.created_by}: {self.content}" |
| #42 | |
| #43 | |
| #44 | class ChatMessage(JSONSerializable): |
| #45 | """ |
| #46 | The base abstract chat message class. |
| #47 | |
| #48 | Chat messages are the pair of (question, answer) conversation |
| #49 | between human and model. |
| #50 | """ |
| #51 | |
| #52 | human_message: Optional[BaseMessage] = None |
| #53 | ai_message: Optional[BaseMessage] = None |
| #54 | |
| #55 | def add_user_message(self, message: str, metadata: Optional[dict] = None): |
| #56 | if self.human_message: |
| #57 | logger.info( |
| #58 | "Human message already exists in the chat message,\ |
| #59 | overwriting it with new message." |
| #60 | ) |
| #61 | |
| #62 | self.human_message = BaseMessage(content=message, created_by="human", metadata=metadata) |
| #63 | |
| #64 | def add_ai_message(self, message: str, metadata: Optional[dict] = None): |
| #65 | if self.ai_message: |
| #66 | logger.info( |
| #67 | "AI message already exists in the chat message,\ |
| #68 | overwriting it with new message." |
| #69 | ) |
| #70 | |
| #71 | self.ai_message = BaseMessage(content=message, created_by="ai", metadata=metadata) |
| #72 | |
| #73 | def __str__(self) -> str: |
| #74 | return f"{self.human_message}\n{self.ai_message}" |
| #75 |