Llama.cpp for FULL LOCAL Semantic Router

James Briggs · Intermediate ·🧠 Large Language Models ·2y ago

Key Takeaways

This video demonstrates how to use llama.cpp LLM and HuggingFace embedding models for a full local semantic router

Full Transcript

today we're going to take a look at the new features in semantic router that allow us to take everything fully local so we'll be using llama CPP for the dynamic routes and the hugging face encoder for our routing decisions now one thing I really like about this is that using a very small model so we're going to be using mistal 7B we add grammars onto that and using this format we seem to be getting much better results for a gentic decision making than I can get with GPT 3.5 now all of this I'm going to be running from my M1 MacBook Pro so it's not like I have anything crazy here and it it will run pretty quick as we'll see so let's jump straight into it now I'm starting off in the semantic router Library coming over to here and I'm going to download that onto my MC once that has been downloaded we should be able to open it and we'll see this and what I'm going to do is just pip install semantic router so I'm just switching across my terminal here if you if you have the local git repo you can install the most recent version like this but I'm going to go ahead and install it from pipy so semantic router and we want 0.0.6 now coming down to here if you are on Mac you want to use this so that's just to speed things up I'm going to use the Myst 7B instruct model it's quantized so you know we can actually run this pretty easily it's not you don't need much to run this and it runs surprising ly quickly while we are waiting for that to download I'm going to come over to here and I'll just point out the pr where we got this implemented so this one from Bugan from aelio super cool and there's one thing in particular I wanted to point out which is that we use these llm grammars here now the LM grammars they are essentially enforcing a particular structured output from your llm which is a big part of why we can get very good performance from a very small model like mol 7B and it is surprisingly good I'm actually seeing better performance with this and M 7B than I am gbt 3.5 which is I think pretty insane now that has been downloaded so the the my model now we'll come down to initial izing our Dynamic route and you might recognize this from the previous example where we demoed a dynamic route I'm using the exact same one here but we're just going to swap out the openingi encoder and the open LM for a hooking face encoder and the mystal 7B LM exact same definitions here so this is our Dynamic route so the get time route and we also have the static routes here as well so they are also in there I'm going to take all of those routes and can drop that time SCH it there and we just put all of our routes in a list here and we're going to use them soon to initialize our route layer but to initialize our rout layer we do need an encoder so we go ahead and we initialize that we're using the Hing face encoder here which by default is the sentence Transformers or Min LM L6 V2 which is a tiny tiny model so you can also run this on pretty much anything as well now we want to come over to here to begin initializing our mystal 7B model there's a little bit of explanation on what we're actually using here we are going to simplify the way that you initialize a llama CPP model but for now this is how you do it and we will still have this option so the idea is we'll probably make it so that if you don't pass in this llm parameter we will use default parameters I when initializing it but for those of you that you know do want to modify your parameters you will be able to so let's run this I'm going to run it on GPU okay so I have that here and then we can initialize our route layer okay so we have our encoder so the H face encoder our routes that we defined before so two static one Dynamic and we have mro 7B okay cool looks good and now let's ask how's the weather today we see that we hit our static route the chat route now let's ask what is the time in New York right now okay and you can see the the grammar coming through here I'm not I'm not actually sure how to stop those from being logged as I'm sure there must be way but we'll figure that out in a future release we have the time and here in the UK it is 1603 so that is correct what is time in Rome right now so I think they're an hour ahead 1703 is correct then I want to try something a little further out the way this so it's I think this is the question where GPT 3.5 actually struggled with quite a lot which is surprising I would kind of expected to be okay with this but it really struggled so what is the time in Bangkok right now I'm going to run this we get 2304 I don't know what the time is in Bangkok right now 2304 so that is correct and then the time in puket as well so I want to you know somewhere that's not a main city because you look at the time zone here and it has Bangkok in the time zone name someone to try okay pck it and then I'm actually not sure why but this this command here or or this question takes way longer to answer than the others and I I yeah I'm not I'm not 100% sure why that is which is kind of interesting but anyway so we're going to be waiting a little moment for this one but I will say that this question that just G gbt 3.5 answering it I I didn't test this exact question but if it couldn't answer bangcock I feel like it would not have been able to answer for bat cool so we come down here we see Asia Bangkok for the time zone and yeah we get the the same time there now let me just double check that they are in the same time I'm pretty sure they are okay yeah cool so it's all good and then we just downloaded the mro model so if you do want to remove that from your computer you can you just run this command the bottom here and yeah that is everything so we have got a fully local semantic router running now it works with LL CPP and it uses llm grammar to just make the performance of small models pretty good as you can see then alongside that we also have the new hugging face encoders which means that any embedding model that is supported by hunging face we most likely support it unless it does some sort of weird pooling mechanism which most of them don't most of them are pretty straightforward so we yeah we can now use semantic router with a ton more models and we can do all that locally which is pretty exciting so that's it for this video I just wanted to show you this very quickly so I will leave it there I hope this has all been interesting and useful but for now thank you very much for watching and I will see you again in the next one bye

Original Description

