-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Fix 02 readme -semantic kernel #89
Open
wtulloch
wants to merge
6
commits into
microsoft:main
Choose a base branch
from
wtulloch:fix-02-readme
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1b9abcd
Fixed C# example code so that it is actually executable
wtulloch 159cd69
minor readability fix
wtulloch 5b90635
Updated c# example of creating function from prompt
wtulloch 68ae52c
Removed the section on planning in Semantic kernel has been deprecated
wtulloch 6ce44e4
Clarified comment in README to specify that the alternative method re…
wtulloch ee99851
Applied fixes from copilot
wtulloch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -150,41 +150,34 @@ async def main(): | |||||
if __name__ == "__main__": | ||||||
asyncio.run(main()) | ||||||
``` | ||||||
|
||||||
```csharp | ||||||
// Semantic Kernel C# example | ||||||
|
||||||
ChatHistory chatHistory = []; | ||||||
|
||||||
chatHistory.AddUserMessage("I'd like to go to New York on January 1, 2025."); | ||||||
using Microsoft.SemanticKernel; | ||||||
using Microsoft.SemanticKernel.ChatCompletion; | ||||||
using System.ComponentModel; | ||||||
using Microsoft.SemanticKernel.Connectors.AzureOpenAI; | ||||||
|
||||||
ChatHistory chatHistory = []; | ||||||
chatHistory.AddUserMessage("I'd like to go to New York on January 1, 2025"); | ||||||
|
||||||
// Define a plugin that contains the function to book travel | ||||||
public class BookTravelPlugin( | ||||||
IPizzaService pizzaService, | ||||||
IUserContext userContext, | ||||||
IPaymentService paymentService) | ||||||
{ | ||||||
|
||||||
[KernelFunction("book_flight")] | ||||||
[Description("Book travel given location and date")] | ||||||
public async Task<Booking> BookFlight( | ||||||
DateTime date, | ||||||
string location, | ||||||
) | ||||||
{ | ||||||
// book travel given date,location | ||||||
} | ||||||
} | ||||||
|
||||||
IKernelBuilder kernelBuilder = new KernelBuilder(); | ||||||
var kernelBuilder = Kernel.CreateBuilder(); | ||||||
kernelBuilder.AddAzureOpenAIChatCompletion( | ||||||
deploymentName: "NAME_OF_YOUR_DEPLOYMENT", | ||||||
apiKey: "YOUR_API_KEY", | ||||||
endpoint: "YOUR_AZURE_ENDPOINT" | ||||||
); | ||||||
kernelBuilder.Plugins.AddFromType<BookTravelPlugin>("BookTravel"); | ||||||
Kernel kernel = kernelBuilder.Build(); | ||||||
kernelBuilder.Plugins.AddFromType<BookTravelPlugin>("BookTravel"); | ||||||
var kernel = kernelBuilder.Build(); | ||||||
|
||||||
var settings = new AzureOpenAIPromptExecutionSettings() | ||||||
{ | ||||||
FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() | ||||||
}; | ||||||
|
||||||
var chatCompletion = kernel.GetRequiredService<IChatCompletionService>(); | ||||||
|
||||||
var response = await chatCompletion.GetChatMessageContentAsync(chatHistory, settings, kernel); | ||||||
|
||||||
/* | ||||||
Behind the scenes, the model recognizes the tool to call, what arguments it already has (location) and (date) | ||||||
|
@@ -202,16 +195,21 @@ Behind the scenes, the model recognizes the tool to call, what arguments it alre | |||||
] | ||||||
*/ | ||||||
|
||||||
ChatResponse response = await chatCompletion.GetChatMessageContentAsync( | ||||||
chatHistory, | ||||||
executionSettings: openAIPromptExecutionSettings, | ||||||
kernel: kernel) | ||||||
|
||||||
|
||||||
Console.WriteLine(response); | ||||||
chatHistory.AddAssistantMessage(response); | ||||||
Console.WriteLine(response.Content); | ||||||
chatHistory.AddMessage(response!.Role, response!.Content!); | ||||||
|
||||||
// Example AI Model Response: Your flight to New York on January 1, 2025, has been successfully booked. Safe travels! ✈️🗽 | ||||||
|
||||||
// Define a plugin that contains the function to book travel | ||||||
public class BookTravelPlugin | ||||||
{ | ||||||
[KernelFunction("book_flight")] | ||||||
[Description("Book travel given location and date")] | ||||||
public async Task<string> BookFlight(DateTime date, string location) | ||||||
{ | ||||||
return await Task.FromResult( $"Travel was booked to {location} on {date}"); | ||||||
} | ||||||
} | ||||||
``` | ||||||
|
||||||
What you can see from this example is how you can leverage a pre-built parser to extract key information from user input, such as the origin, destination, and date of a flight booking request. This modular approach allows you to focus on the high-level logic. | ||||||
|
@@ -275,7 +273,7 @@ There are many ways to compare these frameworks, but let's look at some key diff | |||||
|
||||||
## AutoGen | ||||||
|
||||||
Open-source framework developed by Microsoft Research's AI Frontiers Lab. Focuses on event-driven, distributed *agentic* applications, enabling multiple LLMs and SLMs, tools, and advanced multi-agent design patterns. | ||||||
AutoGen is an open-source framework developed by Microsoft Research's AI Frontiers Lab. It Focuses on event-driven, distributed *agentic* applications, enabling multiple LLMs and SLMs, tools, and advanced multi-agent design patterns. | ||||||
|
||||||
AutoGen is built around the core concept of agents, which are autonomous entities that can perceive their environment, make decisions, and take actions to achieve specific goals. Agents communicate through asynchronous messages, allowing them to work independently and in parallel, enhancing system scalability and responsiveness. | ||||||
|
||||||
|
@@ -479,15 +477,18 @@ Let's first cover some core components: | |||||
string skPrompt = @"Summarize the provided unstructured text in a sentence that is easy to understand. | ||||||
Text to summarize: {{$userInput}}"; | ||||||
|
||||||
// Register the function | ||||||
kernel.CreateSemanticFunction( | ||||||
// create the function from the prompt | ||||||
KernelFunction summarizeFunc = kernel.CreateFunctionFromPrompt( | ||||||
promptTemplate: skPrompt, | ||||||
functionName: "SummarizeText", | ||||||
pluginName: "SemanticFunctions" | ||||||
functionName: "SummarizeText" | ||||||
); | ||||||
|
||||||
//then import into the current kernel | ||||||
kernel.ImportPluginFromFunctions("SemanticFunctions", [summarizeFunc]); | ||||||
|
||||||
Comment on lines
+487
to
+488
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. The array initialization syntax appears incorrect for C#. Consider using 'new[] { summarizeFunc }' instead of '[summarizeFunc]'.
Suggested change
Copilot is powered by AI, so mistakes are possible. Review output carefully before use. |
||||||
``` | ||||||
|
||||||
Here, you first have a template prompt `skPrompt` that leaves room for the user to input text, `$userInput`. Then you register the function `SummarizeText` with the plugin `SemanticFunctions`. Note the name of the function that helps Semantic Kernel understand what the function does and when it should be called. | ||||||
Here, you first have a template prompt `skPrompt` that leaves room for the user to input text, `$userInput`. Then you create the kernel function `SummarizeText` and then import it into the kernel with the plugin name `SemanticFunctions`. Note the name of the function that helps Semantic Kernel understand what the function does and when it should be called. | ||||||
|
||||||
- **Native function**: There's also native functions that the framework can call directly to carry out the task. Here's an example of such a function retrieving the content from a file: | ||||||
|
||||||
|
@@ -508,29 +509,14 @@ Let's first cover some core components: | |||||
string functionName = "RetrieveLocalFile"; | ||||||
|
||||||
var nativeFunctions = new NativeFunctions(); | ||||||
kernel.ImportFunctions(nativeFunctions, plugInName); | ||||||
``` | ||||||
//This imports the instance created | ||||||
kernel.ImportPluginFromObject(nativeFunctions, pluginName); | ||||||
|
||||||
- **Planner**: The planner orchestrates execution plans and strategies based on user input. The idea is to express how things should be carried out which then surveys as an instruction for Semantic Kernel to follow. It then invokes the necessary functions to carry out the task. Here's an example of such a plan: | ||||||
//alternatively just use which will resolve dependencies | ||||||
kernel.ImportPluginFromType<NativeFunctions>(); | ||||||
|
||||||
```csharp | ||||||
string planDefinition = "Read content from a local file and summarize the content."; | ||||||
SequentialPlanner sequentialPlanner = new SequentialPlanner(kernel); | ||||||
|
||||||
string assetsFolder = @"../../assets"; | ||||||
string fileName = Path.Combine(assetsFolder,"docs","06_SemanticKernel", "aci_documentation.txt"); | ||||||
|
||||||
ContextVariables contextVariables = new ContextVariables(); | ||||||
contextVariables.Add("fileName", fileName); | ||||||
|
||||||
var customPlan = await sequentialPlanner.CreatePlanAsync(planDefinition); | ||||||
|
||||||
// Execute the plan | ||||||
KernelResult kernelResult = await kernel.RunAsync(contextVariables, customPlan); | ||||||
Console.WriteLine($"Summarization: {kernelResult.GetValue<string>()}"); | ||||||
``` | ||||||
|
||||||
Note especially `planDefinition` which is a simple instruction for the planner to follow. The appropriate functions are then called based on this plan, in this case our semantic function `SummarizeText` and the native function `RetrieveLocalFile`. | ||||||
- **Memory**: Abstracts and simplifies context management for AI apps. The idea with memory is that this is something the LLM should know about. You can store this information in a vector store which ends up being an in-memory database or a vector database or similar. Here's an example of a very simplified scenario where *facts* are added to the memory: | ||||||
|
||||||
```csharp | ||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Consider using a lowercase 'f' in 'focuses' for consistency with standard grammar.
Copilot is powered by AI, so mistakes are possible. Review output carefully before use.