🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org
Python agentai-societiesartificial-intelligencecommunicative-aicooperative-aideep-learninglarge-language-modelsmulti-agent-systemsnatural-language-processing
feat: Add `top_k` and `similarity_threshold` to RetrievalToolkit (#904)
Wendong-Fan5bcab823898b
diff --git a/camel/retrievers/auto_retriever.py b/camel/retrievers/auto_retriever.pyindex af4cada9b..9e2d0aec1 100644--- a/camel/retrievers/auto_retriever.py+++ b/camel/retrievers/auto_retriever.py@@ -25,15 +25,13 @@ from camel.storages import (VectorDBQuery,)from camel.types import StorageType+from camel.utils import Constantstry:from unstructured.documents.elements import Elementexcept ImportError:Element = None-DEFAULT_TOP_K_RESULTS = 1-DEFAULT_SIMILARITY_THRESHOLD = 0.75-class AutoRetriever:r"""Facilitates the automatic retrieval of information using a@@ -178,8 +176,8 @@ class AutoRetriever:self,query: str,contents: Union[str, List[str], Element, List[Element]],- top_k: int = DEFAULT_TOP_K_RESULTS,- similarity_threshold: float = DEFAULT_SIMILARITY_THRESHOLD,+ top_k: int = Constants.DEFAULT_TOP_K_RESULTS,+ similarity_threshold: float = Constants.DEFAULT_SIMILARITY_THRESHOLD,return_detailed_info: bool = False,max_characters: int = 500,) -> dict[str, Sequence[Collection[str]]]:diff --git a/camel/retrievers/vector_retriever.py b/camel/retrievers/vector_retriever.pyindex f58bfd162..8f0058df5 100644--- a/camel/retrievers/vector_retriever.py+++ b/camel/retrievers/vector_retriever.py@@ -25,15 +25,13 @@ from camel.storages import (VectorDBQuery,VectorRecord,)+from camel.utils import Constantstry:from unstructured.documents.elements import Elementexcept ImportError:Element = None-DEFAULT_TOP_K_RESULTS = 1-DEFAULT_SIMILARITY_THRESHOLD = 0.75-class VectorRetriever(BaseRetriever):r"""An implementation of the `BaseRetriever` by using vector storage and@@ -147,8 +145,8 @@ class VectorRetriever(BaseRetriever):def query(self,query: str,- top_k: int = DEFAULT_TOP_K_RESULTS,- similarity_threshold: float = DEFAULT_SIMILARITY_THRESHOLD,+ top_k: int = Constants.DEFAULT_TOP_K_RESULTS,+ similarity_threshold: float = Constants.DEFAULT_SIMILARITY_THRESHOLD,) -> List[Dict[str, Any]]:r"""Executes a query in vector storage and compiles the retrievedresults into a dictionary.@@ -159,7 +157,8 @@ class VectorRetriever(BaseRetriever):for filtering results. Defaults to`DEFAULT_SIMILARITY_THRESHOLD`.top_k (int, optional): The number of top results to return during- retriever. Must be a positive integer. Defaults to 1.+ retriever. Must be a positive integer. Defaults to+ `DEFAULT_TOP_K_RESULTS`.Returns:List[Dict[str, Any]]: Concatenated list of the query results.diff --git a/camel/toolkits/retrieval_toolkit.py b/camel/toolkits/retrieval_toolkit.pyindex 50b8dd431..22e76d1f3 100644--- a/camel/toolkits/retrieval_toolkit.py+++ b/camel/toolkits/retrieval_toolkit.py@@ -11,12 +11,13 @@# See the License for the specific language governing permissions and# limitations under the License.# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========-from typing import List, Union+from typing import List, Optional, Unionfrom camel.retrievers import AutoRetrieverfrom camel.toolkits import OpenAIFunctionfrom camel.toolkits.base import BaseToolkitfrom camel.types import StorageType+from camel.utils import Constantsclass RetrievalToolkit(BaseToolkit):@@ -26,8 +27,19 @@ class RetrievalToolkit(BaseToolkit):storage system based on a specified query."""+ def __init__(self, auto_retriever: Optional[AutoRetriever] = None) -> None:+ r"""Initializes a new instance of the RetrievalToolkit class."""+ self.ar = auto_retriever or AutoRetriever(+ vector_storage_local_path="camel/temp_storage",+ storage_type=StorageType.QDRANT,+ )+def information_retrieval(- self, query: str, contents: Union[str, List[str]]+ self,+ query: str,+ contents: Union[str, List[str]],+ top_k: int = Constants.DEFAULT_TOP_K_RESULTS,+ similarity_threshold: float = Constants.DEFAULT_SIMILARITY_THRESHOLD,) -> str:r"""Retrieves information from a local vector storage based on thespecified query. This function connects to a local vector storage@@ -39,6 +51,12 @@ class RetrievalToolkit(BaseToolkit):query (str): The question or query for which an answer is required.contents (Union[str, List[str]]): Local file paths, remote URLs orstring contents.+ top_k (int, optional): The number of top results to return during+ retrieve. Must be a positive integer. Defaults to+ `DEFAULT_TOP_K_RESULTS`.+ similarity_threshold (float, optional): The similarity threshold+ for filtering results. Defaults to+ `DEFAULT_SIMILARITY_THRESHOLD`.Returns:str: The information retrieved in response to the query, aggregated@@ -46,16 +64,14 @@ class RetrievalToolkit(BaseToolkit):Example:# Retrieve information about CAMEL AI.- information_retrieval(query = "what is CAMEL AI?",- contents="https://www.camel-ai.org/")+ information_retrieval(query = "How to contribute to CAMEL AI?",+ contents="https://github.com/camel-ai/camel/blob/master/CONTRIBUTING.md")"""- auto_retriever = AutoRetriever(- vector_storage_local_path="camel/temp_storage",- storage_type=StorageType.QDRANT,- )-- retrieved_info = auto_retriever.run_vector_retriever(- query=query, contents=contents, top_k=3+ retrieved_info = self.ar.run_vector_retriever(+ query=query,+ contents=contents,+ top_k=top_k,+ similarity_threshold=similarity_threshold,)return str(retrieved_info)diff --git a/camel/utils/constants.py b/camel/utils/constants.pyindex 65f548f35..5531e977f 100644--- a/camel/utils/constants.py+++ b/camel/utils/constants.py@@ -22,8 +22,14 @@ class Constants:# are extracted from the video.VIDEO_IMAGE_EXTRACTION_INTERVAL = 50- # default plug of imageio to read video+ # Default plug of imageio to read videoVIDEO_DEFAULT_PLUG_PYAV = "pyav"- # return response with json format+ # Return response with json formatFUNC_NAME_FOR_STRUCTURE_OUTPUT = "return_json_response"++ # Default top k vaule for RAG+ DEFAULT_TOP_K_RESULTS = 1++ # Default similarity threshold vaule for RAG+ DEFAULT_SIMILARITY_THRESHOLD = 0.7