From 1fefc5a0f8e0d58ce794f9b1861cc37fca6f221d Mon Sep 17 00:00:00 2001 From: tolgadevAI <164843802+tolgadevAI@users.noreply.github.com> Date: Mon, 26 Aug 2024 14:36:23 +0300 Subject: [PATCH] update the object definitions --- semantic_router/index/base.py | 4 ++-- semantic_router/index/local.py | 4 ++-- semantic_router/index/pinecone.py | 10 ++++++---- semantic_router/index/postgres.py | 2 +- semantic_router/index/qdrant.py | 4 ++-- semantic_router/layer.py | 3 ++- 6 files changed, 15 insertions(+), 12 deletions(-) diff --git a/semantic_router/index/base.py b/semantic_router/index/base.py index 26335ce5..8ef48967 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 d57f563a..7bc12bba 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 a1008833..5b88ba57 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 963f7513..b4e133fe 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 425518ff..0b414cff 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 6e34af8e..a77c5844 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) ), ) -- GitLab