Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(agent/tools): encode GitHub and QWeather query parameters (#19584)

MarMar Labs1df730a34579
diff --git a/agent/tools/github.py b/agent/tools/github.py
index 022c6cf338..cd14aace57 100644
--- a/agent/tools/github.py
+++ b/agent/tools/github.py
@@ -69,9 +69,10 @@ class GitHub(ToolBase, ABC):
return
try:
- url = "https://api.github.com/search/repositories?q=" + kwargs["query"] + "&sort=stars&order=desc&per_page=" + str(self._param.top_n)
+ url = "https://api.github.com/search/repositories"
+ params = {"q": kwargs["query"], "sort": "stars", "order": "desc", "per_page": self._param.top_n}
headers = {"Content-Type": "application/vnd.github+json", "X-GitHub-Api-Version": "2022-11-28"}
- response = requests.get(url=url, headers=headers, timeout=DEFAULT_TIMEOUT).json()
+ response = requests.get(url=url, params=params, headers=headers, timeout=DEFAULT_TIMEOUT).json()
if self.check_if_canceled("GitHub processing"):
return
diff --git a/agent/tools/qweather.py b/agent/tools/qweather.py
index fe7d2a7dde..f2f779c037 100644
--- a/agent/tools/qweather.py
+++ b/agent/tools/qweather.py
@@ -128,7 +128,8 @@ class QWeather(ToolBase, ABC):
try:
lookup = requests.get(
- url="https://geoapi.qweather.com/v2/city/lookup?location=" + location + "&key=" + self._param.web_apikey,
+ url="https://geoapi.qweather.com/v2/city/lookup",
+ params={"location": location, "key": self._param.web_apikey},
timeout=DEFAULT_TIMEOUT,
).json()
diff --git a/test/unit_test/agent/component/test_qweather.py b/test/unit_test/agent/component/test_qweather.py
index 14b9ebdb6e..cac9bda0b9 100644
--- a/test/unit_test/agent/component/test_qweather.py
+++ b/test/unit_test/agent/component/test_qweather.py
@@ -14,7 +14,10 @@
# limitations under the License.
#
+from urllib.parse import parse_qs, urlsplit
+
import pytest
+import requests
from agent.tools import qweather as qweather_mod
from agent.tools.qweather import QWeather, QWeatherParam
@@ -113,3 +116,29 @@ def test_invoke_location_lookup_error_returns_message(monkeypatch):
assert res.startswith("**Error**")
assert "does not exist" in res
assert out["formalized_content"] == res
+
+
+def test_invoke_keeps_location_intact_on_the_wire(monkeypatch):
+ # Regression: the geo lookup concatenated the raw location into the URL, so a
+ # place name containing "&" split into a second parameter and QWeather looked
+ # up a truncated name.
+ calls = []
+ payloads = iter(
+ [
+ {"code": "200", "location": [{"id": "101010100"}]},
+ {"code": "200", "now": {"temp": "25", "text": "Sunny"}},
+ ]
+ )
+
+ def fake_get(*args, **kwargs):
+ calls.append(kwargs)
+ return _FakeResp(next(payloads))
+
+ monkeypatch.setattr(qweather_mod.requests, "get", fake_get)
+ tool, _ = _make_tool(type="weather", time_period="now")
+ tool._invoke(query="Washington, D.C. & Arlington")
+
+ wire = requests.Request("GET", calls[0]["url"], params=calls[0].get("params")).prepare().url
+ query = parse_qs(urlsplit(wire).query)
+ assert query["location"] == ["Washington, D.C. & Arlington"]
+ assert query["key"] == ["test-key"]
diff --git a/test/unit_test/agent/tools/test_github_unit.py b/test/unit_test/agent/tools/test_github_unit.py
index c1840dabf6..54aa1489fa 100644
--- a/test/unit_test/agent/tools/test_github_unit.py
+++ b/test/unit_test/agent/tools/test_github_unit.py
@@ -14,6 +14,10 @@
# limitations under the License.
#
+from urllib.parse import parse_qs, urlsplit
+
+import requests
+
import agent.tools.github as gh_module
from agent.tools.github import GitHub, GitHubParam
@@ -76,3 +80,25 @@ def test_valid_response_returns_items(monkeypatch):
g._invoke(query="anything")
assert captured["chunks"] == items
assert out["json"] == items
+
+
+def test_query_keeps_special_characters_on_the_wire(monkeypatch):
+ # Regression: the search URL was built by string concatenation, so a query such
+ # as "C# parser" ended at the "#" fragment and GitHub received q=C with the
+ # sort, order and per_page controls dropped.
+ sent = {}
+
+ def fake_get(*a, **k):
+ sent.update(k)
+ return _FakeResp({"items": []})
+
+ monkeypatch.setattr(gh_module.requests, "get", fake_get)
+ g, _, _ = _make_tool()
+ g._invoke(query="C# parser")
+
+ wire = requests.Request("GET", sent["url"], params=sent.get("params")).prepare().url
+ query = parse_qs(urlsplit(wire).query)
+ assert query["q"] == ["C# parser"]
+ assert query["sort"] == ["stars"]
+ assert query["order"] == ["desc"]
+ assert query["per_page"] == ["10"]