Skip to content
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

features/175-create-net-plugins-with-semantic-kernel-and-ui #176

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions DotNet-ChatGPT-Plugins/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
!**/.gitignore
!.git/HEAD
!.git/config
!.git/packed-refs
!.git/refs/heads/**
43 changes: 43 additions & 0 deletions DotNet-ChatGPT-Plugins/DotNet-ChatGPT-Plugins.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.7.34202.233
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PluginShared", "PluginShared\PluginShared.csproj", "{96D9AA8D-C1FC-4F6C-BD76-D104D86DA968}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HotelBooking.Api", "HotelBooking.Api\HotelBooking.Api.csproj", "{161C55BD-DF92-413A-ADA4-6FE5A7018711}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WeatherForecast.Api", "WeatherForecast.Api\WeatherForecast.Api.csproj", "{0EB5729F-E463-47A8-9CD4-CF65BC0B35AE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HotelBooking.Web", "HotelBooking.Web\HotelBooking.Web.csproj", "{C7FE29A7-384A-4BFE-BE33-1F99264E4EBD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{96D9AA8D-C1FC-4F6C-BD76-D104D86DA968}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{96D9AA8D-C1FC-4F6C-BD76-D104D86DA968}.Debug|Any CPU.Build.0 = Debug|Any CPU
{96D9AA8D-C1FC-4F6C-BD76-D104D86DA968}.Release|Any CPU.ActiveCfg = Release|Any CPU
{96D9AA8D-C1FC-4F6C-BD76-D104D86DA968}.Release|Any CPU.Build.0 = Release|Any CPU
{161C55BD-DF92-413A-ADA4-6FE5A7018711}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{161C55BD-DF92-413A-ADA4-6FE5A7018711}.Debug|Any CPU.Build.0 = Debug|Any CPU
{161C55BD-DF92-413A-ADA4-6FE5A7018711}.Release|Any CPU.ActiveCfg = Release|Any CPU
{161C55BD-DF92-413A-ADA4-6FE5A7018711}.Release|Any CPU.Build.0 = Release|Any CPU
{0EB5729F-E463-47A8-9CD4-CF65BC0B35AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0EB5729F-E463-47A8-9CD4-CF65BC0B35AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0EB5729F-E463-47A8-9CD4-CF65BC0B35AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0EB5729F-E463-47A8-9CD4-CF65BC0B35AE}.Release|Any CPU.Build.0 = Release|Any CPU
{C7FE29A7-384A-4BFE-BE33-1F99264E4EBD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C7FE29A7-384A-4BFE-BE33-1F99264E4EBD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C7FE29A7-384A-4BFE-BE33-1F99264E4EBD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C7FE29A7-384A-4BFE-BE33-1F99264E4EBD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F57E948F-653A-492E-84D4-8C02948E0F69}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"version": 1,
"isRoot": true,
"tools": {}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions DotNet-ChatGPT-Plugins/HotelBooking.Api/HotelBooking.Api.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<Content Include=".well-known\logo.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.14" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.0.0-beta8" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\PluginShared\PluginShared.csproj" />
</ItemGroup>

</Project>
117 changes: 117 additions & 0 deletions DotNet-ChatGPT-Plugins/HotelBooking.Api/HotelData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
using Microsoft.SemanticKernel.Orchestration;
using Microsoft.SemanticKernel;
using System.ComponentModel;
using System.Text.Json;

public class HotelFilterPlugin
{
private List<Hotel> hotels;

public HotelFilterPlugin(List<Hotel> hotels)
{
this.hotels = new List<Hotel>(hotels);
}

[SKFunction, Description("Filters the list of hotels by some arguments")]
public string FilterHotels(
[Description("The name of the hotel"), SKName("name")] string name,
[Description("The country or location of the hotel"), SKName("country")] string country,
[Description("If the hotel Wifi"), SKName("hasWifi")] string hasWifi,
[Description("The minimum number of stars the hotel has"), SKName("minStars")] string minStars,
[Description("The maximum number of stars the hotel has"), SKName("maxStars")] string maxStars)
{
List<Hotel> filteredHotels = hotels;

if (!string.IsNullOrEmpty(name))
{
Console.WriteLine("name:" + name);
filteredHotels = filteredHotels.Where(hotel => hotel.Name.Contains(name)).ToList();
}

if (!string.IsNullOrEmpty(country))
{
Console.WriteLine("country:" + country);
filteredHotels = filteredHotels.Where(hotel => hotel.Address.Contains(country)).ToList();
}

if (!string.IsNullOrEmpty(minStars))
{
Console.WriteLine("min_stars:" + minStars);
filteredHotels = filteredHotels.Where(hotel => hotel.Stars >= int.Parse(minStars)).ToList();
}

if (!string.IsNullOrEmpty(maxStars))
{
Console.WriteLine("max_stars:" + maxStars);
filteredHotels = filteredHotels.Where(hotel => hotel.Stars <= int.Parse(maxStars)).ToList();
}
if (!string.IsNullOrEmpty(hasWifi))
{
var hasWifiBool = bool.Parse(hasWifi);
filteredHotels = filteredHotels.Where(hotel => hotel.HasWifi ==hasWifiBool).ToList();
}

var json = JsonSerializer.Serialize(filteredHotels);

return json;
}
}


public static class HotelData
{
public static List<Hotel> GetHotels()
{
return new List<Hotel>
{
new() {
Address = "Mercè Rodoreda, 7, 17300, Blanes, Spain",
Name = "Beverly Park Hotel & Spa Blanes",
Description = "Located in Blanes in Costa Brava, Beverly Park Hotel & Spa Blanes is a four-star family hotel with a gym and swimming pool.",
Country = "Spain",
Stars = 4,
Beds = 2,
Bathrooms = 1,
HasWifi = true,
Price = 90,
Currency = "EUR"
},
new() {
Address = "Rue de la Brasserie 11, 1050 Ixelles, Belgium",
Name = "Brussels Marriott Hotel Grand Place",
Description = "Located in Belgium, this is a four-star family hotel with a cinema.",
Country = "Belgium",
Stars = 4,
Beds = 2,
Bathrooms = 1,
HasWifi = true,
Price = 90,
Currency = "EUR"
},
new() {
Address = "Carrer de la Riera, 77, 17300, Blanes, Spain",
Name = "Hotel Blaumar Blanes",
Description = "Located in Blanes in Costa Brava, Hotel Blaumar Blanes is a 2-star family hotel.",
Country = "Spain",
Stars = 2,
Beds = 1,
Bathrooms = 1,
HasWifi = false,
Price = 50,
Currency = "EUR"
},
new() {
Address = "Carrer de la Riera, 17300, Blanes, Spain",
Name = "Hotel Costa Brava Blanes",
Description = "Located in Blanes in Costa Brava, Hotel Costa Brava Blanes is a five-star family hotel with a gym and swimming pool.",
Country = "Spain",
Stars = 5,
Beds = 4,
Bathrooms = 2,
HasWifi = true,
Price = 190,
Currency = "EUR"
},
};
}
}
157 changes: 157 additions & 0 deletions DotNet-ChatGPT-Plugins/HotelBooking.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
using Microsoft.Extensions.FileProviders;
using Microsoft.OpenApi.Models;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Planners;
using Plugins.PluginShared;
using System.Text.Json;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo
{
Title = "Hotel Booking",
Version = "v1",
Description = "A plugin that allows you to search and book hotels"
});
});

builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll", policy =>
{
policy.WithOrigins("https://chat.openai.com", "http://localhost:3000", "http://localhost:3001", "https://localhost:7239").AllowAnyHeader().AllowAnyMethod();
});
});

var app = builder.Build();

app.UseSwagger(c =>
{
c.PreSerializeFilters.Add((swaggerDoc, httpReq) =>
{
swaggerDoc.Servers = new List<OpenApiServer> { new OpenApiServer { Url = $"{httpReq.Scheme}://{httpReq.Host.Value}" } };
});
});
app.UseSwaggerUI(x =>
{
x.SwaggerEndpoint("/swagger/v1/swagger.yaml", "Hotel.Booker v1");

});

app.UseCors("AllowAll");

app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), ".well-known")),
RequestPath = "/.well-known"
});

app.UseHttpsRedirection();

app.MapGet("/.well-known/ai-plugin.json", (HttpContext context) =>
{
var pluginManifest = new PluginManifest()
{
NameForModel = "hotelbooker",
NameForHuman = "Hotel Booker",
DescriptionForModel = "Plugin for Hotel Bookings, Get Hotels, Book hotel, Cancel bookings, etc.",
DescriptionForHuman = "Manages hotel bookings, advice hotels, book a hotel or room, etc.",
Auth = new PluginAuth()
{
Type = "none"
},
Api = new PluginApi()
{
Type = "openapi",
Url = $"{context.Request.Scheme}://{context.Request.Host}/swagger/v1/swagger.yaml"
},
LogoUrl = $"{context.Request.Scheme}://{context.Request.Host}/.well-known/logo.png",
};

return pluginManifest;
})
.ExcludeFromDescription();

app.MapGet("/GetHotels", () =>
{
return HotelData.GetHotels();
})
.WithName("GetHotels")
.WithOpenApi(generatedOperation =>
{
generatedOperation.Description = "Get the list of hotels";
return generatedOperation;
});

app.MapPost("/GetHotelsWithSK", async (PromptData promptData) =>
{
var text = promptData.Text;
var result = new List<Hotel>();

if (!string.IsNullOrWhiteSpace(text))
{
var builder = new KernelBuilder()
.WithAzureOpenAIChatCompletionService(
Environment.GetEnvironmentVariable("aoai-deployment-name"),
Environment.GetEnvironmentVariable("aoai-aoai-endpoint"),
Environment.GetEnvironmentVariable("aoai-key"));

var kernel = builder.Build();

var ask = $@"
System: You summarize the users question into in a single sentence.

User: {text}";

//User: I need to relax, what hotels do you have without Wifi?";

kernel.ImportFunctions(new HotelFilterPlugin(HotelData.GetHotels()), "HotelFilter");

var planner = new SequentialPlanner(kernel);

var plan = await planner.CreatePlanAsync(ask);

var kernelResult = await kernel.RunAsync(plan);

var data = kernelResult.GetValue<string>();
result = JsonSerializer.Deserialize<List<Hotel>>(data);

return result;
}

return HotelData.GetHotels();
})
.ExcludeFromDescription();
//.WithName("GetHotelsWithSK")
//.WithOpenApi(generatedOperation =>
//{
// generatedOperation.Description = "Get the list of hotels filteres by a LLM";
// return generatedOperation;
//});

app.MapPost("/BookHotel", (Booking booking) =>
{
return $"Hotel booked successfully. Reservation Id: {Guid.NewGuid()}";
})
.WithName("BookHotel")
.WithOpenApi(generatedOperation =>
{
generatedOperation.Description = "Performs an Hotel booking. Requires the hotel name and the number of days to book for.";
return generatedOperation;
});

app.MapPost("/CancelBooking", (CancelBooking cancelBooking) =>
{
return $"Booking cancelled successfully. ID: {cancelBooking.ReservationId}";
})
.WithName("CancelBooking")
.WithOpenApi(generatedOperation =>
{
generatedOperation.Description = "Cancel an Hotel Booking. Requires the reservation id.";
return generatedOperation;
});

app.Run();
Loading