mirror of
https://github.com/BerriAI/litellm.git
synced 2026-12-01 01:18:34 +00:00
* refactor(typing): narrow Any sources in proxy, guardrails, bedrock and responses code Turns untyped json.loads, response.json(), header lookups and forwarded kwargs into object or precise types at their source, so downstream code type-checks without reportAny / reportExplicitAny. No runtime behavior changes. * refactor(typing): narrow Any sources in caching, MCP, providers and proxy request code Decoded JSON, cache reads and header lookups now enter as object or precise types, prisma tables used by the shadow-eval logger get typed Protocol views, and a few SDK parameters move from dict[str, Any] to Mapping[str, object]. No runtime behavior changes. * refactor(typing): narrow Any sources in proxy server, router, logging and shared utils * refactor(typing): narrow Any sources in provider transformations, integrations and proxy endpoints * refactor(typing): narrow Any sources across providers, proxy and router Deepens the Any cleanup on the hottest files: typed helpers in core_helpers for attribute and item access, identity TypeAdapter(object) wraps at untyped boundaries, explicit return annotations on shared provider and proxy helpers, and Protocols in place of Any parameters. No intended behavior change.
333 lines
11 KiB
Python
333 lines
11 KiB
Python
"""
|
|
Main Search function for LiteLLM.
|
|
"""
|
|
|
|
import asyncio
|
|
import contextvars
|
|
from collections.abc import Coroutine, Mapping
|
|
from functools import partial
|
|
from types import MappingProxyType
|
|
from typing import Final
|
|
|
|
import httpx
|
|
from pydantic import TypeAdapter
|
|
|
|
import litellm
|
|
from litellm._logging import verbose_logger
|
|
from litellm.constants import request_timeout
|
|
from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
|
|
from litellm.llms.base_llm.search.transformation import BaseSearchConfig, SearchResponse
|
|
from litellm.llms.custom_httpx.llm_http_handler import BaseLLMHTTPHandler
|
|
from litellm.types.utils import SearchProviders
|
|
from litellm.utils import ProviderConfigManager, client, filter_out_litellm_params
|
|
|
|
####### ENVIRONMENT VARIABLES ###################
|
|
base_llm_http_handler = BaseLLMHTTPHandler()
|
|
#################################################
|
|
|
|
|
|
_SEARCH_KWARGS: Final = TypeAdapter(Mapping[str, object])
|
|
|
|
|
|
def _search_pricing_params(kwargs: Mapping[str, object]) -> Mapping[str, object]:
|
|
"""Always carries the key so an unpriced fallback tool clears the price a failed primary left in the
|
|
shared logging object (`Logging.update_environment_variables` merges litellm_params)."""
|
|
return MappingProxyType({"input_cost_per_query": kwargs.get("input_cost_per_query")})
|
|
|
|
|
|
def _build_search_optional_params(
|
|
max_results: int | None = None,
|
|
search_domain_filter: list[str] | None = None,
|
|
max_tokens_per_page: int | None = None,
|
|
country: str | None = None,
|
|
) -> dict[str, object]:
|
|
"""
|
|
Helper function to build optional_params dict from Perplexity Search API parameters.
|
|
|
|
Args:
|
|
max_results: Maximum number of results (1-20)
|
|
search_domain_filter: List of domains to filter (max 20)
|
|
max_tokens_per_page: Max tokens per page
|
|
country: Country code filter
|
|
|
|
Returns:
|
|
Dict with non-None optional parameters
|
|
"""
|
|
optional_params: Final[dict[str, object]] = {}
|
|
|
|
if max_results is not None:
|
|
optional_params["max_results"] = max_results
|
|
if search_domain_filter is not None:
|
|
optional_params["search_domain_filter"] = search_domain_filter
|
|
if max_tokens_per_page is not None:
|
|
optional_params["max_tokens_per_page"] = max_tokens_per_page
|
|
if country is not None:
|
|
optional_params["country"] = country
|
|
|
|
return optional_params
|
|
|
|
|
|
@client
|
|
async def asearch(
|
|
query: str | list[str],
|
|
search_provider: str,
|
|
max_results: int | None = None,
|
|
search_domain_filter: list[str] | None = None,
|
|
max_tokens_per_page: int | None = None,
|
|
country: str | None = None,
|
|
api_key: str | None = None,
|
|
api_base: str | None = None,
|
|
timeout: float | httpx.Timeout | None = None,
|
|
extra_headers: dict[str, object] | None = None,
|
|
**kwargs,
|
|
) -> SearchResponse:
|
|
"""
|
|
Async Search function.
|
|
|
|
Args:
|
|
query: Search query (string or list of strings)
|
|
search_provider: Provider name (e.g., "perplexity")
|
|
max_results: Optional maximum number of results (1-20), default 10
|
|
search_domain_filter: Optional list of domains to filter (max 20)
|
|
max_tokens_per_page: Optional max tokens per page, default 1024
|
|
country: Optional country code filter (e.g., 'US', 'GB', 'DE')
|
|
api_key: Optional API key
|
|
api_base: Optional API base URL
|
|
timeout: Optional timeout
|
|
extra_headers: Optional extra headers
|
|
**kwargs: Additional parameters
|
|
|
|
Returns:
|
|
SearchResponse with results list following Perplexity format
|
|
|
|
Example:
|
|
```python
|
|
import litellm
|
|
|
|
# Basic search
|
|
response = await litellm.asearch(
|
|
query="latest AI developments 2024",
|
|
search_provider="perplexity"
|
|
)
|
|
|
|
# Search with options
|
|
response = await litellm.asearch(
|
|
query="AI developments",
|
|
search_provider="perplexity",
|
|
max_results=10,
|
|
search_domain_filter=["arxiv.org", "nature.com"],
|
|
max_tokens_per_page=1024,
|
|
country="US"
|
|
)
|
|
|
|
# Access results
|
|
for result in response.results:
|
|
print(f"{result.title}: {result.url}")
|
|
print(f"Snippet: {result.snippet}")
|
|
```
|
|
"""
|
|
local_vars: Final = locals()
|
|
try:
|
|
loop: Final = asyncio.get_event_loop()
|
|
kwargs["asearch"] = True
|
|
|
|
func: Final = partial(
|
|
search,
|
|
query=query,
|
|
search_provider=search_provider,
|
|
max_results=max_results,
|
|
search_domain_filter=search_domain_filter,
|
|
max_tokens_per_page=max_tokens_per_page,
|
|
country=country,
|
|
api_key=api_key,
|
|
api_base=api_base,
|
|
timeout=timeout,
|
|
extra_headers=extra_headers,
|
|
**kwargs,
|
|
)
|
|
|
|
ctx: Final = contextvars.copy_context()
|
|
func_with_context: Final = partial(ctx.run, func)
|
|
init_response: Final = await loop.run_in_executor(None, func_with_context)
|
|
|
|
if asyncio.iscoroutine(init_response):
|
|
response = await init_response
|
|
else:
|
|
response = init_response
|
|
|
|
if response is None:
|
|
raise ValueError(f"Got an unexpected None response from the Search API: {response}")
|
|
|
|
return response
|
|
except Exception as e:
|
|
model_name: Final = f"{search_provider}/search"
|
|
raise litellm.exception_type(
|
|
model=model_name,
|
|
custom_llm_provider=search_provider,
|
|
original_exception=e,
|
|
completion_kwargs=local_vars,
|
|
extra_kwargs=kwargs,
|
|
)
|
|
|
|
|
|
@client
|
|
def search(
|
|
query: str | list[str],
|
|
search_provider: str,
|
|
max_results: int | None = None,
|
|
search_domain_filter: list[str] | None = None,
|
|
max_tokens_per_page: int | None = None,
|
|
country: str | None = None,
|
|
api_key: str | None = None,
|
|
api_base: str | None = None,
|
|
timeout: float | httpx.Timeout | None = None,
|
|
extra_headers: dict[str, object] | None = None,
|
|
**kwargs,
|
|
) -> SearchResponse | Coroutine[object, object, SearchResponse]:
|
|
"""
|
|
Synchronous Search function.
|
|
|
|
Args:
|
|
query: Search query (string or list of strings)
|
|
search_provider: Provider name (e.g., "perplexity")
|
|
max_results: Optional maximum number of results (1-20), default 10
|
|
search_domain_filter: Optional list of domains to filter (max 20)
|
|
max_tokens_per_page: Optional max tokens per page, default 1024
|
|
country: Optional country code filter (e.g., 'US', 'GB', 'DE')
|
|
api_key: Optional API key
|
|
api_base: Optional API base URL
|
|
timeout: Optional timeout
|
|
extra_headers: Optional extra headers
|
|
**kwargs: Additional parameters
|
|
|
|
Returns:
|
|
SearchResponse with results list following Perplexity format
|
|
|
|
Example:
|
|
```python
|
|
import litellm
|
|
|
|
# Basic search
|
|
response = litellm.search(
|
|
query="latest AI developments 2024",
|
|
search_provider="perplexity"
|
|
)
|
|
|
|
# Search with options
|
|
response = litellm.search(
|
|
query="AI developments",
|
|
search_provider="perplexity",
|
|
max_results=10,
|
|
search_domain_filter=["arxiv.org", "nature.com"],
|
|
max_tokens_per_page=1024,
|
|
country="US"
|
|
)
|
|
|
|
# Multi-query search
|
|
response = litellm.search(
|
|
query=["AI developments", "machine learning trends"],
|
|
search_provider="perplexity"
|
|
)
|
|
|
|
# Access results
|
|
for result in response.results:
|
|
print(f"{result.title}: {result.url}")
|
|
print(f"Snippet: {result.snippet}")
|
|
if result.date:
|
|
print(f"Date: {result.date}")
|
|
```
|
|
"""
|
|
local_vars: Final = locals()
|
|
try:
|
|
litellm_logging_obj: Final[LiteLLMLoggingObj] = kwargs.pop("litellm_logging_obj")
|
|
litellm_call_id: Final[str | None] = kwargs.get("litellm_call_id", None)
|
|
_is_async: Final = kwargs.pop("asearch", False) is True
|
|
|
|
# Validate query parameter
|
|
if not isinstance(query, (str, list)):
|
|
raise ValueError(f"query must be a string or list of strings, got {type(query)}")
|
|
|
|
if isinstance(query, list) and not all(isinstance(q, str) for q in query):
|
|
raise ValueError("All items in query list must be strings")
|
|
|
|
# Get provider config
|
|
search_provider_config: Final[BaseSearchConfig | None] = ProviderConfigManager.get_provider_search_config(
|
|
provider=SearchProviders(search_provider),
|
|
)
|
|
|
|
if search_provider_config is None:
|
|
raise ValueError(f"Search is not supported for provider: {search_provider}")
|
|
|
|
verbose_logger.debug("Search call - provider: %s", search_provider)
|
|
|
|
# Build optional_params from explicit parameters
|
|
optional_params: Final = _build_search_optional_params(
|
|
max_results=max_results,
|
|
search_domain_filter=search_domain_filter,
|
|
max_tokens_per_page=max_tokens_per_page,
|
|
country=country,
|
|
)
|
|
|
|
# Filter out internal LiteLLM parameters from kwargs
|
|
search_kwargs: Final = _SEARCH_KWARGS.validate_python(kwargs)
|
|
filtered_kwargs: Final = filter_out_litellm_params(kwargs=search_kwargs)
|
|
|
|
# Add remaining kwargs to optional_params (for provider-specific params)
|
|
for key, value in filtered_kwargs.items():
|
|
if key not in optional_params:
|
|
optional_params[key] = value
|
|
|
|
verbose_logger.debug("Search optional_params: %s", optional_params)
|
|
|
|
# Validate environment and get headers
|
|
headers: Final = search_provider_config.validate_environment(
|
|
api_key=api_key,
|
|
api_base=api_base,
|
|
headers=extra_headers or {},
|
|
)
|
|
|
|
# Get complete URL
|
|
complete_url: Final = search_provider_config.get_complete_url(
|
|
api_base=api_base,
|
|
optional_params=optional_params,
|
|
api_key=api_key,
|
|
)
|
|
|
|
# Pre Call logging
|
|
model_name = f"{search_provider}/search"
|
|
litellm_logging_obj.update_from_kwargs(
|
|
kwargs=kwargs,
|
|
model=model_name,
|
|
optional_params=optional_params,
|
|
litellm_params={
|
|
"litellm_call_id": litellm_call_id,
|
|
"api_base": complete_url,
|
|
**_search_pricing_params(search_kwargs),
|
|
},
|
|
custom_llm_provider=search_provider,
|
|
)
|
|
|
|
# Call the handler
|
|
response: Final = base_llm_http_handler.search(
|
|
query=query,
|
|
optional_params=optional_params,
|
|
timeout=timeout or request_timeout,
|
|
logging_obj=litellm_logging_obj,
|
|
api_key=api_key,
|
|
api_base=complete_url,
|
|
custom_llm_provider=search_provider,
|
|
asearch=_is_async,
|
|
headers=headers,
|
|
provider_config=search_provider_config,
|
|
)
|
|
|
|
return response
|
|
except Exception as e:
|
|
model_name = f"{search_provider}/search"
|
|
raise litellm.exception_type(
|
|
model=model_name,
|
|
custom_llm_provider=search_provider,
|
|
original_exception=e,
|
|
completion_kwargs=local_vars,
|
|
extra_kwargs=kwargs,
|
|
)
|