Crate ollama_td

Crate ollama_td 

Source
Expand description

ollama_td : ollama tool download crate , which is a crate used exclusively to download the ollama command line tool or the binary itself ,

this is Not a crate to download the models, but to download the tool that is used to manage and download the models !!!

you can automate the download of the compressed (e.g. zip or tgz) CLI tool and

automatically unpack and place it where ever you want .

§Breaking Changes

Ollama tool v5.8 and beyond changed ollama-darwin to ollama-darwin.tgz.

download_customize(o_download, f_stream) now accepts function to give you the ability to customize the process of downloading the tool.

otherwise , you can use the default implementation provided by the crate which is download(o_download).

§Examples

Different platforms have different several options available .

§Windows

you have three options :

  • ollama-windows-amd64.zip
  • ollama-windows-arm64.zip
  • OllamaSetup.exe
use ollama_td::*;
use reqwest::Response;
use std::fs::File;
use std::io::{Write, stdout};
use std::path::{Path, PathBuf};

#[tokio::main]
async fn main() -> Result<()> {
    let d_location = Path::new(".");

    let down_x86 = o_d_x86(d_location).await?;
    let down_arm = o_d_arm(d_location).await?;
    let down_exe = o_d_exe(d_location).await?;

    assert_eq!(down_x86.to_str().unwrap(), "./ollama-windows-amd64.zip");
    assert_eq!(down_arm.to_str().unwrap(), "./ollama-windows-arm64.zip");
    assert_eq!(down_exe.to_str().unwrap(), "./OllamaSetup.exe");

    Ok(())
}

// general function function to be used among defferent other function examples !!
async fn download_ollama(
    d_location: &Path,
    platform: Platform,
    tag_version: TVersion,
) -> Result<PathBuf> {
    let o_download = OllamaDownload::builder()?
        .platform(platform)
        .d_location(d_location)
        .tag_version(tag_version)
        .build()?;

    download_customize(o_download, download_custom_helper).await
}

// downloads [ollama-windows-amd64.zip]
async fn o_d_x86(d_location: &Path) -> Result<PathBuf> {
    let platform = Platform::Windows(Windows::X86);
    download_ollama(d_location, platform, TVersion::Latest).await
}
// downloads [ollama-windows-arm64.zip]
async fn o_d_arm(d_location: &Path) -> Result<PathBuf> {
    let platform = Platform::Windows(Windows::Arm);
    download_ollama(d_location, platform, TVersion::Tag("v0.5.7".to_string())).await
}

// downloads [OllamaSetup.exe]
async fn o_d_exe(d_location: &Path) -> Result<PathBuf> {
    let platform = Platform::Windows(Windows::BinExe);
    download_ollama(d_location, platform, TVersion::Tag("v0.5.7".to_string())).await
}

// is used with download_custom function , here we stream to the disk storage and to the stdout!!
async fn download_custom_helper(mut res: Response, full_path: PathBuf) -> Result<PathBuf> {
    let content_length = res.content_length().unwrap_or(1) as f64;
    let mut file = File::create(&full_path)?;
    let mut recived = 0.0;
    while let Some(c) = res.chunk().await? {
        recived += c.len() as f64;
        print!("\r{:.2}", (recived / content_length) * 100.0);
        file.write_all(&c)?;
        stdout().flush()?;
    }
    file.flush()?;
    Ok(full_path.to_path_buf())
}

§Unix

you have two options :

  • ollama-darwin.tgz
  • Ollama-darwin.zip
use ollama_td::*;
use std::path::{Path, PathBuf};

#[tokio::main]
async fn main() -> Result<()> {
   let d_location = Path::new(".");

   let o_d_bin = o_d_bin(d_location).await?;
   let o_d_zip = o_d_zip(d_location).await?;

   assert_eq!(o_d_bin.to_str().unwrap(), "./ollama-darwin.tgz");
   assert_eq!(o_d_zip.to_str().unwrap(), "./Ollama-darwin.zip");
   Ok(())
}

// this example attempts to download the ```ollama-darwin``` binary version !!!
async fn o_d_bin(d_location: &Path) -> Result<PathBuf> {
   let u_bin = Platform::Unix(Unix::DarwinBin);

   let o_bin = OllamaDownload::builder()?
       .platform(u_bin)
       .tag_version(TVersion::Latest) //you can always set it to the latest version!!
       .d_location(d_location)
       .build()?;
   download(o_bin).await
}

