Skip to content

Commit ec13870

Browse files
committed
Added Open AI implementation for summarization
1 parent d8fb846 commit ec13870

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

src/Minifier.Frontend/Configuration.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace Minifier.Frontend
55
internal class Configuration
66
{
77
public UrlMinifierRepository UrlMinifierRepository { get; } = new UrlMinifierRepository();
8+
public OpenAi OpenAi { get; } = new OpenAi();
89
}
910

1011
internal class UrlMinifierRepository
@@ -14,4 +15,13 @@ internal class UrlMinifierRepository
1415
public string DatabaseName { get; } = Environment.GetEnvironmentVariable($"{nameof(UrlMinifierRepository)}__{nameof(DatabaseName)}", EnvironmentVariableTarget.Process);
1516
public string CollectionName { get; } = Environment.GetEnvironmentVariable($"{nameof(UrlMinifierRepository)}__{nameof(CollectionName)}", EnvironmentVariableTarget.Process);
1617
}
18+
19+
internal class OpenAi
20+
{
21+
public string ApiKey { get; } = Environment.GetEnvironmentVariable("OpenAiServiceKey", EnvironmentVariableTarget.Process);
22+
23+
public string Endpoint { get; } = Environment.GetEnvironmentVariable("OpenAiServiceCompletionEndpoint", EnvironmentVariableTarget.Process);
24+
25+
public string DeploymentId { get; } = Environment.GetEnvironmentVariable("OpenAiServiceDeploymentId", EnvironmentVariableTarget.Process);
26+
}
1727
}

src/Minifier.Frontend/Minifier.Frontend.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFramework>net6.0</TargetFramework>
44
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
55
</PropertyGroup>
66
<ItemGroup>
7+
<PackageReference Include="Azure.AI.OpenAI" Version="1.0.0-beta.5" />
78
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
89
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.CosmosDB" Version="4.0.0-preview3" />
910
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="5.2.0" />

src/Minifier.Frontend/OpenAI/Summarize.cs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,43 @@
11
using System;
22
using System.Threading.Tasks;
3+
using Azure;
4+
using Azure.AI.OpenAI;
5+
36

47
namespace Minifier.Frontend.OpenAI
58
{
69
internal class Summarize : ISummarize
710
{
8-
public Task<string> Invoke(string url)
11+
private readonly Configuration configuration;
12+
13+
public Summarize(
14+
Configuration configuration)
915
{
10-
throw new NotImplementedException();
16+
this.configuration = configuration;
17+
}
18+
19+
public async Task<string> Invoke(string url)
20+
{
21+
var request = $"Summarize the contents of the following web page: {url}";
22+
OpenAIClient client = new OpenAIClient(
23+
new Uri("https://westeurope.api.cognitive.microsoft.com/"),
24+
new AzureKeyCredential(this.configuration.OpenAi.ApiKey));
25+
26+
Response<Completions> completionsResponse = await client.GetCompletionsAsync(
27+
deploymentOrModelName: this.configuration.OpenAi.DeploymentId,
28+
new CompletionsOptions()
29+
{
30+
Prompts = { request },
31+
Temperature = (float)1,
32+
MaxTokens = 1000,
33+
NucleusSamplingFactor = (float)0.5,
34+
FrequencyPenalty = (float)0,
35+
PresencePenalty = (float)0,
36+
GenerationSampleCount = 1,
37+
});
38+
Completions completions = completionsResponse.Value;
39+
40+
return completions.Choices[0].Text;
1141
}
1242
}
1343

src/Minifier.Frontend/Startup.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ internal class Startup : FunctionsStartup
1212
{
1313
public override void Configure(IFunctionsHostBuilder builder)
1414
{
15-
builder.Services.AddLogging();
1615
builder.Services.AddTransient<IGetFullUrlFromSlug, GetFullUrlFromSlug>();
1716
builder.Services.AddSingleton<Configuration>();
1817
builder.Services.AddTransient<CosmosClient>(s =>

0 commit comments

Comments
 (0)