Super fast semantic router for AI decision making
Project description
Semantic Router is a superfast decision-making layer for your LLMs and agents. Rather than waiting for slow LLM generations to make tool-use decisions, we use the magic of semantic vector space to make those decisions — routing our requests using semantic meaning.
Quickstart
To get started with semantic-router we install it like so:
pip install -qU semantic-router
❗️ If wanting to use a fully local version of semantic router you can use HuggingFaceEncoder
and LlamaCppLLM
(pip install -qU "semantic-router[local]"
, see here). To use the HybridRouteLayer
you must pip install -qU "semantic-router[hybrid]"
.
We begin by defining a set of Route
objects. These are the decision paths that the semantic router can decide to use, let's try two simple routes for now — one for talk on politics and another for chitchat:
from semantic_router import Route
# we could use this as a guide for our chatbot to avoid political conversations
politics = Route(
name="politics",
utterances=[
"isn't politics the best thing ever",
"why don't you tell me about your political opinions",
"don't you just love the president",
"they're going to destroy this country!",
"they will save the country!",
],
)
# this could be used as an indicator to our chatbot to switch to a more
# conversational prompt
chitchat = Route(
name="chitchat",
utterances=[
"how's the weather today?",
"how are things going?",
"lovely weather today",
"the weather is horrendous",
"let's go to the chippy",
],
)
# we place both of our decisions together into single list
routes = [politics, chitchat]
We have our routes ready, now we initialize an embedding / encoder model. We currently support a CohereEncoder
and OpenAIEncoder
— more encoders will be added soon. To initialize them we do:
import os
from semantic_router.encoders import CohereEncoder, OpenAIEncoder
# for Cohere
os.environ["COHERE_API_KEY"] = "<YOUR_API_KEY>"
encoder = CohereEncoder()
# or for OpenAI
os.environ["OPENAI_API_KEY"] = "<YOUR_API_KEY>"
encoder = OpenAIEncoder()
With our routes
and encoder
defined we now create a RouteLayer
. The route layer handles our semantic decision making.
from semantic_router.layer import RouteLayer
rl = RouteLayer(encoder=encoder, routes=routes)
We can now use our route layer to make super fast decisions based on user queries. Let's try with two queries that should trigger our route decisions:
rl("don't you love politics?").name
[Out]: 'politics'
Correct decision, let's try another:
rl("how's the weather today?").name
[Out]: 'chitchat'
We get both decisions correct! Now lets try sending an unrelated query:
rl("I'm interested in learning about llama 2").name
[Out]:
In this case, no decision could be made as we had no matches — so our route layer returned None
!
Integrations
The encoders of semantic router include easy-to-use integrations with Cohere, OpenAI, Hugging Face, FastEmbed, and more — we even support multi-modality!.
Our utterance vector space also integrates with Pinecone and Qdrant!
📚 Resources
Docs
Notebook | Description |
---|---|
Introduction | Introduction to Semantic Router and static routes |
Dynamic Routes | Dynamic routes for parameter generation and functionc calls |
Save/Load Layers | How to save and load RouteLayer from file |
LangChain Integration | How to integrate Semantic Router with LangChain Agents |
Local Execution | Fully local Semantic Router with dynamic routes — local models such as Mistral 7B outperform GPT-3.5 in most tests |
Route Optimization | How to train route layer thresholds to optimize performance |
Multi-Modal Routes | Using multi-modal routes to identify Shrek vs. not-Shrek pictures |
Online Course
Community
- Dimitrios Manias, Ali Chouman, Abdallah Shami, Semantic Routing for Enhanced Performance of LLM-Assisted Intent-Based 5G Core Network Management and Orchestration, IEEE GlobeCom 2024
- Julian Horsey, Semantic Router superfast decision layer for LLMs and AI agents, Geeky Gadgets
- azhar, Beyond Basic Chatbots: How Semantic Router is Changing the Game, AI Insights @ Medium
- Daniel Avila, Semantic Router: Enhancing Control in LLM Conversations, CodeGPT @ Medium
- Yogendra Sisodia, Stop Chat-GPT From Going Rogue In Production With Semantic Router, Medium
- Aniket Hingane, LLM Apps: Why you Must Know Semantic Router in 2024: Part 1, Medium
- Adrien Sales, 🔀 Semantic Router w. ollama/gemma2 : real life 10ms hotline challenge 🤯
- Adrien Sales, Kaggle Notebook 🔀 Semantic Router:
ollama
/gemma2:9b
hotline
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file semantic_router-0.0.60.tar.gz
.
File metadata
- Download URL: semantic_router-0.0.60.tar.gz
- Upload date:
- Size: 61.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.11.9 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 70a98a6e547a9a6911482d60f210f28995799cc1f97a10aaacc9089b98a1ff27 |
|
MD5 | 671870deb813fac33d7422b4e6d90c6a |
|
BLAKE2b-256 | cccfca97602521e8124474964b1855670561f1a3697a9f46f6b818a48c8a4c47 |
File details
Details for the file semantic_router-0.0.60-py3-none-any.whl
.
File metadata
- Download URL: semantic_router-0.0.60-py3-none-any.whl
- Upload date:
- Size: 82.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.11.9 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6e8a1c1d6b57097d19df00a8074860c4d56c9b4ea04719d98b534581c1edf4b7 |
|
MD5 | 720ec1589f5b75bf402c741411b4bf9a |
|
BLAKE2b-256 | 075eb6bc2983e68857d8e9c0b0c610bbc73bc5da1885fcc3d169f46da7e51fa1 |