--- title: "Hugging Face Models" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{hf-gguf} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- If you are looking for a model to use, you should probably search for it on the [Ollama website](https://ollama.com/search). However, the models listed there are not all models that can be used in (r)ollama. Models in the GGUF format from *Hugging Face Hub*, a very popular platform for sharing machine learning models. To look for a specific model, all you need to do is visit (this is already filtered to GGUF models, other model formats are not compatible with Ollama). Once you have identified a model, you can simply pass the URL to the `pull_model` function: ``` r library(rollama) pull_model("https://huggingface.co/oxyapi/oxy-1-small-GGUF:Q2_K") #> ✔ model https://huggingface.co/oxyapi/oxy-1-small-GGUF:Q2_K pulled succesfully ``` Note that the `:Q2_K` at the end is the [quantization scheme](https://huggingface.co/docs/optimum/en/concept_guides/quantization). Q2_K is the smallest available version of the model, which gives up some performance, but is faster to run. You can find the different quantization versions when clicking the `Use this model` on a model site. When downloading, Ollama converts the URL automatically into a name, we need to query our model list first to see how the model is named now: ``` r grep("oxy-1-small", list_models()$name, value = TRUE) #> [1] "huggingface.co/oxyapi/oxy-1-small-GGUF:Q2_K" ``` But except for the awkward name, we can now use this model as any other one: ``` r chat("Why is the sky blue?", model = "huggingface.co/oxyapi/oxy-1-small-GGUF:Q2_K") #> #> ── Answer from huggingface.co/oxyapi/oxy-1-small-GGUF:Q2_K ─────────── #> The sky appears blue to our eyes due to a process called Rayleigh #> scattering. Sunlight entering the Earth’s atmosphere collides with #> the air molecules, causing them to scatter in all directions. #> Nitrogen and oxygen make up about 95% of the earth's atmosphere. #> #> When sunlight encounters these gases, it gets scattered widely #> throughout the sky. Shorter wavelengths of visible light fall within #> blue’s wavelength range; thus, when sunlight scatters into our eyes #> as blue light, we perceive a blue sky. #> #> However, at sunrise and sunset hours, the sun is lower on the horizon #> compared to noon time. As such the path of sunlight passes through #> more atmosphere than usual, which causes the longer wavelengths (red) #> to dominate over shorter ones, giving us the shades of red hues like #> pink or crimson skies. ``` Note that this also works with [text embedding models](https://jbgruber.github.io/rollama/articles/text-embedding.html). Hugging Face Hub has some nice filters with which you can pre-select appropriate models and then use full text search to find more. This search looks for embedding models with the correct model type, for example: The trending models are often quite good for general tasks, but more information is available in leaderboards and blog posts. For no particular reason, let's use Snowflake's Arctic-embed-m-v1.5 embed for demonstration purposes here: ``` r pull_model("https://huggingface.co/Snowflake/snowflake-arctic-embed-m-v1.5:BF16") #> ✔ model https://huggingface.co/Snowflake/snowflake-arctic-embed-m-v1.5:BF16 pulled succesfully embed_text(c("Why is the sky blue?", "I am pretty happy we can work with GGUF models in R"), model = "huggingface.co/Snowflake/snowflake-arctic-embed-m-v1.5:BF16") #> # A tibble: 2 × 768 #> dim_1 dim_2 dim_3 dim_4 dim_5 dim_6 dim_7 dim_8 dim_9 dim_10 #> #> 1 0.221 0.128 0.698 0.0980 0.891 -0.145 0.168 0.722 -0.308 -0.674 #> 2 0.239 -0.332 0.699 -0.125 0.856 0.345 -0.0625 0.361 -0.309 -0.657 #> # ℹ 758 more variables: dim_11 , dim_12 , dim_13 , #> # dim_14 , dim_15 , dim_16 , dim_17 , #> # dim_18 , dim_19 , dim_20 , dim_21 , #> # dim_22 , dim_23 , dim_24 , dim_25 , #> # dim_26 , dim_27 , dim_28 , dim_29 , #> # dim_30 , dim_31 , dim_32 , dim_33 , #> # dim_34 , dim_35 , dim_36 , dim_37 , … ```