|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +using System.Text.Json; |
| 4 | +using System.Text.Json.Serialization; |
| 5 | +using Microsoft.Extensions.DependencyInjection; |
| 6 | +using Microsoft.Extensions.Logging; |
| 7 | +using Microsoft.SemanticKernel; |
| 8 | +using Microsoft.SemanticKernel.Plugins.OpenApi; |
| 9 | + |
| 10 | +namespace Plugins; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// Sample with demonstration of logging in OpenAPI plugins. |
| 14 | +/// </summary> |
| 15 | +public sealed class OpenApiPlugin_Telemetry(ITestOutputHelper output) : BaseTest(output) |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// Default logging in OpenAPI plugins. |
| 19 | + /// It's possible to use HTTP logging middleware in ASP.NET applications to log information about HTTP request, headers, body, response etc. |
| 20 | + /// More information here: <see href="https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-logging"/>. |
| 21 | + /// For custom logging logic, use <see cref="DelegatingHandler"/>. |
| 22 | + /// More information here: <see href="https://learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/http-message-handlers"/>. |
| 23 | + /// </summary> |
| 24 | + [Fact] |
| 25 | + public async Task LoggingAsync() |
| 26 | + { |
| 27 | + // Arrange |
| 28 | + using var stream = File.OpenRead("Resources/Plugins/RepairServicePlugin/repair-service.json"); |
| 29 | + using HttpClient httpClient = new(); |
| 30 | + |
| 31 | + var kernelBuilder = Kernel.CreateBuilder(); |
| 32 | + |
| 33 | + // If ILoggerFactory is registered in kernel's DI container, it will be used for logging purposes in OpenAPI functionality. |
| 34 | + kernelBuilder.Services.AddSingleton<ILoggerFactory>(this.LoggerFactory); |
| 35 | + |
| 36 | + var kernel = kernelBuilder.Build(); |
| 37 | + |
| 38 | + var plugin = await OpenApiKernelPluginFactory.CreateFromOpenApiAsync( |
| 39 | + "RepairService", |
| 40 | + stream, |
| 41 | + new OpenApiFunctionExecutionParameters(httpClient) |
| 42 | + { |
| 43 | + IgnoreNonCompliantErrors = true, |
| 44 | + EnableDynamicPayload = false, |
| 45 | + // For non-DI scenarios, it's possible to set ILoggerFactory in execution parameters when creating a plugin. |
| 46 | + // If ILoggerFactory is provided in both ways through the kernel's DI container and execution parameters, |
| 47 | + // the one from execution parameters will be used. |
| 48 | + LoggerFactory = kernel.LoggerFactory |
| 49 | + }); |
| 50 | + |
| 51 | + kernel.Plugins.Add(plugin); |
| 52 | + |
| 53 | + var arguments = new KernelArguments |
| 54 | + { |
| 55 | + ["payload"] = """{ "title": "Engine oil change", "description": "Need to drain the old engine oil and replace it with fresh oil.", "assignedTo": "", "date": "", "image": "" }""", |
| 56 | + ["content-type"] = "application/json" |
| 57 | + }; |
| 58 | + |
| 59 | + // Create Repair |
| 60 | + var result = await plugin["createRepair"].InvokeAsync(kernel, arguments); |
| 61 | + Console.WriteLine(result.ToString()); |
| 62 | + |
| 63 | + // List All Repairs |
| 64 | + result = await plugin["listRepairs"].InvokeAsync(kernel, arguments); |
| 65 | + var repairs = JsonSerializer.Deserialize<Repair[]>(result.ToString()); |
| 66 | + |
| 67 | + Assert.True(repairs?.Length > 0); |
| 68 | + |
| 69 | + var id = repairs[repairs.Length - 1].Id; |
| 70 | + |
| 71 | + // Update Repair |
| 72 | + arguments = new KernelArguments |
| 73 | + { |
| 74 | + ["payload"] = $"{{ \"id\": {id}, \"assignedTo\": \"Karin Blair\", \"date\": \"2024-04-16\", \"image\": \"https://www.howmuchisit.org/wp-content/uploads/2011/01/oil-change.jpg\" }}", |
| 75 | + ["content-type"] = "application/json" |
| 76 | + }; |
| 77 | + |
| 78 | + result = await plugin["updateRepair"].InvokeAsync(kernel, arguments); |
| 79 | + Console.WriteLine(result.ToString()); |
| 80 | + |
| 81 | + // Delete Repair |
| 82 | + arguments = new KernelArguments |
| 83 | + { |
| 84 | + ["payload"] = $"{{ \"id\": {id} }}", |
| 85 | + ["content-type"] = "application/json" |
| 86 | + }; |
| 87 | + |
| 88 | + result = await plugin["deleteRepair"].InvokeAsync(kernel, arguments); |
| 89 | + Console.WriteLine(result.ToString()); |
| 90 | + |
| 91 | + // Output: |
| 92 | + // Registering Rest function RepairService.listRepairs |
| 93 | + // Created KernelFunction 'listRepairs' for '<CreateRestApiFunction>g__ExecuteAsync|0' |
| 94 | + // Registering Rest function RepairService.createRepair |
| 95 | + // Created KernelFunction 'createRepair' for '<CreateRestApiFunction>g__ExecuteAsync|0' |
| 96 | + // Registering Rest function RepairService.updateRepair |
| 97 | + // Created KernelFunction 'updateRepair' for '<CreateRestApiFunction>g__ExecuteAsync|0' |
| 98 | + // Registering Rest function RepairService.deleteRepair |
| 99 | + // Created KernelFunction 'deleteRepair' for '<CreateRestApiFunction>g__ExecuteAsync|0' |
| 100 | + // Function RepairService - createRepair invoking. |
| 101 | + // Function RepairService - createRepair arguments: { "payload":"{ \u0022title\u0022: \u0022Engine oil change... |
| 102 | + // Function RepairService-createRepair succeeded. |
| 103 | + // Function RepairService-createRepair result: { "Content":"New repair created",... |
| 104 | + // Function RepairService-createRepair completed. Duration: 0.2793481s |
| 105 | + // New repair created |
| 106 | + // Function RepairService-listRepairs invoking. |
| 107 | + // Function RepairService-listRepairs arguments: { "payload":"{ \u0022title\u0022: \u0022Engine oil change... |
| 108 | + // Function RepairService-listRepairs succeeded. |
| 109 | + // Function RepairService-listRepairs result: { "Content":"[{\u0022id\u0022:79,\u0022title... |
| 110 | + // Function RepairService - updateRepair invoking. |
| 111 | + // Function RepairService-updateRepair arguments: { "payload":"{ \u0022id\u0022: 96, ... |
| 112 | + // Function RepairService-updateRepair succeeded. |
| 113 | + // Function RepairService-updateRepair result: { "Content":"Repair updated",... |
| 114 | + // Function RepairService-updateRepair completed. Duration: 0.0430169s |
| 115 | + // Repair updated |
| 116 | + // Function RepairService - deleteRepair invoking. |
| 117 | + // Function RepairService-deleteRepair arguments: { "payload":"{ \u0022id\u0022: 96 ... |
| 118 | + // Function RepairService-deleteRepair succeeded. |
| 119 | + // Function RepairService-deleteRepair result: { "Content":"Repair deleted",... |
| 120 | + // Function RepairService-deleteRepair completed. Duration: 0.049715s |
| 121 | + // Repair deleted |
| 122 | + } |
| 123 | + |
| 124 | + private sealed class Repair |
| 125 | + { |
| 126 | + [JsonPropertyName("id")] |
| 127 | + public int? Id { get; set; } |
| 128 | + |
| 129 | + [JsonPropertyName("title")] |
| 130 | + public string? Title { get; set; } |
| 131 | + |
| 132 | + [JsonPropertyName("description")] |
| 133 | + public string? Description { get; set; } |
| 134 | + |
| 135 | + [JsonPropertyName("assignedTo")] |
| 136 | + public string? AssignedTo { get; set; } |
| 137 | + |
| 138 | + [JsonPropertyName("date")] |
| 139 | + public string? Date { get; set; } |
| 140 | + |
| 141 | + [JsonPropertyName("image")] |
| 142 | + public string? Image { get; set; } |
| 143 | + } |
| 144 | +} |
0 commit comments