-
Notifications
You must be signed in to change notification settings - Fork 31
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
Lazy model switch #210
Merged
Merged
Lazy model switch #210
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
28688fc
model loader external interface changes
noxware 9832f1d
simplify model loader unused error
noxware ed529c2
allow model loader to be used from other threads
noxware cd8dbc0
allow model loader to work without file details
noxware f470215
have access to unloded models when sending messages
noxware 0d829c2
load model when sending message
noxware c3304d7
save used model immediately
noxware ac8f590
unused declarations
noxware 62321cf
do not load model on chat change
noxware 54724b1
display chat last used model
noxware 2125fa5
grey out unloaded model
noxware fe9b454
on model change, record used model but don't load it
noxware 4fa7879
blocking loading and nice error reporting
noxware 078672f
details
noxware cba8bd6
more imperative update_selected_model_info
noxware 7f83e82
remove unused model loader error wrapper
noxware b7c0a08
rename model loader load funcs
noxware a67e23b
revert some borrowing changes
noxware 7dc8971
extract `hex_rgb_color` fn to utils
noxware 2d0dd6e
fix - default to loaded model when no model in chat
noxware c6f59f6
extract fallback logic of the previous fix
noxware 483c348
restore load model on selector change
noxware f1865ea
fix - refcell borrow crash when referenced file deleted
noxware 7a69f74
remove "resume chat" visuals
noxware a0d50bd
show always "chat with model" in downloads table
noxware ab90163
show always "chat with model" in discover
noxware b5bfedd
set last used model when clicking "chat with model"
noxware 323018f
optimized always set last used model when load is called
noxware e2a27af
remove leftover reference to play_arrow.svg
noxware c9947d0
Merge pull request #216 from moxin-org/remove-resume-chat
jmbejar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,8 @@ use serde::{Deserialize, Serialize}; | |
|
||
use crate::data::filesystem::{read_from_file, write_to_file}; | ||
|
||
use super::model_loader::ModelLoader; | ||
|
||
pub type ChatID = u128; | ||
|
||
#[derive(Clone, Debug)] | ||
|
@@ -203,7 +205,13 @@ impl Chat { | |
} | ||
} | ||
} | ||
pub fn send_message_to_model(&mut self, prompt: String, loaded_file: &File, backend: &Backend) { | ||
pub fn send_message_to_model( | ||
&mut self, | ||
prompt: String, | ||
wanted_file: &File, | ||
mut model_loader: ModelLoader, | ||
backend: &Backend, | ||
) { | ||
let (tx, rx) = channel(); | ||
let mut messages: Vec<_> = self | ||
.messages | ||
|
@@ -228,7 +236,7 @@ impl Chat { | |
content: system_prompt.clone(), | ||
role: Role::System, | ||
name: None, | ||
} | ||
}, | ||
); | ||
} else { | ||
messages.insert( | ||
|
@@ -237,15 +245,15 @@ impl Chat { | |
content: "You are a helpful, respectful, and honest assistant.".to_string(), | ||
role: Role::System, | ||
name: None, | ||
} | ||
}, | ||
); | ||
} | ||
|
||
let ip = &self.inferences_params; | ||
let cmd = Command::Chat( | ||
ChatRequestData { | ||
messages, | ||
model: loaded_file.name.clone(), | ||
model: wanted_file.name.clone(), | ||
frequency_penalty: Some(ip.frequency_penalty), | ||
logprobs: None, | ||
top_logprobs: None, | ||
|
@@ -277,51 +285,60 @@ impl Chat { | |
content: prompt.clone(), | ||
}); | ||
|
||
self.last_used_file_id = Some(loaded_file.id.clone()); | ||
|
||
self.messages.push(ChatMessage { | ||
id: next_id + 1, | ||
role: Role::Assistant, | ||
username: Some(loaded_file.name.clone()), | ||
username: Some(wanted_file.name.clone()), | ||
content: "".to_string(), | ||
}); | ||
|
||
let store_chat_tx = self.messages_update_sender.clone(); | ||
backend.command_sender.send(cmd).unwrap(); | ||
self.is_streaming = true; | ||
thread::spawn(move || loop { | ||
if let Ok(response) = rx.recv() { | ||
match response { | ||
Ok(ChatResponse::ChatResponseChunk(data)) => { | ||
let mut is_done = false; | ||
|
||
let _ = store_chat_tx.send(ChatTokenArrivalAction::AppendDelta( | ||
data.choices[0].delta.content.clone(), | ||
)); | ||
|
||
if let Some(_reason) = &data.choices[0].finish_reason { | ||
is_done = true; | ||
let _ = store_chat_tx.send(ChatTokenArrivalAction::StreamingDone); | ||
} | ||
|
||
SignalToUI::set_ui_signal(); | ||
if is_done { | ||
let store_chat_tx = self.messages_update_sender.clone(); | ||
let wanted_file = wanted_file.clone(); | ||
let command_sender = backend.command_sender.clone(); | ||
thread::spawn(move || { | ||
if let Err(err) = model_loader.load(wanted_file.id, command_sender.clone()) { | ||
eprintln!("Error loading model: {}", err); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Future idea: Eventually, all |
||
return; | ||
} | ||
|
||
command_sender.send(cmd).unwrap(); | ||
|
||
loop { | ||
if let Ok(response) = rx.recv() { | ||
match response { | ||
Ok(ChatResponse::ChatResponseChunk(data)) => { | ||
let mut is_done = false; | ||
|
||
let _ = store_chat_tx.send(ChatTokenArrivalAction::AppendDelta( | ||
data.choices[0].delta.content.clone(), | ||
)); | ||
|
||
if let Some(_reason) = &data.choices[0].finish_reason { | ||
is_done = true; | ||
let _ = store_chat_tx.send(ChatTokenArrivalAction::StreamingDone); | ||
} | ||
|
||
SignalToUI::set_ui_signal(); | ||
if is_done { | ||
break; | ||
} | ||
} | ||
Ok(ChatResponse::ChatFinalResponseData(data)) => { | ||
let _ = store_chat_tx.send(ChatTokenArrivalAction::AppendDelta( | ||
data.choices[0].message.content.clone(), | ||
)); | ||
let _ = store_chat_tx.send(ChatTokenArrivalAction::StreamingDone); | ||
SignalToUI::set_ui_signal(); | ||
break; | ||
} | ||
Err(err) => eprintln!("Error receiving response chunk: {:?}", err), | ||
} | ||
Ok(ChatResponse::ChatFinalResponseData(data)) => { | ||
let _ = store_chat_tx.send(ChatTokenArrivalAction::AppendDelta( | ||
data.choices[0].message.content.clone(), | ||
)); | ||
let _ = store_chat_tx.send(ChatTokenArrivalAction::StreamingDone); | ||
SignalToUI::set_ui_signal(); | ||
break; | ||
} | ||
Err(err) => eprintln!("Error receiving response chunk: {:?}", err), | ||
} | ||
} else { | ||
break; | ||
}; | ||
} else { | ||
break; | ||
}; | ||
} | ||
}); | ||
|
||
self.update_title_based_on_first_message(); | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The flag was completely changing what the function does, so I spitted the function (motivated by this).