Skip to content

Commit

Permalink
Merge pull request #1197 from phidatahq/fix/make-sure-chat-history-st…
Browse files Browse the repository at this point in the history
…arts-with-user-message

Make sure chat history added to LLM messages starts with a user message
  • Loading branch information
ashpreetbedi authored Oct 3, 2024
2 parents ce090d1 + 6792aa1 commit 45a1ffd
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
9 changes: 8 additions & 1 deletion cookbook/assistants/cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
from phi.assistant import Assistant
from phi.tools.duckduckgo import DuckDuckGo

assistant = Assistant(tools=[DuckDuckGo()], show_tool_calls=True, read_chat_history=True)
assistant = Assistant(
tools=[DuckDuckGo()],
show_tool_calls=True,
read_chat_history=True,
debug_mode=True,
add_chat_history_to_messages=True,
num_history_messages=3,
)
assistant.cli_app(markdown=True)
8 changes: 6 additions & 2 deletions phi/assistant/assistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,9 @@ def _run(

# -*- Add chat history to the messages list
if self.add_chat_history_to_messages:
llm_messages += self.memory.get_last_n_messages(last_n=self.num_history_messages)
llm_messages += self.memory.get_last_n_messages_starting_from_the_user_message(
last_n=self.num_history_messages
)

# -*- Build the User prompt
# References to add to the user_prompt if add_references_to_prompt is True
Expand Down Expand Up @@ -1055,7 +1057,9 @@ async def _arun(
# -*- Add chat history to the messages list
if self.add_chat_history_to_messages:
if self.memory is not None:
llm_messages += self.memory.get_last_n_messages(last_n=self.num_history_messages)
llm_messages += self.memory.get_last_n_messages_starting_from_the_user_message(
last_n=self.num_history_messages
)

# -*- Build the User prompt
# References to add to the user_prompt if add_references_to_prompt is True
Expand Down
21 changes: 16 additions & 5 deletions phi/memory/assistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,25 @@ def get_chat_history(self) -> List[Dict[str, Any]]:
"""
return [message.model_dump(exclude_none=True) for message in self.chat_history]

def get_last_n_messages(self, last_n: Optional[int] = None) -> List[Message]:
"""Returns the last n messages in the llm_messages.
def get_last_n_messages_starting_from_the_user_message(self, last_n: Optional[int] = None) -> List[Message]:
"""Returns the last n messages in the llm_messages always starting with the user message greater than or equal to last_n.
:param last_n: The number of messages to return from the end of the conversation.
If None, returns all messages.
:return: A list of Messages in the chat_history.
"""
return self.llm_messages[-last_n:] if last_n else self.llm_messages
if last_n is None or last_n >= len(self.llm_messages):
return self.llm_messages

# Iterate from the end to find the first user message greater than or equal to last_n
last_user_message_ge_n = None
for i, message in enumerate(reversed(self.llm_messages)):
if message.role == "user" and i >= last_n:
last_user_message_ge_n = len(self.llm_messages) - i - 1
break

# If no user message is found, return all messages; otherwise, return from the found index
return self.llm_messages[last_user_message_ge_n:] if last_user_message_ge_n is not None else self.llm_messages

def get_llm_messages(self) -> List[Dict[str, Any]]:
"""Returns the llm_messages as a list of dictionaries."""
Expand All @@ -90,12 +101,12 @@ def get_llm_messages(self) -> List[Dict[str, Any]]:
def get_formatted_chat_history(self, num_messages: Optional[int] = None) -> str:
"""Returns the chat_history as a formatted string."""

messages = self.get_last_n_messages(num_messages)
messages = self.get_last_n_messages_starting_from_the_user_message(num_messages)
if len(messages) == 0:
return ""

history = ""
for message in self.get_last_n_messages(num_messages):
for message in self.get_last_n_messages_starting_from_the_user_message(num_messages):
if message.role == "user":
history += "\n---\n"
history += f"{message.role.upper()}: {message.content}\n"
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "phidata"
version = "2.4.39"
version = "2.4.40"
description = "Memory, knowledge and tools for LLMs."
requires-python = ">=3.7"
readme = "README.md"
Expand Down

0 comments on commit 45a1ffd

Please sign in to comment.