Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 9 additions & 78 deletions src/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion src/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ optional = true
[tool.poetry.dependencies]
python = "<3.14,>=3.10"
langchain-core = ">=1.5.0,<2.0"
langchain-community = ">=0.4.2,<0.5"
langchain-text-splitters = ">=1.1.2,<2.0"
python-dotenv = "^1.0.0"
langchain-openai = ">=1.3.5,<2.0"
Expand Down Expand Up @@ -53,6 +52,11 @@ nltk = "^3.9.1"
# optional even though nothing in sherpa_ai/ imports it directly. It was
# previously pulled in transitively by `unstructured`.
transformers = "^5.14.1"
# database/user_usage_tracker.py imports sqlalchemy directly for the usage-
# tracking DB. It was previously only a transitive dependency of
# langchain-community (via langchain-classic); dropping that package
# exposed this as a missing direct dependency.
sqlalchemy = "^2.0.51"

[tool.poetry.group.test.dependencies]
pytest = "^9.0.3"
Expand Down
33 changes: 30 additions & 3 deletions src/sherpa_ai/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
from typing import Any, List, Tuple, Union

import requests
from langchain_community.utilities import GoogleSerperAPIWrapper
from langchain_core.tools import BaseTool
from langchain_core.vectorstores import VectorStoreRetriever
from loguru import logger
from pydantic import BaseModel, Field
from typing_extensions import Literal

import sherpa_ai.config as cfg
Expand All @@ -19,6 +19,34 @@
HTTP_GET_TIMEOUT = 20.0


class GoogleSerperAPIWrapper(BaseModel):
"""Wrapper around the Serper.dev Google Search API.

Replaces langchain_community's GoogleSerperAPIWrapper, which was the
package's only remaining use of langchain-community (a project that
now prints its own "being sunset, no longer actively maintained"
deprecation warning on import).
"""

url: str = "https://google.serper.dev/search"
api_key: str = Field(default_factory=lambda: cfg.SERPER_API_KEY or "")

def search(self, query: str) -> dict:
"""Query the Serper.dev Google Search API directly."""
headers = {
"X-API-KEY": self.api_key,
"Content-Type": "application/json",
}
response = requests.post(
self.url,
headers=headers,
params={"q": query},
timeout=HTTP_GET_TIMEOUT,
)
response.raise_for_status()
return response.json()


def get_tools(memory, config):
"""Factory function to create and configure a set of tools for the agent.

Expand Down Expand Up @@ -328,8 +356,7 @@ def _run_single_query(
Link: https://example.com/python
"""
logger.debug(f"Search query: {query}")
google_serper = GoogleSerperAPIWrapper()
search_results = google_serper._google_serper_api_results(query)
search_results = GoogleSerperAPIWrapper().search(query)
logger.debug(f"Google Search Result: {search_results}")

# case 1: answerBox in the result dictionary
Expand Down
2 changes: 1 addition & 1 deletion src/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def mock_env(external_api):
os.environ["OPENAI_API_KEY"] = "dummy"

with mock.patch(
"langchain_community.utilities.GoogleSerperAPIWrapper._google_serper_api_results"
"sherpa_ai.tools.GoogleSerperAPIWrapper.search"
) as mock_search, mock.patch("sherpa_ai.utils.scrape_with_url") as mock_scrape:
mock_search.return_value = GOOGLE_SEARCH_MOCK
# mock_socket.side_effect = guard
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def test_task_agent_succeeds(get_llm, external_api): # noqa: F811
}

with mock.patch(
"langchain_community.utilities.GoogleSerperAPIWrapper._google_serper_api_results"
"sherpa_ai.tools.GoogleSerperAPIWrapper.search"
) as mock_search:
mock_search.return_value = GOOGLE_SEARCH_MOCK
result = task_agent.run()
Expand Down
30 changes: 28 additions & 2 deletions src/tests/unit_tests/tools/test_search_tool.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import re
from unittest.mock import patch
from unittest.mock import MagicMock, patch

import pytest
from loguru import logger

import sherpa_ai.config as cfg
from sherpa_ai.config import AgentConfig
from sherpa_ai.tools import SearchTool
from sherpa_ai.tools import GoogleSerperAPIWrapper, SearchTool

# Captured at collection time, before the autouse `mock_env` fixture (conftest.py)
# patches GoogleSerperAPIWrapper.search for every test.
_real_search = GoogleSerperAPIWrapper.search


def _extract_links(search_result: str) -> list:
Expand All @@ -16,6 +21,27 @@ def _extract_links(search_result: str) -> list:
return re.findall(r"Link:(\S+)", search_result)


def test_google_serper_search_calls_serper_api_directly():
"""Regression test for the langchain-community removal: this hits
Serper's API directly (no GoogleSerperAPIWrapper in between), so pin
down the request shape it relies on."""
mock_response = MagicMock()
mock_response.json.return_value = {"organic": []}

with patch.object(cfg, "SERPER_API_KEY", "test-key"), \
patch("sherpa_ai.tools.requests.post", return_value=mock_response) as mock_post:
result = _real_search(GoogleSerperAPIWrapper(), "what is the weather today?")

mock_post.assert_called_once_with(
"https://google.serper.dev/search",
headers={"X-API-KEY": "test-key", "Content-Type": "application/json"},
params={"q": "what is the weather today?"},
timeout=pytest.approx(20.0),
)
mock_response.raise_for_status.assert_called_once()
assert result == {"organic": []}


def test_formulate_search_query():
config = AgentConfig(verbose=True)
search_tool = SearchTool(config=config)
Expand Down