🐫 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(embeddings): allow optional output dimension in OpenAI compatible embedding (#2076)
Lei Zhang6646361c8d46
diff --git a/camel/embeddings/openai_compatible_embedding.py b/camel/embeddings/openai_compatible_embedding.pyindex fc21188de..8426eaff3 100644--- a/camel/embeddings/openai_compatible_embedding.py+++ b/camel/embeddings/openai_compatible_embedding.py@@ -30,6 +30,9 @@ class OpenAICompatibleEmbedding(BaseEmbedding[str]):model_type (str): The model type to be used for text embeddings.api_key (str): The API key for authenticating with the model service.url (str): The url to the model service.+ output_dim (Optional[int]): The dimensionality of the embedding+ vectors. If None, it will be determined during the first+ embedding call."""@api_keys_required(@@ -43,9 +46,10 @@ class OpenAICompatibleEmbedding(BaseEmbedding[str]):model_type: str,api_key: Optional[str] = None,url: Optional[str] = None,+ output_dim: Optional[int] = None,) -> None:self.model_type = model_type- self.output_dim: Optional[int] = None+ self.output_dim: Optional[int] = output_dimself._api_key = api_key or os.environ.get("OPENAI_COMPATIBILITY_API_KEY"@@ -87,10 +91,14 @@ class OpenAICompatibleEmbedding(BaseEmbedding[str]):Returns:int: The dimensionality of the embedding for the current model.++ Raises:+ ValueError: If the embedding dimension cannot be determined."""if self.output_dim is None:- raise ValueError(- "Output dimension is not yet determined. Call "- "'embed_list' first."- )+ self.embed_list(["test"])++ if self.output_dim is None:+ raise ValueError("Failed to determine embedding dimension")+return self.output_dimdiff --git a/test/embeddings/test_openai_compatible_embedding.py b/test/embeddings/test_openai_compatible_embedding.pyindex 53e45e6e5..c41e4546a 100644--- a/test/embeddings/test_openai_compatible_embedding.py+++ b/test/embeddings/test_openai_compatible_embedding.py@@ -85,15 +85,24 @@ class TestOpenAICompatibleEmbedding(unittest.TestCase):def test_get_output_dim_without_embeddings(self):# Test if ValueError is raised when get_output_dim is called before- # embed_list+ # embed_list and embed_list fails to set output_dimembedding = OpenAICompatibleEmbedding("text-embedding-model", "test_api_key", "http://test-url.com")- with self.assertRaises(ValueError) as context:- embedding.get_output_dim()+ # Mock the embed_list method to simulate a failed embedding attempt+ with patch.object(embedding, 'embed_list') as mock_embed_list:+ # Configure the mock to not change output_dim (simulating failure)+ mock_embed_list.return_value = []- self.assertEqual(- str(context.exception),- "Output dimension is not yet determined. Call 'embed_list' first.",- )+ with self.assertRaises(ValueError) as context:+ embedding.get_output_dim()++ # Verify that embed_list was called+ mock_embed_list.assert_called_once_with(["test"])++ # Check the error message+ self.assertEqual(+ str(context.exception),+ "Failed to determine embedding dimension",+ )