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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Python Equivalents for Bing & Google Search Plugins in Semantic Kernel #229

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 81 additions & 2 deletions semantic-kernel/concepts/text-search/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a valid import path -- semantic_kernel.plugins does not exist. This is valid: from semantic_kernel.connectors.search.bing import BingSearch

from semantic_kernel.functions import KernelArguments

# Create a kernel with OpenAI chat completion
kernel = Kernel()
kernel.add_service(
OpenAIChatCompletion(
model_id="gpt-4o",
api_key="<Your OpenAI API Key>"
)
)

# Create a text search using Bing search
text_search = BingTextSearch(api_key="<Your Bing API Key>")

# Build a text search plugin with Bing search and add to the kernel
search_plugin = text_search.create_with_search("SearchPlugin")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have any code called create_with_search.

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)
```

#### 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
from semantic_kernel.functions import KernelArguments

# Create a kernel with OpenAI chat completion
kernel = Kernel()
kernel.add_service(
OpenAIChatCompletion(
model_id="gpt-4o",
api_key="<Your OpenAI API Key>"
)
)

# Create an ITextSearch instance using Google search
text_search = GoogleTextSearch(
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"
Expand Down