Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Semantic Router

PyPI - Python Version GitHub Contributors GitHub Last Commit GitHub Issues GitHub Pull Requests Github License

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, it decides in milliseconds: it compares the semantic meaning of a request against your routes in vector space, or asks a small decision model in a single fast call. Every decision comes with a confidence you can calibrate into a real probability.

Read the Docs


Versions

Semantic Router has two release lines. The 1.x line (main branch) rebuilds the routing layer around calibrated confidence, decision models and composable routers. The 0.x line (v0 branch) finishes at 0.2.0 and receives bug fixes only.

pip install -qU "semantic-router>=1,<2"   # 1.x
pip install -qU "semantic-router<1"       # 0.x

Upgrading from 0.x? The migration guide covers every change, with before and after code. The 0.x docs stay on the versions page.


Quickstart

This quickstart runs entirely on your machine, with a small local embedding model and no API keys:

pip install -qU "semantic-router[fastembed]"

We begin by defining a set of Route objects. These are the decisions the router can make. 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]

Next we pick an encoder, the model that turns text into vectors. We use a local FastEmbed model here; OpenAI, Cohere, Hugging Face and many more work the same way.

from semantic_router.encoders import FastEmbedEncoder

encoder = FastEmbedEncoder(name="BAAI/bge-small-en-v1.5")

With our routes and encoder defined, we create an IndexedRouter. It embeds every utterance once and matches each query against them.

from semantic_router import IndexedRouter

router = IndexedRouter(encoder=encoder, routes=routes)

Now we can make super fast decisions based on user queries:

router("don't you love politics?").name
[Out]: 'politics'

Correct decision, let's try another:

router("how's the weather today?").name
[Out]: 'chitchat'

We get both decisions correct! By default the best route always wins, so to let the router say "none of these" we calibrate it on a few labelled queries it hasn't seen (None means no route should match) and set a confidence threshold:

router.fit(
    X=[
        "who should I vote for in the election?", "what do you think of the prime minister?", "is the government doing a good job?",
        "tell me your opinion on the new tax law", "are you left wing or right wing?", "the opposition party is a joke",
        "nice day out, isn't it?", "it's freezing outside", "how was your weekend?",
        "fancy grabbing some fish and chips?", "what a lovely sunny afternoon", "how have you been?",
        "how do I reset my password?", "what's the capital of australia?", "write me a python function to sort a list",
        "what is the boiling point of water?", "recommend a good sci-fi book", "how many legs does a spider have?",
    ],
    y=["politics"] * 6 + ["chitchat"] * 6 + [None] * 6,
)
router.confidence_threshold = 0.5  # now a probability: act only when it's more likely right than wrong

router("I'm interested in learning about llama 2").name
[Out]:

No route cleared the bar, so the router returned None!

Beyond embeddings

  • DecisionRouter asks a decision model (TypeSafe's Jev, Laya or Von) which route applies, and can answer several yes/no, choice and score questions about one message in a single call.
  • Chain answers easy queries from a fast router and falls through to a slower one only when it isn't sure. Merge averages several routers.
  • Hybrid search: give IndexedRouter a sparse_encoder to weigh exact keywords alongside meaning.
  • Production indexes: keep your routes in Pinecone, Qdrant or Postgres.
  • Multi-modal: route on images as well as text.

📚 Resources

Docs

Notebook Description
Introduction Introduction to Semantic Router and the IndexedRouter
Save/Load Routers Save any router to YAML or JSON and load it back, calibration included
LangChain Integration How to integrate Semantic Router with LangChain Agents
Local Execution Fully local routing with FastEmbed, no API keys
Calibration Turn raw scores into probabilities with fit(), and set thresholds that mean what they say
Multi-Modal Routes Using multi-modal routes to identify Shrek vs. not-Shrek pictures
Healthcare Administrative Routing Fully local routing, calibration, and evaluation across seven synthetic healthcare administrative workflows

Online Course

Semantic Router Course

Community

Metadata

Release files for semantic-router 1.0.0rc1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for semantic-router 1.0.0rc1
File Size Uploaded
semantic_router-1.0.0rc1.tar.gz 79.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for semantic-router 1.0.0rc1
File Interpreter ABI Platform
semantic_router-1.0.0rc1-py3-none-any.whl Python 3 none any Details

Total release size: 184.3 kB

Release files / semantic_router-1.0.0rc1.tar.gz

Download URL semantic_router-1.0.0rc1.tar.gz
Size 79.4 kB
Tags Source
SHA-256 checksum
How to use checksums
598b652abc0c05c697fdd3ec21d67ebeb725a2982302fd14c4afeec6214679c3
BLAKE2b-256 checksum
How to use checksums
19adfeaccf794ce22d3bdf5f32894f74d1eca5d0a49a9a07640370cb07faf024
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.1 {"installer":{"name":"uv","version":"0.13.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / semantic_router-1.0.0rc1-py3-none-any.whl

Download URL semantic_router-1.0.0rc1-py3-none-any.whl
Size 104.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6001279d838a1dec257af7e1983785ce04eedd551a835bb5587804a566d4cacd
BLAKE2b-256 checksum
How to use checksums
950a4f808bee0d331eaa8a57c5b46978d6da727b693ad0b155ff74ab6020552d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.1 {"installer":{"name":"uv","version":"0.13.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

1.0.0

2 release files

This release

1.0.0rc1 This release

2 release files

0.1.16

2 release files

0.1.15

2 release files

0.1.14

2 release files

0.1.13

2 release files

0.1.12

2 release files

0.1.11

2 release files

0.1.10

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.72

2 release files

0.0.68

2 release files

0.0.67

2 release files

0.0.66

2 release files

0.0.62

2 release files

0.0.61

2 release files

0.0.60

2 release files

0.0.59

2 release files

0.0.55

2 release files

0.0.54

2 release files

0.0.53

2 release files

0.0.52

2 release files

0.0.51

2 release files

0.0.48

2 release files

0.0.47

2 release files

0.0.45

2 release files

0.0.44

2 release files

0.0.43

2 release files

0.0.42

2 release files

0.0.41

2 release files

0.0.40

2 release files

0.0.39

2 release files

0.0.37

2 release files

0.0.36

2 release files

0.0.35

2 release files

0.0.34

2 release files

0.0.33

2 release files

0.0.29

2 release files

0.0.28

2 release files

0.0.26

2 release files

0.0.25

2 release files

0.0.24

2 release files

0.0.23

2 release files

0.0.22

2 release files

0.0.21

2 release files

0.0.20

2 release files

0.0.19

2 release files

0.0.18

2 release files

0.0.17

2 release files

0.0.16

2 release files

0.0.14

2 release files

0.0.13

2 release files

0.0.12

2 release files

0.0.11

2 release files

0.0.10

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.5

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page