Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bootstraprag/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def create(project_name, framework, template, observability):
'rag-with-react',
'rag-with-hyde',
'rag-with-flare',
'rag-with-vision',
'rag-with-self-correction',
'rag-with-controllable-agents',
'rag-with-llama-parse',
Expand All @@ -50,7 +51,7 @@ def create(project_name, framework, template, observability):
elif framework == 'standalone-qdrant':
framework = 'qdrant'
template_choices = ['simple-search', 'multimodal-search', 'hybrid-search', 'hybrid-search-advanced',
'retrieval-quality']
'retrieval-quality', 'semantic-cache']
elif framework == 'standalone-evaluations':
framework = 'evaluations'
template_choices = ['deep-evals', 'mlflow-evals', 'phoenix-evals', 'ragas-evals']
Expand Down
3 changes: 3 additions & 0 deletions bootstraprag/templates/qdrant/semantic_routing/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
encoder_model='sentence-transformers/all-MiniLM-L6-v2'
qdrant_api_key='th3s3cr3tk3y'
qdrant_url='http://localhost:6333/'
45 changes: 45 additions & 0 deletions bootstraprag/templates/qdrant/semantic_routing/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from semantic_router import Route

from semantic_routing_core import SemanticRouter
# Example usage
if __name__ == "__main__":
# Initialize the SemanticRouter
semantic_router = SemanticRouter()

# Define routes
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!",
],
)

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]

# Set up routes
semantic_router.setup_routes(routes)

# Test the routing
queries = [
"don't you love politics?",
"how's the weather today?",
]

for query in queries:
routed_route = semantic_router.route_query(query)
print(f"Query: '{query}' routed to: '{routed_route}'")
6 changes: 6 additions & 0 deletions bootstraprag/templates/qdrant/semantic_routing/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## Semantic Routing
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.

### How to execute code
1. `pip install -r requirements.txt`
2. `python main.py`
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
semantic-router[qdrant]
python-dotenv==1.0.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import os
from semantic_router import RouteLayer
from semantic_router.encoders import FastEmbedEncoder
from semantic_router.index import QdrantIndex
from dotenv import load_dotenv, find_dotenv

# load the data from env file
load_dotenv(find_dotenv())


class SemanticRouter:
def __init__(self, qdrant_api_key: str = os.environ.get('qdrant_api_key'),
qdrant_url: str = os.environ.get('qdrant_url'),
index_name="semantic-router-index"):
"""
Initialize the SemanticRouter with OpenAI API key and Qdrant configurations.

:param qdrant_api_key: Your Qdrant API key.
:param qdrant_url: URL of the Qdrant instance.
:param index_name: Name of the Qdrant index to use.
:param location: None not to consider the in memory instance.
"""
self.encoder = FastEmbedEncoder(name=os.environ.get('encoder_model'))
self.qdrant_index = QdrantIndex(url=qdrant_url, api_key=qdrant_api_key, index_name=index_name, location=None)
self.route_layer = None

def setup_routes(self, routes):
"""
Set up the routing layer with the provided routes.

:param routes: List of Route objects.
"""
self.route_layer = RouteLayer(encoder=self.encoder, routes=routes, index=self.qdrant_index)

def route_query(self, query):
"""
Route a query to the appropriate route based on semantic similarity.

:param query: The input query string.
:return: Name of the routed route.
"""
if not self.route_layer:
raise ValueError("Routes have not been set up. Call setup_routes() first.")
result = self.route_layer(query)
return result.name
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name='bootstrap-rag',
version='0.0.12',
version='0.0.13',
long_description=long_description,
long_description_content_type="text/markdown",
packages=find_packages(),
Expand Down