-
Notifications
You must be signed in to change notification settings - Fork 129
Add Python Equivalents for Bing & Google Search Plugins in Semantic Kernel #229
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 5 commits
6738f71
8d39f17
bcbba73
19cf1f2
de14297
eb065f0
898718d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -200,9 +200,88 @@ Next we recommend looking at [Text Search Abstractions](./text-search-abstractio | |
::: zone-end | ||
::: zone pivot="programming-language-python" | ||
|
||
## Coming soon | ||
## Implementing RAG using web text search | ||
|
||
More coming soon. | ||
In the following sample code, you can choose between using Bing or Google to perform web search operations. | ||
|
||
> [!TIP] | ||
> Install the required packages using: | ||
> | ||
> `pip install semantic-kernel` | ||
> `pip install semantic-kernel-plugins-web` | ||
|
||
### Create text search instance | ||
|
||
Each sample creates a text search instance and then performs a search operation to get results for the provided query. | ||
|
||
#### Bing web search | ||
|
||
```python | ||
from semantic_kernel import Kernel | ||
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion | ||
from semantic_kernel.plugins.web import BingTextSearch | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not a valid import path --
shethaadit marked this conversation as resolved.
Show resolved
Hide resolved
|
||
from semantic_kernel.functions import KernelArguments | ||
|
||
# Create a kernel with OpenAI chat completion | ||
kernel = Kernel() | ||
kernel.add_service( | ||
OpenAIChatCompletion( | ||
shethaadit marked this conversation as resolved.
Show resolved
Hide resolved
|
||
model_id="gpt-4o", | ||
shethaadit marked this conversation as resolved.
Show resolved
Hide resolved
|
||
api_key="<Your OpenAI API Key>" | ||
shethaadit marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
) | ||
|
||
# Create a text search using Bing search | ||
text_search = BingTextSearch(api_key="<Your Bing API Key>") | ||
shethaadit marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Build a text search plugin with Bing search and add to the kernel | ||
search_plugin = text_search.create_with_search("SearchPlugin") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't have any code called |
||
kernel.plugins.add(search_plugin) | ||
shethaadit marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Invoke prompt and use text search plugin to provide grounding information | ||
query = "What is Semantic Kernel?" | ||
prompt = "{{SearchPlugin.Search $query}}. {{$query}}" | ||
arguments = KernelArguments({"query": query}) | ||
|
||
response = kernel.invoke_prompt(prompt, arguments) | ||
print(response) | ||
``` | ||
|
||
#### Google web search | ||
|
||
```python | ||
from semantic_kernel import Kernel | ||
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion | ||
from semantic_kernel.plugins.web import GoogleTextSearch | ||
shethaadit marked this conversation as resolved.
Show resolved
Hide resolved
|
||
from semantic_kernel.functions import KernelArguments | ||
|
||
# Create a kernel with OpenAI chat completion | ||
kernel = Kernel() | ||
kernel.add_service( | ||
OpenAIChatCompletion( | ||
shethaadit marked this conversation as resolved.
Show resolved
Hide resolved
|
||
model_id="gpt-4o", | ||
api_key="<Your OpenAI API Key>" | ||
) | ||
) | ||
|
||
# Create an ITextSearch instance using Google search | ||
text_search = GoogleTextSearch( | ||
shethaadit marked this conversation as resolved.
Show resolved
Hide resolved
|
||
search_engine_id="<Your Google Search Engine Id>", | ||
api_key="<Your Google API Key>" | ||
) | ||
|
||
# Build a text search plugin with Google search and add to the kernel | ||
search_plugin = text_search.create_with_search("SearchPlugin") | ||
kernel.plugins.add(search_plugin) | ||
|
||
# Invoke prompt and use text search plugin to provide grounding information | ||
query = "What is Semantic Kernel?" | ||
prompt = "{{SearchPlugin.Search $query}}. {{$query}}" | ||
arguments = KernelArguments({"query": query}) | ||
|
||
response = kernel.invoke_prompt(prompt, arguments) | ||
print(response) | ||
``` | ||
|
||
::: zone-end | ||
::: zone pivot="programming-language-java" | ||
|
Uh oh!
There was an error while loading. Please reload this page.