diff --git a/semantic_router/index/base.py b/semantic_router/index/base.py index 26335ce5469266264852d967fe18a6fc859277e8..8ef4896786707e2e1aa34b6968d57c3a6215ec08 100644 --- a/semantic_router/index/base.py +++ b/semantic_router/index/base.py @@ -26,7 +26,7 @@ class BaseIndex(BaseModel): embeddings: List[List[float]], routes: List[str], utterances: List[Any], - function_schemas: List[Dict[str, Any]] = None, # type: ignore + function_schemas: List[Dict[str, Any]] | None = None, ): """ Add embeddings to the index. @@ -114,7 +114,7 @@ class BaseIndex(BaseModel): local_route_names: List[str], local_utterances: List[str], dimensions: int, - local_function_schemas: List[str] = None, # type: ignore + local_function_schemas: List[str] | None = None, ): """ Synchronize the local index with the remote index based on the specified mode. diff --git a/semantic_router/index/local.py b/semantic_router/index/local.py index d57f563a24b2586ef7ab7dcf9c482c378debf007..7bc12bba8bdedcdb03e21e7dcfbbe17a7a8a2caf 100644 --- a/semantic_router/index/local.py +++ b/semantic_router/index/local.py @@ -27,7 +27,7 @@ class LocalIndex(BaseIndex): embeddings: List[List[float]], routes: List[str], utterances: List[str], - function_schemas: List[Dict[str, Any]] = None, # type: ignore + function_schemas: List[Dict[str, Any]] | None = None, ): embeds = np.array(embeddings) # type: ignore routes_arr = np.array(routes) @@ -53,7 +53,7 @@ class LocalIndex(BaseIndex): local_route_names: List[str], local_utterances: List[str], dimensions: int, - local_function_schemas: List[str] = None, # type: ignore + local_function_schemas: List[str] | None = None, ): if self.sync is not None: logger.error("Sync remove is not implemented for LocalIndex.") diff --git a/semantic_router/index/pinecone.py b/semantic_router/index/pinecone.py index a1008833bba5f2c05de39f8b8da1dc092161bc09..5b88ba57be4340cd1769bcaa7b3b7b9da765c2a8 100644 --- a/semantic_router/index/pinecone.py +++ b/semantic_router/index/pinecone.py @@ -3,6 +3,8 @@ import asyncio import hashlib import os import time +import json + from typing import Any, Dict, List, Optional, Tuple, Union import numpy as np @@ -211,7 +213,7 @@ class PineconeIndex(BaseIndex): local_route_names: List[str], local_utterances: List[str], dimensions: int, - local_function_schemas: List[str] = None, # type: ignore + local_function_schemas: List[str] | None = None, ): if self.index is None: self.dimensions = self.dimensions or dimensions @@ -314,7 +316,7 @@ class PineconeIndex(BaseIndex): embeddings: List[List[float]], routes: List[str], utterances: List[str], - function_schemas: List[Dict[str, Any]] = None, # type: ignore + function_schemas: List[Dict[str, Any]] | None = None, batch_size: int = 100, ): """Add vectors to Pinecone in batches.""" @@ -327,10 +329,10 @@ class PineconeIndex(BaseIndex): values=vector, route=route, utterance=utterance, - function_schema=str(function_schema), + function_schema=json.dumps(function_schema), ).to_dict() for vector, route, utterance, function_schema in zip( - embeddings, routes, utterances, function_schemas + embeddings, routes, utterances, function_schemas # type: ignore ) ] diff --git a/semantic_router/index/postgres.py b/semantic_router/index/postgres.py index 963f751353404ad744e45ed85e5cc02c538319b4..b4e133fe8611cbc1fdc13677db484b2b6fcb7da3 100644 --- a/semantic_router/index/postgres.py +++ b/semantic_router/index/postgres.py @@ -259,7 +259,7 @@ class PostgresIndex(BaseIndex): embeddings: List[List[float]], routes: List[str], utterances: List[Any], - function_schemas: List[Dict[str, Any]] = None, # type: ignore + function_schemas: List[Dict[str, Any]] | None = None, ) -> None: """ Adds vectors to the index. diff --git a/semantic_router/index/qdrant.py b/semantic_router/index/qdrant.py index 425518ffadd560d6788682c63586b2e3ebc7276a..0b414cff3ca4e746ad48f7aecf0a39395677c964 100644 --- a/semantic_router/index/qdrant.py +++ b/semantic_router/index/qdrant.py @@ -169,7 +169,7 @@ class QdrantIndex(BaseIndex): local_route_names: List[str], local_utterances: List[str], dimensions: int, - local_function_schemas: List[str] = None, # type: ignore + local_function_schemas: List[str] | None = None, ): if self.sync is not None: logger.error("Sync remove is not implemented for QdrantIndex.") @@ -179,7 +179,7 @@ class QdrantIndex(BaseIndex): embeddings: List[List[float]], routes: List[str], utterances: List[str], - function_schemas: List[Dict[str, Any]] = None, # type: ignore + function_schemas: List[Dict[str, Any]] | None = None, batch_size: int = DEFAULT_UPLOAD_BATCH_SIZE, ): self.dimensions = self.dimensions or len(embeddings[0]) diff --git a/semantic_router/layer.py b/semantic_router/layer.py index 6e34af8ead46244b67561665273d7881e5600793..a77c58445ddfa8675c8f3565ef227da3cda0020e 100644 --- a/semantic_router/layer.py +++ b/semantic_router/layer.py @@ -428,6 +428,7 @@ class RouteLayer: if route.score_threshold is None: route.score_threshold = self.score_threshold + # add routes to the index self.index.add( embeddings=embeds, routes=[route.name] * len(route.utterances), @@ -435,7 +436,7 @@ class RouteLayer: function_schemas=( route.function_schemas * len(route.utterances) if route.function_schemas - else [""] * len(route.utterances) # type: ignore + else [{}] * len(route.utterances) ), )