Repository navigation
feat(search): add TinyFish as search provider #30158
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
Closed
simantak-dabhade
wants to merge
6
commits into
BerriAI:litellm_oss_staging_120626
from
simantak-dabhade:feat/add-tinyfish-search-provider
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a5939c8
fix(thinking): handle None thinking param in is_thinking_enabled (#28…
Terrajlz 2c144fc
feat(helm): support tpl rendering in podAnnotations (#28609)
devauxbr a4bb402
Forward custom_llm_provider through the Responses API bridge (Fixes #…
Cyberfilo 3aa5376
feat(search): add TinyFish as search provider
simantak-dabhade 1ba5baf
fix(search/tinyfish): handle max_results client-side and fix include_…
simantak-dabhade 00a7a43
fix(search/tinyfish): clamp max_results lower bound in transform_sear…
simantak-dabhade 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
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,3 @@ | ||
| from litellm.llms.tinyfish.search.transformation import TinyfishSearchConfig | ||
|
|
||
| __all__ = ["TinyfishSearchConfig"] |
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,141 @@ | ||
| """ | ||
| TinyFish Search API. | ||
| Endpoint: GET https://api.search.tinyfish.ai | ||
| Docs: https://docs.tinyfish.ai/search-api | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Dict, List, Literal, Optional, TypedDict, Union | ||
| from urllib.parse import urlencode | ||
|
|
||
| import httpx | ||
|
|
||
| from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj | ||
| from litellm.llms.base_llm.search.transformation import ( | ||
| BaseSearchConfig, | ||
| SearchResponse, | ||
| SearchResult, | ||
| ) | ||
| from litellm.secret_managers.main import get_secret_str | ||
|
|
||
|
|
||
| class _TinyfishSearchRequestRequired(TypedDict): | ||
| query: str | ||
|
|
||
|
|
||
| class TinyfishSearchRequest(_TinyfishSearchRequestRequired, total=False): | ||
| location: str | ||
| language: str | ||
| page: int | ||
| include_thumbnail: bool | ||
| max_results: int | ||
|
|
||
|
|
||
| class TinyfishSearchConfig(BaseSearchConfig): | ||
| TINYFISH_API_BASE = "https://api.search.tinyfish.ai" | ||
|
|
||
| @staticmethod | ||
| def ui_friendly_name() -> str: | ||
| return "TinyFish" | ||
|
|
||
| def get_http_method(self) -> Literal["GET", "POST"]: | ||
| return "GET" | ||
|
|
||
| def validate_environment( | ||
| self, | ||
| headers: Dict, | ||
| api_key: Optional[str] = None, | ||
| api_base: Optional[str] = None, | ||
| **kwargs, | ||
| ) -> Dict: | ||
| api_key = api_key or get_secret_str("TINYFISH_API_KEY") | ||
| if not api_key: | ||
| raise ValueError( | ||
| "TINYFISH_API_KEY is not set. Set `TINYFISH_API_KEY` environment variable." | ||
| ) | ||
| headers["X-API-Key"] = api_key | ||
| headers["Accept"] = "application/json" | ||
| return headers | ||
|
|
||
| def get_complete_url( | ||
| self, | ||
| api_base: Optional[str], | ||
| optional_params: dict, | ||
| data: Optional[Union[Dict, List[Dict]]] = None, | ||
| **kwargs, | ||
| ) -> str: | ||
| api_base = ( | ||
| api_base or get_secret_str("TINYFISH_API_BASE") or self.TINYFISH_API_BASE | ||
| ) | ||
| if data and isinstance(data, dict) and "_tinyfish_params" in data: | ||
| query_string = urlencode(data["_tinyfish_params"], doseq=True) | ||
| return f"{api_base}?{query_string}" | ||
| return api_base | ||
|
|
||
| def transform_search_request( | ||
| self, | ||
| query: Union[str, List[str]], | ||
| optional_params: dict, | ||
| **kwargs, | ||
| ) -> Dict: | ||
| if isinstance(query, list): | ||
| query = " ".join(query) | ||
|
|
||
| request_data: TinyfishSearchRequest = {"query": query} | ||
|
|
||
| if "country" in optional_params: | ||
| request_data["location"] = optional_params["country"] | ||
|
|
||
| if "max_results" in optional_params: | ||
| request_data["max_results"] = max( | ||
| 1, min(int(optional_params["max_results"]), 20) | ||
| ) | ||
|
|
||
| if "search_domain_filter" in optional_params: | ||
| domains = optional_params["search_domain_filter"] | ||
| if isinstance(domains, list) and len(domains) > 0: | ||
| request_data["query"] = self._append_domain_filters( | ||
| request_data["query"], domains | ||
| ) | ||
|
|
||
| result_data = dict(request_data) | ||
|
|
||
| for param, value in optional_params.items(): | ||
| if ( | ||
| param not in self.get_supported_perplexity_optional_params() | ||
| and param not in result_data | ||
| ): | ||
| result_data[param] = value | ||
|
|
||
| return {"_tinyfish_params": result_data} | ||
|
|
||
| @staticmethod | ||
| def _append_domain_filters(query: str, domains: List[str]) -> str: | ||
| domain_clauses = " OR ".join(f"site:{d}" for d in domains) | ||
| return f"({query}) ({domain_clauses})" | ||
|
|
||
| def transform_search_response( | ||
| self, | ||
| raw_response: httpx.Response, | ||
| logging_obj: Optional[LiteLLMLoggingObj], | ||
| **kwargs, | ||
| ) -> SearchResponse: | ||
| response_json = raw_response.json() | ||
|
|
||
| query_params = raw_response.request.url.params if raw_response.request else {} | ||
| max_results = max(1, min(int(query_params.get("max_results", 20)), 20)) | ||
|
|
||
| results: List[SearchResult] = [] | ||
| for item in response_json.get("results", []): | ||
| if len(results) >= max_results: | ||
| break | ||
| results.append( | ||
| SearchResult( | ||
| title=item.get("title", ""), | ||
| url=item.get("url", ""), | ||
| snippet=item.get("snippet", ""), | ||
| ) | ||
| ) | ||
|
|
||
| return SearchResponse(results=results, object="search") | ||
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
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 |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |
| "duckduckgo", | ||
| "searchapi", | ||
| "serper", | ||
| "tinyfish", | ||
| ] | ||
|
|
||
| ALLOWED_FILES_IN_LLMS_FOLDER = [ | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
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.