diff --git a/modules/connectors/connector_tavily.py b/modules/connectors/connector_tavily.py index 4f57fb94..bcb38e3f 100644 --- a/modules/connectors/connector_tavily.py +++ b/modules/connectors/connector_tavily.py @@ -1,5 +1,6 @@ """Tavily web search class.""" +import logging import os from dataclasses import dataclass from modules.interfaces.interface_web_model import ( @@ -15,6 +16,9 @@ from tavily import AsyncTavilyClient from modules.shared.timezoneUtils import get_utc_timestamp +logger = logging.getLogger(__name__) + + @dataclass class ConnectorTavily(WebSearchBase): client: AsyncTavilyClient = None @@ -47,6 +51,9 @@ class ConnectorTavily(WebSearchBase): # Perform actual API call response = await self.client.search(query=query, max_results=max_results) + + logger.info(f"Tavily API response:\n{response}") + return response["results"] def _build_action_result(self, search_results: list) -> WebSearchActionResult: diff --git a/tests/connectors/test_connector_tavily.py b/tests/connectors/test_connector_tavily.py index 54ea382c..3b23f69a 100644 --- a/tests/connectors/test_connector_tavily.py +++ b/tests/connectors/test_connector_tavily.py @@ -11,6 +11,7 @@ logger = logging.getLogger(__name__) @pytest.mark.asyncio +@pytest.mark.expensive async def test_tavily_connector_search_test_live_api(): logger.info("Testing Tavliy connector with live API calls") diff --git a/tests/fixtures/__init__.py b/tests/fixtures/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/fixtures/tavily_responses.py b/tests/fixtures/tavily_responses.py new file mode 100644 index 00000000..789f62f0 --- /dev/null +++ b/tests/fixtures/tavily_responses.py @@ -0,0 +1,47 @@ +"""Sample tavily responses for patching responses in tests.""" + +RESPONSE_HOW_OLD_IS_EARTH_NO_ANSWER = { + "query": "How old is the earth", + "follow_up_questions": None, + "answer": None, + "images": [], + "results": [ + { + "url": "https://en.wikipedia.org/wiki/Age_of_Earth", + "title": "Age of Earth - Wikipedia", + "content": 'Scientific dating of the age of Earth The **age of Earth** is estimated to be 4.54 ± 0.05 billion years. In 1862, the physicist William Thomson, 1st Baron Kelvin published calculations that fixed the age of Earth at between 20 million and 400 million years. This suggested that it might be possible to measure the age of Earth by determining the relative proportions of radioactive materials in geological samples. Holmes published *The Age of the Earth, an Introduction to Geological Ideas* in 1927 in which he presented a range of 1.6 to 3.0 billion years. "The age of the Earth and the invention of geological time".', + "score": 0.8775715, + "raw_content": None, + }, + { + "url": "https://answersingenesis.org/age-of-the-earth/how-old-earth/?srsltid=AfmBOorqG4wgNP3fQ457C11mdj7kVx0IcByShaqH3wwc1VivvrqvJnCF", + "title": "How Old Is the Earth? | Answers in Genesis", + "content": "If you ask this question of most scientifically literate people, they will answer that the earth is about 4.54 billion years old.", + "score": 0.8703443, + "raw_content": None, + }, + { + "url": "https://sites.nd.edu/james-applewhite/2020/03/22/age-of-our-earth/", + "title": "Age of Our Earth: 6000 or 4.5 billion years old? - Notre Dame Sites", + "content": "If the Earth is only 6,000 years old, why does radiometric dating techniques used by geologists suggest the age is around much older? Each technique demonstrates the earth is much older than 6,000 years old and when combined with the various different techniques of relative dating using rock strata and formations, it becomes apparent that we have solid scientific evidence that the earth is much older than what AIG thinks. With this, as they try to discount radiometric dating as evidence since we were not around back then, they invalidate their own argument as they suggest that we should accept the words of the Bible as evidence.", + "score": 0.7975099, + "raw_content": None, + }, + { + "url": "https://www.tomorrowsworld.org/magazines/2013/march-april/how-old-is-the-earth", + "title": "How Old Is the Earth? | Tomorrow's World", + "content": "Was it billions of years ago—close to the scientists' estimate of a 4.5 billion-year-old Earth? Or was it earlier or later? On these details, the Bible is", + "score": 0.78944516, + "raw_content": None, + }, + { + "url": "https://www.planetary.org/articles/how-old-is-the-earth", + "title": "How old is the Earth? | The Planetary Society", + "content": "Skip to main content Community Account Renew Search * Become A Member * Renew Back To Main Menu Learn how our members and community are changing the worlds. Back To Main Menu * ### The Planetary Report Back To Main Menu + Become A Member + Action Center + Renew Membership Back To Main Menu Back To Main Menu + Become A Member + Renew Membership * Take Action * Member Community * Account Center * Search Public Education Specialist, The Planetary Society Along with other planets, the Earth was born in the early days of the Solar System, which first started forming about 4.6 billion years ago. thanks to techniques including radiometric dating of rocks and minerals,", + "score": 0.7756902, + "raw_content": None, + }, + ], + "response_time": 0.96, + "request_id": "3c36cccd-0918-49fd-bd1c-23c62ba7ec2d", +} diff --git a/tests/methods/test_method_web.py b/tests/methods/test_method_web.py index 078d2902..9c0b7671 100644 --- a/tests/methods/test_method_web.py +++ b/tests/methods/test_method_web.py @@ -3,12 +3,15 @@ import logging import pytest +from unittest.mock import patch from modules.methods.method_web import MethodWeb +from tests.fixtures.tavily_responses import RESPONSE_HOW_OLD_IS_EARTH_NO_ANSWER logger = logging.getLogger(__name__) @pytest.mark.asyncio +@pytest.mark.expensive async def test_method_web_search_live(): """Tests method web search with live API calls.""" @@ -34,3 +37,36 @@ async def test_method_web_search_live(): logger.info(f" - Document Name: {doc.documentName}") logger.info(f" - Document Mime Type: {doc.mimeType}") logger.info(f" - Document Data: {doc.documentData}") + + +@pytest.mark.asyncio +async def test_method_web_search_dummy(): + """Tests method web search with dummy response data - no external API calls.""" + + method_web = MethodWeb(serviceCenter=None) + + # Mock the Tavily API response + with patch( + "tavily.AsyncTavilyClient.search", + return_value=RESPONSE_HOW_OLD_IS_EARTH_NO_ANSWER, + ) as mock_client: + action_result = await method_web.search( + {"query": "How old is the earth", "maxResults": 5} + ) + mock_client.assert_called_once() + + # Evaluate results + assert action_result.success + assert len(action_result.documents) > 0 + + logger.info("=" * 20) + logger.info(f"Action result success status: {action_result.success}") + logger.info(f"Action result error: {action_result.error}") + logger.info(f"Action result label: {action_result.resultLabel}") + + logger.info("Documents:") + for doc in action_result.documents: + logger.info("-" * 10) + logger.info(f" - Document Name: {doc.documentName}") + logger.info(f" - Document Mime Type: {doc.mimeType}") + logger.info(f" - Document Data: {doc.documentData}")