// this example attempts to download the ```Ollama-darwin.zip``` CLI compressed version !!!
async fn o_d_zip(d_location: &Path) -> Result<PathBuf> {
   let u_zip = Platform::Unix(Unix::DarwinZip);

   let o_zip = OllamaDownload::builder()?
       .platform(u_zip)
       .tag_version(TVersion::Tag("v0.5.7".to_owned())) // you can specify the tag version!!
       .d_location(d_location)
       .build()?;
   download(o_zip).await
}

§Linux

you have five options :

  • ollama-linux-amd64.tgz
  • ollama-linux-amd64-rocm.tgz
  • ollama-linux-arm64.tgz
  • ollama-linux-arm64-jetpack5.tgz
  • ollama-linux-arm64-jetpack6.tgz
use ollama_td::*;
use std::path::{Path, PathBuf};

#[tokio::main]
async fn main() -> Result<()> {
   let d_location = Path::new(".");

   let o_d_x86 = o_d_x86(d_location).await?;
   let o_d_x86_rocm = o_d_x86_rocm(d_location).await?;
   let o_d_arm = o_d_arm(d_location).await?;
   let o_d_arm_jet5 = o_d_arm_jet5(d_location).await?;
   let o_d_arm_jet6 = o_d_arm_jet6(d_location).await?;

   assert_eq!(o_d_x86.to_str().unwrap(), "./ollama-linux-amd64.tgz");

   assert_eq!(
       o_d_x86_rocm.to_str().unwrap(),
       "./ollama-linux-amd64-rocm.tgz"
   );

   assert_eq!(o_d_arm.to_str().unwrap(), "./ollama-linux-arm64.tgz");

   assert_eq!(
       o_d_arm_jet5.to_str().unwrap(),
       "./ollama-linux-arm64-jetpack5.tgz"
   );

   assert_eq!(
       o_d_arm_jet6.to_str().unwrap(),
       "./ollama-linux-arm64-jetpack6.tgz"
   );

   Ok(())
}

// general function to be reused among other functions!!
async fn download_ollama(d_location: &Path, platform: Platform) -> Result<PathBuf> {
   let o_download = OllamaDownload::builder()?
       .platform(platform)
       .tag_version(TVersion::Latest)
       .d_location(d_location)
       .build()?;

   download(o_download).await
}

// downloads [ollama-linux-amd64.tgz]
async fn o_d_x86(d_location: &Path) -> Result<PathBuf> {
   let platform = Platform::Linux(Linux::X86 { rocm: false });
   download_ollama(d_location, platform).await
}

// downloads [ollama-linux-amd64-rocm.tgz]
async fn o_d_x86_rocm(d_location: &Path) -> Result<PathBuf> {
   let platform = Platform::Linux(Linux::X86 { rocm: true });
   download_ollama(d_location, platform).await
}

// downloads [ollama-linux-arm64.tgz]
async fn o_d_arm(d_location: &Path) -> Result<PathBuf> {
   let platform = Platform::Linux(Linux::Arm(LinuxArm::Arm));
   download_ollama(d_location, platform).await
}
// downloads [ollama-linux-arm64-jetpack5.tgz]
async fn o_d_arm_jet5(d_location: &Path) -> Result<PathBuf> {
   let platform = Platform::Linux(Linux::Arm(LinuxArm::Jetpack5));
   download_ollama(d_location, platform).await
}
// downloads [ollama-linux-arm64-jetpack6.tgz]
async fn o_d_arm_jet6(d_location: &Path) -> Result<PathBuf> {
   let platform = Platform::Linux(Linux::Arm(LinuxArm::Jetpack6));
   download_ollama(d_location, platform).await
}

Structs§

OllamaDownload
Accepts the platform (Operating system) and the download location.
OllamaDownloadBuilder
used to build the OllamaDownload struct with nice minimal functionality to set the fields!!!

Enums§

Linux
The linux platform , either the tool you request is on Arm or X86 architecture.
LinuxArm
Platform
this is used as a unified handler to be stored in the OllamaDownload struct as a single field !!!
TVersion
provide the tag number or choose to set it to Latest which will choose that latest up-to-data available Asset!!!
Unix
Either the binary version or the zipped one!!!
Windows
three architecture options for the Windows platform {X86,Arm ,BinExe} , the later one is the excutable binary for windows!!!

Functions§

download
Accepts OllamaDownload object and after successfully downloading your tool it will return the path to where your tool is stored!!!
download_customize
used to give you the freedom to customize the download process , for example if you want to plug a GUI progress bar that views the download progress.

Type Aliases§

Result
Result<T, Error>