-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSummarizeText.cs
58 lines (49 loc) · 2.21 KB
/
SummarizeText.cs
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
using System.Collections.Generic;
using System.Net;
using System.Text.Json;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Orchestration;
namespace pattern_ai_doc_recognizer
{
public class SummarizeText
{
private readonly ILogger _logger;
private readonly IKernel _kernel;
public SummarizeText(ILoggerFactory loggerFactory, IKernel kernel)
{
_logger = loggerFactory.CreateLogger<SummarizeText>();
_kernel = kernel;
}
[Function("SummarizeText")]
public async Task<HttpResponseData> Run(
[HttpTrigger(AuthorizationLevel.Function, "post")]
HttpRequestData req,
FunctionContext executionContext)
{
_logger.LogInformation("SummarizeText function triggered.");
// Read text and Id from request body
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var requestBodyJson = JsonDocument.Parse(requestBody);
var text = requestBodyJson.RootElement.GetProperty("text").GetString();
var id = requestBodyJson.RootElement.GetProperty("id").GetString();
// Find the plugins folder and import the semantic skill
var pluginDirectory = Path.Combine(System.IO.Directory.GetCurrentDirectory(), "Plugins");
var plugin = _kernel.ImportSemanticSkillFromDirectory(pluginDirectory, "SemanticPlugin");
// A semantic skill is a collection of functions
var functionName = "Summarize";
var function = plugin[functionName];
// Run the function and pass in the input variable
var context = new ContextVariables();
context.Set("input", text);
var result = await _kernel.RunAsync(context, function);
// Return the result
var response = req.CreateResponse(HttpStatusCode.OK);
response.Headers.Add("Content-Type", "text/plain; charset=utf-8");
response.WriteString(result.ToString());
return response;
}
}
}