Using fully local semantic router for agentic AI with llama.cpp LLM and HuggingFace embedding models. There are many reasons we might decide to use local LLMs rather than use a third-party service like OpenAI. It could be cost, privacy, compliance, or fear of the OpenAI apocalypse. To help you out, we made Semantic Router fully local with local LLMs available via llama.cpp like Mistral 7B. Using llama.cpp also enables the use of quantized GGUF models, reducing the memory footprint of deployed models and allowing even 13-billion parameter models to run with hardware acceleration on an Apple M1 Pro chip. We also use LLM grammars to enable high output reliability even from the smallest of models. In this video, we'll use HuggingFace's MiniLM encoder, and llama.cpp's Mistral-7B-instruct GGUF quantized. ⭐ GitHub Repo: https://github.com/aurelio-labs/semantic-router/ 📌 Code: https://github.com/aurelio-labs/semantic-router/blob/main/docs/05-local-execution.ipynb 🔥 Semantic Router Course: https://www.aurelio.ai/course/semantic-router 👋🏼 AI Consulting: https://aurelio.ai 👾 Discord: https://discord.gg/c5QtDB9RAP Twitter: https://twitter.com/jamescalam LinkedIn: https://www.linkedin.com/in/jamescalam/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from James Briggs · James Briggs · 0 of 60

