Running local LLMs on your NPU from R with Foundry Local and ellmer

Wait 5 sec.

[This article was first published on Data Analytics and AI Archives - Giles, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.TLDR: this post summarizes how I was able to leverage my Surface Pro 11’s Neural Processing Unit (NPU) to chat with Large Language Models (LLMs) from R (for some reason). The code has been adapted from this guide from Microsoft. Success!While I’ve been obsessively utilizing LLMs since the hype train first set off, in the last 12 months I’ve been experimenting with local LLMs so I can better utilize them in my work. For the most part, I’ve been using my laptop’s RTX 4060 for this, but I’ve long been curious what the integrated NPU in my Surface Pro 11 can (or can’t) do. Unfortunately, at the time of writing, neither Ollama or LM Studio1 natively support this functionality which has made satisfying my curiosity trickier than I’d like.At the outset, I’m still not entirely sure this is a question worth answering (or a blog worth writing) given the RTX 4060’s performance thoroughly trumps that of my NPU, but I’m a tinkerer and the idea of being stranded without a stochastic parrot on hand is nothing short of terrifying.Foundry LocalBefore starting, you’ll need to install Foundry Local which can be installed using the Bash command below:npm install foundry-local-sdk-winml openaiYou’ll also need to make sure that Foundry is available on PATH, so your OS knows how to launch it (see here). The R CodeMicrosoft provides a getting started guide here, which includes a set of Python code that this was based on (with Claude’s help):# Load necessary packages ---------------------------------------------------library(ellmer) # LLM chat interface (chat_openai_compatible)library(httr2) # resolve the exact loaded model id from /v1/models# Set project assumptions and define functions ------------------------------ref_model_alias