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
- 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
Installation
Add ollama-rs to your Cargo.toml
[]
= "0.3.1"
If you absolutely want the latest version, you can use the master
branch by adding the following to your Cargo.toml
file:
[]
= { = "https://github.com/pepperoni21/ollama-rs.git", = "master" }
Note that the master
branch may not be stable and may contain breaking changes.
Initialization
Initialize Ollama
use Ollama;
// By default, it will connect to localhost:11434
let ollama = default;
// For custom values:
let ollama = new;
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 GenerationRequest;
let model = "llama2:latest".to_string;
let prompt = "Why is the sky blue?".to_string;
let res = ollama.generate.await;
if let Ok = res
OUTPUTS: The sky appears blue because of a phenomenon called Rayleigh scattering...
Completion Generation (Streaming)
Requires the stream
feature.
use GenerationRequest;
use ;
use StreamExt;
let model = "llama2:latest".to_string;
let prompt = "Why is the sky blue?".to_string;
let mut stream = ollama.generate_stream.await.unwrap;
let mut stdout = stdout;
while let Some = stream.next.await
Same output as above but streamed.
Completion Generation (With Options)
use GenerationRequest;
use ModelOptions;
let model = "llama2:latest".to_string;
let prompt = "Why is the sky blue?".to_string;
let options = default
.temperature
.repeat_penalty
.top_k
.top_p;
let res = ollama.generate.await;
if let Ok = res
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 ;
use ChatHistory;
let model = "llama2:latest".to_string;
let prompt = "Why is the sky blue?".to_string;
// `Vec<ChatMessage>` 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
.await;
if let Ok = res
Check chat with history examples for default and stream
List Local Models
let res = ollama.list_local_models.await.unwrap;
Returns a vector of LocalModel
structs.
Show Model Information
let res = ollama.show_model_info.await.unwrap;
Returns a ModelInfo
struct.
Create a Model
use CreateModelRequest;
let res = ollama.create_model.await.unwrap;
Returns a CreateModelStatus
struct representing the final status of the model creation.
Create a Model (Streaming)
Requires the stream
feature.
use CreateModelRequest;
use StreamExt;
let mut res = ollama.create_model_stream.await.unwrap;
while let Some = res.next.await
Returns a CreateModelStatusStream
that will stream every status update of the model creation.
Copy a Model
let _ = ollama.copy_model.await.unwrap;
Delete a Model
let _ = ollama.delete_model.await.unwrap;
Generate Embeddings
use GenerateEmbeddingsRequest;
let request = new;
let res = ollama.generate_embeddings.await.unwrap;
Generate Embeddings (Batch)
use GenerateEmbeddingsRequest;
let request = new;
let res = ollama.generate_embeddings.await.unwrap;
Returns a GenerateEmbeddingsResponse
struct containing the embeddings (a vector of floats).
Make a Function Call
use Coordinator;
use ;
use ;
use ModelOptions;
let mut history = vec!;
let mut coordinator = new
.options
.add_tool
.add_tool
.add_tool;
let resp = coordinator
.chat
.await.unwrap;
println!;
Uses the given tools (such as searching the web) to find an answer, feeds that answer back into the LLM, and returns a ChatMessageResponse
with the answer to the question.
Create a custom tool
The function
macro simplifies the creation of custom tools. Below is an example of a tool that retrieves the current weather for a specified city:
/// Retrieve the weather for a specified city.
///
/// * city - The city for which to get the weather.
async
To create a custom tool, define a function that returns a Result<String, Box<dyn std::error::Error + Sync + Send>>
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.
For a more detailed example, see the function call example.