-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
feat(kb): make knowledge base URL ingestion provider-aware (Tavily + Firecrawl) #8976
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rakshith48
wants to merge
3
commits into
AstrBotDevs:master
Choose a base branch
from
rakshith48:feat/kb-url-ingestion-provider-aware
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+295
−29
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| import pytest | ||
|
|
||
| from astrbot.core.knowledge_base.parsers import url_parser | ||
|
|
||
|
|
||
| class _FakeResponse: | ||
| def __init__(self, status: int, json_data: dict): | ||
| self.status = status | ||
| self._json_data = json_data | ||
|
|
||
| async def __aenter__(self): | ||
| return self | ||
|
|
||
| async def __aexit__(self, *args): | ||
| return False | ||
|
|
||
| async def json(self): | ||
| return self._json_data | ||
|
|
||
| async def text(self): | ||
| return "error body" | ||
|
|
||
|
|
||
| class _FakeSession: | ||
| """Captures the request and returns a canned response.""" | ||
|
|
||
| def __init__(self, response: _FakeResponse, recorder: dict): | ||
| self._response = response | ||
| self._recorder = recorder | ||
|
|
||
| async def __aenter__(self): | ||
| return self | ||
|
|
||
| async def __aexit__(self, *args): | ||
| return False | ||
|
|
||
| def post(self, url, json=None, headers=None, timeout=None): | ||
| self._recorder["url"] = url | ||
| self._recorder["json"] = json | ||
| self._recorder["headers"] = headers | ||
| return self._response | ||
|
|
||
|
|
||
| def _patch_session(monkeypatch, response: _FakeResponse) -> dict: | ||
| recorder: dict = {} | ||
|
|
||
| def fake_client_session(*args, **kwargs): | ||
| return _FakeSession(response, recorder) | ||
|
|
||
| monkeypatch.setattr(url_parser.aiohttp, "ClientSession", fake_client_session) | ||
| return recorder | ||
|
|
||
|
|
||
| def test_unsupported_provider_raises(): | ||
| with pytest.raises(ValueError): | ||
| url_parser.URLExtractor(["k"], provider="bocha") | ||
|
|
||
|
|
||
| def test_missing_keys_for_selected_provider_raises(): | ||
| # Firecrawl selected but only Tavily keys supplied. | ||
| with pytest.raises(ValueError): | ||
| url_parser.URLExtractor(["tavily-key"], provider="firecrawl") | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_tavily_extraction_hits_tavily(monkeypatch): | ||
| response = _FakeResponse(200, {"results": [{"raw_content": "tavily body"}]}) | ||
| recorder = _patch_session(monkeypatch, response) | ||
|
|
||
| content = await url_parser.extract_text_from_url( | ||
| "https://example.com", ["tavily-key"], provider="tavily" | ||
| ) | ||
|
|
||
| assert content == "tavily body" | ||
| assert recorder["url"] == "https://api.tavily.com/extract" | ||
| assert recorder["headers"]["Authorization"] == "Bearer tavily-key" | ||
| assert recorder["json"]["urls"] == ["https://example.com"] | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_firecrawl_extraction_hits_firecrawl(monkeypatch): | ||
| response = _FakeResponse(200, {"data": {"markdown": "# firecrawl body"}}) | ||
| recorder = _patch_session(monkeypatch, response) | ||
|
|
||
| content = await url_parser.extract_text_from_url( | ||
| "https://example.com", | ||
| tavily_keys=[], | ||
| provider="firecrawl", | ||
| firecrawl_keys=["firecrawl-key"], | ||
| ) | ||
|
|
||
| assert content == "# firecrawl body" | ||
| assert recorder["url"] == "https://api.firecrawl.dev/v2/scrape" | ||
| assert recorder["headers"]["Authorization"] == "Bearer firecrawl-key" | ||
| assert recorder["json"] == { | ||
| "url": "https://example.com", | ||
| "formats": ["markdown"], | ||
| "onlyMainContent": True, | ||
| } | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_firecrawl_empty_content_raises(monkeypatch): | ||
| response = _FakeResponse(200, {"data": {"markdown": ""}}) | ||
| _patch_session(monkeypatch, response) | ||
|
|
||
| with pytest.raises(ValueError): | ||
| await url_parser.extract_text_from_url( | ||
| "https://example.com", | ||
| provider="firecrawl", | ||
| firecrawl_keys=["firecrawl-key"], | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_single_string_key_is_not_split_into_chars(monkeypatch): | ||
| # Legacy configs may store a single key as a bare string; it must be | ||
| # treated as one key, not split into individual characters. | ||
| response = _FakeResponse(200, {"data": {"markdown": "body"}}) | ||
| recorder = _patch_session(monkeypatch, response) | ||
|
|
||
| await url_parser.extract_text_from_url( | ||
| "https://example.com", | ||
| provider="firecrawl", | ||
| firecrawl_keys="firecrawl-key", | ||
| ) | ||
|
|
||
| assert recorder["headers"]["Authorization"] == "Bearer firecrawl-key" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_default_provider_is_tavily_backward_compatible(monkeypatch): | ||
| response = _FakeResponse(200, {"results": [{"raw_content": "legacy body"}]}) | ||
| recorder = _patch_session(monkeypatch, response) | ||
|
|
||
| # Legacy positional call signature must keep working. | ||
| content = await url_parser.extract_text_from_url( | ||
| "https://example.com", ["tavily-key"] | ||
| ) | ||
|
|
||
| assert content == "legacy body" | ||
| assert recorder["url"] == "https://api.tavily.com/extract" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.