Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

moved history methods to WorkingMemory #977

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 1 addition & 49 deletions core/cat/looking_glass/stray_cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,55 +546,7 @@ def classify(
)

# set 0.5 as threshold - let's see if it works properly
return best_label if score < 0.5 else None

def stringify_chat_history(self, latest_n: int = 5) -> str:
"""Serialize chat history.
Converts to text the recent conversation turns.

Parameters
----------
latest_n : int
Hoe many latest turns to stringify.

Returns
-------
history : str
String with recent conversation turns.

Notes
-----
Such context is placed in the `agent_prompt_suffix` in the place held by {chat_history}.

The chat history is a dictionary with keys::
'who': the name of who said the utterance;
'message': the utterance.

"""

history = self.working_memory.history[-latest_n:]

history_string = ""
for turn in history:
history_string += f"\n - {turn['who']}: {turn['message']}"

return history_string

def langchainfy_chat_history(self, latest_n: int = 5) -> List[BaseMessage]:
chat_history = self.working_memory.history[-latest_n:]

langchain_chat_history = []
for message in chat_history:
if message["role"] == Role.Human:
langchain_chat_history.append(
HumanMessage(name=message["who"], content=message["message"])
)
else:
langchain_chat_history.append(
AIMessage(name=message["who"], content=message["message"])
)

return langchain_chat_history
return best_label if score < 0.5 else None

async def close_connection(self):
if self.__ws:
Expand Down
49 changes: 49 additions & 0 deletions core/cat/memory/working_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,55 @@ class WorkingMemory(BaseModelDict):

# track models usage
model_interactions: List[ModelInteraction] = []

def stringify_chat_history(self, latest_n: int = 5) -> str:
"""Serialize chat history.
Converts to text the recent conversation turns.

Parameters
----------
latest_n : int
Hoe many latest turns to stringify.

Returns
-------
history : str
String with recent conversation turns.

Notes
-----
Such context is placed in the `agent_prompt_suffix` in the place held by {chat_history}.

The chat history is a dictionary with keys::
'who': the name of who said the utterance;
'message': the utterance.

"""

history = self.working_memory.history[-latest_n:]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this should be self.history[-latest_n:], isn't it?


history_string = ""
for turn in history:
history_string += f"\n - {turn['who']}: {turn['message']}"

return history_string

def langchainfy_chat_history(self, latest_n: int = 5) -> List[BaseMessage]:
chat_history = self.working_memory.history[-latest_n:]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above

Copy link
Author

@giovannimarra giovannimarra Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I missed it. I'm going to fix it now.


langchain_chat_history = []
for message in chat_history:
if message["role"] == Role.Human:
langchain_chat_history.append(
HumanMessage(name=message["who"], content=message["message"])
)
else:
langchain_chat_history.append(
AIMessage(name=message["who"], content=message["message"])
)

return langchain_chat_history


def update_conversation_history(self, who, message, why={}):
"""Update the conversation history.
Expand Down