AI Decision Making — Optimizing Routes

James Briggs · Advanced ·🚀 Entrepreneurship & Startups ·2y ago

Key Takeaways

The video demonstrates AI decision-making for route optimization using the semantic router library, with a focus on modifying thresholds and training methods to optimize route values. It utilizes tools such as the semantic router library, collab, kab, T4 GPU, E5 base V2 model, and Open AI embedding model to achieve route optimization with high accuracy.

Full Transcript

today we're going to be taking a look at how we can improve the accuracy of our routes within semantic router so what we've added to the library is the ability to modify your thresholds on a route by Route basis so previously it was a whole layer now you have individual thresholds per route and of course if you have many routes modifying each one of those thresholds one by one it would probably become quite tedious so we've also added in training methods so that you can optimize your route values with a few lines of code which is great so let's jump straight into it I'm going to go over to the semantic router Library I'm going to go to the dots and then I'm going to go down to this threshold optimization notebook and we'll open that in collab now we're going to be using the local models so just the local encoded in this example because we're using static routes only but that does mean that we can also speed up by using a GPU so in kab I'm going to switch across to a T4 GPU that will speed things up although is very fast anyway like literally a couple of seconds but nonetheless you know if you're doing this for many routes and you have a fairly large training set then you might want to turn GPU on okay so that has installed and we can come down to here where we're just defining our route layer we have a few routes in here to give us you know more to optimize with so they're pretty lightweight one thing that I would recommend okay you can optimize using just the method that we have here but really what you want to be doing as well is you want to be adding utterances that you see you know where it doesn't trigger the route you would expect it to you should be adding those to your routes and you should probably be adding just generally more utterances maybe breaking apart routes into different routes if you're seeing that they don't work so well and the other thing as you'll see in a moment is that we have a training data set and we can modify that to improve the performance as well we we'll get around to that in a moment so with our encoder we are going to initialize our encoder and I'm going to use a different one so the default encoded that uses mini LM so it's a pretty old model to be honest but it's very small and efficient so we just have it as the default but at some point that will probably change maybe to this model so this is kind like a more recent still very small but generally you can you should be able to get better performance with this model so yeah we're going to switch across to that so it's E5 base V2 model that we just download quickly okay now once that has downloaded we can come down and initialize our route layer so to initialize a route layer we just need our routes and our encoder both of which we have and there we go so we have our route layer now let's try with these queries so this one should go to politics this one to um chitchat this one to I think we had a biology question and this one should just be nonone okay and you can see actually we get we almost we get three of four okay this one actually goes to chitchat where it shouldn't so okay let's take that and let's try and improve uh what we have so first I'm just going to show you the evaluation or evaluate method so this is a format that we use for both the evaluate and fit methods so you see that we have these x x y y x refers to the utterances that are the input data for our fit method whereas these so these are the the labels like the intended routes that they should trigger okay and I I just like to keep it like this when I'm going through it so it it feels a bit easier than creating two separate lists so I create this test data set which is just a list of the two PS and then I unpack that so we have our utterances here our labels here and then we're going to evaluate to see what we get now if I run this we actually 75% okay so you know um not that great obviously we can we can improve that and you can see with the actually I think it's with mini LM we do actually get perfect accuracy but I think it's it's better to show with this now what we need to do is create a test data set so what we did here but bigger so that's what I do here when you're creating this one thing that you can do obviously is just use an llm to generate this for you so gp4 ask it to generate a set of these so we have politics chitchat mathematics biology and we have these as well so and we should probably add some more of those so these are the routes that shouldn't be classified as anything and we add those in there because we well if we just kind of have named it's always going to choose like the similarity thresholds can just increase or decrease sorry to capture more area and that means that you know it might work on this test data set but maybe not when we have new queries coming in so one thing that I would also recommend doing is okay we have mathematics politics chitchat let's add some more routes here or more test data that is kind of similar to those other ones but we don't actually want it to be classified as those other ones that will basically just make it harder for the model the training function to get a high accuracy and that's a good thing because we're kind of pushing the model more it needs to try a bit harder to get something something good so I'm going to I'm going to write for you very quickly okay so I've added these five here you can see okay kind of similar to biology these two are similar to The like mathematics routes or or tests that we have this one kind of similar to the chitchat one and this one obviously to politics but at least for me they don't quite fit into those so they're very similar just not quite there so that should be good enough I think to get some you know reasonable performance won't be anything incredible I don't think but we should get something now let's try and calculate the accuracy on the using the default thresholds that we have so you can see mini LM that was actually [Music] 34.8516 point6 there now let's come down and let's see what we have for the default routes so you can see they're all just 0.5 okay that that's coming from the hooking phase encoder it's just a default score threshold that we have set there now we have our our train data so the the X and we have our labels which is y so then we just train okay and by default it will go over 500 iterations of training or steps we can we can try that I'm not sure we need 500 but let's see okay so it's pretty quick you see the accuracy increasing over here and we've got it up to 82 okay so nothing special but I tend to find with these smaller open source models it does tend to be a bit lower so let's try that okay and then we can see the updated route thresholds are these so interestingly mathem itics is incredibly low here which would make me think maybe we need to add some non routes to that I'm not sure but that's something that I would probably consider trying but yeah the rest seem reasonable okay so probably around this 75 down to around 60 is where we have that sort of similar to or not similar up to similar threshold hold for this model and as I mentioned that will vary depending on which model you're using so yeah I mean that that is it really uh we can just have a look at the valuation again we get 81 69 okay so that is it for this very quick introduction to the optimization function here you can also try this with other models so for example all 002 which is not quite the the latest open AI embedding model anymore and also coh here I think as well they would both get up to about 92% after training on the same data set maybe that there's some some differences but not a huge number so the model does matter a lot here but also how we're optimizing so we can obviously evaluate and fit but realistically we also should be adding new utterances to our routes and we should also be adding more data to our our test dator as we as we go along we really want to be iterating on this and sort of improving it over time rather than just hoping that it will fit and then that's it so yeah I mean that's it for now I hope this has been useful and interesting so thank you very much for watching and I will see you again in the next one bye now

