pub struct OllamaClient { /* private fields */ }Expand description
Ollama client for LLM inference
Implementations§
Source§impl OllamaClient
impl OllamaClient
Sourcepub fn new(config: OllamaConfig) -> Self
pub fn new(config: OllamaConfig) -> Self
Create a new Ollama client
Sourcepub fn get_stats(&self) -> &OllamaUsageStats
pub fn get_stats(&self) -> &OllamaUsageStats
Get usage statistics
Sourcepub fn config(&self) -> &OllamaConfig
pub fn config(&self) -> &OllamaConfig
Access the underlying Ollama configuration
Sourcepub async fn generate(&self, prompt: &str) -> Result<String>
pub async fn generate(&self, prompt: &str) -> Result<String>
Generate text completion using Ollama API
Sourcepub async fn generate_with_params(
&self,
prompt: &str,
params: OllamaGenerationParams,
) -> Result<String>
pub async fn generate_with_params( &self, prompt: &str, params: OllamaGenerationParams, ) -> Result<String>
Generate text completion with custom parameters
Sourcepub async fn generate_with_full_response(
&self,
prompt: &str,
params: OllamaGenerationParams,
) -> Result<OllamaGenerateResponse>
pub async fn generate_with_full_response( &self, prompt: &str, params: OllamaGenerationParams, ) -> Result<OllamaGenerateResponse>
Generate text and return the full response including KV cache context and token stats.
Use this for the two-step contextual enrichment pattern:
let client = OllamaClient::new(OllamaConfig::default());
// Step 1: Prime — load the document into Ollama's KV cache
let prime_params = OllamaGenerationParams {
num_predict: Some(1), // generate minimal output; we just want the context
keep_alive: Some("1h".to_string()),
num_ctx: Some(32768),
..Default::default()
};
let prime = client.generate_with_full_response("<document>..full doc..</document>", prime_params).await?;
println!("Prompt tokens evaluated: {}", prime.prompt_eval_count); // ~doc_tokens
// Step 2: Per chunk — only the chunk tokens are evaluated
for chunk in chunks {
let params = OllamaGenerationParams {
num_predict: Some(80),
context: Some(prime.context.clone()), // ← KV cache reuse!
keep_alive: Some("1h".to_string()),
..Default::default()
};
let resp = client.generate_with_full_response(&chunk, params).await?;
println!("Chunk tokens evaluated: {}", resp.prompt_eval_count); // ~chunk_tokens, not doc_tokens!
}Sourcepub async fn generate_streaming(&self, prompt: &str) -> Result<Receiver<String>>
pub async fn generate_streaming(&self, prompt: &str) -> Result<Receiver<String>>
Generate streaming completion
Returns a channel receiver that yields tokens as they are generated. This enables real-time display of generation progress.
§Example
use graphrag_core::ollama::{OllamaClient, OllamaConfig};
let client = OllamaClient::new(OllamaConfig::default());
let mut rx = client.generate_streaming("Write a story").await?;
while let Some(token) = rx.recv().await {
print!("{}", token);
}Trait Implementations§
Source§impl Clone for OllamaClient
impl Clone for OllamaClient
Source§fn clone(&self) -> OllamaClient
fn clone(&self) -> OllamaClient
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for OllamaClient
impl !RefUnwindSafe for OllamaClient
impl Send for OllamaClient
impl Sync for OllamaClient
impl Unpin for OllamaClient
impl UnsafeUnpin for OllamaClient
impl !UnwindSafe for OllamaClient
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
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more