Skip to content

Commit

Permalink
addressed comments
Browse files Browse the repository at this point in the history
  • Loading branch information
eavanvalkenburg committed Nov 14, 2024
1 parent dcf31b9 commit ed6d152
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
11 changes: 11 additions & 0 deletions python/samples/concepts/search/google_text_search_as_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@
from semantic_kernel.filters.functions.function_invocation_context import FunctionInvocationContext
from semantic_kernel.functions import KernelArguments, KernelParameterMetadata, KernelPlugin

# This sample shows how to setup Google Search as a plugin in the Semantic Kernel.
# With that plugin you can do function calling to augment your chat bot capabilities.
# The plugin uses the search function of the GoogleSearch instance,
# which returns only the snippet of the search results.
# It also shows how the Parameters of the function can be used to pass arguments to the plugin,
# this is shown with the siteSearch parameter.
# The LLM can choose to override that but it will take the default value otherwise.
# You can also set this up with the 'get_search_results', this returns a object with the full results of the search
# and then you can add a `string_mapper` to the function to return the desired string of information
# that you want to pass to the LLM.

kernel = Kernel()
kernel.add_service(OpenAIChatCompletion(service_id="chat"))
kernel.add_plugin(
Expand Down
16 changes: 8 additions & 8 deletions python/semantic_kernel/connectors/search/google/google_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def __init__(
"""Initializes a new instance of the Google Search class.
Args:
api_key: The Bing Search API key. If provided, will override
api_key: The Google Search API key. If provided, will override
the value in the env vars or .env file.
search_engine_id: The Google search engine ID.
If provided, will override the value in the env vars or .env file.
Expand All @@ -55,13 +55,13 @@ def __init__(
"""
try:
settings = GoogleSearchSettings.create(
search_api_key=api_key,
search_engine_id=search_engine_id,
api_key=api_key,
engine_id=search_engine_id,
env_file_path=env_file_path,
env_file_encoding=env_file_encoding,
)
except ValidationError as ex:
raise ServiceInitializationError("Failed to create Bing settings.") from ex
raise ServiceInitializationError("Failed to create Google settings.") from ex

super().__init__(settings=settings)

Expand Down Expand Up @@ -174,13 +174,13 @@ async def _inner_search(self, query: str, options: TextSearchOptions) -> GoogleS
raise ServiceInvalidRequestError("An unexpected error occurred while getting search results.") from ex

def _validate_options(self, options: TextSearchOptions) -> None:
if options.top >= 10:
if options.top > 10:
raise ServiceInvalidRequestError("count value must be less than or equal to 10.")

def _build_query(self, query: str, options: TextSearchOptions) -> str:
params = {
"key": self.settings.search_api_key.get_secret_value(),
"cx": self.settings.search_engine_id,
"key": self.settings.api_key.get_secret_value(),
"cx": self.settings.engine_id,
"num": options.top,
"start": options.skip,
}
Expand All @@ -190,5 +190,5 @@ def _build_query(self, query: str, options: TextSearchOptions) -> str:
if filter.field_name in QUERY_PARAMETERS:
params[filter.field_name] = quote_plus(filter.value)
elif isinstance(filter, AnyTagsEqualTo):
logger.debug("Any tag equals to filter is not supported by Google Search API.")
logger.debug("AnyTagEqualTo filter is not supported by Google Search API.")
return f"?q={quote_plus(query)}&{'&'.join(f'{k}={v}' for k, v in params.items())}"
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ class GoogleSearchSettings(KernelBaseSettings):
encoding 'utf-8'. If the settings are not found in the .env file, the settings are ignored;
however, validation will fail alerting that the settings are missing.
Required settings for prefix 'GOOGLE_' are:
- search_api_key: SecretStr - The Google Search API key (Env var GOOGLE_API_KEY)
Required settings for prefix 'GOOGLE_SEARCH_' are:
- api_key: SecretStr - The Google Search API key (Env var GOOGLE_SEARCH_API_KEY)
Optional settings for prefix 'GOOGLE_' are:
- search_engine_id: str - The Google search engine ID (Env var GOOGLE_SEARCH_ENGINE_ID)
Optional settings for prefix 'GOOGLE_SEARCH_' are:
- engine_id: str - The Google search engine ID (Env var GOOGLE_SEARCH_ENGINE_ID)
- env_file_path: str | None - if provided, the .env settings are read from this file path location
- env_file_encoding: str - if provided, the .env file encoding used. Defaults to "utf-8".
"""

env_prefix: ClassVar[str] = "GOOGLE_"
env_prefix: ClassVar[str] = "GOOGLE_SEARCH_"

search_api_key: SecretStr
search_engine_id: str | None = None
api_key: SecretStr
engine_id: str | None = None

0 comments on commit ed6d152

Please sign in to comment.