Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

fix: change jina and mistral embedding tests into mock (#814)

Isaac Jin86ecf7a6bfe8
diff --git a/camel/loaders/jina_url_reader.py b/camel/loaders/jina_url_reader.py
index 5416212b3..2cf3a0ed6 100644
--- a/camel/loaders/jina_url_reader.py
+++ b/camel/loaders/jina_url_reader.py
@@ -14,6 +14,7 @@
import os
from typing import Any, Optional
+from warnings import warn
from camel.types.enums import JinaReturnFormat
@@ -54,11 +55,10 @@ class JinaURLReader:
**kwargs: Any,
) -> None:
api_key = api_key or os.getenv('JINA_API_KEY')
- if api_key is None:
- print(
- "[JinaURLReader] JINA_API_KEY not set. This will result in a "
- "low rate limit of Jina URL Reader. Get API key here: "
- "https://jina.ai/reader."
+ if not api_key:
+ warn(
+ "JINA_API_KEY not set. This will result in a low rate limit "
+ "of Jina URL Reader. Get API key here: https://jina.ai/reader."
)
# if the following field not provided, it will be None
@@ -94,6 +94,6 @@ class JinaURLReader:
resp = requests.get(full_url, headers=self._headers)
resp.raise_for_status()
except Exception as e:
- raise Exception(f"Failed to read content from {url}: {e}") from e
+ raise ValueError(f"Failed to read content from {url}: {e}") from e
return resp.text
diff --git a/test/embeddings/test_mistral_embedding.py b/test/embeddings/test_mistral_embedding.py
index eb84b6917..f2472ae10 100644
--- a/test/embeddings/test_mistral_embedding.py
+++ b/test/embeddings/test_mistral_embedding.py
@@ -11,11 +11,39 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
+import os
+from unittest.mock import MagicMock, patch
+
from camel.embeddings import MistralEmbedding
-def test_openai_embedding():
- embedding_model = MistralEmbedding()
- text = "test 1."
- vector = embedding_model.embed(text)
- assert len(vector) == embedding_model.get_output_dim()
[email protected](os.environ, {"MISTRAL_API_KEY": "fake_api_key"})
+@patch('mistralai.client.MistralClient', autospec=True)
+def test_embed_list(mock_mistral_client):
+ # Set up the mock client and its return values
+ mock_client_instance = mock_mistral_client.return_value
+ mock_client_instance.embeddings.return_value.data = [
+ MagicMock(embedding=[0.1, 0.2, 0.3]),
+ MagicMock(embedding=[0.4, 0.5, 0.6]),
+ ]
+
+ # Instantiate the MistralEmbedding and call embed_list
+ embedding = MistralEmbedding()
+ result = embedding.embed_list(["text1", "text2"])
+
+ # Validate that embeddings method was called with correct parameters
+ mock_client_instance.embeddings.assert_called_once_with(
+ input=["text1", "text2"], model="mistral-embed"
+ )
+
+ # Validate the result against the mocked data
+ assert result == [[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]]
+
+
+@patch('mistralai.client.MistralClient', autospec=True)
+def test_get_output_dim(mock_mistral_client):
+ # Instantiate the MistralEmbedding with specified dimensions
+ embedding = MistralEmbedding(dimensions=256)
+
+ # Validate that get_output_dim returns the correct value
+ assert embedding.get_output_dim() == 256
diff --git a/test/loaders/test_jina_url_reader.py b/test/loaders/test_jina_url_reader.py
index 0d209b275..1377ead40 100644
--- a/test/loaders/test_jina_url_reader.py
+++ b/test/loaders/test_jina_url_reader.py
@@ -11,66 +11,61 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
-import json
+from unittest.mock import Mock, patch
import pytest
from camel.loaders import JinaURLReader
-from camel.types.enums import JinaReturnFormat
+SUCCESSFUL_RESP = """Title: Hollow Knight
-def default_reader():
- return JinaURLReader()
+URL Source: https://en.wikipedia.org/wiki/Hollow_Knight
+Published Time: 2017-03-05T15:54:50Z
-def test_read_content_default(default_reader: JinaURLReader):
- test_url = "https://en.wikipedia.org/wiki/Hollow_Knight"
- content = default_reader.read_content(test_url)
- assert "Title: Hollow Knight" in content
+Markdown Content:
+| Hollow Knight |
+| --- |
+| [![Image 1](https://upload.wikimedia.org/wikipedia/en/thumb/0/04/Hollow_Knight_first_cover_art.webp/220px-Hollow_Knight_first_cover_art.webp.png)](https://en.wikipedia.org/wiki/File:Hollow_Knight_first_cover_art.webp) |
+| [Developer(s)](https://en.wikipedia.org/wiki/Video_game_developer "Video game developer") | Team Cherry |
+| [Publisher(s)](https://en.wikipedia.org/wiki/Video_game_publisher "Video game publisher") | Team Cherry |
+| [Designer(s)](https://en.wikipedia.org/wiki/Video_game_designer "Video game designer") |
+* Ari Gibson
+* William Pellen
-def test_read_content_no_protocol(default_reader: JinaURLReader):
- test_url = "en.wikipedia.org/wiki/Hollow_Knight"
- content = default_reader.read_content(test_url)
- assert "Title: Hollow Knight" in content
+ |
+| [Programmer(s)](https://en.wikipedia.org/wiki/Video_game_programmer "Video game programmer") |
-def test_read_content_markdown():
- test_url = "https://en.wikipedia.org/wiki/Hollow_Knight"
- markdown_reader = JinaURLReader(return_format=JinaReturnFormat.MARKDOWN)
- content = markdown_reader.read_content(test_url)
- assert "_Hollow Knight_" in content
+* William Pellen
+* David Kazi
+""" # noqa: E501
+FAILED_RESP = r'''{"data":null,"cause":{"name":"TimeoutError"},"code":422,"name":"AssertionFailureError","status":42206,"message":"Failed to goto http://bad_url/: TimeoutError: Navigation timeout of 30000 ms exceeded","readableMessage":"AssertionFailureError: Failed to goto http://bad_url/: TimeoutError: Navigation timeout of 30000 ms exceeded"}
+''' # noqa: E501
-def test_read_content_html():
- test_url = "https://en.wikipedia.org/wiki/Hollow_Knight"
- html_reader = JinaURLReader(return_format=JinaReturnFormat.HTML)
- content = html_reader.read_content(test_url)
- assert "<title>Hollow Knight - Wikipedia</title>" in content
-
-def test_read_content_text():
+@patch("requests.get")
+def test_read_content_success(mock_get: Mock):
+ mock_get.return_value.text = SUCCESSFUL_RESP
+ mock_get.return_value.status_code = 200
+ reader = JinaURLReader()
test_url = "https://en.wikipedia.org/wiki/Hollow_Knight"
- text_reader = JinaURLReader(return_format=JinaReturnFormat.TEXT)
- content = text_reader.read_content(test_url)
- assert "Hollow Knight" in content
+ content = reader.read_content(test_url)
+ assert "Title: Hollow Knight" in content
-def test_read_content_json():
- test_url = "https://en.wikipedia.org/wiki/Hollow_Knight"
- json_reader = JinaURLReader(json_response=True)
- content = json_reader.read_content(test_url)
- content_json = json.loads(content)
- assert "data" in content_json
- assert "title" in content_json["data"]
- assert "Hollow Knight" in content_json["data"]["title"]
-
-
-def test_read_content_json_webpage():
- test_url = "https://httpbin.org/json"
- json_reader = JinaURLReader(return_format=JinaReturnFormat.TEXT)
- content = json_reader.read_content(test_url)
- content_json = json.loads(content)
- assert "slideshow" in content_json
- assert "slides" in content_json["slideshow"]
+@patch("requests.get")
+def test_read_content_fail(mock_get: Mock):
+ mock_get.return_value.text = FAILED_RESP
+ mock_get.return_value.status_code = 422
+ mock_get.return_value.raise_for_status.side_effect = Exception(
+ "Failed to goto http://bad_url/: TimeoutError: Navigation timeout of "
+ "30000 ms exceeded"
+ )
+
+ reader = JinaURLReader()
+ test_url = "bad_url"
+ with pytest.raises(ValueError):
+ reader.read_content(test_url)