pepperoni21 / ollama-rs

A Rust library allowing to interact with the Ollama API.
MIT License
542 stars 81 forks source link

Fixed chat history free issue and added test #78

Closed pepperoni21 closed 2 months ago

pepperoni21 commented 2 months ago

Fixes https://github.com/pepperoni21/ollama-rs/issues/73

Replaced

let index_to_remove = messages
            .first()
            .map(|m| if m.role == MessageRole::System { 1 } else { 0 })
            .unwrap_or(0);

        messages.remove(index_to_remove);

With

if messages.len() >= self.messages_number_limit as usize {
        if let Some(index_to_remove) = messages.iter().position(|m| m.role != MessageRole::System) {
            messages.remove(index_to_remove);
        }
}

Added the following test: "test_chat_history_freed_if_limit_exceeded"