Semantic Router

0.1.12 · active · verified Wed Apr 15

Semantic Router is a superfast decision-making layer for LLMs and agents. It routes requests based on semantic meaning using vector space, rather than relying on slow LLM generations for tool-use or safety decisions. It supports various embedding models (e.g., OpenAI, Cohere, Hugging Face) and integrates with vector databases like Pinecone and Qdrant. The library is actively maintained with frequent updates and a focus on speed, safety, and scalability.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define semantic routes, initialize an encoder (OpenAI in this case), create a router with a local index, and then use it to categorize incoming queries based on their semantic meaning. Ensure your OpenAI API key is set as an environment variable.

import os
from semantic_router import Route
from semantic_router.encoders import OpenAIEncoder
from semantic_router.routers import SemanticRouter
from semantic_router.index import LocalIndex

# Ensure API key is set or prompt for it
if not os.getenv("OPENAI_API_KEY"): #
    print("Please set the OPENAI_API_KEY environment variable.")
    # In a real application, you might raise an error or use getpass
    os.environ["OPENAI_API_KEY"] = os.environ.get("OPENAI_API_KEY", "sk-YOUR_OPENAI_KEY_HERE")

# 1. Define routes
politics = Route(
    name="politics",
    utterances=[
        "isn't politics the best thing ever",
        "why don't you tell me about your political opinions",
        "they're going to destroy this country!"
    ]
)
chitchat = Route(
    name="chitchat",
    utterances=[
        "how's the weather today?",
        "how are things going?",
        "lovely weather today"
    ]
)
routes = [politics, chitchat]

# 2. Initialize an encoder (e.g., OpenAIEncoder)
encoder = OpenAIEncoder()

# 3. Create a SemanticRouter instance with an index (e.g., LocalIndex)
# For persistent storage, consider PineconeIndex, QdrantIndex, etc.
router = SemanticRouter(
    encoder=encoder,
    routes=routes,
    index=LocalIndex()
)

# 4. Route a query
query1 = "What do you think about the government?"
route_result1 = router(query1)
print(f"Query: '{query1}' -> Routed to: {route_result1.name} (Score: {route_result1.score:.2f})")

query2 = "How's life treating you?"
route_result2 = router(query2)
print(f"Query: '{query2}' -> Routed to: {route_result2.name} (Score: {route_result2.score:.2f})")

query3 = "Tell me a story."
route_result3 = router(query3) # Should return None if no match above threshold
print(f"Query: '{query3}' -> Routed to: {route_result3.name if route_result3 else 'None'} (Score: {route_result3.score:.2f} if route_result3 else 'N/A')")

view raw JSON →