pub struct ModelClient { /* private fields */ }Expand description
A client for interacting with the Ollama API.
Implementations§
Source§impl ModelClient
impl ModelClient
Sourcepub async fn chat(&self, request: ChatRequest) -> Result<ChatResponse>
pub async fn chat(&self, request: ChatRequest) -> Result<ChatResponse>
Generate a chat completion.
Sourcepub async fn chat_stream(
&self,
request: ChatRequest,
) -> Result<impl Stream<Item = Result<ChatResponse>> + '_>
pub async fn chat_stream( &self, request: ChatRequest, ) -> Result<impl Stream<Item = Result<ChatResponse>> + '_>
Generate a streaming chat completion.
Source§impl ModelClient
impl ModelClient
Sourcepub fn builder() -> ModelClientBuilder
pub fn builder() -> ModelClientBuilder
Create a new builder for a ModelClient.
Sourcepub fn is_authenticated(&self) -> bool
pub fn is_authenticated(&self) -> bool
Check if authentication is configured.
Sourcepub async fn handle_response<T>(
&self,
response: Response,
model: Option<&str>,
) -> Result<T>where
for<'a> T: Deserialize<'a>,
pub async fn handle_response<T>(
&self,
response: Response,
model: Option<&str>,
) -> Result<T>where
for<'a> T: Deserialize<'a>,
Helper method to handle responses consistently.
Sourcepub async fn handle_void_response(&self, response: Response) -> Result<()>
pub async fn handle_void_response(&self, response: Response) -> Result<()>
Helper method to handle responses that return nothing (Result<()>).
Sourcepub async fn get_version(&self) -> Result<VersionResponse>
pub async fn get_version(&self) -> Result<VersionResponse>
Get the version of the Ollama API.
Source§impl ModelClient
impl ModelClient
Sourcepub async fn embed(&self, request: EmbedRequest) -> Result<EmbedResponse>
pub async fn embed(&self, request: EmbedRequest) -> Result<EmbedResponse>
Generate embeddings from text.
Sourcepub async fn embeddings(
&self,
request: EmbeddingsRequest,
) -> Result<EmbeddingsResponse>
pub async fn embeddings( &self, request: EmbeddingsRequest, ) -> Result<EmbeddingsResponse>
Generate legacy embeddings from text.
Source§impl ModelClient
impl ModelClient
Sourcepub async fn generate(
&self,
request: GenerateRequest,
) -> Result<GenerateResponse>
pub async fn generate( &self, request: GenerateRequest, ) -> Result<GenerateResponse>
Generate text from a prompt.
Sourcepub async fn generate_stream(
&self,
request: GenerateRequest,
) -> Result<impl Stream<Item = Result<GenerateResponse>> + '_>
pub async fn generate_stream( &self, request: GenerateRequest, ) -> Result<impl Stream<Item = Result<GenerateResponse>> + '_>
Generate text from a prompt with streaming.
Source§impl ModelClient
impl ModelClient
Sourcepub async fn list_models(&self) -> Result<Vec<ModelInfo>>
pub async fn list_models(&self) -> Result<Vec<ModelInfo>>
List local models.
Sourcepub async fn show_model(
&self,
request: ShowModelRequest,
) -> Result<ShowModelResponse>
pub async fn show_model( &self, request: ShowModelRequest, ) -> Result<ShowModelResponse>
Show information about a model.
Sourcepub async fn copy_model(&self, request: CopyModelRequest) -> Result<()>
pub async fn copy_model(&self, request: CopyModelRequest) -> Result<()>
Copy a model.
Sourcepub async fn delete_model(&self, request: DeleteModelRequest) -> Result<()>
pub async fn delete_model(&self, request: DeleteModelRequest) -> Result<()>
Delete a model.
Sourcepub async fn pull_model(
&self,
request: PullModelRequest,
) -> Result<impl Stream<Item = Result<StatusResponse>> + '_>
pub async fn pull_model( &self, request: PullModelRequest, ) -> Result<impl Stream<Item = Result<StatusResponse>> + '_>
Pull a model.
Sourcepub async fn push_model(
&self,
request: PushModelRequest,
) -> Result<impl Stream<Item = Result<StatusResponse>> + '_>
pub async fn push_model( &self, request: PushModelRequest, ) -> Result<impl Stream<Item = Result<StatusResponse>> + '_>
Push a model.
Sourcepub async fn create_model(
&self,
request: CreateModelRequest,
) -> Result<impl Stream<Item = Result<StatusResponse>> + '_>
pub async fn create_model( &self, request: CreateModelRequest, ) -> Result<impl Stream<Item = Result<StatusResponse>> + '_>
Create a model.
Source§impl ModelClient
impl ModelClient
Sourcepub async fn chat_completions(
&self,
request: ChatCompletionsRequest,
) -> Result<ChatCompletionsResponse>
pub async fn chat_completions( &self, request: ChatCompletionsRequest, ) -> Result<ChatCompletionsResponse>
Create chat completions using OpenAI-compatible API
This endpoint is compatible with OpenAI client libraries.
Use base URL http://localhost:11434/v1/ with any API key.
§Example
use oai_sdk::{ModelClient, ChatCompletionsRequest, ChatMessage};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = ModelClient::builder()
.base_url("http://localhost:11434")
.build()?;
let request = ChatCompletionsRequest {
model: "llama3.1:8b".to_string(),
messages: vec![ChatMessage::user("Why is the sky blue?")],
stream: Some(false),
..Default::default()
};
let response = client.chat_completions(request).await?;
println!("{}", response.choices[0].message.content);
Ok(())
}Sourcepub async fn openai_embeddings(
&self,
request: OpenAIEmbeddingsRequest,
) -> Result<OpenAIEmbeddingsResponse>
pub async fn openai_embeddings( &self, request: OpenAIEmbeddingsRequest, ) -> Result<OpenAIEmbeddingsResponse>
Generate embeddings using OpenAI-compatible API
This endpoint is compatible with OpenAI client libraries.
Use base URL http://localhost:11434/v1/ with any API key.
§Example
use oai_sdk::{ModelClient, OpenAIEmbeddingsRequest, OpenAIEmbeddingsInput};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = ModelClient::builder()
.base_url("http://localhost:11434")
.build()?;
let request = OpenAIEmbeddingsRequest {
model: "llama3.1:8b".to_string(),
input: OpenAIEmbeddingsInput::Single("Why is the sky blue?".to_string()),
encoding_format: Some("float".to_string()),
..Default::default()
};
let response = client.openai_embeddings(request).await?;
println!("Embeddings: {:?}", response.data[0].embedding);
Ok(())
}Sourcepub async fn responses(
&self,
request: ResponsesRequest,
) -> Result<ResponsesResponse>
pub async fn responses( &self, request: ResponsesRequest, ) -> Result<ResponsesResponse>
Generate responses using OpenAI-compatible API
This endpoint is compatible with OpenAI client libraries.
Use base URL http://localhost:11434/v1/ with any API key.
§Example
use oai_sdk::{ModelClient, ResponsesRequest};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = ModelClient::builder()
.base_url("http://localhost:11434")
.build()?;
let request = ResponsesRequest {
model: "llama3.1:8b".to_string(),
input: Some("Why is the sky blue?".to_string()),
stream: Some(false),
..Default::default()
};
let response = client.responses(request).await?;
println!("{}", response.output);
Ok(())
}Trait Implementations§
Source§impl Clone for ModelClient
impl Clone for ModelClient
Source§fn clone(&self) -> ModelClient
fn clone(&self) -> ModelClient
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more