gbserver/src/services/keywords/llm_keyword.rs

29 lines
950 B
Rust
Raw Normal View History

2025-07-29 21:39:24 -03:00
use rhai::{Dynamic, Engine};
use crate::services::{state::AppState, utils::call_llm};
pub fn llm_keyword(state: &AppState, engine: &mut Engine) {
let ai_config = state.config.clone().unwrap().ai.clone();
engine.register_custom_syntax(
2025-08-02 00:07:54 -03:00
&["LLM", "$expr$"], // Syntax: LLM "text to process"
2025-07-29 21:39:24 -03:00
false, // Expression, not statement
move |context, inputs| {
let text = context.eval_expression_tree(&inputs[0])?;
let text_str = text.to_string();
println!("LLM processing text: {}", text_str);
// Use the same pattern as GET
2025-08-02 00:07:54 -03:00
let fut = call_llm(
&text_str, &ai_config);
2025-07-29 21:39:24 -03:00
let result = tokio::task::block_in_place(|| {
tokio::runtime::Handle::current().block_on(fut)
}).map_err(|e| format!("LLM call failed: {}", e))?;
Ok(Dynamic::from(result))
}
).unwrap();
}