Performance: Embeddings are recalculated on each startup with PostgresIndex despite existing in database #459
Replies: 2 comments
-
Maybe @jamescalam or @itsfrankjames can enlighten me? 🙂 |
Beta Was this translation helpful? Give feedback.
0 replies
-
FYI: I created a new optimized route layer which solves my issue. class OptimizedRouteLayer(RouteLayer):
"""
An optimized version of RouteLayer that efficiently handles route initialization
by reusing existing embeddings and using batch operations.
"""
def _add_routes(self, routes):
if not routes:
return
# Get existing routes from the index
existing_routes = {(route, utterance) for route, utterance in self.index.get_routes()}
# Separate new routes that need embeddings
new_routes = []
new_utterances = []
new_function_schemas = []
new_metadata = []
for route in routes:
for utterance in route.utterances:
if (route.name, utterance) not in existing_routes:
new_routes.append(route.name)
new_utterances.append(utterance)
new_function_schemas.append(
route.function_schemas[0] if route.function_schemas is not None else {}
)
new_metadata.append(route.metadata if route.metadata else {})
# Only calculate embeddings for new routes
if new_utterances:
logger.debug(f"Calculating embeddings for {len(new_utterances)} new utterances")
start = time.time()
embedded_utterances = self.encoder(new_utterances)
logger.debug(f"Embeddings calculated in {(time.time() - start) * 1000:.2f} ms")
try:
# Batch insertion into the index
start = time.time()
self.index.add(
embeddings=embedded_utterances,
routes=new_routes,
utterances=new_utterances,
function_schemas=new_function_schemas,
metadata_list=new_metadata,
)
logger.debug(f"Vectors added to index in {(time.time() - start) * 1000:.2f} ms")
except Exception as e:
logger.error(f"Failed to add routes to the index: {e}")
raise
else:
logger.debug("All routes already exist in the index")
# Update the local routes list
self.routes = routes |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi all,
OK, I am sure I am holding it the wrong way and doing something completely silly wrong. So, here goes...
I'm using semantic-router with
PostgresIndex
and notice that embeddings are being recalculated on each startup, even though they should already exist in the Postgres database from previous runs. This makes startup slow, especially with our larger set of utterances.Current behavior:
Example code:
Questions:
PostgresIndex
when dealing with a larger number of utterances (~350 in our case)?Environment:
The recalculation is particularly noticeable with our larger set of utterances. We'd like to leverage Postgres's persistence to avoid this overhead on subsequent runs.
Any guidance on the proper way to handle embedding persistence and reuse would be greatly appreciated.
Thanks.
Beta Was this translation helpful? Give feedback.
All reactions