Skip to content

Commit

Permalink
get rid of run()
Browse files Browse the repository at this point in the history
  • Loading branch information
AsbjornOlling committed Dec 3, 2024
1 parent f538e92 commit a6fb385
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
1 change: 0 additions & 1 deletion example-game/nobody_who_prompt_chat.gd
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
extends NobodyWhoPromptChat

func _ready() -> void:
run()
say("Hi there! Who are you?")

func _on_completion_updated(text) -> void:
Expand Down
31 changes: 18 additions & 13 deletions nobodywho/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand All @@ -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<NobodyWhoModel> = 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<NobodyWhoModel> = 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![];
Expand Down

0 comments on commit a6fb385

Please sign in to comment.