pub struct LlmWorker<T> { /* private fields */ }Expand description
Worker for executing LLM calls with tool support and typed responses.
LlmWorker<T> manages the full lifecycle of LLM interactions including:
- Sending messages to the LLM
- Executing tool calls requested by the LLM
- Managing multi-turn tool execution loops
- Combining multiple toolsets
- Deserializing responses into typed values
§Type Parameters
T- The type to deserialize the LLM response into. Must implementDeserializeOwned. UseThreadif you want the raw conversation thread back without deserialization.
§Examples
use serde::Deserialize;
#[derive(Deserialize, JsonSchema)]
struct MyResponse {
answer: String,
confidence: f64,
}
let worker = LlmWorker::<MyResponse>::builder(my_llm)
.with_tool(tool1)
.build();
let response: MyResponse = worker.run(thread).await?;Use [LlmWorker::builder(model)] to construct instances with the desired configuration.
Implementations§
Source§impl<T> LlmWorker<T>
impl<T> LlmWorker<T>
Sourcepub fn builder(model: impl BaseLlm + 'static) -> LlmWorkerBuilder<T>
pub fn builder(model: impl BaseLlm + 'static) -> LlmWorkerBuilder<T>
Creates a new builder for constructing an LlmWorker<T>.
The model is required and must be provided upfront.
§Type Parameters
T- The response type to deserialize into
§Arguments
model- The LLM model to use for this worker
§Examples
use serde::Deserialize;
#[derive(Deserialize)]
struct Response { answer: String }
let worker = LlmWorker::<Response>::builder(my_llm)
.with_tool(tool1)
.build();Creates a new builder from an already-Arc-wrapped LLM.
Use this when you have a shared Arc<dyn BaseLlm> from the runtime
and want to avoid re-wrapping it.
Sourcepub async fn run<IT>(&self, input: IT) -> AgentResult<T>
pub async fn run<IT>(&self, input: IT) -> AgentResult<T>
Runs the worker on the given input thread.
This method executes the LLM with the provided thread, handling any
tool calls requested by the LLM. The worker will continue executing
tools in a loop until the LLM produces a final response, which is then
deserialized into type T.
§Arguments
input- Thread or any type that can be converted into a Thread
§Returns
Returns the deserialized response of type T.
§Errors
Returns an error if:
- The LLM call fails
- Tool execution fails
- Response deserialization fails
§Examples
use radkit::models::Thread;
use serde::Deserialize;
#[derive(Deserialize)]
struct WeatherInfo {
temp: f64,
condition: String,
}
let thread = Thread::new().with_user("What's the weather?");
let response: WeatherInfo = worker.run(thread).await?;
println!("Temperature: {}°F", response.temp);Sourcepub async fn run_and_continue<IT>(&self, input: IT) -> AgentResult<(T, Thread)>
pub async fn run_and_continue<IT>(&self, input: IT) -> AgentResult<(T, Thread)>
Runs the worker and returns both the deserialized result and the thread for follow-up work.
This method executes the LLM with the provided thread, handling tool calls and executing them in a loop. After completion, it returns both the deserialized response and the updated thread, allowing for multi-turn conversations.
§Arguments
input- Thread or any type that can be converted into a Thread
§Returns
Returns a tuple of:
- The deserialized response of type
T - The updated
Threadwith all tool calls and responses included
§Errors
Returns an error if:
- The LLM call fails
- Tool execution fails
- Response deserialization fails
§Examples
use radkit::models::Thread;
use serde::Deserialize;
#[derive(Deserialize)]
struct Response { answer: String }
let thread = Thread::new().with_user("What's the weather?");
let (response, continued_thread) = worker.run_and_continue(thread).await?;
println!("Answer: {}", response.answer);
// Continue the conversation with the same thread
let continued_thread = continued_thread
.with_user("What about tomorrow?");
let (next_response, _) = worker.run_and_continue(continued_thread).await?;