use super::command::LlamaCommand;
use super::engine::LlamaEngine;
use crate::error::{AmbiError, Result};
use log::error;
use tokio::sync::mpsc::Sender;
use tokio::sync::oneshot;
impl LlamaEngine {
pub(crate) async fn chat_internal(&self, prompt: &str) -> Result<String> {
let (reply_tx, reply_rx) = oneshot::channel();
self.cmd_tx
.send(LlamaCommand::Chat {
prompt: prompt.to_owned(),
reply_tx,
})
.map_err(|_| {
AmbiError::EngineError("Llama engine thread terminated unexpectedly".to_string())
})?;
reply_rx.await.map_err(|_| {
AmbiError::EngineError("Reply channel closed before response".to_string())
})?
}
pub(crate) async fn stream_internal(&self, prompt: &str, tx: Sender<Result<String>>) {
let (done_tx, done_rx) = oneshot::channel();
if self
.cmd_tx
.send(LlamaCommand::ChatStream {
prompt: prompt.to_owned(),
chunk_tx: tx,
done_tx,
})
.is_err()
{
error!("Llama engine thread terminated unexpectedly – cannot start stream");
return;
}
let _ = done_rx.await;
}
pub(crate) fn reset_internal(&self) {
let _ = self.cmd_tx.send(LlamaCommand::Reset);
}
pub(crate) async fn evaluate_sentence_entropy_internal(&self, sentence: &str) -> Result<f32> {
let (reply_tx, reply_rx) = oneshot::channel();
self.cmd_tx
.send(LlamaCommand::EvaluateEntropy {
sentence: sentence.to_owned(),
reply_tx,
})
.map_err(|_| {
AmbiError::EngineError("Llama engine thread terminated unexpectedly".to_string())
})?;
reply_rx.await.map_err(|_| {
AmbiError::EngineError("Reply channel closed before response".to_string())
})?
}
}