Skip to content

Commit a2da17d

Browse files
Python: google connector (microsoft#2071)
### 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. --> Implementation of Google search connector for web search skill. Fixes: microsoft#1862 Sample usage can be found in `python/samples/kernel-syntax-examples/google_search_skill.py` ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> ### 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#dev-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: Abby Harrison <54643756+awharrison-28@users.noreply.github.com> Co-authored-by: Abby Harrison <abby.harrison@microsoft.com>
1 parent 6826955 commit a2da17d

6 files changed

Lines changed: 197 additions & 2 deletions

File tree

‎python/.env.example‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ AZURE_COGNITIVE_SEARCH_ADMIN_KEY=""
88
PINECONE_API_KEY=""
99
PINECONE_ENVIRONMENT=""
1010
POSTGRES_CONNECTION_STRING=""
11+
GOOGLE_API_KEY=""
12+
GOOGLE_SEARCH_ENGINE_ID=""
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Copyright (c) Microsoft. All rights reserved.
2+
3+
import os
4+
5+
from dotenv import load_dotenv
6+
7+
import semantic_kernel as sk
8+
from semantic_kernel.connectors.ai.open_ai import OpenAITextCompletion
9+
from semantic_kernel.connectors.search_engine import GoogleConnector
10+
from semantic_kernel.core_skills import WebSearchEngineSkill
11+
12+
load_dotenv()
13+
14+
15+
async def main():
16+
kernel = sk.Kernel()
17+
api_key, org_id = sk.openai_settings_from_dot_env()
18+
kernel.add_text_completion_service(
19+
"dv", OpenAITextCompletion("text-davinci-003", api_key, org_id)
20+
)
21+
22+
"""
23+
Instantiate a Google Connector
24+
Make sure to have the following keys in a .env file or set as environment variables
25+
- GOOGLE_API_KEY
26+
- GOOGLE_SEARCH_ENGINE_ID
27+
28+
A Google Custom Search API has to be created in order to have an API key and a search engine ID.
29+
To create a Google Custom Search API, follow the guide - https://developers.google.com/custom-search/v1/overview.
30+
If you have already created the service, the credentials can be found in the Credentials tab on the page
31+
https://console.cloud.google.com/apis/api/customsearch.googleapis.com
32+
"""
33+
connector = GoogleConnector(
34+
api_key=os.getenv("GOOGLE_API_KEY"),
35+
search_engine_id=os.getenv("GOOGLE_SEARCH_ENGINE_ID"),
36+
)
37+
38+
# Import the WebSearchEngineSkill and pass the Google Connector to it.
39+
web_skill = kernel.import_skill(WebSearchEngineSkill(connector), "WebSearch")
40+
41+
# The search query
42+
prompt = "Who is Leonardo DiCaprio's current girlfriend?"
43+
search_async = web_skill["searchAsync"]
44+
45+
# By default, only one search result is provided
46+
result = await search_async.invoke_async(prompt)
47+
print(result)
48+
49+
"""
50+
Output:
51+
["Celebrity Celebrity News Everything You Need to Know About Leonardo DiCaprio and Camila Morrone's
52+
Relationship From the beginning of their romance to today, we track their relationship here. By..."]
53+
"""
54+
55+
# Following example demonstrates the use of the skill within a semantic function
56+
prompt = """
57+
Answer the question using only the data that is provided in the data section.
58+
Do not use any prior knowledge to answer the question.
59+
Data: {{WebSearch.SearchAsync "What is semantic kernel?"}}
60+
Question: What is semantic kernel?
61+
Answer:
62+
"""
63+
64+
qna = kernel.create_semantic_function(prompt, temperature=0.2)
65+
context = kernel.create_new_context()
66+
67+
"""
68+
Two context parameters can be passed to the search engine skill.
69+
- num_results controls the number of results returned by the web search.
70+
- offset controls the number of results to omit.
71+
"""
72+
context["num_results"] = "10"
73+
context["offset"] = "0"
74+
75+
result = await qna.invoke_async(context=context)
76+
print(result)
77+
78+
"""
79+
Output:
80+
Semantic Kernel is an open-source SDK that lets you easily combine AI services like OpenAI,
81+
Azure OpenAI, and Hugging Face with conventional programming languages like C# and Python.
82+
By doing so, you can create AI apps that combine the best of both worlds.
83+
Semantic Kernel is at the center of the copilot stack.
84+
"""
85+
86+
87+
if __name__ == "__main__":
88+
import asyncio
89+
90+
asyncio.run(main())
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Copyright (c) Microsoft. All rights reserved.
2+
13
from semantic_kernel.connectors.search_engine.bing_connector import BingConnector
4+
from semantic_kernel.connectors.search_engine.google_connector import GoogleConnector
25

3-
__all__ = ["BingConnector"]
6+
__all__ = ["BingConnector", "GoogleConnector"]

‎python/semantic_kernel/connectors/search_engine/bing_connector.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Copyright (c) Microsoft. All rights reserved.
2+
13
import urllib
24
from logging import Logger
35
from typing import List, Optional
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
# Copyright (c) Microsoft. All rights reserved.
2+
3+
from typing import List
4+
5+
16
class ConnectorBase:
27
"""
38
Base class for search engine connectors
49
"""
510

6-
def search_async(self, query: str, num_results: str, offset: str) -> str:
11+
def search_async(self, query: str, num_results: str, offset: str) -> List[str]:
712
pass
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Copyright (c) Microsoft. All rights reserved.
2+
3+
import urllib
4+
from logging import Logger
5+
from typing import List, Optional
6+
7+
import aiohttp
8+
9+
from semantic_kernel.connectors.search_engine.connector import ConnectorBase
10+
from semantic_kernel.utils.null_logger import NullLogger
11+
12+
13+
class GoogleConnector(ConnectorBase):
14+
"""
15+
A search engine connector that uses the Google Custom Search API to perform a web search.
16+
"""
17+
18+
_api_key: str
19+
_search_engine_id: str
20+
_logger: Logger
21+
22+
def __init__(
23+
self, api_key: str, search_engine_id: str, logger: Optional[Logger] = None
24+
) -> None:
25+
self._api_key = api_key
26+
self._search_engine_id = search_engine_id
27+
self._logger = logger if logger else NullLogger()
28+
29+
if not self._api_key:
30+
raise ValueError("Google Custom Search API key cannot be null.")
31+
32+
if not self._search_engine_id:
33+
raise ValueError("Google search engine ID cannot be null.")
34+
35+
async def search_async(
36+
self, query: str, num_results: str, offset: str
37+
) -> List[str]:
38+
"""
39+
Returns the search results of the query provided by pinging the Google Custom search API.
40+
Returns `num_results` results and ignores the first `offset`.
41+
42+
:param query: search query
43+
:param num_results: the number of search results to return
44+
:param offset: the number of search results to ignore
45+
:return: list of search results
46+
"""
47+
if not query:
48+
raise ValueError("query cannot be 'None' or empty.")
49+
50+
if not num_results:
51+
num_results = 1
52+
if not offset:
53+
offset = 0
54+
55+
num_results = int(num_results)
56+
offset = int(offset)
57+
58+
if num_results <= 0:
59+
raise ValueError("num_results value must be greater than 0.")
60+
if num_results > 10:
61+
raise ValueError("num_results value must be less than or equal to 10.")
62+
63+
if offset < 0:
64+
raise ValueError("offset must be greater than 0.")
65+
66+
self._logger.info(
67+
f"Received request for google search with \
68+
params:\nquery: {query}\nnum_results: {num_results}\noffset: {offset}"
69+
)
70+
71+
_base_url = "https://www.googleapis.com/customsearch/v1"
72+
_request_url = (
73+
f"{_base_url}?q={urllib.parse.quote_plus(query)}"
74+
f"&key={self._api_key}&cx={self._search_engine_id}"
75+
f"&num={num_results}&start={offset}"
76+
)
77+
78+
self._logger.info("Sending GET request to Google Search API.")
79+
80+
async with aiohttp.ClientSession() as session:
81+
async with session.get(_request_url, raise_for_status=True) as response:
82+
if response.status == 200:
83+
data = await response.json()
84+
self._logger.info("Request successful.")
85+
self._logger.info(f"API Response: {data}")
86+
items = data["items"]
87+
result = [x["snippet"] for x in items]
88+
return result
89+
else:
90+
self._logger.error(
91+
f"Request to Google Search API failed with status code: {response.status}."
92+
)
93+
return []

0 commit comments

Comments
 (0)