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

Fix panic when removing a message in the history with 0 value #65

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 8 additions & 7 deletions src/generation/chat/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,16 @@ impl Ollama {
/// Without impact for existing history
/// Used to prepare history for request
fn get_chat_messages_by_id(&mut self, history_id: impl ToString) -> Vec<ChatMessage> {
let mut binding = {
let new_history =
std::sync::Arc::new(std::sync::RwLock::new(MessagesHistory::default()));
self.messages_history = Some(new_history);
self.messages_history.clone().unwrap()
};
let chat_history = match self.messages_history.as_mut() {
Some(history) => history,
None => &mut binding,
None => {
self.messages_history = Some(std::sync::Arc::new(std::sync::RwLock::new(
MessagesHistory::default(),
)));
self.messages_history
.as_mut()
.expect("Expect to fetch history")
}
};
// Clone the current chat messages to avoid borrowing issues
// And not to add message to the history if the request fails
Expand Down
13 changes: 7 additions & 6 deletions src/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ 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
.first()
.map(|m| if m.role == MessageRole::System { 1 } else { 0 })
{
messages.remove(index_to_remove);
}
}

if message.role == MessageRole::System {
Expand Down