Skip to main content

Super fast semantic router for AI decision making

Reason this release was yanked:

Incorrect dependencies for pinecone package

Project description

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.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

Semantic Router Course

Community

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

semantic_router-0.0.69.tar.gz (64.1 kB view details)

Uploaded Source

Built Distribution

semantic_router-0.0.69-py3-none-any.whl (85.1 kB view details)

Uploaded Python 3

File details

Details for the file semantic_router-0.0.69.tar.gz.

File metadata

  • Download URL: semantic_router-0.0.69.tar.gz
  • Upload date:
  • Size: 64.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.11.10 Linux/6.8.0-1014-azure

File hashes

Hashes for semantic_router-0.0.69.tar.gz
Algorithm Hash digest
SHA256 5d7dc1022a5505c3376a1baf424b808e3b8a2b1d71ffc4bd1750281abbcfa4ed
MD5 547bc139c8db3843987e83b070393f12
BLAKE2b-256 ef91d62b598f243d0a53c7d4f31d61a16d0db878f2c1017381b0cdd0d1c766c1

See more details on using hashes here.

File details

Details for the file semantic_router-0.0.69-py3-none-any.whl.

File metadata

  • Download URL: semantic_router-0.0.69-py3-none-any.whl
  • Upload date:
  • Size: 85.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.11.10 Linux/6.8.0-1014-azure

File hashes

Hashes for semantic_router-0.0.69-py3-none-any.whl
Algorithm Hash digest
SHA256 7b159de4b60b7ef4567140d7be426160e3ce60b6c63506531b9885c59d371df6
MD5 62d96424655dfd5d023d401c4b2ab4b7
BLAKE2b-256 ba2903850afd436b99f200534b5449cbe4bca1252158e6fe4eabd40b7a6640eb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page