-
Notifications
You must be signed in to change notification settings - Fork 12
feat: add Tavily Extract as configurable WebFetch backend alongside Jina #5
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -355,13 +355,22 @@ def call(self, params: Union[str, dict], **kwargs) -> str: | |||||||||||||
| except ValueError as exc: | ||||||||||||||
| return f"[WebFetch] {exc}" | ||||||||||||||
|
|
||||||||||||||
| response = self.readpage_jina( | ||||||||||||||
| url, | ||||||||||||||
| start_line=start_line, | ||||||||||||||
| end_line=end_line, | ||||||||||||||
| max_chars=max_chars, | ||||||||||||||
| runtime_deadline=runtime_deadline, | ||||||||||||||
| ) | ||||||||||||||
| provider = os.getenv("WEBFETCH_PROVIDER", "jina").strip().lower() | ||||||||||||||
| if provider == "tavily": | ||||||||||||||
| response = self.readpage_tavily( | ||||||||||||||
| url, | ||||||||||||||
| start_line=start_line, | ||||||||||||||
| end_line=end_line, | ||||||||||||||
| max_chars=max_chars, | ||||||||||||||
| ) | ||||||||||||||
| else: | ||||||||||||||
| response = self.readpage_jina( | ||||||||||||||
| url, | ||||||||||||||
| start_line=start_line, | ||||||||||||||
| end_line=end_line, | ||||||||||||||
| max_chars=max_chars, | ||||||||||||||
| runtime_deadline=runtime_deadline, | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
| if visit_debug_enabled(): | ||||||||||||||
| print(f"WebFetch Length {len(response)}") | ||||||||||||||
|
|
@@ -435,6 +444,35 @@ def readpage_jina( | |||||||||||||
| max_chars=max_chars, | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
| def readpage_tavily( | ||||||||||||||
| self, | ||||||||||||||
| url: str, | ||||||||||||||
| *, | ||||||||||||||
| start_line: int = 1, | ||||||||||||||
| end_line: Optional[int] = None, | ||||||||||||||
| max_chars: int = DEFAULT_WEBFETCH_MAX_CHARS, | ||||||||||||||
| ) -> str: | ||||||||||||||
| tavily_api_key = os.getenv("TAVILY_API_KEY", "").strip() | ||||||||||||||
|
Comment on lines
+447
to
+455
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add the def readpage_tavily(
self,
url: str,
*,
start_line: int = 1,
end_line: Optional[int] = None,
max_chars: int = DEFAULT_WEBFETCH_MAX_CHARS,
runtime_deadline: Optional[float] = None,
) -> str:
remaining = self._remaining_budget_seconds(runtime_deadline)
if remaining is not None and remaining <= 0:
return "[WebFetch] Failed to read page: agent runtime limit reached."
tavily_api_key = os.getenv("TAVILY_API_KEY", "").strip() |
||||||||||||||
| if not tavily_api_key: | ||||||||||||||
| return "[WebFetch] TAVILY_API_KEY is not set." | ||||||||||||||
| try: | ||||||||||||||
| from tavily import TavilyClient | ||||||||||||||
| client = TavilyClient(api_key=tavily_api_key) | ||||||||||||||
| result = client.extract(urls=[url]) | ||||||||||||||
| except Exception as exc: | ||||||||||||||
| return f"[WebFetch] Failed to read page: {exc}" | ||||||||||||||
| results = result.get("results", []) | ||||||||||||||
| if not results or not results[0].get("raw_content"): | ||||||||||||||
| return "[WebFetch] Failed to read page: the provided webpage content could not be accessed. Please check the URL or file format." | ||||||||||||||
|
Comment on lines
+464
to
+466
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a check to ensure
Suggested change
|
||||||||||||||
| content = results[0]["raw_content"] | ||||||||||||||
| return self._format_page_content( | ||||||||||||||
| url=url, | ||||||||||||||
| content=content, | ||||||||||||||
| start_line=start_line, | ||||||||||||||
| end_line=end_line, | ||||||||||||||
| max_chars=max_chars, | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def main(argv: Optional[list[str]] = None) -> int: | ||||||||||||||
| parser = argparse.ArgumentParser(description="Run web tools directly.") | ||||||||||||||
|
|
||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pass the
runtime_deadlineparameter toreadpage_tavilyto ensure consistency with the Jina backend and respect the agent's runtime budget.