Baike.dev
All toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
O

ollama-rs

> 编程语言
Open source

A simple and easy-to-use library for interacting with the Ollama API.

1.1K stars0 likes0 views
WebsiteGitHub

About

A simple and easy-to-use library for interacting with the Ollama API.

Ollama-rs

A simple and easy-to-use library for interacting with the Ollama API.

This library was created following the Ollama API documentation.

Table of Contents

  • Installation
  • Initialization
    • Using with llmman
  • Usage
    • Completion Generation
    • Completion Generation (Streaming)
    • Completion Generation (With Options)
    • Chat Mode
    • List Local Models
    • Show Model Information
    • Create a Model
    • Create a Model (Streaming)
    • Copy a Model
    • Delete a Model
    • Generate Embeddings
    • Generate Embeddings (Batch)
    • Make a Function Call
    • Create a custom tool
    • Completion Generation (With Thinking)

Installation

Add ollama-rs to your Cargo.toml

[dependencies]
ollama-rs = "0.3.6"

If you absolutely want the latest version, you can use the master branch by adding the following to your Cargo.toml file:

[dependencies]
ollama-rs = { git = "https://github.com/pepperoni21/ollama-rs.git", branch = "master" }

Note that the master branch may not be stable and may contain breaking changes.

Initialization

Initialize Ollama

use ollama_rs::Ollama;

// By default, it will connect to localhost:11434
let ollama = Ollama::default();

// For custom values:
let ollama = Ollama::new("http://localhost".to_string(), 11434);

Using with llmman

llmman is a local model runner that serves the Ollama API on port 17434, so ollama-rs works with it unchanged; just point the client at that port:

use ollama_rs::Ollama;

// llmman listens on 127.0.0.1:17434 by default
let ollama = Ollama::builder()
    .host("http://localhost")
    .port(17434)
    .build();

Start the server with llmman serve and pull a model with llmman pull gemma4 (or llmman pull hf.co/unsloth/Qwen3.5-0.8B-GGUF to pull from Hugging Face).

Usage

Feel free to check the Chatbot example that shows how to use the library to create a simple chatbot in less than 50 lines of code. You can also check some other examples.

These examples use poor error handling for simplicity, but you should handle errors properly in your code.

Completion Generation

use ollama_rs::generation::completion::GenerationRequest;

let model = "llama2:latest".to_string();
let prompt = "Why is the sky blue?".to_string();

let res = ollama.generate(GenerationRequest::new(model, prompt)).await;

if let Ok(res) = res {
    println!("{}", res.response);
}

OUTPUTS: The sky appears blue because of a phenomenon called Rayleigh scattering...

Completion Generation (Streaming)

Requires the stream feature.

use ollama_rs::generation::completion::GenerationRequest;
use tokio::io::{self, AsyncWriteExt};
use tokio_stream::StreamExt;

let model = "llama2:latest".to_string();
let prompt = "Why is the sky blue?".to_string();

let mut stream = ollama.generate_stream(GenerationRequest::new(model, prompt)).await.unwrap();

let mut stdout = io::stdout();
while let Some(res) = stream.next().await {
    let responses = res.unwrap();
    for resp in responses {
        stdout.write_all(resp.response.as_bytes()).await.unwrap();
        stdout.flush().await.unwrap();
    }
}

Same output as above but streamed.

Completion Generation (With Options)

use ollama_rs::generation::completion::GenerationRequest;
use ollama_rs::models::ModelOptions;

let model = "llama2:latest".to_string();
let prompt = "Why is the sky blue?".to_string();

let options = ModelOptions::default()
    .temperature(0.2)
    .repeat_penalty(1.5)
    .top_k(25)
    .top_p(0.25);

let res = ollama.generate(GenerationRequest::new(model, prompt).options(options)).await;

if let Ok(res) = res {
    println!("{}", res.response);
}

OUTPUTS: 1. Sun emits white sunlight: The sun consists primarily ...

Chat Mode

Every message sent and received will be stored in the library's history.

Example with history:

use ollama_rs::generation::chat::{request::ChatMessageRequest, ChatMessage};
use ollama_rs::history::ChatHistory;

let model = "llama2:latest".to_string();
let prompt = "Why is the sky blue?".to_string();
// `Vec` implements `ChatHistory`,
// but you could also implement it yourself on a custom type
let mut history = vec![];

let res = ollama
    .send_chat_messages_with_history(
        &mut history, //  Result> {
    let url = format!("https://wttr.in/{city}?format=%C+%t");
    let response = reqwest::get(&url).await?.text().await?;
    Ok(response)
}

To create a custom tool, define a function that returns a Result> and annotate it with the function macro. This function will be automatically converted into a tool that can be used with the Coordinator, just like any other tool.

Ensure that the doc comment above the function clearly describes the tool's purpose and its parameters. This information will be provided to the LLM to help it understand how to use the tool.

When using streaming chat directly, you may also attach tool schemas to the request:

use ollama_rs::generation::chat::request::ChatMessageRequest;

let request = ChatMessageRequest::new("lfm2.5:8b".to_owned(), Vec::new()).add_tool(get_weather);
let mut stream = ollama.send_chat_messages_stream(request).await.unwrap();

send_chat_messages_stream yields streamed ChatMessageResponse chunks. If a chunk contains message.tool_calls, run the requested tools and include their results in a follow-up request.

For a more detailed example, see the function call example.

Completion Generation (With Thinking)

let model = "qwen3:latest".to_string();
let prompt = "Why is the sky blue?".to_string();

let res = ollama.generate(GenerationRequest::new(model, prompt).think(true)).await;

if let Ok(res) = res {
    println!("{}", res.response);
}

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rust

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言