Skip to content
Snippets Groups Projects
Commit 41d1c15b authored by tolgadevAI's avatar tolgadevAI
Browse files

optimize the '_add_routes' function

parent f2302230
No related branches found
No related tags found
No related merge requests found
...@@ -482,28 +482,44 @@ class RouteLayer: ...@@ -482,28 +482,44 @@ class RouteLayer:
self.routes.append(route) self.routes.append(route)
def _add_routes(self, routes: List[Route]): def _add_routes(self, routes: List[Route]):
if routes: if not routes:
for route in routes: logger.warning("No routes provided to add.")
logger.info(f"Adding `{route.name}` route") return
embeddings = self.encoder(route.utterances)
if route.score_threshold is None: route_names = []
route.score_threshold = self.score_threshold all_embeddings = []
try: all_utterances = []
self.index.add( all_function_schemas = []
embeddings=embeddings,
routes=[route.name] * len(route.utterances), for route in routes:
utterances=route.utterances, logger.info(f"Adding `{route.name}` route")
function_schemas=( route_embeddings = self.encoder(route.utterances)
route.function_schemas * len(route.utterances)
if route.function_schemas # Set score_threshold if not already set
else [{}] * len(route.utterances) route.score_threshold = route.score_threshold or self.score_threshold
),
) # Prepare data for batch insertion
except Exception as e: route_names.extend([route.name] * len(route.utterances))
logger.error( all_embeddings.extend(route_embeddings)
f"Failed to add route `{route.name}` to the index: {e}" all_utterances.extend(route.utterances)
) all_function_schemas.extend(
raise Exception(f"Indexing error for route `{route.name}`") from e route.function_schemas * len(route.utterances)
if route.function_schemas
else [{}] * len(route.utterances)
)
try:
# Batch insertion into the index
self.index.add(
embeddings=all_embeddings,
routes=route_names,
utterances=all_utterances,
function_schemas=all_function_schemas,
)
except Exception as e:
logger.error(f"Failed to add routes to the index: {e}")
raise Exception("Indexing error occurred") from e
def _add_and_sync_routes(self, routes: List[Route]): def _add_and_sync_routes(self, routes: List[Route]):
# create embeddings for all routes and sync at startup with remote ones based on sync setting # create embeddings for all routes and sync at startup with remote ones based on sync setting
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment