pub struct GenAIProvider { /* private fields */ }Expand description
Thread-safe LLM provider implementation using Arc<RwLock<>>.
This provider can be cheaply cloned and shared across multiple agents. Each clone shares the same underlying client and rate limiting state.
Implementations§
Source§impl GenAIProvider
impl GenAIProvider
Sourcepub fn new_with_config(
provider_type: Option<&str>,
api_key: Option<&str>,
) -> Result<Self>
pub fn new_with_config( provider_type: Option<&str>, api_key: Option<&str>, ) -> Result<Self>
Creates a new GenAI provider with explicit configuration.
Sourcepub fn from_config(
config: &Config,
cli_model: Option<&str>,
) -> Result<(Self, ResolvedProvider)>
pub fn from_config( config: &Config, cli_model: Option<&str>, ) -> Result<(Self, ResolvedProvider)>
Build a provider from a Config, merging in environment detection and an
optional CLI model override, and return the effective provider/model.
Precedence:
- provider:
config.provider> environment detection - model:
cli_model>config.model> provider default - api_key:
config.api_key> ambient environment - base_url:
config.base_url> ambient environment
The returned client is bound to the resolved adapter, so custom/local
OpenAI-compatible model names (e.g. phi-4-npu-ov) route correctly while
recognized names still resolve by prefix. Model names are passed through
verbatim so genai namespacing (openai::model) keeps working.
Sourcepub async fn get_total_tokens_used(&self) -> usize
pub async fn get_total_tokens_used(&self) -> usize
Get total tokens used across all requests
Sourcepub async fn get_request_count(&self) -> usize
pub async fn get_request_count(&self) -> usize
Get total request count
Sourcepub async fn add_tokens(&self, count: usize)
pub async fn add_tokens(&self, count: usize)
Add tokens to the total count
Sourcepub async fn get_available_models(&self, provider: &str) -> Result<Vec<String>>
pub async fn get_available_models(&self, provider: &str) -> Result<Vec<String>>
Retrieves all available models for a specific provider.
Sourcepub async fn generate_response_simple(
&self,
model: &str,
prompt: &str,
) -> Result<LlmResponse>
pub async fn generate_response_simple( &self, model: &str, prompt: &str, ) -> Result<LlmResponse>
Generates a simple text response without streaming. Includes exponential backoff retry for rate limits and transient errors.
Sourcepub async fn generate_response_with_retry(
&self,
model: &str,
prompt: &str,
max_retries: usize,
) -> Result<LlmResponse>
pub async fn generate_response_with_retry( &self, model: &str, prompt: &str, max_retries: usize, ) -> Result<LlmResponse>
Generates a response with configurable retry count and exponential backoff.
Sourcepub async fn generate_response_stream_to_channel(
&self,
model: &str,
prompt: &str,
tx: UnboundedSender<String>,
) -> Result<()>
pub async fn generate_response_stream_to_channel( &self, model: &str, prompt: &str, tx: UnboundedSender<String>, ) -> Result<()>
Generates a streaming response and sends chunks via mpsc channel.
Sourcepub fn get_supported_providers() -> Vec<&'static str>
pub fn get_supported_providers() -> Vec<&'static str>
Get a list of supported providers
Sourcepub async fn get_available_providers(&self) -> Result<Vec<String>>
pub async fn get_available_providers(&self) -> Result<Vec<String>>
Get all available providers
Sourcepub async fn test_model(&self, model: &str) -> Result<bool>
pub async fn test_model(&self, model: &str) -> Result<bool>
Test if a model is available and working
Source§impl GenAIProvider
impl GenAIProvider
Sourcepub async fn chat_turn_with_tools(
&self,
model: &str,
messages: &[CoreMessage],
tools: &[CoreToolSpec],
choice: CoreToolChoice,
) -> Result<CoreTurnOutput>
pub async fn chat_turn_with_tools( &self, model: &str, messages: &[CoreMessage], tools: &[CoreToolSpec], choice: CoreToolChoice, ) -> Result<CoreTurnOutput>
Execute one tool-calling chat turn against model.
Returns the model’s tool calls when any were issued, otherwise its text. The caller records the output as an observation before acting on it (R2); this driver records nothing.
Sourcepub async fn chat_turn_configured(
&self,
model: &str,
messages: &[CoreMessage],
tools: &[CoreToolSpec],
choice: CoreToolChoice,
config: CoreGenerationConfig,
) -> Result<CoreTurnOutput>
pub async fn chat_turn_configured( &self, model: &str, messages: &[CoreMessage], tools: &[CoreToolSpec], choice: CoreToolChoice, config: CoreGenerationConfig, ) -> Result<CoreTurnOutput>
Execute a tool-calling turn with explicit sampling controls.
Sourcepub async fn stream_tool_turn(
&self,
model: &str,
messages: &[CoreMessage],
tools: &[CoreToolSpec],
choice: CoreToolChoice,
emit: impl FnMut(CoreTurnStreamEvent),
) -> Result<CoreTurnOutput>
pub async fn stream_tool_turn( &self, model: &str, messages: &[CoreMessage], tools: &[CoreToolSpec], choice: CoreToolChoice, emit: impl FnMut(CoreTurnStreamEvent), ) -> Result<CoreTurnOutput>
Stream one tool-aware turn while retaining the complete tool calls.
Reasoning is forwarded as it arrives. Answer text is held until the terminal event proves that the model did not select a tool, preventing tool-call preambles and protocol fragments from leaking into the chat.
Trait Implementations§
Source§impl Clone for GenAIProvider
impl Clone for GenAIProvider
Source§fn clone(&self) -> GenAIProvider
fn clone(&self) -> GenAIProvider
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for GenAIProvider
impl !RefUnwindSafe for GenAIProvider
impl Send for GenAIProvider
impl Sync for GenAIProvider
impl Unpin for GenAIProvider
impl UnsafeUnpin for GenAIProvider
impl !UnwindSafe for GenAIProvider
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read more§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.