Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python: Support Bing Custom Search #6278

Merged
merged 9 commits into from
May 31, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class BingConnector(ConnectorBase):
def __init__(
self,
api_key: str | None = None,
custom_config: str | None = None,
env_file_path: str | None = None,
env_file_encoding: str | None = None,
) -> None:
Expand All @@ -28,12 +29,15 @@ def __init__(
Args:
api_key (str | None): The Bing Search API key. If provided, will override
the value in the env vars or .env file.
custom_config (str | None): The Bing Custom Search instance's unique identifier.
If provided, will override the value in the env vars or .env file.
env_file_path (str | None): The optional path to the .env file. If provided,
the settings are read from this file path location.
env_file_encoding (str | None): The optional encoding of the .env file.
"""
self._settings = BingSettings.create(
api_key=api_key,
custom_config=custom_config,
env_file_path=env_file_path,
env_file_encoding=env_file_encoding,
)
Expand All @@ -56,8 +60,14 @@ async def search(self, query: str, num_results: int = 1, offset: int = 0) -> lis
params:\nquery: {query}\nnum_results: {num_results}\noffset: {offset}"
)

_base_url = "https://api.bing.microsoft.com/v7.0/search"
_request_url = f"{_base_url}?q={urllib.parse.quote_plus(query)}&count={num_results}&offset={offset}"
_base_url = (
"https://api.bing.microsoft.com/v7.0/custom/search"
if self._custom_config
else "https://api.bing.microsoft.com/v7.0/search"
)
_request_url = f"{_base_url}?q={urllib.parse.quote_plus(query)}&count={num_results}&offset={offset}" + (
f"&customConfig={self._custom_config}" if self._custom_config else ""
)

logger.info(f"Sending GET request to {_request_url}")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ class BingSettings(KernelBaseSettings):

Optional settings for prefix 'BING_' are:
- api_key: SecretStr - The Bing API key (Env var BING_API_KEY)
- custom_config: str - The Bing Custom Search instance's unique identifier (Env var BING_CUSTOM_CONFIG)

"""

env_prefix: ClassVar[str] = "BING_"

api_key: SecretStr | None = None
custom_config: str | None = None
Loading