-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from Azure/websearch
Lesson 4 - Bing Web Search plugin
- Loading branch information
Showing
23 changed files
with
368 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
docs/wksp/05-semantic-kernel-workshop/simple-ai-orchestration/lesson1.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
docs/wksp/05-semantic-kernel-workshop/simple-ai-orchestration/lesson2.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
docs/wksp/05-semantic-kernel-workshop/simple-ai-orchestration/lesson3.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
docs/wksp/05-semantic-kernel-workshop/simple-ai-orchestration/lesson4.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# Lesson 4: Semantic Kernel chatbot with Web Search engine plugin | ||
|
||
In this lesson we will add a Web Search Engine plugin that uses Bing Search to our semantic kernel chatbot. | ||
|
||
1. Ensure all [pre-requisites](pre-reqs.md) are met and installed (including updating the `BingSearchService` `apiKey` value in the `appSettings.json` file using the key from **Bing Search Service v7** in Azure (https://portal.azure.com)). | ||
|
||
1. Switch to Lesson 4 directory: | ||
|
||
```bash | ||
cd ../Lesson4 | ||
``` | ||
|
||
1. Start by copying `appsettings.json` from Lesson 1: | ||
|
||
```bash | ||
cp ../Lesson1/appsettings.json . | ||
``` | ||
|
||
1. Run program and ask what the sentiment on Microsoft stock is: | ||
|
||
```bash | ||
dotnet run | ||
``` | ||
|
||
At the prompt enter | ||
|
||
```bash | ||
What is the sentiment on Microsoft stock? | ||
``` | ||
|
||
Assistant will give a generic response: | ||
|
||
```txt | ||
Assistant > The sentiment on Microsoft (ticker symbol: MSFT) largely hinges on factors like: | ||
- Tech innovation (e.g., AI, Azure cloud service, and gaming) | ||
- Quarterly earnings reports | ||
- Overall market conditions | ||
- How much caffeine traders have consumed | ||
``` | ||
|
||
1. Notice it does not provide a specific answer. We can add the Web Search Engine plugin to be able to provide a better answer. | ||
|
||
1. Next locate **TODO: Step 1** in `Program.cs` and add the following import lines: | ||
|
||
```csharp | ||
using Microsoft.SemanticKernel.Plugins.Web; | ||
using Microsoft.SemanticKernel.Plugins.Web.Bing; | ||
``` | ||
|
||
1. Next locate **TODO: Step 2** in `Program.cs` and provide the following lines to initialize and register the `WebSearchEnginePlugin`: | ||
|
||
```csharp | ||
var bingApiKey = AISettingsProvider.GetSettings().BingSearchService.ApiKey; | ||
if (!string.IsNullOrEmpty(bingApiKey)) | ||
{ | ||
var bingConnector = new BingConnector(bingApiKey); | ||
var bing = new WebSearchEnginePlugin(bingConnector); | ||
kernel.ImportPluginFromObject(bing, "bing"); | ||
} | ||
``` | ||
1. Re-run the program and ask what the sentiment on Microsoft stock is: | ||
```bash | ||
dotnet run | ||
User > What is the sentiment on Microsoft stock? | ||
``` | ||
Assistant response: | ||
```txt | ||
Assistant > Ah, Microsoft stock seems to be the belle of the ball with mixed but leaning-positive vibes. Let me serve up the sentiment soup: | ||
- **Positives:** There's more excitement than usual, with higher-than-average media sentiment compared to other tech companies. Plus, analysts are dishing out 12-month price targets like cocktails, with an average around $489.55 and some saying it could go as high as $600—a nice little pie in the sky for investors! 🍰🚀 | ||
- **Concerns:** Clouds (pun intended!) aren't all silver-lining for Microsoft, as they've got capacity constraints in their cloud services. Choppy waters ahead, perhaps? 🌩️ | ||
- **Buzz:** Oh, it's trending alright! Tons of people are searching, sharing, and probably debating MSFT more than their weekend plans. | ||
Feeling FOMO or ready to YOLO-invest? Don’t forget to watch that ticker—MSFT! | ||
``` | ||
Expect to see a more specific response. With the Web Search Engine plugin, you can now tap into any web search, so your agent will leverage that plugin to find information not available via other plugins or within the LLM being used. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<ItemGroup> | ||
<None Update="appsettings.json"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using Core.Utilities.Config; | ||
// Add import for Plugins | ||
using Core.Utilities.Plugins; | ||
// Add import required for StockService | ||
using Core.Utilities.Services; | ||
using Microsoft.SemanticKernel; | ||
using Microsoft.SemanticKernel.Connectors.OpenAI; | ||
// TODO: Step 1 - Add imports for Bing Search plugin | ||
|
||
// Add ChatCompletion import | ||
using Microsoft.SemanticKernel.ChatCompletion; | ||
// Temporarily added to enable Semantic Kernel tracing | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
|
||
|
||
// Initialize the kernel with chat completion | ||
IKernelBuilder builder = KernelBuilderProvider.CreateKernelWithChatCompletion(); | ||
// Enable tracing | ||
//builder.Services.AddLogging(services => services.AddConsole().SetMinimumLevel(LogLevel.Trace)); | ||
Kernel kernel = builder.Build(); | ||
|
||
// Initialize Time plugin and registration in the kernel | ||
kernel.Plugins.AddFromObject(new TimeInformationPlugin()); | ||
|
||
// Initialize Stock Data Plugin and register it in the kernel | ||
HttpClient httpClient = new(); | ||
StockDataPlugin stockDataPlugin = new(new StocksService(httpClient)); | ||
kernel.Plugins.AddFromObject(stockDataPlugin); | ||
|
||
// TODO: Step 2 - Initialize Bing Search plugin | ||
|
||
|
||
// Get chatCompletionService and initialize chatHistory with system prompt | ||
var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>(); | ||
ChatHistory chatHistory = new("You are a friendly financial advisor that only emits financial advice in a creative and funny tone"); | ||
// Remove the promptExecutionSettings and kernelArgs initialization code | ||
// Add system prompt | ||
OpenAIPromptExecutionSettings promptExecutionSettings = new() | ||
{ | ||
// Add Auto invoke kernel functions as the tool call behavior | ||
ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions | ||
}; | ||
|
||
// Initialize kernel arguments | ||
KernelArguments kernelArgs = new(promptExecutionSettings); | ||
|
||
// Execute program. | ||
const string terminationPhrase = "quit"; | ||
string? userInput; | ||
do | ||
{ | ||
Console.Write("User > "); | ||
userInput = Console.ReadLine(); | ||
|
||
if (userInput != null && userInput != terminationPhrase) | ||
{ | ||
Console.Write("Assistant > "); | ||
// Initialize fullMessage variable and add user input to chat history | ||
string fullMessage = ""; | ||
chatHistory.AddUserMessage(userInput); | ||
|
||
// Step 4 - Provide promptExecutionSettings and kernel arguments | ||
await foreach (var chatUpdate in chatCompletionService.GetStreamingChatMessageContentsAsync(chatHistory, promptExecutionSettings, kernel)) | ||
{ | ||
Console.Write(chatUpdate.Content); | ||
fullMessage += chatUpdate.Content ?? ""; | ||
} | ||
chatHistory.AddAssistantMessage(fullMessage); | ||
|
||
Console.WriteLine(); | ||
} | ||
} | ||
while (userInput != terminationPhrase); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<ItemGroup> | ||
<None Update="appsettings.json"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
</Project> |
Oops, something went wrong.