!pip install -qU "semantic-router[qdrant]"
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" "don't you just hate 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]
import os
from getpass import getpass
from semantic_router.encoders import CohereEncoder
os.environ["COHERE_API_KEY"] = os.environ.get("COHERE_API_KEY") or getpass(
"Enter COHERE API key: "
)
encoder = CohereEncoder()
from semantic_router.index.qdrant import QdrantIndex
qd_index = QdrantIndex(location=":memory:")
from semantic_router.layer import RouteLayer
rl = RouteLayer(encoder=encoder, routes=routes, index=qd_index)
2024-03-27 18:22:42 INFO semantic_router.utils.logger local
We can check our route layer and index information.
rl.list_route_names()
['politics', 'chitchat']
len(rl.index)
10
And query:
rl("don't you love politics?").name
'politics'
rl("how's the weather today?").name
'chitchat'
rl("I'm interested in learning about llama 2").name
We can delete or update routes.
len(rl.index)
10
import time
rl.delete(route_name="chitchat")
time.sleep(1)
len(rl.index)
5
rl("how's the weather today?").name
rl.index.get_routes()
[('politics', 'they will save the country!'), ('politics', "isn't politics the best thing ever"), ('politics', "why don't you tell me about your political opinions"), ('politics', "they're going to destroy this country!"), ('politics', "don't you just love the presidentdon't you just hate the president")]
rl.index.describe()
{'type': 'qdrant', 'dimensions': 1024, 'vectors': 5}