#!/usr/bin/env python # coding: utf-8 # # Semantic Router: Hybrid Layer # # The `HybridRouter` in the Semantic Router library can improve making performance particularly for niche use-cases that contain specific terminology, such as finance or medical. # # It helps us provide more importance to making based on the keywords contained in our utterances and user queries. # ## Getting Started # # We start by installing the library: # # In[1]: #!pip install -qU semantic-router==0.1.0.dev3 # We start by defining a dictionary mapping s to example phrases that should trigger those s. # # In[1]: from semantic_router.route import Route 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", "don't you just hate the president", "they're going to destroy this country!", "they will save the country!", ], ) # Let's define another for good measure: # # In[2]: 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", ], ) routes = [politics, chitchat] # Now we initialize our embedding model: # # In[4]: import os from semantic_router.encoders import OpenAIEncoder, TfidfEncoder from getpass import getpass os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY") or getpass( "Enter OpenAI API Key: " ) dense_encoder = OpenAIEncoder() # sparse_encoder = BM25Encoder() sparse_encoder = TfidfEncoder() # Now we define the `RouteLayer`. When called, the route layer will consume text (a query) and output the category (`Route`) it belongs to — to initialize a `RouteLayer` we need our `encoder` model and a list of `routes`. # # In[5]: from semantic_router.routers import HybridRouter router = HybridRouter( encoder=dense_encoder, sparse_encoder=sparse_encoder, routes=routes ) # In[6]: router("don't you love politics?") # In[7]: router("how's the weather today?") # --- #