1
1
using System . Text . Json ;
2
2
using Microsoft . Extensions . Configuration ;
3
+ using Microsoft . Extensions . Logging ;
3
4
using Microsoft . SemanticKernel ;
4
5
using Microsoft . SemanticKernel . Orchestration ;
5
6
using Microsoft . SemanticKernel . Planning ;
6
- using AssemblyAI . SemanticKernel ;
7
- using AssemblyAI . SemanticKernel . Sample ;
8
- using Microsoft . Extensions . Logging ;
9
7
10
- var config = new ConfigurationBuilder ( )
11
- . AddEnvironmentVariables ( )
12
- . AddUserSecrets < Program > ( )
13
- . AddCommandLine ( args )
14
- . Build ( ) ;
15
-
16
- using var loggerFactory = LoggerFactory . Create ( builder => { builder . SetMinimumLevel ( 0 ) ; } ) ;
17
- var kernel = new KernelBuilder ( )
18
- . WithOpenAIChatCompletionService (
19
- "gpt-3.5-turbo" ,
20
- config [ "OpenAI:ApiKey" ] ?? throw new Exception ( "OpenAI:ApiKey configuration is required." )
21
- )
22
- . WithLoggerFactory ( loggerFactory )
23
- . Build ( ) ;
24
-
25
- var apiKey = config [ "AssemblyAI:ApiKey" ] ?? throw new Exception ( "AssemblyAI:ApiKey configuration is required." ) ;
26
-
27
- var transcriptPlugin = kernel . ImportSkill (
28
- new TranscriptPlugin ( apiKey : apiKey )
8
+ namespace AssemblyAI . SemanticKernel . Sample ;
9
+
10
+ internal class Program
11
+ {
12
+ public static async Task Main ( string [ ] args )
29
13
{
30
- AllowFileSystemAccess = true
31
- } ,
32
- TranscriptPlugin . PluginName
33
- ) ;
14
+ var config = BuildConfig ( args ) ;
34
15
35
- await TranscribeFileUsingPlugin ( kernel ) ;
16
+ var kernel = BuildKernel ( config ) ;
36
17
37
- async Task TranscribeFileUsingPlugin ( IKernel kernel )
38
- {
39
- var variables = new ContextVariables
18
+ await TranscribeFileUsingPluginDirectly ( kernel ) ;
19
+ //await TranscribeFileUsingPluginFromSemanticFunction(kernel);
20
+ //await TranscribeFileUsingPlan(kernel);
21
+ }
22
+
23
+ private static IKernel BuildKernel ( IConfiguration config )
40
24
{
41
- [ "audioUrl" ] = "https://storage.googleapis.com/aai-docs-samples/espn.m4a" ,
42
- } ;
25
+ var loggerFactory = LoggerFactory . Create ( builder => { builder . SetMinimumLevel ( 0 ) ; } ) ;
26
+ var kernel = new KernelBuilder ( )
27
+ . WithOpenAIChatCompletionService (
28
+ "gpt-3.5-turbo" ,
29
+ config [ "OpenAI:ApiKey" ] ?? throw new Exception ( "OpenAI:ApiKey configuration is required." )
30
+ )
31
+ . WithLoggerFactory ( loggerFactory )
32
+ . Build ( ) ;
43
33
44
- var result = await kernel . Skills
45
- . GetFunction ( TranscriptPlugin . PluginName , TranscriptPlugin . TranscribeFunctionName )
46
- . InvokeAsync ( variables ) ;
47
- Console . WriteLine ( result . Result ) ;
48
- }
34
+ var apiKey = config [ "AssemblyAI:ApiKey" ] ?? throw new Exception ( "AssemblyAI:ApiKey configuration is required." ) ;
49
35
50
- var findFilePlugin = kernel . ImportSkill (
51
- new FindFilePlugin ( kernel : kernel ) ,
52
- "FindFilePlugin"
53
- ) ;
36
+ kernel . ImportSkill (
37
+ new TranscriptPlugin ( apiKey : apiKey )
38
+ {
39
+ AllowFileSystemAccess = true
40
+ } ,
41
+ TranscriptPlugin . PluginName
42
+ ) ;
54
43
55
- await TranscribeFileUsingPlan ( kernel ) ;
44
+ kernel . ImportSkill (
45
+ new FindFilePlugin ( kernel : kernel ) ,
46
+ FindFilePlugin . PluginName
47
+ ) ;
48
+ return kernel ;
49
+ }
56
50
57
- async Task TranscribeFileUsingPlan ( IKernel kernel )
58
- {
59
- var planner = new SequentialPlanner ( kernel ) ;
51
+ private static IConfigurationRoot BuildConfig ( string [ ] args )
52
+ {
53
+ var config = new ConfigurationBuilder ( )
54
+ . AddEnvironmentVariables ( )
55
+ . AddUserSecrets < Program > ( )
56
+ . AddCommandLine ( args )
57
+ . Build ( ) ;
58
+ return config ;
59
+ }
60
+
61
+ private static async Task TranscribeFileUsingPluginDirectly ( IKernel kernel )
62
+ {
63
+ Console . WriteLine ( "Transcribing file using plugin directly" ) ;
64
+ var variables = new ContextVariables
65
+ {
66
+ [ "audioUrl" ] = "https://storage.googleapis.com/aai-docs-samples/espn.m4a" ,
67
+ // ["filePath"] = "./espn.m4a" // you can also use `filePath` which will upload the file and override `audioUrl`
68
+ } ;
69
+
70
+ var result = await kernel . Skills
71
+ . GetFunction ( TranscriptPlugin . PluginName , TranscriptPlugin . TranscribeFunctionName )
72
+ . InvokeAsync ( variables ) ;
73
+
74
+ Console . WriteLine ( result . Result ) ;
75
+ Console . WriteLine ( ) ;
76
+ }
77
+
78
+ private static async Task TranscribeFileUsingPluginFromSemanticFunction ( IKernel kernel )
79
+ {
80
+ Console . WriteLine ( "Transcribing file and summarizing from within a semantic function" ) ;
81
+ // This will pass the URL to the `INPUT` variable.
82
+ // If `INPUT` is a URL, it'll use `INPUT` as `audioUrl`, otherwise, it'll use `INPUT` as `filePath`.
83
+ const string prompt = """
84
+ Here is a transcript:
85
+ {{TranscriptPlugin.Transcribe "https://storage.googleapis.com/aai-docs-samples/espn.m4a"}}
86
+ ---
87
+ Summarize the transcript.
88
+ """ ;
89
+ var context = kernel . CreateNewContext ( ) ;
90
+ var function = kernel . CreateSemanticFunction ( prompt ) ;
91
+ await function . InvokeAsync ( context ) ;
92
+ Console . WriteLine ( context . Result ) ;
93
+ Console . WriteLine ( ) ;
94
+ }
95
+
96
+ private static async Task TranscribeFileUsingPlan ( IKernel kernel )
97
+ {
98
+ Console . WriteLine ( "Transcribing file from a plan" ) ;
99
+ var planner = new SequentialPlanner ( kernel ) ;
60
100
61
- const string prompt = "Transcribe the espn.m4a in my downloads folder." ;
62
- var plan = await planner . CreatePlanAsync ( prompt ) ;
101
+ const string prompt = "Transcribe the espn.m4a in my downloads folder." ;
102
+ var plan = await planner . CreatePlanAsync ( prompt ) ;
63
103
64
- Console . WriteLine ( "Plan:\n " ) ;
65
- Console . WriteLine ( JsonSerializer . Serialize ( plan , new JsonSerializerOptions { WriteIndented = true } ) ) ;
104
+ Console . WriteLine ( "Plan:\n " ) ;
105
+ Console . WriteLine ( JsonSerializer . Serialize ( plan , new JsonSerializerOptions { WriteIndented = true } ) ) ;
66
106
67
- var transcript = ( await kernel . RunAsync ( plan ) ) . Result ;
68
- Console . WriteLine ( "Transcript:" ) ;
69
- Console . WriteLine ( transcript ) ;
107
+ var transcript = ( await kernel . RunAsync ( plan ) ) . Result ;
108
+ Console . WriteLine ( transcript ) ;
109
+ Console . WriteLine ( ) ;
110
+ }
70
111
}
0 commit comments