🐫 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: gemini embedding support. (#2325)
LastWhispere7fe81b8ef9d
diff --git a/camel/embeddings/__init__.py b/camel/embeddings/__init__.pyindex 420c0db20..79c464c8f 100644--- a/camel/embeddings/__init__.py+++ b/camel/embeddings/__init__.py@@ -13,6 +13,7 @@# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========from .azure_embedding import AzureEmbeddingfrom .base import BaseEmbedding+from .gemini_embedding import GeminiEmbeddingfrom .jina_embedding import JinaEmbeddingfrom .mistral_embedding import MistralEmbeddingfrom .openai_compatible_embedding import OpenAICompatibleEmbedding@@ -31,4 +32,5 @@ __all__ = ["OpenAICompatibleEmbedding","JinaEmbedding","TogetherEmbedding",+ "GeminiEmbedding",]diff --git a/camel/embeddings/gemini_embedding.py b/camel/embeddings/gemini_embedding.pynew file mode 100644index 000000000..7dbeb56a3--- /dev/null+++ b/camel/embeddings/gemini_embedding.py@@ -0,0 +1,115 @@+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+from __future__ import annotations++import os+from typing import Any, Optional++from camel.embeddings.base import BaseEmbedding+from camel.types import EmbeddingModelType, GeminiEmbeddingTaskType+from camel.utils import api_keys_required+++class GeminiEmbedding(BaseEmbedding[str]):+ r"""Provides text embedding functionalities using Google's Gemini models.++ Args:+ model_type (EmbeddingModelType, optional): The model type to be+ used for text embeddings.+ (default: :obj:`GEMINI_EMBEDDING_EXP`)+ api_key (str, optional): The API key for authenticating with the+ Gemini service. (default: :obj:`None`)+ dimensions (int, optional): The text embedding output dimensions.+ (default: :obj:`None`)+ task_type (GeminiEmbeddingTaskType, optional): The task type for which+ to optimize the embeddings. (default: :obj:`None`)++ Raises:+ RuntimeError: If an unsupported model type is specified.+ """++ @api_keys_required(+ [+ ("api_key", 'GEMINI_API_KEY'),+ ]+ )+ def __init__(+ self,+ model_type: EmbeddingModelType = (+ EmbeddingModelType.GEMINI_EMBEDDING_EXP+ ),+ api_key: Optional[str] = None,+ dimensions: Optional[int] = None,+ task_type: Optional[GeminiEmbeddingTaskType] = None,+ ) -> None:+ from google import genai++ if not model_type.is_gemini:+ raise ValueError("Invalid Gemini embedding model type.")++ self.model_type = model_type+ if dimensions is None:+ self.output_dim = model_type.output_dim+ else:+ assert isinstance(dimensions, int)+ self.output_dim = dimensions++ self._api_key = api_key or os.environ.get("GEMINI_API_KEY")+ self._task_type = task_type++ # Initialize Gemini client+ self._client = genai.Client(api_key=self._api_key)++ def embed_list(+ self,+ objs: list[str],+ **kwargs: Any,+ ) -> list[list[float]]:+ r"""Generates embeddings for the given texts.++ Args:+ objs (list[str]): The texts for which to generate the embeddings.+ **kwargs (Any): Extra kwargs passed to the embedding API.++ Returns:+ list[list[float]]: A list that represents the generated embedding+ as a list of floating-point numbers.+ """+ from google.genai import types++ # Create embedding config if task_type is specified+ embed_config = None+ if self._task_type:+ embed_config = types.EmbedContentConfig(+ task_type=self._task_type.value+ )++ # Process each text separately since Gemini API+ # expects single content item+ responses = self._client.models.embed_content(+ model=self.model_type.value,+ contents=objs, # type: ignore[arg-type]+ config=embed_config,+ **kwargs,+ )++ return [response.values for response in responses.embeddings] # type: ignore[misc,union-attr]++ def get_output_dim(self) -> int:+ r"""Returns the output dimension of the embeddings.++ Returns:+ int: The dimensionality of the embedding for the current model.+ """+ return self.output_dimdiff --git a/camel/embeddings/openai_embedding.py b/camel/embeddings/openai_embedding.pyindex a708d78a9..2bf77a749 100644--- a/camel/embeddings/openai_embedding.py+++ b/camel/embeddings/openai_embedding.py@@ -14,7 +14,7 @@from __future__ import annotationsimport os-from typing import Any+from typing import Any, Optional, Unionfrom openai import OpenAI@@ -27,15 +27,15 @@ class OpenAIEmbedding(BaseEmbedding[str]):r"""Provides text embedding functionalities using OpenAI's models.Args:- model_type (EmbeddingModelType, optional): The model type to be- used for text embeddings.+ model_type (EmbeddingModelType): The model type to be used for text+ embeddings.(default: :obj:`TEXT_EMBEDDING_3_SMALL`)- url (Optional[str], optional): The url to the OpenAI service.+ url (Optional[str]): The url to the OpenAI service.(default: :obj:`None`)- api_key (str, optional): The API key for authenticating with the- OpenAI service. (default: :obj:`None`)- dimensions (int, optional): The text embedding output dimensions.- (default: :obj:`NOT_GIVEN`)+ api_key (Optional[str]): The API key for authenticating with+ the OpenAI service. (default: :obj:`None`)+ dimensions (Union[int, NotGiven]): The text embedding output+ dimensions. (default: :obj:`NOT_GIVEN`)Raises:RuntimeError: If an unsupported model type is specified.@@ -51,9 +51,9 @@ class OpenAIEmbedding(BaseEmbedding[str]):model_type: EmbeddingModelType = (EmbeddingModelType.TEXT_EMBEDDING_3_SMALL),- url: str | None = None,- api_key: str | None = None,- dimensions: int | NotGiven = NOT_GIVEN,+ url: Optional[str] = None,+ api_key: Optional[str] = None,+ dimensions: Union[int, NotGiven] = NOT_GIVEN,) -> None:if not model_type.is_openai:raise ValueError("Invalid OpenAI embedding model type.")diff --git a/camel/embeddings/together_embedding.py b/camel/embeddings/together_embedding.pyindex 4880a66fb..e95f3529d 100644--- a/camel/embeddings/together_embedding.py+++ b/camel/embeddings/together_embedding.py@@ -27,7 +27,7 @@ class TogetherEmbedding(BaseEmbedding[str]):r"""Provides text embedding functionalities using Together AI's models.Args:- model_name (str, optional): The model name to be used for text+ model_type (str, optional): The model name to be used for textembeddings.(default: :obj:`togethercomputer/m2-bert-80M-8k-retrieval`)api_key (str, optional): The API key for authenticating with the@@ -44,17 +44,17 @@ class TogetherEmbedding(BaseEmbedding[str]):@api_keys_required([("api_key", 'TOGETHER_API_KEY')])def __init__(self,- model_name: str = "togethercomputer/m2-bert-80M-8k-retrieval",+ model_type: str = "togethercomputer/m2-bert-80M-8k-retrieval",api_key: Optional[str] = None,dimensions: Optional[int] = None,) -> None:- if not isinstance(model_name, str) or not model_name.strip():+ if not isinstance(model_type, str) or not model_type.strip():raise ValueError("Model name must be a non-empty string")if dimensions is not None and dimensions <= 0:raise ValueError("Dimensions must be a positive integer")- self.model_name = model_name+ self.model_type = model_typeself._api_key = api_key or os.environ.get("TOGETHER_API_KEY")self.output_dim = dimensions@@ -91,7 +91,7 @@ class TogetherEmbedding(BaseEmbedding[str]):try:response = self.client.embeddings.create(input=objs,- model=self.model_name,+ model=self.model_type,**kwargs,)@@ -100,7 +100,7 @@ class TogetherEmbedding(BaseEmbedding[str]):self.output_dim = len(response.data[0].embedding)logger.debug(f"Set output dimension to {self.output_dim} for model "- f"{self.model_name}"+ f"{self.model_type}")return [data.embedding for data in response.data]@@ -130,7 +130,7 @@ class TogetherEmbedding(BaseEmbedding[str]):if self.output_dim is None:raise ValueError("Failed to determine embedding dimension for model: "- f"{self.model_name}"+ f"{self.model_type}")return self.output_dimdiff --git a/camel/types/__init__.py b/camel/types/__init__.pyindex 8a0729e2a..27d1c4902 100644--- a/camel/types/__init__.py+++ b/camel/types/__init__.py@@ -14,6 +14,7 @@from .enums import (AudioModelType,EmbeddingModelType,+ GeminiEmbeddingTaskType,HuggingFaceRepoType,ModelPlatformType,ModelType,@@ -75,6 +76,7 @@ __all__ = ['UnifiedModelType','ParsedChatCompletion','HuggingFaceRepoType',+ 'GeminiEmbeddingTaskType','NOT_GIVEN','NotGiven',]diff --git a/camel/types/enums.py b/camel/types/enums.pyindex 9c3679858..72e856209 100644--- a/camel/types/enums.py+++ b/camel/types/enums.py@@ -1202,6 +1202,8 @@ class EmbeddingModelType(Enum):MISTRAL_EMBED = "mistral-embed"+ GEMINI_EMBEDDING_EXP = "gemini-embedding-exp-03-07"+@propertydef is_openai(self) -> bool:r"""Returns whether this type of models is an OpenAI-released model."""@@ -1230,6 +1232,13 @@ class EmbeddingModelType(Enum):EmbeddingModelType.MISTRAL_EMBED,}+ @property+ def is_gemini(self) -> bool:+ r"""Returns whether this type of models is an Gemini-released model."""+ return self in {+ EmbeddingModelType.GEMINI_EMBEDDING_EXP,+ }+@propertydef output_dim(self) -> int:if self in {@@ -1253,10 +1262,29 @@ class EmbeddingModelType(Enum):return 3072elif self is EmbeddingModelType.MISTRAL_EMBED:return 1024+ elif self is EmbeddingModelType.GEMINI_EMBEDDING_EXP:+ return 3072else:raise ValueError(f"Unknown model type {self}.")+class GeminiEmbeddingTaskType(str, Enum):+ r"""Task types for Gemini embedding models.++ For more information, please refer to:+ https://ai.google.dev/gemini-api/docs/embeddings#task-types+ """++ SEMANTIC_SIMILARITY = "SEMANTIC_SIMILARITY"+ CLASSIFICATION = "CLASSIFICATION"+ CLUSTERING = "CLUSTERING"+ RETRIEVAL_DOCUMENT = "RETRIEVAL_DOCUMENT"+ RETRIEVAL_QUERY = "RETRIEVAL_QUERY"+ QUESTION_ANSWERING = "QUESTION_ANSWERING"+ FACT_VERIFICATION = "FACT_VERIFICATION"+ CODE_RETRIEVAL_QUERY = "CODE_RETRIEVAL_QUERY"++class TaskType(Enum):AI_SOCIETY = "ai_society"CODE = "code"diff --git a/docs/key_modules/embeddings.md b/docs/key_modules/embeddings.mdindex 73d74d9ad..c70278b71 100644--- a/docs/key_modules/embeddings.md+++ b/docs/key_modules/embeddings.md@@ -109,7 +109,7 @@ embeddings = azure_openai_embedding.embed_list(["Hello, world!", "Another examplfrom camel.embeddings import TogetherEmbedding# Initialize the Together AI embedding with a specific model-together_embedding = TogetherEmbedding(model_name="togethercomputer/m2-bert-80M-8k-retrieval")+together_embedding = TogetherEmbedding(model_type="togethercomputer/m2-bert-80M-8k-retrieval")# Generate embeddings for a list of textsembeddings = together_embedding.embed_list(["Hello, world!", "Another example"])diff --git a/examples/embeddings/gemini_embedding_example.py b/examples/embeddings/gemini_embedding_example.pynew file mode 100644index 000000000..f80f84cd7--- /dev/null+++ b/examples/embeddings/gemini_embedding_example.py@@ -0,0 +1,30 @@+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========++from camel.embeddings import GeminiEmbedding+from camel.types import EmbeddingModelType++gemini_embed = GeminiEmbedding(+ model_type=EmbeddingModelType.GEMINI_EMBEDDING_EXP+)++# Embed a single text+text_embeddings = gemini_embed.embed_list(["What is the capital of France?"])++print("Default model embedding dimension:", len(text_embeddings[0]))+'''+===============================================================================+Default model embedding dimension: 3072+===============================================================================+'''diff --git a/examples/embeddings/together_embedding_example.py b/examples/embeddings/together_embedding_example.pyindex 69f20db35..9cca5eb13 100644--- a/examples/embeddings/together_embedding_example.py+++ b/examples/embeddings/together_embedding_example.py@@ -28,7 +28,7 @@ Default model embedding dimension: 768# Example with a different modeltogether_embed_alt = TogetherEmbedding(- model_name="togethercomputer/m2-bert-80M-2k-retrieval",+ model_type="togethercomputer/m2-bert-80M-2k-retrieval",)# Embed multiple textsdiff --git a/pyproject.toml b/pyproject.tomlindex 81554d09f..bf7842eac 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -78,6 +78,7 @@ rag = ["pandasai>=2.3.0,<3","crawl4ai>=0.3.745","pyobvector>=0.1.18",+ "google-genai>=1.13.0",]web_tools = ["duckduckgo-search>=6.3.5,<7",@@ -359,6 +360,7 @@ all = ["scrapegraph-py>=1.12.0,<2","daytona-sdk==0.14.0","ibm-watsonx-ai>=1.3.11",+ "google-genai>=1.13.0",][project.urls]@@ -548,6 +550,7 @@ module = ["daytona_sdk","scrapegraph_py.*","ibm_watsonx_ai.*",+ "google_genai",]ignore_missing_imports = truediff --git a/test/embeddings/test_gemini_embedding.py b/test/embeddings/test_gemini_embedding.pynew file mode 100644index 000000000..44f647f5f--- /dev/null+++ b/test/embeddings/test_gemini_embedding.py@@ -0,0 +1,101 @@+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+import os+from unittest.mock import MagicMock, patch++from camel.embeddings import GeminiEmbedding+from camel.types import GeminiEmbeddingTaskType++[email protected](os.environ, {"GEMINI_API_KEY": "fake_api_key"})+@patch('google.genai.Client.models', autospec=True)+def test_embed_list(mock_models):+ # Set up the mock models and its return values+ mock_embed_content = MagicMock()+ mock_models.embed_content = mock_embed_content++ # Create two different responses for the two texts+ mock_response = MagicMock()+ mock_embedding1 = MagicMock()+ mock_embedding1.values = [0.1, 0.2, 0.3]+ mock_embedding2 = MagicMock()+ mock_embedding2.values = [0.4, 0.5, 0.6]+ mock_response.embeddings = [mock_embedding1, mock_embedding2]++ # Set up the mock to return different responses for each call+ mock_embed_content.side_effect = [mock_response]++ # Create the embedding instance and test embed_list+ embedding = GeminiEmbedding()+ result = embedding.embed_list(["text1", "text2"])++ # Verify the calls to the mock+ assert mock_embed_content.call_count == 1++ # Check the first call+ args1, kwargs1 = mock_embed_content.call_args_list[0]+ assert kwargs1['model'] == "gemini-embedding-exp-03-07"+ assert kwargs1['contents'] == ["text1", "text2"]+ assert kwargs1['config'] is None++ # Verify the result+ assert result == [[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]]++[email protected](os.environ, {"GEMINI_API_KEY": "fake_api_key"})+@patch('google.genai.Client.models', autospec=True)+def test_embed_list_with_task_type(mock_models):+ # Set up the mock models and its return values+ mock_embed_content = MagicMock()+ mock_models.embed_content = mock_embed_content++ # Create response for the text+ mock_response = MagicMock()+ mock_embedding = MagicMock()+ mock_embedding.values = [0.1, 0.2, 0.3]+ mock_response.embeddings = [mock_embedding]++ # Set up the mock to return the response+ mock_embed_content.return_value = mock_response++ # Create the embedding instance with task type and test embed_list+ embedding = GeminiEmbedding(+ task_type=GeminiEmbeddingTaskType.SEMANTIC_SIMILARITY+ )+ result = embedding.embed_list(["text1"])++ # Verify the call to the mock includes task_type in config+ mock_embed_content.assert_called_once()+ args, kwargs = mock_embed_content.call_args+ assert kwargs['model'] == "gemini-embedding-exp-03-07"+ assert kwargs['contents'] == ["text1"]+ assert kwargs['config'] is not None++ # Verify the result+ assert result == [[0.1, 0.2, 0.3]]++[email protected](os.environ, {"GEMINI_API_KEY": "fake_api_key"})+def test_get_output_dim():+ # Instantiate the GeminiEmbedding with specified dimensions+ embedding = GeminiEmbedding(dimensions=3072)++ # Validate that get_output_dim returns the correct value+ assert embedding.get_output_dim() == 3072++ # Instantiate the GeminiEmbedding without specified dimensions+ embedding = GeminiEmbedding()++ # Validate that get_output_dim returns the correct value+ assert embedding.get_output_dim() == 3072diff --git a/uv.lock b/uv.lockindex 897b1d8f9..79a153ffa 100644--- a/uv.lock+++ b/uv.lock@@ -589,6 +589,7 @@ all = [{ name = "google-auth-httplib2" },{ name = "google-auth-oauthlib" },{ name = "google-cloud-storage" },+ { name = "google-genai" },{ name = "googlemaps" },{ name = "gradio" },{ name = "html2text" },@@ -821,6 +822,7 @@ owl = [rag = [{ name = "cohere" },{ name = "crawl4ai" },+ { name = "google-genai" },{ name = "nebula3-python" },{ name = "neo4j" },{ name = "numpy" },@@ -968,6 +970,8 @@ requires-dist = [{ name = "google-auth-oauthlib", marker = "extra == 'web-tools'", specifier = "==1.2.1" },{ name = "google-cloud-storage", marker = "extra == 'all'", specifier = ">=2.18.0,<3" },{ name = "google-cloud-storage", marker = "extra == 'storage'", specifier = ">=2.18.0,<3" },+ { name = "google-genai", marker = "extra == 'all'", specifier = ">=1.13.0" },+ { name = "google-genai", marker = "extra == 'rag'", specifier = ">=1.13.0" },{ name = "googlemaps", marker = "extra == 'all'", specifier = ">=4.10.0,<5" },{ name = "googlemaps", marker = "extra == 'web-tools'", specifier = ">=4.10.0,<5" },{ name = "gradio", marker = "extra == 'all'", specifier = ">=3,<4" },@@ -2472,6 +2476,24 @@ wheels = [{ url = "https://files.pythonhosted.org/packages/fd/3c/2a19a60a473de48717b4efb19398c3f914795b64a96cf3fbe82588044f78/google_crc32c-1.7.1-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6efb97eb4369d52593ad6f75e7e10d053cf00c48983f7a973105bc70b0ac4d82", size = 28048 },]+[[package]]+name = "google-genai"+version = "1.13.0"+source = { registry = "https://pypi.org/simple" }+dependencies = [+ { name = "anyio" },+ { name = "google-auth" },+ { name = "httpx" },+ { name = "pydantic" },+ { name = "requests" },+ { name = "typing-extensions" },+ { name = "websockets" },+]+sdist = { url = "https://files.pythonhosted.org/packages/5e/1f/0861dad47febbb391123a529f3655582e2a7ce14aadb6ddc527dd7dea470/google_genai-1.13.0.tar.gz", hash = "sha256:de7b4960d361c7ea85b715ce0e07fe34903d94affa82d566d9eb04e8f92512b9", size = 165727 }+wheels = [+ { url = "https://files.pythonhosted.org/packages/c5/2c/1833fa15311fa4f0790e9c8ea3488382fe428c3eca62b329d6aa355871ae/google_genai-1.13.0-py3-none-any.whl", hash = "sha256:e88417c95f333827ed051282803db627192fd8e5fdf5b49b6f26644a67775d1a", size = 164405 },+]+[[package]]name = "google-resumable-media"version = "2.7.2"