Skip to content

Commit

Permalink
Merge pull request #78 from pepperoni21/fix/73
Browse files Browse the repository at this point in the history
Fixed chat history free issue and added test
  • Loading branch information
pepperoni21 authored Sep 21, 2024
2 parents 1933c2d + 12bf91c commit 56ba0b8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@ impl MessagesHistory {
// Replacing the oldest message if the limit is reached
// The oldest message is the first one, unless it's a system message
if messages.len() >= self.messages_number_limit as usize {
let index_to_remove = messages
.first()
.map(|m| if m.role == MessageRole::System { 1 } else { 0 })
.unwrap_or(0);

messages.remove(index_to_remove);
if let Some(index_to_remove) =
messages.iter().position(|m| m.role != MessageRole::System)
{
messages.remove(index_to_remove);
}
}

if message.role == MessageRole::System {
Expand Down
23 changes: 23 additions & 0 deletions tests/chat_history_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,26 @@ fn clear_chat_history_for_all() {
assert!(ollama.get_messages_history(first_chat_id).is_none());
assert!(ollama.get_messages_history(another_chat_id).is_none());
}

#[test]
fn test_chat_history_freed_if_limit_exceeded() {
let mut ollama = Ollama::new_default_with_history(3);
let chat_id = "default";

ollama.add_user_response(chat_id, "Hello");
ollama.add_assistant_response(chat_id, "Hi");

ollama.add_user_response(chat_id, "Tell me 'hi' again");
ollama.add_assistant_response(chat_id, "Hi again");

ollama.add_user_response(chat_id, "Tell me 'hi' again");
ollama.add_assistant_response(chat_id, "Hi again");

let history = ollama.get_messages_history(chat_id).unwrap();

assert_eq!(history.len(), 3);

let last = history.last();
assert!(last.is_some());
assert_eq!(last.unwrap().content, "Hi again".to_string());
}

0 comments on commit 56ba0b8

Please sign in to comment.