diff --git a/semantic_router/index/pinecone.py b/semantic_router/index/pinecone.py index 846890fcf15e543b159467cc58bd0c8f927af0b4..2aae42b873d76d60ecdfd1da14ebf63bd8b9d8d3 100644 --- a/semantic_router/index/pinecone.py +++ b/semantic_router/index/pinecone.py @@ -678,6 +678,8 @@ class PineconeIndex(BaseIndex): return np.array(scores), route_names def _read_hash(self) -> ConfigParameter: + if self.index is None: + raise ValueError("Index has not been initialized.") hash_record = self.index.fetch( ids=[f"sr_hash#{self.namespace}"], namespace="sr_config", @@ -698,6 +700,10 @@ class PineconeIndex(BaseIndex): :param config: The config parameter to write to the index. :type config: ConfigParameter """ + if self.index is None: + raise ValueError("Index has not been initialized.") + if self.dimensions is None: + raise ValueError("Must set PineconeIndex.dimensions before writing config.") self.index.upsert( vectors=[config.to_pinecone(dimensions=self.dimensions)], namespace="sr_config", diff --git a/semantic_router/layer.py b/semantic_router/layer.py index 1bf13dbe2de5b9120addfcdac1b6bbd5b525cce4..e2f7f2667a890957222d325d14d3a8d9fa3aa41a 100644 --- a/semantic_router/layer.py +++ b/semantic_router/layer.py @@ -539,9 +539,8 @@ class RouteLayer: return config.get_hash() def is_synced(self) -> bool: - """Check if the local and remote route layer instances are synchronized. - """ - #if not self.index.sync: + """Check if the local and remote route layer instances are synchronized.""" + # if not self.index.sync: # raise ValueError("Index is not set to sync with remote index.") # first check hash @@ -549,7 +548,7 @@ class RouteLayer: remote_hash = self.index._read_hash() if local_hash.value == remote_hash.value: return True - #Â TODO: we may be able to remove the below logic + # TODO: we may be able to remove the below logic # if hashes are different, double check local_route_names, local_utterances, local_function_schemas, local_metadata = ( self._extract_routes_details(self.routes, include_metadata=True) diff --git a/semantic_router/schema.py b/semantic_router/schema.py index 755792d6a0c0060da31be0ff2a3746cf1331b41a..bbd5dc1bd02b40d6c68294c1441a4dfd89cf1c5a 100644 --- a/semantic_router/schema.py +++ b/semantic_router/schema.py @@ -64,15 +64,18 @@ class DocumentSplit(BaseModel): def content(self) -> str: return " ".join([doc if isinstance(doc, str) else "" for doc in self.docs]) + class ConfigParameter(BaseModel): field: str value: str - namespace: str = "" + namespace: Optional[str] = None created_at: str = Field(default_factory=lambda: datetime.utcnow().isoformat()) def to_pinecone(self, dimensions: int): + if self.namespace is None: + namespace = "" return { - "id": f"{self.field}#{self.namespace}", + "id": f"{self.field}#{namespace}", "values": [0.1] * dimensions, "metadata": { "value": self.value, @@ -82,6 +85,7 @@ class ConfigParameter(BaseModel): }, } + class Metric(Enum): COSINE = "cosine" DOTPRODUCT = "dotproduct"