Original Description

AI decision-making can now be easily trained using the optimization methods available in semantic router. Route score thresholds define whether a route should be chosen. If the score we identify for any given route is higher than the Route.score_threshold, it passes; otherwise, it does not, and either another route is chosen or we return no route. Given that this one score_threshold parameter can define the choice of a route, it's important to get it right — but it's incredibly inefficient to do so manually. Instead, we can use the fit and evaluate methods of our RouteLayer. All we must do is pass a smaller number of (utterance, target route) examples to our methods, and with the fit, we will often see dramatically improved performance within seconds — we will see how to measure that performance gain with evaluation. ⭐ GitHub Repo: https://github.com/aurelio-labs/semantic-router/ 📌 Code: https://github.com/aurelio-labs/semantic-router/blob/main/docs/06-threshold-optimization.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

This video teaches how to use the semantic router library to optimize routes using AI decision-making, with a focus on modifying thresholds and training methods. It demonstrates how to achieve high accuracy in route optimization using local models and GPU speedup. The key takeaway is that the model matters, but also how we're optimizing, and that adding new utterances and data to the test set is crucial for improving performance.

Key Takeaways
  1. Modify thresholds on a route by route basis
  2. Optimize route values with training methods
  3. Initialize route layer with routes and encoder
  4. Evaluate route layer with test data set
  5. Add utterances to improve performance
  6. Use Open AI embedding model for optimization
  7. Train model on data set to achieve high accuracy
💡 The model matters, but also how we're optimizing, and that adding new utterances and data to the test set is crucial for improving performance.

Related Reads

📰
€2,000 and Nineteen Months of “Almost.” My Rival Vibe-Coded the Same App.
Learn from a startup founder's 19-month journey of developing an app, only to find a rival with a similar idea, and understand the importance of speed and uniqueness in the market
Medium · Startup
📰
I switched from hourly billing to fixed pricing in 2026. My effective rate went from $58/hr to $235/hr.
Learn how switching from hourly to fixed pricing can significantly increase your effective rate, as evidenced by a freelancer's 6-month experiment with 14 clients
Dev.to · HAL GOBVAN
📰
How to Prepare for Your First International Payment
Learn how to prepare for your first international payment with a beginner's checklist
Medium · Startup
📰
Big Pharma And Psychedelic Medicine: Eli Lilly’s $3.8 Billion Bet
Eli Lilly's $3.8 billion acquisition of AtaiBeckley signals a significant investment in psychedelic medicine, why it matters for the future of mental health treatment
Forbes Innovation
Up next
Watch this before applying for jobs as a developer.
Tech With Tim
Watch →