File size: 4,747 Bytes
a00a692
 
7d7d91d
 
 
 
 
 
 
cca2c82
920638b
7d7d91d
 
 
 
 
 
 
 
7d48943
7d7d91d
 
 
920638b
7d7d91d
 
 
 
920638b
7d7d91d
 
46165c0
fcb59bc
7d7d91d
 
94e843c
a00a692
 
 
 
 
 
ac5d1bb
a00a692
 
 
 
cca2c82
a00a692
 
 
 
 
 
920638b
 
 
 
94e843c
 
 
 
 
 
7d7d91d
 
 
 
 
94e843c
 
 
 
 
 
 
 
 
 
920638b
7d7d91d
94e843c
 
 
7d7d91d
 
94e843c
 
 
 
 
 
 
920638b
94e843c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7d7d91d
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using FoodHealthChecker.Models;
using FoodHealthChecker.Options;
using FoodHealthChecker.SemanticKernel.Plugins;
using Microsoft.SemanticKernel;

namespace FoodHealthChecker
{
    public class FoodCheckerService
    {
        private Kernel _kernel;
        private bool isValid = false;
        private readonly FoodCheckerPlugin _foodCheckerPlugin;
        public FoodCheckerService(IConfiguration config, FoodCheckerPlugin foodCheckerPlugin)
        {
            _foodCheckerPlugin = foodCheckerPlugin;
            var azureOptions = config.GetSection("AzureOpenAI").Get<AzureOpenAIOptions>();
            var oaiOptions = config.GetSection("OpenAI").Get<OpenAIOptions>();
            var kernelBuilder = Kernel.CreateBuilder();

            kernelBuilder.Services.AddLogging(config => { config.AddConsole(); config.SetMinimumLevel(LogLevel.Information); });
            if (azureOptions != null && azureOptions.isValid())
            {
                kernelBuilder.AddAzureOpenAIChatCompletion(azureOptions.DeploymentName, azureOptions.Endpoint, azureOptions.ApiKey);
                isValid = true;
            }
            else if (oaiOptions != null && oaiOptions.isValid())
            {
                kernelBuilder.AddOpenAIChatCompletion(oaiOptions.ModelID, oaiOptions.ApiKey);
                isValid = true;
            }
            var kernel = kernelBuilder.Build();

            kernel.Plugins.AddFromObject(foodCheckerPlugin);
            _kernel = kernel;
        }

        public void UpdateTemporaryKernel(TemporaryConfig config)
        {
            var kernelBuilder = Kernel.CreateBuilder();

            if (config.isAzureOpenAIConfigValid())
            {
                kernelBuilder.AddAzureOpenAIChatCompletion(config.AzureOpenAI_DeploymentName, config.AzureOpenAI_Endpoint, config.AzureOpenAI_ApiKey);
                isValid = true;
            }
            else if (config.isOpenAIConfigValid())
            {
                kernelBuilder.AddOpenAIChatCompletion(config.OpenAI_ModelId, config.OpenAI_ApiKey);
                isValid = true;
            }
            var kernel = kernelBuilder.Build();
            kernel.Plugins.AddFromObject(_foodCheckerPlugin);
            _kernel = kernel;
        }
        public bool IsValid()
        {
            return isValid;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ingredientResponse"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public IAsyncEnumerable<string> CheckFoodHealthAsync(string ingredientResponse, CancellationToken cancellationToken = default)
        {
            return _foodCheckerPlugin.CheckFoodHealthAsync(ingredientResponse, _kernel, cancellationToken);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="imageData"></param>
        /// <param name="fileName"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// Inconsistant behaviour https://github.com/microsoft/semantic-kernel/pull/6319
        /// Related to https://github.com/dotnet/runtime/issues/96544
        public IAsyncEnumerable<string> GetIngredirentsAsync(ReadOnlyMemory<byte> imageData, string fileName, CancellationToken cancellationToken = default)
        {

            var imageDataUrl = new ImageContent(imageData) { MimeType = GetMimeType(fileName) }.ToString();

            return _foodCheckerPlugin.GetIngredientsAsync(imageDataUrl, _kernel, cancellationToken);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="imageUrl"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public IAsyncEnumerable<string> GetIngredirentsAsync(string imageUrl, CancellationToken cancellationToken = default)
        {
            return _foodCheckerPlugin.GetIngredientsAsync(imageUrl, _kernel, cancellationToken);
        }

        /// <summary>
        /// 
        /// </summary>  
        /// <param name="fileName"></param>
        /// <returns></returns>
        /// <exception cref="NotSupportedException"></exception>
        private static string GetMimeType(string fileName)
        {
            return Path.GetExtension(fileName) switch
            {
                ".jpg" or ".jpeg" => "image/jpeg",
                ".png" => "image/png",
                ".gif" => "image/gif",
                ".bmp" => "image/bmp",
                ".tiff" => "image/tiff",
                ".ico" => "image/x-icon",
                ".svg" => "image/svg+xml",
                _ => throw new NotSupportedException("Unsupported image format.")
            };
        }
    }
}