-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
93 lines (80 loc) · 3.38 KB
/
Program.cs
File metadata and controls
93 lines (80 loc) · 3.38 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
using System.Text.Json;
using Microsoft.Extensions.Configuration;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Planning.Handlebars;
namespace AssemblyAI.SemanticKernel.Sample;
internal class Program
{
public static async Task Main(string[] args)
{
var config = BuildConfig(args);
var kernel = BuildKernel(config);
await TranscribeFileUsingPluginDirectly(kernel);
await TranscribeFileUsingPluginFromSemanticFunction(kernel);
await TranscribeFileUsingPlan(kernel);
}
private static IConfigurationRoot BuildConfig(string[] args)
{
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables()
.AddUserSecrets<Program>()
.AddCommandLine(args)
.Build();
return config;
}
private static Kernel BuildKernel(IConfiguration config)
{
var kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddOpenAIChatCompletion(
"gpt-3.5-turbo",
config["OpenAI:ApiKey"] ?? throw new Exception("OpenAI:ApiKey configuration is required.")
)
.AddAssemblyAIPlugin(config);
var kernel = kernelBuilder.Build();
kernel.ImportPluginFromType<FindFilePlugin>();
return kernel;
}
private static async Task TranscribeFileUsingPluginDirectly(Kernel kernel)
{
Console.WriteLine("Transcribing file using plugin directly");
var result = await kernel.InvokeAsync<string>(
nameof(AssemblyAIPlugin),
AssemblyAIPlugin.TranscribeFunctionName,
new KernelArguments
{
["INPUT"] = "https://storage.googleapis.com/aai-docs-samples/espn.m4a"
}
);
Console.WriteLine(result);
Console.WriteLine();
}
private static async Task TranscribeFileUsingPluginFromSemanticFunction(Kernel kernel)
{
Console.WriteLine("Transcribing file and summarizing from within a semantic function");
// This will pass the URL to the `INPUT` variable.
// If `INPUT` is a URL, it'll use `INPUT` as `audioUrl`, otherwise, it'll use `INPUT` as `filePath`.
const string prompt = """
Here is a transcript:
{{AssemblyAIPlugin.Transcribe "https://storage.googleapis.com/aai-docs-samples/espn.m4a"}}
---
Summarize the transcript.
""";
var result = await kernel.InvokePromptAsync<string>(prompt);
Console.WriteLine(result);
Console.WriteLine();
}
private static async Task TranscribeFileUsingPlan(Kernel kernel)
{
var jsonSerializerOptions = new JsonSerializerOptions { WriteIndented = true };
Console.WriteLine("Transcribing file from a plan");
var planner = new HandlebarsPlanner(new HandlebarsPlannerOptions { AllowLoops = true });
const string prompt = "Find the espn.m4a in my downloads folder and transcribe it.";
var plan = await planner.CreatePlanAsync(kernel, prompt);
Console.WriteLine("Plan:\n");
Console.WriteLine(JsonSerializer.Serialize(plan, jsonSerializerOptions));
var transcript = await plan.InvokeAsync(kernel);
Console.WriteLine(transcript);
Console.WriteLine();
}
}