← Previous Next →
1 Stoic Philosophy Text Generation with TensorFlow
Stoic Philosophy Text Generation with TensorFlow
James Briggs
2 How to Build TensorFlow Pipelines with tf.data.Dataset
How to Build TensorFlow Pipelines with tf.data.Dataset
James Briggs
3 Every New Feature in Python 3.10.0a2
Every New Feature in Python 3.10.0a2
James Briggs
4 How-to Build a Transformer for Language Classification in TensorFlow
How-to Build a Transformer for Language Classification in TensorFlow
James Briggs
5 How-to use the Kaggle API in Python
How-to use the Kaggle API in Python
James Briggs
6 Language Generation with OpenAI's GPT-2 in Python
Language Generation with OpenAI's GPT-2 in Python
James Briggs
7 Text Summarization with Google AI's T5 in Python
Text Summarization with Google AI's T5 in Python
James Briggs
8 How-to do Sentiment Analysis with Flair in Python
How-to do Sentiment Analysis with Flair in Python
James Briggs
9 Python Environment Setup for Machine Learning
Python Environment Setup for Machine Learning
James Briggs
10 Sequential Model - TensorFlow Essentials #1
Sequential Model - TensorFlow Essentials #1
James Briggs
11 Functional API - TensorFlow Essentials #2
Functional API - TensorFlow Essentials #2
James Briggs
12 Training Parameters - TensorFlow Essentials #3
Training Parameters - TensorFlow Essentials #3
James Briggs
13 Input Data Pipelines - TensorFlow Essentials #4
Input Data Pipelines - TensorFlow Essentials #4
James Briggs
14 6 of Python's Newest and Best Features (3.7-3.9)
6 of Python's Newest and Best Features (3.7-3.9)
James Briggs
15 Novice to Advanced RegEx in Less-than 30 Minutes + Python
Novice to Advanced RegEx in Less-than 30 Minutes + Python
James Briggs
16 Building a PlotLy $GME Chart in Python
Building a PlotLy $GME Chart in Python
James Briggs
17 How-to Use The Reddit API in Python
How-to Use The Reddit API in Python
James Briggs
18 How to Build Custom Q&A Transformer Models in Python
How to Build Custom Q&A Transformer Models in Python
James Briggs
19 How to Build Q&A Models in Python (Transformers)
How to Build Q&A Models in Python (Transformers)
James Briggs
20 How-to Decode Outputs From NLP Models (Python)
How-to Decode Outputs From NLP Models (Python)
James Briggs
21 Identify Stocks on Reddit with SpaCy (NER in Python)
Identify Stocks on Reddit with SpaCy (NER in Python)
James Briggs
22 Sentiment Analysis on ANY Length of Text With Transformers (Python)
Sentiment Analysis on ANY Length of Text With Transformers (Python)
James Briggs
23 Unicode Normalization for NLP in Python
Unicode Normalization for NLP in Python
James Briggs
24 The NEW Match-Case Statement in Python 3.10
The NEW Match-Case Statement in Python 3.10
James Briggs
25 Multi-Class Language Classification With BERT in TensorFlow
Multi-Class Language Classification With BERT in TensorFlow
James Briggs
26 How to Build Python Packages for Pip
How to Build Python Packages for Pip
James Briggs
27 How-to Structure a Q&A ML App
How-to Structure a Q&A ML App
James Briggs
28 How to Index Q&A Data With Haystack and Elasticsearch
How to Index Q&A Data With Haystack and Elasticsearch
James Briggs
29 Q&A Document Retrieval With DPR
Q&A Document Retrieval With DPR
James Briggs
30 How to Use Type Annotations in Python
How to Use Type Annotations in Python
James Briggs
31 Extractive Q&A With Haystack and FastAPI in Python
Extractive Q&A With Haystack and FastAPI in Python
James Briggs
32 Sentence Similarity With Sentence-Transformers in Python
Sentence Similarity With Sentence-Transformers in Python
James Briggs
33 Sentence Similarity With Transformers and PyTorch (Python)
Sentence Similarity With Transformers and PyTorch (Python)
James Briggs
34 NER With Transformers and spaCy (Python)
NER With Transformers and spaCy (Python)
James Briggs
35 Training BERT #1 - Masked-Language Modeling (MLM)
Training BERT #1 - Masked-Language Modeling (MLM)
James Briggs
36 Training BERT #2 - Train With Masked-Language Modeling (MLM)
Training BERT #2 - Train With Masked-Language Modeling (MLM)
James Briggs
37 Training BERT #3 - Next Sentence Prediction (NSP)
Training BERT #3 - Next Sentence Prediction (NSP)
James Briggs
38 Training BERT #4 - Train With Next Sentence Prediction (NSP)
Training BERT #4 - Train With Next Sentence Prediction (NSP)
James Briggs
39 FREE 11 Hour NLP Transformers Course (Next 3 Days Only)
FREE 11 Hour NLP Transformers Course (Next 3 Days Only)
James Briggs
40 New Features in Python 3.10
New Features in Python 3.10
James Briggs
41 Training BERT #5 - Training With BertForPretraining
Training BERT #5 - Training With BertForPretraining
James Briggs
42 How-to Use HuggingFace's Datasets - Transformers From Scratch #1
How-to Use HuggingFace's Datasets - Transformers From Scratch #1
James Briggs
43 Build a Custom Transformer Tokenizer - Transformers From Scratch #2
Build a Custom Transformer Tokenizer - Transformers From Scratch #2
James Briggs
44 3 Traditional Methods for Similarity Search (Jaccard, w-shingling, Levenshtein)
3 Traditional Methods for Similarity Search (Jaccard, w-shingling, Levenshtein)
James Briggs
45 3 Vector-based Methods for Similarity Search (TF-IDF, BM25, SBERT)
3 Vector-based Methods for Similarity Search (TF-IDF, BM25, SBERT)
James Briggs
46 Building MLM Training Input Pipeline - Transformers From Scratch #3
Building MLM Training Input Pipeline - Transformers From Scratch #3
James Briggs
47 Training and Testing an Italian BERT - Transformers From Scratch #4
Training and Testing an Italian BERT - Transformers From Scratch #4
James Briggs
48 Faiss - Introduction to Similarity Search
Faiss - Introduction to Similarity Search
James Briggs
49 Angular App Setup With Material - Stoic Q&A #5
Angular App Setup With Material - Stoic Q&A #5
James Briggs
50 Why are there so many Tokenization methods in HF Transformers?
Why are there so many Tokenization methods in HF Transformers?
James Briggs
51 Choosing Indexes for Similarity Search (Faiss in Python)
Choosing Indexes for Similarity Search (Faiss in Python)
James Briggs
52 Locality Sensitive Hashing (LSH) for Search with Shingling + MinHashing (Python)
Locality Sensitive Hashing (LSH) for Search with Shingling + MinHashing (Python)
James Briggs
53 How LSH Random Projection works in search (+Python)
How LSH Random Projection works in search (+Python)
James Briggs
54 IndexLSH for Fast Similarity Search in Faiss
IndexLSH for Fast Similarity Search in Faiss
James Briggs
55 Faiss - Vector Compression with PQ and IVFPQ (in Python)
Faiss - Vector Compression with PQ and IVFPQ (in Python)
James Briggs
56 Product Quantization for Vector Similarity Search (+ Python)
Product Quantization for Vector Similarity Search (+ Python)
James Briggs
57 How to Build a Bert WordPiece Tokenizer in Python and HuggingFace
How to Build a Bert WordPiece Tokenizer in Python and HuggingFace
James Briggs
58 Metadata Filtering for Vector Search + Latest Filter Tech
Metadata Filtering for Vector Search + Latest Filter Tech
James Briggs
59 Build NLP Pipelines with HuggingFace Datasets
Build NLP Pipelines with HuggingFace Datasets
James Briggs
60 Composite Indexes and the Faiss Index Factory
Composite Indexes and the Faiss Index Factory
James Briggs

Related Reads

📰
GPT-5.5 Complete Guide in 2026
Learn about GPT-5.5, its features, performance, and why it matters in 2026, and how to leverage it for improved AI-assisted tasks
Dev.to AI
📰
AI Simplified — Why Structured Output Matters More Than a Fluent Answer
Learn why structured output is crucial for AI answers and how it can improve usability and automation
Dev.to AI
📰
I ran a 110B LLM on 16GB of RAM. Here's the equation that predicts any model's speed on your machine
Learn how to predict a large language model's speed on your machine using a simple equation, and discover how to run a 110B LLM on limited RAM
Dev.to · Federico Sciuca
📰
A Fidelity-First Workflow for Editing GPT-Generated Text
Learn a fidelity-first workflow for editing GPT-generated text to improve its quality and readability
Dev.to · Bisrat
Up next
5 Levels of AI Agents - From Simple LLM Calls to Multi-Agent Systems
Dave Ebbelaar (LLM Eng)
Watch →