Friday, 21 February 2025

Using Handlebar Prompt in Semantic Kernal

>dotnet add package Microsoft.SemanticKernel.PromptTemplates.Handlebars --version 1.30.0


using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.PromptTemplates.Handlebars;

// Populate values from your OpenAI deployment
var modelId = "";
var endpoint = "";
var apiKey = "";

// Create a kernel with Azure OpenAI chat completion
var builder = Kernel.CreateBuilder();
builder.AddAzureOpenAIChatCompletion(modelId, endpoint, apiKey);

// Build the kernel
Kernel kernel = builder.Build();

string prompt = """
    <message role="system">Instructions: Identify the from and to destinations
    and dates from the user's request</message>

    <message role="user">Can you give me a list of flights from Seattle to Tokyo?
    I want to travel from March 11 to March 18.</message>

    <message role="assistant">
    Origin: Seattle
    Destination: Tokyo
    Depart: 03/11/2025
    Return: 03/18/2025
    </message>

    <message role="user">{{input}}</message>
    """;



string input = "I want to travel from June 1 to July 22. I want to go to Greece. I live in Chicago.";

// Create the kernel arguments
var arguments = new KernelArguments { ["input"] = input };

// Create the prompt template config using handlebars format
var templateFactory = new HandlebarsPromptTemplateFactory();
var promptTemplateConfig = new PromptTemplateConfig()
{
    Template = prompt,
    TemplateFormat = "handlebars",
    Name = "FlightPrompt",
};

// Invoke the prompt function
var function = kernel.CreateFunctionFromPrompt(promptTemplateConfig, templateFactory);
var response = await kernel.InvokeAsync(function, arguments);
Console.WriteLine(response);

Invoking Prompt Functions in Semantic Kernal

 using Microsoft.SemanticKernel;


// Populate values from your OpenAI deployment
var modelId = "";
var endpoint = "";
var apiKey = "";

// Create a kernel with Azure OpenAI chat completion
var builder = Kernel.CreateBuilder().AddAzureOpenAIChatCompletion(modelId, endpoint, apiKey);

// Build the kernel
Kernel kernel = builder.Build();

string prompt = """
    You are a helpful travel guide.
    I'm visiting {{$city}}. {{$background}}. What are some activities I should do today?
    """;
string city = "Barcelona";
string background = "I really enjoy art and dance.";

// Create the kernel function from the prompt
var activitiesFunction = kernel.CreateFunctionFromPrompt(prompt);

// Create the kernel arguments
var arguments = new KernelArguments { ["city"] = city, ["background"] = background };

// InvokeAsync on the kernel object
var result = await kernel.InvokeAsync(activitiesFunction, arguments);
Console.WriteLine(result);

Invoking prompt templates in Semantic Kernal

using Microsoft.SemanticKernel;

// Populate values from your OpenAI deployment
var modelId = "";
var endpoint = "";
var apiKey = "";

// Create a kernel with Azure OpenAI chat completion
var builder = Kernel.CreateBuilder().AddAzureOpenAIChatCompletion(modelId, endpoint, apiKey);

// Build the kernel
Kernel kernel = builder.Build();

string city = "Rome";
var prompt = "I'm visiting {{$city}}. What are some activities I should do today?";

var activitiesFunction = kernel.CreateFunctionFromPrompt(prompt);
var arguments = new KernelArguments { ["city"] = city };

// InvokeAsync on the KernelFunction object
var result = await activitiesFunction.InvokeAsync(kernel, arguments);
Console.WriteLine(result);

// InvokeAsync on the kernel object
result = await kernel.InvokeAsync(activitiesFunction, arguments);
Console.WriteLine(result);

Prompting Techniques in Semantic Kernal

 using Microsoft.SemanticKernel;


// Populate values from your OpenAI deployment
var modelId = "";
var endpoint = "";
var apiKey = "";

// Create a kernel with Azure OpenAI chat completion
var builder = Kernel.CreateBuilder().AddAzureOpenAIChatCompletion(modelId, endpoint, apiKey);

// Build the kernel
Kernel kernel = builder.Build();

string request = "How are you?";

// Zaro Shot Learning
/*
string prompt = $"""
Instructions: What is the intent of this request?
If you don't know the intent, don't guess; instead respond with "Unknown".
Choices: SendEmail, SendMessage, CompleteTask, CreateDocument, Unknown.
User Input: {request}
Intent:
""";
*/
//Few Shot Learning
/*
string prompt = $"""
Instructions: What is the intent of this request?
If you don't know the intent, don't guess; instead respond with "Unknown".
Choices: SendEmail, SendMessage, CompleteTask, CreateDocument, Unknown.

User Input: Can you send a very quick approval to the marketing team?
Intent: SendMessage

User Input: Can you send the full update to the marketing team?
Intent: SendEmail

User Input: {request}
Intent:
""";
*/

//Persona in Prompting
/*
string prompt = $"""
You are a highly experienced software engineer. Explain the concept of asynchronous programming to a beginner.
""";
*/

//Chain of Thought Prompting
string prompt = $"""
A farmer has 150 apples and wants to sell them in baskets. Each basket can hold 12 apples. If any apples remain after filling as many baskets as possible, the farmer will eat them. How many apples will the farmer eat?
Instructions: Explain your reasoning step by step before providing the answer.
""";


var result = await kernel.InvokePromptAsync(prompt);
Console.WriteLine(result);

AI Agents using Semantic Kernel SDK

 >dotnet new console (After creating a folder in VSCode run this command to create a C# project)

>dotnet add package Microsoft.SemanticKernel --version 1.30.0 (This command will install the Semantic Kernal SDK

>dotnet run

Create Azure OpenAI end-point and deployment and the following code is a hello-world code to test your setup:

using Microsoft.SemanticKernel;

// Populate values from your OpenAI deployment
var modelId = "<Model Name From Deployment>";
var endpoint = "<EndPoint URL>";
var apiKey = "<API KEY>";

// Create a kernel with Azure OpenAI chat completion
var builder = Kernel.CreateBuilder().AddAzureOpenAIChatCompletion(modelId, endpoint, apiKey);

// Build the kernel
Kernel kernel = builder.Build();

var result = await kernel.InvokePromptAsync("Give me a list of breakfast foods with eggs and cheese");
Console.WriteLine(result);

Thursday, 6 February 2025

Links to LLM Articles

  • Crescendo Attack: 2404.01833
  • The Price of Intelligence: The Price of Intelligence - ACM Queue
  • https://arxiv.org/abs/2502.11371v1
  • https://techcommunity.microsoft.com/blog/adforpostgresql/introducing-the-graphrag-solution-for-azure-database-for-postgresql/4299871
  • https://www.microsoft.com/en-us/research/blog/lazygraphrag-setting-a-new-standard-for-quality-and-cost/?msockid=3a065a80a9a960181f744951a89b618b
  • https://techcommunity.microsoft.com/blog/azure-ai-services-blog/graphrag-costs-explained-what-you-need-to-know/4207978
  • https://github.com/Azure-Samples/graphrag-accelerator
  • https://microsoft.github.io/graphrag/
  • https://techcommunity.microsoft.com/blog/azure-ai-services-blog/announcing-new-fine-tuning-capabilities-with-o1-mini-model-on-azure-openai-servi/4358186



Using Handlebar Prompt in Semantic Kernal

> dotnet add package Microsoft.SemanticKernel.PromptTemplates.Handlebars --version 1.30.0 using Microsoft . SemanticKernel ; using Micr...