-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
96 lines (74 loc) · 3.36 KB
/
Program.cs
File metadata and controls
96 lines (74 loc) · 3.36 KB
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
using Microsoft.SemanticKernel;
using Microsoft.Extensions.Configuration;
using System.ClientModel;
using OpenAI;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Microsoft.SemanticKernel.PromptTemplates.Handlebars;
public class Program
{
public static async Task Main(string[] args)
{
// Create a chat completion service with a model from GitHub Models
var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();
//var modelId = "Phi-3.5-mini-instruct";
var modelId = "gpt-4o-mini";
var uri = "https://models.inference.ai.azure.com";
var githubPAT = config["GH_PAT"];
// create client
var client = new OpenAIClient(new ApiKeyCredential(githubPAT), new OpenAIClientOptions { Endpoint = new Uri(uri) });
// Create a chat completion service
var builder = Kernel.CreateBuilder();
builder.AddOpenAIChatCompletion(modelId, client);
// Get the chat completion service
Kernel kernel = builder.Build();
string handlebarsPrompt = """
<message role="system">Instructions: Before providing the the user with a travel itinerary, ask how many days their trip is.</message>
<message role="user">I'm going to Rome. Can you create an itinerary for me?</message>
<message role="assistant">Sure, how many days is your trip?</message>
<message role="user">{{input}}</message>
""";
var templateFactory = new HandlebarsPromptTemplateFactory();
var promptTemplateConfig = new PromptTemplateConfig()
{
Template = handlebarsPrompt,
TemplateFormat = "handlebars",
Name = "CreateItenary"
};
kernel.ImportPluginFromType<CurrencyConverter>();
var function = kernel.CreateFunctionFromPrompt(promptTemplateConfig, templateFactory);
var plugin = kernel.CreatePluginFromFunctions("TravelItinerary", [function]);
kernel.Plugins.Add(plugin);
kernel.Plugins.AddFromType<FlightBookingPlugin>("FlightBooking");
kernel.Plugins.AddFromType<HotelBookingPlugin>("HotelBooking");
kernel.FunctionInvocationFilters.Add(new PermissionFilter());
OpenAIPromptExecutionSettings settings = new()
{
FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()
};
var history = new ChatHistory();
history.AddSystemMessage("Before providing destination recommendations, ask the user if they have a budget for their trip.");
Console.WriteLine("Press enter to exit");
Console.WriteLine("Assistant: How may I help you?");
Console.Write("User: ");
string input = Console.ReadLine();
while (input != "")
{
history.AddUserMessage(input);
await GetReply();
Console.Write("User: ");
input = Console.ReadLine()!;
}
async Task GetReply()
{
var reply = await kernel.GetRequiredService<IChatCompletionService>().GetChatMessageContentAsync(
history,
executionSettings: settings,
kernel: kernel
);
Console.WriteLine("Assistant: " + reply.ToString());
history.AddAssistantMessage(reply.ToString());
}
Console.ReadKey();
}
}