Skip to content

Commit

Permalink
Chat buffer should return last messages, not first (#6979)
Browse files Browse the repository at this point in the history
  • Loading branch information
proDOOMman committed Jul 20, 2023
1 parent be0ded3 commit 991c0eb
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# ChangeLog

## Unreleased

### Bug Fixes / Nits
- Fix returned order of messages in large chat memory (#6979)

## [v0.7.11] - 2023-07-19

### New Features
Expand Down
2 changes: 1 addition & 1 deletion llama_index/memory/chat_memory_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def get(self) -> List[ChatMessage]:
if token_count > self.token_limit:
return []

return self.chat_history[:message_count]
return self.chat_history[-message_count:]

def get_all(self) -> List[ChatMessage]:
"""Get all chat history."""
Expand Down
4 changes: 3 additions & 1 deletion tests/memory/test_chat_memory_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_set() -> None:
def test_max_tokens() -> None:
memory = ChatMemoryBuffer.from_defaults(chat_history=[CHAT_MESSAGE], token_limit=5)

memory.put(ChatMessage(role=MessageRole.USER, content="test message2"))
memory.put(CHAT_MESSAGE)
assert len(memory.get()) == 2

# do we limit properly
Expand All @@ -39,4 +39,6 @@ def test_max_tokens() -> None:
assert len(memory.get_all()) == 4

# does get return in the correct order?
memory.put(ChatMessage(role=MessageRole.USER, content="test message2"))
assert memory.get()[-1].content == "test message2"
assert len(memory.get()) == 2

0 comments on commit 991c0eb

Please sign in to comment.