From 6738f7108d4e8090532d3c03b2c2fca2ecdebd04 Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Mon, 17 Mar 2025 11:57:50 -0700 Subject: [PATCH 1/6] Added Python documentation for SK Issue-10992. --- semantic-kernel/concepts/text-search/index.md | 83 ++++++++++++++++++- 1 file changed, 81 insertions(+), 2 deletions(-) diff --git a/semantic-kernel/concepts/text-search/index.md b/semantic-kernel/concepts/text-search/index.md index 81a3f1af..2122f44f 100644 --- a/semantic-kernel/concepts/text-search/index.md +++ b/semantic-kernel/concepts/text-search/index.md @@ -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 +from semantic_kernel.kernel_arguments import KernelArguments + +# Create a kernel with OpenAI chat completion +kernel = Kernel() +kernel.add_service( + OpenAIChatCompletion( + model_id="gpt-4o", + api_key="" + ) +) + +# Create a text search using Bing search +text_search = BingTextSearch(api_key="") + +# Build a text search plugin with Bing 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 the 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.kernel_arguments import KernelArguments + +# Create a kernel with OpenAI chat completion +kernel = Kernel() +kernel.add_service( + OpenAIChatCompletion( + model_id="gpt-4o", + api_key="" + ) +) + +# Create an ITextSearch instance using Google search +text_search = GoogleTextSearch( + search_engine_id="", + 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 the 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" From 8d39f1711a3e9faad58e20ae37a7a2276d72b69e Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Mon, 17 Mar 2025 17:23:26 -0700 Subject: [PATCH 2/6] Update semantic-kernel/concepts/text-search/index.md Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com> --- semantic-kernel/concepts/text-search/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/semantic-kernel/concepts/text-search/index.md b/semantic-kernel/concepts/text-search/index.md index 2122f44f..c1663b50 100644 --- a/semantic-kernel/concepts/text-search/index.md +++ b/semantic-kernel/concepts/text-search/index.md @@ -239,7 +239,7 @@ 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 the Semantic Kernel?" +query = "What is Semantic Kernel?" prompt = "{{SearchPlugin.Search $query}}. {{$query}}" arguments = KernelArguments({"query": query}) From bcbba7360ed3390c85a7912145ad34742337acc1 Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Mon, 17 Mar 2025 17:23:50 -0700 Subject: [PATCH 3/6] Update semantic-kernel/concepts/text-search/index.md Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com> --- semantic-kernel/concepts/text-search/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/semantic-kernel/concepts/text-search/index.md b/semantic-kernel/concepts/text-search/index.md index c1663b50..2c7af81c 100644 --- a/semantic-kernel/concepts/text-search/index.md +++ b/semantic-kernel/concepts/text-search/index.md @@ -220,7 +220,7 @@ Each sample creates a text search instance and then performs a search operation from semantic_kernel import Kernel from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion from semantic_kernel.plugins.web import BingTextSearch -from semantic_kernel.kernel_arguments import KernelArguments +from semantic_kernel.functions import KernelArguments # Create a kernel with OpenAI chat completion kernel = Kernel() From 19cf1f220d64feabb68c915a6e58aa86cee9fd5a Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Mon, 17 Mar 2025 17:24:06 -0700 Subject: [PATCH 4/6] Update semantic-kernel/concepts/text-search/index.md Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com> --- semantic-kernel/concepts/text-search/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/semantic-kernel/concepts/text-search/index.md b/semantic-kernel/concepts/text-search/index.md index 2c7af81c..c8179376 100644 --- a/semantic-kernel/concepts/text-search/index.md +++ b/semantic-kernel/concepts/text-search/index.md @@ -275,7 +275,7 @@ 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 the Semantic Kernel?" +query = "What is Semantic Kernel?" prompt = "{{SearchPlugin.Search $query}}. {{$query}}" arguments = KernelArguments({"query": query}) From de14297251a3f98f569b81522a779ab77b21310e Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Mon, 17 Mar 2025 17:24:24 -0700 Subject: [PATCH 5/6] Update semantic-kernel/concepts/text-search/index.md Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com> --- semantic-kernel/concepts/text-search/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/semantic-kernel/concepts/text-search/index.md b/semantic-kernel/concepts/text-search/index.md index c8179376..14532473 100644 --- a/semantic-kernel/concepts/text-search/index.md +++ b/semantic-kernel/concepts/text-search/index.md @@ -253,7 +253,7 @@ print(response) from semantic_kernel import Kernel from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion from semantic_kernel.plugins.web import GoogleTextSearch -from semantic_kernel.kernel_arguments import KernelArguments +from semantic_kernel.functions import KernelArguments # Create a kernel with OpenAI chat completion kernel = Kernel() From eb065f0c200e300622a816a083800c3013af5e69 Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Tue, 18 Mar 2025 17:49:29 -0700 Subject: [PATCH 6/6] Comments Fix. --- semantic-kernel/concepts/text-search/index.md | 95 ++++++++++--------- 1 file changed, 51 insertions(+), 44 deletions(-) diff --git a/semantic-kernel/concepts/text-search/index.md b/semantic-kernel/concepts/text-search/index.md index 2122f44f..b4eeb6d5 100644 --- a/semantic-kernel/concepts/text-search/index.md +++ b/semantic-kernel/concepts/text-search/index.md @@ -208,7 +208,6 @@ In the following sample code, you can choose between using Bing or Google to per > Install the required packages using: > > `pip install semantic-kernel` -> `pip install semantic-kernel-plugins-web` ### Create text search instance @@ -217,70 +216,78 @@ Each sample creates a text search instance and then performs a search operation #### Bing web search ```python +# Create a kernel with OpenAI chat completion from semantic_kernel import Kernel -from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion -from semantic_kernel.plugins.web import BingTextSearch -from semantic_kernel.kernel_arguments import KernelArguments +from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion +from semantic_kernel.connectors.search.bing import BingSearch +from semantic_kernel.functions import KernelArguments, KernelPlugin -# Create a kernel with OpenAI chat completion +# Initialize the Kernel kernel = Kernel() -kernel.add_service( - OpenAIChatCompletion( - model_id="gpt-4o", - api_key="" - ) + +# Add OpenAI/Azure OpenAI chat completion service +kernel.add_service(AzureChatCompletion(service_id="chat")) + +# Create a Bing Search instance (API key will be picked up from the environment) +bing_search = BingSearch() + +# Build a Bing Search plugin and add it to the kernel +search_plugin = KernelPlugin.from_text_search_with_search( + bing_search, + description="Get search results using Bing." ) -# Create a text search using Bing search -text_search = BingTextSearch(api_key="") +# Add plugin to the kernel with a specific plugin name +kernel.add_plugin(search_plugin, plugin_name="bing") -# Build a text search plugin with Bing search and add to the kernel -search_plugin = text_search.create_with_search("SearchPlugin") -kernel.plugins.add(search_plugin) +# Define query and prompt +query = "What is Semantic Kernel?" +prompt = "{{bing.search $query}}. {{$query}}" -# Invoke prompt and use text search plugin to provide grounding information -query = "What is the Semantic Kernel?" -prompt = "{{SearchPlugin.Search $query}}. {{$query}}" -arguments = KernelArguments({"query": query}) +# Execute search query using the plugin +arguments = KernelArguments(query=query) +result = kernel.invoke_prompt(prompt, arguments) -response = kernel.invoke_prompt(prompt, arguments) -print(response) +# Display result +print(result) ``` #### 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.kernel_arguments import KernelArguments +from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion +from semantic_kernel.connectors.search.google import GoogleSearch +from semantic_kernel.functions import KernelArguments, KernelPlugin -# Create a kernel with OpenAI chat completion +# Initialize the Kernel kernel = Kernel() -kernel.add_service( - OpenAIChatCompletion( - model_id="gpt-4o", - api_key="" - ) -) -# Create an ITextSearch instance using Google search -text_search = GoogleTextSearch( - search_engine_id="", - api_key="" +# Add OpenAI/Azure OpenAI chat completion service +kernel.add_service(AzureChatCompletion(service_id="chat")) + +# Create a Google Search instance (API key and Search Engine ID picked from environment) +google_search = GoogleSearch() + +# Build a Google Search plugin and add it to the kernel +search_plugin = KernelPlugin.from_text_search_with_search( + google_search, + description="Get details about Semantic Kernel concepts." ) -# 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) +# Add plugin to the kernel with a specific plugin name +kernel.add_plugin(search_plugin, plugin_name="google") + +# Define query and prompt +query = "What is Semantic Kernel?" +prompt = "{{google.search $query}}. {{$query}}" -# Invoke prompt and use text search plugin to provide grounding information -query = "What is the Semantic Kernel?" -prompt = "{{SearchPlugin.Search $query}}. {{$query}}" -arguments = KernelArguments({"query": query}) +# Execute search query using the plugin +arguments = KernelArguments(query=query) +result = kernel.invoke_prompt(prompt, arguments) -response = kernel.invoke_prompt(prompt, arguments) -print(response) +# Display result +print(result) ``` ::: zone-end