MCPcopy
hub / github.com/aurelio-labs/semantic-router

github.com/aurelio-labs/semantic-router @v0.1.15 sqlite

repository ↗ · DeepWiki ↗ · release v0.1.15 ↗
1,154 symbols 4,012 edges 89 files 510 documented · 44%
README

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, we use the magic of semantic vector space to make those decisions — routing our requests using semantic meaning.

Read the Docs


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.routers import SemanticRouter

rl = SemanticRouter(encoder=encoder, routes=routes, auto_sync="local")

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

Semantic Router Course

Community

Core symbols most depended-on inside this repo

get
called by 60
semantic_router/routers/base.py
add
called by 31
semantic_router/index/base.py
get_utterances
called by 31
semantic_router/index/base.py
_get_table_name
called by 25
semantic_router/index/postgres.py
items
called by 18
semantic_router/schema.py
to_str
called by 17
semantic_router/schema.py
litellm_to_list
called by 16
semantic_router/encoders/litellm.py
_is_valid_inputs
called by 15
semantic_router/llms/base.py

Shape

Method 869
Function 128
Class 126
Route 31

Languages

Python100%

Modules by API surface

tests/integration/test_router_integration.py75 symbols
tests/unit/test_router.py72 symbols
semantic_router/routers/base.py72 symbols
tests/unit/test_sync.py55 symbols
semantic_router/index/pinecone.py50 symbols
tests/unit/test_qdrant_index.py49 symbols
semantic_router/index/postgres.py48 symbols
semantic_router/index/base.py38 symbols
semantic_router/schema.py34 symbols
semantic_router/index/qdrant.py33 symbols
tests/unit/llms/test_llm_openai.py28 symbols
tests/unit/llms/test_llm_base.py26 symbols

Dependencies from manifests, versioned

aurelio-sdk0.0.19 · 1×
litellm1.83.7 · 1×
numpy1.25.2 · 1×
regex2023.12.25 · 1×

For agents

$ claude mcp add semantic-router \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact