From a6fb38525491a7eef1c0b6c2219fb8caf1394ecc Mon Sep 17 00:00:00 2001 From: AsbjornOlling Date: Tue, 3 Dec 2024 17:19:40 +0100 Subject: [PATCH] get rid of run() --- example-game/nobody_who_prompt_chat.gd | 1 - nobodywho/src/lib.rs | 31 +++++++++++++++----------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/example-game/nobody_who_prompt_chat.gd b/example-game/nobody_who_prompt_chat.gd index 0f3957f..9445f97 100644 --- a/example-game/nobody_who_prompt_chat.gd +++ b/example-game/nobody_who_prompt_chat.gd @@ -1,7 +1,6 @@ extends NobodyWhoPromptChat func _ready() -> void: - run() say("Hi there! Who are you?") func _on_completion_updated(text) -> void: diff --git a/nobodywho/src/lib.rs b/nobodywho/src/lib.rs index 83fdcc9..3fefce3 100644 --- a/nobodywho/src/lib.rs +++ b/nobodywho/src/lib.rs @@ -163,13 +163,17 @@ macro_rules! run_model { } macro_rules! send_text { - ($self:ident, $text:expr) => { + ($self:ident, $text:expr) => {{ + if $self.prompt_tx.is_none() { + godot_warn!("Model context not initialized yet. Initializing now..."); + run_model!($self); + } if let Some(tx) = $self.prompt_tx.as_ref() { tx.send($text).unwrap(); } else { - godot_error!("Model not initialized. Call `run` first"); + unreachable!("prompt_tx should always be set after run_model!"); } - }; + }}; } macro_rules! emit_tokens { @@ -244,7 +248,7 @@ impl INode for NobodyWhoPromptCompletion { #[godot_api] impl NobodyWhoPromptCompletion { #[func] - fn run(&mut self) { + fn start_model_worker(&mut self) { run_model!(self) } @@ -306,7 +310,7 @@ impl INode for NobodyWhoPromptChat { #[godot_api] impl NobodyWhoPromptChat { #[func] - fn run(&mut self) { + fn start_model_worker(&mut self) { run_model!(self) } @@ -315,14 +319,15 @@ impl NobodyWhoPromptChat { // simple closure that returns Err(String) if something fails let say_result = || -> Result<(), String> { // get the model instance - let gd_model_node = self.model_node.as_mut().ok_or( - "No model node provided. Remember to set a model node on NobodyWhoPromptChat.", - )?; - let nobody_model: GdRef = gd_model_node.bind(); - let model: llm::Model = nobody_model - .model - .clone() - .ok_or("Could not access LlamaModel from model node.".to_string())?; + let model: llm::Model = { + let gd_model_node = self.model_node.as_mut().ok_or( + "No model node provided. Remember to set a model node on NobodyWhoPromptChat.", + )?; + let mut nobody_model: GdMut = gd_model_node.bind_mut(); + nobody_model + .get_model() + .map_err(|_| "Could not access LlamaModel from model node.".to_string())? + }; // make a chat string let mut messages: Vec<(String, String)> = vec![];