Skip to content

Commit

Permalink
Python: Support Bing Custom Search (#6278)
Browse files Browse the repository at this point in the history
### Motivation and Context

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->
Update BingConnector to add support for Bing Custom Search, which can be
used to specify domains and webpages to search and allows custom ranking
adjustments.

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Note that API keys for Bing are separate from Bing Custom Search.

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [x] All unit tests pass, and I have added new tests where possible
- [x] I didn't break anyone 😄

---------

Co-authored-by: Evan Mattson <[email protected]>
  • Loading branch information
bochris and moonbox3 authored May 31, 2024
1 parent bf42fed commit 5f40f57
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
14 changes: 12 additions & 2 deletions python/semantic_kernel/connectors/search_engine/bing_connector.py
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

0 comments on commit 5f40f57

Please sign in to comment.