feat(chat): add Querit web search provider (#17813)

This commit is contained in:
EthanZhang
2026-08-05 09:54:46 +08:00
committed by GitHub
parent 4d68e154ce
commit bdcd8aadde
32 changed files with 1253 additions and 134 deletions

View File

@@ -1668,6 +1668,25 @@ def test_chatbot_routes_auth_stream_nonstream_unit(monkeypatch):
assert res["data"]["avatar"] == "avatar.png"
assert res["data"]["prologue"] == "Hello!"
assert res["data"]["has_tavily_key"] is True
assert res["data"]["has_web_search_provider"] is True
# Explicit Querit configuration also enables the provider-neutral flag.
querit_dialog = SimpleNamespace(
name="My Querit Bot",
icon="avatar.png",
tenant_id="tenant-1",
status="1",
llm_id="",
prompt_config={
"prologue": "Hello!",
"web_search_provider": "querit",
"querit_api_key": "querit-key123",
},
)
monkeypatch.setattr(module.DialogService, "get_by_id", lambda _dialog_id: (True, querit_dialog))
res = _run(inspect.unwrap(module.chatbots_inputs)("dialog-querit"))
assert res["code"] == 0
assert res["data"]["has_web_search_provider"] is True
@pytest.mark.p2

View File

@@ -0,0 +1,123 @@
#
# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from rag.utils import querit_conn
class _Response:
status_code = 200
def raise_for_status(self):
return None
def json(self):
return {
"results": {
"result": [
{
"title": "RAGFlow",
"url": "https://example.com/ragflow",
"snippet": "RAGFlow is an open-source RAG engine.",
}
]
}
}
def test_querit_search_uses_chat_defaults_and_normalizes_results(monkeypatch):
request = {}
def fake_post(url, *, headers, json, timeout):
request.update(url=url, headers=headers, json=json, timeout=timeout)
return _Response()
monkeypatch.setattr(querit_conn.requests, "post", fake_post)
results = querit_conn.Querit("querit-test").search("What is RAGFlow?")
assert request["url"] == "https://api.querit.ai/v1/search"
assert request["headers"]["Authorization"] == "Bearer querit-test"
assert request["json"] == {
"query": "What is RAGFlow?",
"count": 6,
"chunksPerDoc": 1,
}
assert results == [
{
"url": "https://example.com/ragflow",
"title": "RAGFlow",
"content": "RAGFlow is an open-source RAG engine.",
"score": 1.0,
}
]
def test_querit_retrieve_chunks_returns_ragflow_reference_shape(monkeypatch):
monkeypatch.setattr(
querit_conn.Querit,
"search",
lambda _self, _question: [
{
"url": "https://example.com/ragflow",
"title": "RAGFlow",
"content": "RAGFlow is an open-source RAG engine.",
"score": 1.0,
}
],
)
monkeypatch.setattr(querit_conn, "get_uuid", lambda: "chunk-1")
monkeypatch.setattr(querit_conn.rag_tokenizer, "tokenize", lambda content: f"tokens:{content}")
result = querit_conn.Querit("querit-test").retrieve_chunks("What is RAGFlow?")
assert result["chunks"] == [
{
"chunk_id": "chunk-1",
"content_ltks": "tokens:RAGFlow is an open-source RAG engine.",
"content_with_weight": "RAGFlow is an open-source RAG engine.",
"doc_id": "chunk-1",
"docnm_kwd": "RAGFlow",
"kb_id": [],
"important_kwd": [],
"image_id": "",
"similarity": 1.0,
"vector_similarity": 1.0,
"term_similarity": 0,
"vector": [],
"positions": [],
"url": "https://example.com/ragflow",
}
]
assert result["doc_aggs"] == [
{
"doc_name": "RAGFlow",
"doc_id": "chunk-1",
"count": 1,
"url": "https://example.com/ragflow",
}
]
def test_querit_search_redacts_api_key_from_failures(monkeypatch, caplog):
class _FailedResponse:
def raise_for_status(self):
raise ValueError("request failed with querit-secret")
monkeypatch.setattr(querit_conn.requests, "post", lambda *_args, **_kwargs: _FailedResponse())
assert querit_conn.Querit("querit-secret").search("RAGFlow") == []
assert "querit-secret" not in caplog.text
assert "[REDACTED]" in caplog.text

View File

@@ -0,0 +1,101 @@
#
# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from rag.utils import web_search_conn
def test_create_web_search_provider_uses_existing_tavily_config_without_provider_field(monkeypatch):
created_with = []
provider = object()
monkeypatch.setattr(web_search_conn, "Tavily", lambda api_key: created_with.append(api_key) or provider)
result = web_search_conn.create_web_search_provider({"tavily_api_key": "tvly-test"})
assert result is provider
assert created_with == ["tvly-test"]
def test_create_web_search_provider_uses_selected_querit_config(monkeypatch):
created_with = []
provider = object()
monkeypatch.setattr(web_search_conn, "Querit", lambda api_key: created_with.append(api_key) or provider)
result = web_search_conn.create_web_search_provider(
{
"web_search_provider": "querit",
"querit_api_key": "querit-test",
"tavily_api_key": "tvly-test",
}
)
assert result is provider
assert created_with == ["querit-test"]
def test_create_web_search_provider_trims_selected_key(monkeypatch):
created_with = []
provider = object()
monkeypatch.setattr(web_search_conn, "Querit", lambda api_key: created_with.append(api_key) or provider)
result = web_search_conn.create_web_search_provider(
{
"web_search_provider": "querit",
"querit_api_key": " querit-test ",
}
)
assert result is provider
assert created_with == ["querit-test"]
def test_create_web_search_provider_requires_key_for_selected_provider():
assert web_search_conn.create_web_search_provider({}) is None
assert web_search_conn.create_web_search_provider(None) is None
assert web_search_conn.create_web_search_provider({"web_search_provider": "tavily"}) is None
assert web_search_conn.create_web_search_provider({"web_search_provider": "querit"}) is None
assert web_search_conn.create_web_search_provider({"tavily_api_key": " "}) is None
assert (
web_search_conn.create_web_search_provider(
{
"web_search_provider": "querit",
"querit_api_key": " ",
}
)
is None
)
def test_has_web_search_provider_follows_selected_provider():
assert web_search_conn.has_web_search_provider({"tavily_api_key": "tvly-test"})
assert not web_search_conn.has_web_search_provider({"tavily_api_key": ""})
assert web_search_conn.has_web_search_provider({"web_search_provider": "querit", "querit_api_key": "querit-test"})
assert not web_search_conn.has_web_search_provider(
{
"web_search_provider": "querit",
"querit_api_key": "",
"tavily_api_key": "tvly-test",
}
)
assert not web_search_conn.has_web_search_provider(
{
"web_search_provider": "unsupported",
"querit_api_key": "querit-test",
"tavily_api_key": "tvly-test",
}
)