Created
January 21, 2025 04:54
-
-
Save amul047/2f71d4c065d941a3033ee982551cb011 to your computer and use it in GitHub Desktop.
Using Azure OpenAI and Semantic Kernel to Analyze Citrus Fruits with Confidence Scores
This file contains hidden or 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
using Azure.Identity; | |
using Microsoft.SemanticKernel; | |
using Microsoft.SemanticKernel.Connectors.OpenAI; | |
using OpenAI.Chat; | |
using System.Text.Json; | |
using table.lib; | |
Kernel kernel = Kernel.CreateBuilder() | |
.AddAzureOpenAIChatCompletion( | |
deploymentName: "gpt-4o", // Specify your deployment name | |
endpoint: "https://<my-azure-open-ai-instance-name>.openai.azure.com/", // Specify your endpoint | |
credentials: new DefaultAzureCredential()// Specify your credentials | |
) | |
.Build(); | |
#pragma warning disable SKEXP0010 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed. | |
var executionSettings = new OpenAIPromptExecutionSettings | |
{ | |
ResponseFormat = typeof(FruitResult), // Specify response format | |
Logprobs = true | |
}; | |
#pragma warning restore SKEXP0010 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed. | |
// Send a request and pass prompt execution settings with desired response format. | |
var result = await kernel.InvokePromptAsync("Give me five fruits that are citrus.", new(executionSettings)); | |
var fruitResult = JsonSerializer.Deserialize<FruitResult>(result.ToString()); | |
#pragma warning disable CS8602 // Dereference of a possibly null reference. | |
var contentTokenLogProbablities = result.Metadata["ContentTokenLogProbabilities"] as List<ChatTokenLogProbabilityDetails>; | |
var logProbsAsArray = contentTokenLogProbablities.ToArray(); | |
#pragma warning restore CS8602 // Dereference of a possibly null reference. | |
List<double> yellowColorProbablities = new(); | |
for (int i = 0; i < logProbsAsArray.Length; i++) | |
{ | |
// If previous token was ":" and the one before that was "Color", then it is the yellow color probability. | |
if (i > 1 && string.Equals(logProbsAsArray[i - 2].Token, "Color", StringComparison.OrdinalIgnoreCase) && | |
string.Equals(logProbsAsArray[i - 1].Token, "\":\"", StringComparison.OrdinalIgnoreCase)) | |
{ | |
yellowColorProbablities.Add(100 * Math.Exp(logProbsAsArray[i].LogProbability)); | |
} | |
} | |
Table<Fruit>.Add(fruitResult?.Fruits ?? new List<Fruit>()).ToConsole(); | |
foreach (var probablity in yellowColorProbablities) | |
{ | |
Console.WriteLine($"Color probablity: {probablity} %"); | |
} | |
#region structured output classes | |
public class Fruit | |
{ | |
public required string Name { get; set; } | |
public required string Color { get; set; } | |
public required string Type { get; set; } | |
public int Calories { get; set; } | |
public int AverageMassInGrams { get; set; } | |
} | |
public class FruitResult | |
{ | |
public required List<Fruit> Fruits { get; set; } | |
} | |
#endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment