🐫 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: Auto retrieval support Element as input (#856)
Wendong-Fan50c93fa7ff25
diff --git a/camel/retrievers/auto_retriever.py b/camel/retrievers/auto_retriever.pyindex b84679f7f..4cbc2c4bc 100644--- a/camel/retrievers/auto_retriever.py+++ b/camel/retrievers/auto_retriever.py@@ -28,6 +28,11 @@ from camel.storages import ()from camel.types import StorageType+try:+ from unstructured.documents.elements import Element+except ImportError:+ Element = None+DEFAULT_TOP_K_RESULTS = 1DEFAULT_SIMILARITY_THRESHOLD = 0.75@@ -97,15 +102,19 @@ class AutoRetriever:f"Unsupported vector storage type: {self.storage_type}")- def _collection_name_generator(self, content: str) -> str:+ def _collection_name_generator(self, content: Union[str, Element]) -> str:r"""Generates a valid collection name from a given file path or URL.Args:- contents (str): Local file path, remote URL or string content.+ content (Union[str, Element]): Local file path, remote URL,+ string content or Element object.Returns:str: A sanitized, valid collection name suitable for use."""+ if isinstance(content, Element):+ content = content.metadata.file_directory+# Check if the content is URLparsed_url = urlparse(content)is_url = all([parsed_url.scheme, parsed_url.netloc])@@ -193,7 +202,7 @@ class AutoRetriever:def run_vector_retriever(self,query: str,- contents: Union[str, List[str]],+ contents: Union[str, List[str], Element, List[Element]],top_k: int = DEFAULT_TOP_K_RESULTS,similarity_threshold: float = DEFAULT_SIMILARITY_THRESHOLD,return_detailed_info: bool = False,@@ -203,8 +212,8 @@ class AutoRetriever:Args:query (str): Query string for information retriever.- contents (Union[str, List[str]]): Local file paths, remote URLs or- string contents.+ contents (Union[str, List[str], Element, List[Element]]): Local+ file paths, remote URLs, string contents or Element objects.top_k (int, optional): The number of top results to return duringretrieve. Must be a positive integer. Defaults to`DEFAULT_TOP_K_RESULTS`.@@ -230,7 +239,9 @@ class AutoRetriever:if not contents:raise ValueError("content cannot be empty.")- contents = [contents] if isinstance(contents, str) else contents+ contents = (+ [contents] if isinstance(contents, (str, Element)) else contents+ )all_retrieved_info = []for content in contents:@@ -246,6 +257,7 @@ class AutoRetriever:file_is_modified = False # initialize with a default valueif (vector_storage_instance.status().vector_count != 0+ and isinstance(content, str)and os.path.exists(content)):# Get original modified date from filediff --git a/camel/retrievers/vector_retriever.py b/camel/retrievers/vector_retriever.pyindex 082c4370e..5ba1d2ef3 100644--- a/camel/retrievers/vector_retriever.py+++ b/camel/retrievers/vector_retriever.py@@ -13,7 +13,7 @@# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========import osimport warnings-from typing import Any, Dict, List, Optional+from typing import Any, Dict, List, Optional, Unionfrom urllib.parse import urlparsefrom camel.embeddings import BaseEmbedding, OpenAIEmbedding@@ -26,6 +26,11 @@ from camel.storages import (VectorRecord,)+try:+ from unstructured.documents.elements import Element+except ImportError:+ Element = None+DEFAULT_TOP_K_RESULTS = 1DEFAULT_SIMILARITY_THRESHOLD = 0.75@@ -69,7 +74,7 @@ class VectorRetriever(BaseRetriever):def process(self,- content: str,+ content: Union[str, Element],chunk_type: str = "chunk_by_title",**kwargs: Any,) -> None:@@ -78,18 +83,22 @@ class VectorRetriever(BaseRetriever):vector storage.Args:- contents (str): Local file path, remote URL or string content.+ content (Union[str, Element]): Local file path, remote URL,+ string content or Element object.chunk_type (str): Type of chunking going to apply. Defaults to"chunk_by_title".**kwargs (Any): Additional keyword arguments for content parsing."""- # Check if the content is URL- parsed_url = urlparse(content)- is_url = all([parsed_url.scheme, parsed_url.netloc])- if is_url or os.path.exists(content):- elements = self.uio.parse_file_or_url(content, **kwargs)+ if isinstance(content, Element):+ elements = [content]else:- elements = [self.uio.create_element_from_text(text=content)]+ # Check if the content is URL+ parsed_url = urlparse(content)+ is_url = all([parsed_url.scheme, parsed_url.netloc])+ if is_url or os.path.exists(content):+ elements = self.uio.parse_file_or_url(content, **kwargs) or []+ else:+ elements = [self.uio.create_element_from_text(text=content)]if elements:chunks = self.uio.chunk_elements(chunk_type=chunk_type, elements=elements@@ -110,7 +119,12 @@ class VectorRetriever(BaseRetriever):# Prepare the payload for each vector record, includes the content# path, chunk metadata, and chunk textfor vector, chunk in zip(batch_vectors, batch_chunks):- content_path_info = {"content path": content}+ if isinstance(content, str):+ content_path_info = {"content path": content}+ elif isinstance(content, Element):+ content_path_info = {+ "content path": content.metadata.file_directory+ }chunk_metadata = {"metadata": chunk.metadata.to_dict()}chunk_text = {"text": str(chunk)}combined_dict = {diff --git a/test/retrievers/test_auto_retriever.py b/test/retrievers/test_auto_retriever.pyindex c41324d0d..21f04237c 100644--- a/test/retrievers/test_auto_retriever.py+++ b/test/retrievers/test_auto_retriever.py@@ -18,6 +18,7 @@ from unittest.mock import patchimport pytest+from camel.loaders import UnstructuredIOfrom camel.retrievers import AutoRetrieverfrom camel.storages import QdrantStoragefrom camel.types import StorageType@@ -100,3 +101,38 @@ def test_run_vector_retriever(auto_retriever):)assert "No suitable information retrieved from" in str(result_unrelated)+++def test_run_vector_retriever_with_element_input(auto_retriever):+ uio = UnstructuredIO()+ test_element = uio.create_element_from_text(+ text="""Introducing 🦀 CRAB: Cross-environment Agent Benchmark for+ Multimodal Language Model Agents++ 🦀 CRAB provides an end-to-end and easy-to-use framework to build+ multimodal agents, operate environments, and create benchmarks to evaluate+ them, featuring three key components:++ - 🔀 Cross-environment support - agents can operate tasks in 📱 Android+ and 💻 Ubuntu.+ - 🕸️ Graph evaluator - provides a fine-grain evaluation metric for agents.+ - 🤖 Task generation - composes subtasks to automatically generate tasks.++ By connecting all devices to agents, 🦀CRAB unlocks greater capabilities+ for human-like tasks than ever before.++ Use 🦀 CRAB to benchmark your multimodal agents! """,+ file_directory="https://x.com/CamelAIOrg/status/1821970132606058943",+ )++ output = auto_retriever.run_vector_retriever(+ query="CRAB provides an end-to-end and easy-to-use framework to build"+ " multimodal agents",+ contents=test_element,+ return_detailed_info=True,+ )++ assert (+ output["Retrieved Context"][0]['content path']+ == 'https://x.com/CamelAIOrg/status/1821970132606058943'+ )