-
Notifications
You must be signed in to change notification settings - Fork 269
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #783 from valentimarco/develop
[HUGE PR] Chat Messages for ChatModels
- Loading branch information
Showing
7 changed files
with
231 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,61 @@ | ||
|
||
|
||
|
||
|
||
|
||
from typing import List, Dict | ||
from cat.utils import BaseModelDict | ||
from langchain_core.messages import BaseMessage, AIMessage, HumanMessage | ||
from enum import Enum | ||
|
||
#class WorkingMemory(BaseModelDict): | ||
# history : List = [] | ||
|
||
class Role(Enum): | ||
AI = "AI" | ||
Human = "Human" | ||
|
||
class MessageWhy(BaseModelDict): | ||
"""Class for wrapping message why | ||
Variables: | ||
input (str): input message | ||
intermediate_steps (List): intermediate steps | ||
memory (dict): memory | ||
""" | ||
input: str | ||
intermediate_steps: List | ||
memory: dict | ||
|
||
|
||
class CatMessage(BaseModelDict): | ||
"""Class for wrapping cat message | ||
Variables: | ||
content (str): cat message | ||
user_id (str): user id | ||
""" | ||
content: str | ||
user_id: str | ||
type: str = "chat" | ||
why: MessageWhy | None = None | ||
|
||
|
||
class UserMessage(BaseModelDict): | ||
"""Class for wrapping user message | ||
Variables: | ||
text (str): user message | ||
user_id (str): user id | ||
""" | ||
text: str | ||
user_id: str | ||
|
||
|
||
|
||
def convert_to_Langchain_message(messages: List[UserMessage | CatMessage] ) -> List[BaseMessage]: | ||
messages = [] | ||
for m in messages: | ||
if isinstance(m, CatMessage): | ||
messages.append(HumanMessage(content=m.content, response_metadata={"userId": m.user_id})) | ||
else: | ||
messages.append(AIMessage(content=m.text, response_metadata={"userId": m.user_id})) | ||
return messages | ||
|
||
def convert_to_Cat_message(cat_message: AIMessage, why: MessageWhy) -> CatMessage: | ||
return CatMessage(content=cat_message.content, user_id=cat_message.response_metadata["userId"], why=why) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.