Skip to content

Commit

Permalink
Add content query generator feature. (#2013)
Browse files Browse the repository at this point in the history
  • Loading branch information
tusmester committed Jan 15, 2024
1 parent 4cc38ae commit dd5daf1
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 29 deletions.
40 changes: 32 additions & 8 deletions src/Services.Core/Operations/AIOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace SenseNet.Services.Core.Operations;
public static class AIOperations
{
/// <summary>
/// Gets the summary of a long text using AI.
/// Generates the summary of a long text using AI.
/// </summary>
/// <snCategory>AI</snCategory>
/// <param name="content"></param>
Expand All @@ -25,16 +25,13 @@ public static class AIOperations
[ODataAction]
[ContentTypes(N.CT.PortalRoot)]
[AllowedRoles(N.R.AITextUsers)]
public static async Task<object> GetSummary(Content content, HttpContext context,
public static async Task<object> GenerateSummary(Content content, HttpContext context,
int maxWordCount, int maxSentenceCount, string text)
{
//TODO: limit max parameters and text length by configuration
//TODO: add caller permissions: allowed roles

var summaryProvider = context.RequestServices.GetService<ISummaryProvider>() ??
throw new InvalidOperationException("AI summary provider is not available.");
var summaryGenerator = context.RequestServices.GetService<ISummaryGenerator>() ??
throw new InvalidOperationException("AI summary generator is not available.");

var summary = await summaryProvider.GetSummary(text, maxWordCount, maxSentenceCount,
var summary = await summaryGenerator.GenerateSummaryAsync(text, maxWordCount, maxSentenceCount,
context.RequestAborted).ConfigureAwait(false);

return new
Expand All @@ -43,6 +40,33 @@ public static async Task<object> GetSummary(Content content, HttpContext context
};
}

/// <summary>
/// Generates a content query from natural language text using AI.
/// </summary>
/// <snCategory>AI</snCategory>
/// <param name="content"></param>
/// <param name="context"></param>
/// <param name="text">A natural language text to generate a from.</param>
/// <param name="threadId">Optional thread id to chain requests.</param>
/// <returns>An object containing the generated query and a thread ID.</returns>
[ODataAction]
[ContentTypes(N.CT.PortalRoot)]
[AllowedRoles(N.R.AITextUsers)]
public static async Task<object> GenerateContentQuery(Content content, HttpContext context, string text, string threadId = null)
{
//TODO: remove the root restriction and add context info to the query

var queryGenerator = context.RequestServices.GetService<IContentQueryGenerator>() ??
throw new InvalidOperationException("AI content query generator is not available.");

var queryData = await queryGenerator.GenerateQueryAsync(text, threadId, context.RequestAborted).ConfigureAwait(false);

return new
{
queryData
};
}

/// <summary>
/// Generates an image from the provided text.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Services.Core/SenseNet.Services.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Http.Extensions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="6.0.0" />
<PackageReference Include="SenseNet.AI.Text" Version="0.0.1.3" />
<PackageReference Include="SenseNet.AI.Text" Version="0.0.1.4" />
<PackageReference Include="SenseNet.AI.Vision" Version="0.0.0.2" />
<PackageReference Include="SenseNet.Tools" Version="3.2.12.4" />
</ItemGroup>
Expand Down
36 changes: 24 additions & 12 deletions src/Tests/WebAppTests/ServicesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -618,10 +618,12 @@ public void WebApp_Services_Api_InMem_Admin()
{typeof(ISnTracer), typeof(SnDebugViewTracer)},
{typeof(ISnFeature), new []
{
typeof(SummaryProvider),
typeof(SummaryGenerator),
typeof(ContentQueryGenerator),
typeof(ImageGenerator)
}},
{typeof(ISummaryProvider), typeof(SummaryProvider)},
{typeof(ISummaryGenerator), typeof(SummaryGenerator)},
{typeof(IContentQueryGenerator), typeof(ContentQueryGenerator)},
{typeof(IImageGenerator), typeof(ImageGenerator)},
},
includedProvidersByType: _defaultIncludedProvidersByType,
Expand Down Expand Up @@ -657,10 +659,12 @@ public void WebApp_Services_Api_InMem_TokenAuth()
{typeof(ISnTracer), typeof(SnDebugViewTracer)},
{typeof(ISnFeature), new []
{
typeof(SummaryProvider),
typeof(SummaryGenerator),
typeof(ContentQueryGenerator),
typeof(ImageGenerator)
}},
{typeof(ISummaryProvider), typeof(SummaryProvider)},
{typeof(ISummaryGenerator), typeof(SummaryGenerator)},
{typeof(IContentQueryGenerator), typeof(ContentQueryGenerator)},
{typeof(IImageGenerator), typeof(ImageGenerator)},
},
includedProvidersByType: _defaultIncludedProvidersByType,
Expand Down Expand Up @@ -696,10 +700,12 @@ public void WebApp_Services_Api_Sql_Admin()
{typeof(IClientMetadataProvider), typeof(ClientMetadataProvider)},
{typeof(ISnFeature), new []
{
typeof(SummaryProvider),
typeof(SummaryGenerator),
typeof(ContentQueryGenerator),
typeof(ImageGenerator)
}},
{typeof(ISummaryProvider), typeof(SummaryProvider)},
{typeof(ISummaryGenerator), typeof(SummaryGenerator)},
{typeof(IContentQueryGenerator), typeof(ContentQueryGenerator)},
{typeof(IImageGenerator), typeof(ImageGenerator)},
},
includedProvidersByType: _defaultIncludedProvidersByType,
Expand Down Expand Up @@ -736,10 +742,12 @@ public void WebApp_Services_Api_Sql_TokenAuth()
{typeof(IClientMetadataProvider), typeof(ClientMetadataProvider)},
{typeof(ISnFeature), new []
{
typeof(SummaryProvider),
typeof(SummaryGenerator),
typeof(ContentQueryGenerator),
typeof(ImageGenerator)
}},
{typeof(ISummaryProvider), typeof(SummaryProvider)},
{typeof(ISummaryGenerator), typeof(SummaryGenerator)},
{typeof(IContentQueryGenerator), typeof(ContentQueryGenerator)},
{typeof(IImageGenerator), typeof(ImageGenerator)},
},
includedProvidersByType: _defaultIncludedProvidersByType,
Expand Down Expand Up @@ -785,10 +793,12 @@ public void WebApp_Services_Api_Sql_SearchService_Admin()
{typeof(IClientMetadataProvider), typeof(ClientMetadataProvider)},
{typeof(ISnFeature), new []
{
typeof(SummaryProvider),
typeof(SummaryGenerator),
typeof(ContentQueryGenerator),
typeof(ImageGenerator)
}},
{typeof(ISummaryProvider), typeof(SummaryProvider)},
{typeof(ISummaryGenerator), typeof(SummaryGenerator)},
{typeof(IContentQueryGenerator), typeof(ContentQueryGenerator)},
{typeof(IImageGenerator), typeof(ImageGenerator)},
},
includedProvidersByType: _defaultIncludedProvidersByType,
Expand Down Expand Up @@ -833,10 +843,12 @@ public void WebApp_Services_Api_Sql_SearchService_TokenAuth()
{typeof(IClientMetadataProvider), typeof(ClientMetadataProvider)},
{typeof(ISnFeature), new []
{
typeof(SummaryProvider),
typeof(SummaryGenerator),
typeof(ContentQueryGenerator),
typeof(ImageGenerator)
}},
{typeof(ISummaryProvider), typeof(SummaryProvider)},
{typeof(ISummaryGenerator), typeof(SummaryGenerator)},
{typeof(IContentQueryGenerator), typeof(ContentQueryGenerator)},
{typeof(IImageGenerator), typeof(ImageGenerator)},
},
includedProvidersByType: _defaultIncludedProvidersByType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.9" />
<PackageReference Include="SenseNet.AI.Text.SemanticKernel" Version="0.0.1.3" />
<PackageReference Include="SenseNet.AI.Text.SemanticKernel" Version="0.0.1.5" />
<PackageReference Include="SenseNet.AI.Vision.Azure" Version="0.0.0.2" />
<PackageReference Include="Serilog.AspNetCore" Version="6.0.1" />
<PackageReference Include="Serilog.Sinks.Graylog" Version="2.3.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.9" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.17.0" />
<PackageReference Include="SenseNet.AI.Text.SemanticKernel" Version="0.0.1.3" />
<PackageReference Include="SenseNet.AI.Text.SemanticKernel" Version="0.0.1.5" />
<PackageReference Include="SenseNet.AI.Vision.Azure" Version="0.0.0.2" />
<PackageReference Include="Serilog.AspNetCore" Version="6.0.1" />
<PackageReference Include="Serilog.Sinks.Graylog" Version="2.3.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.9" />
<PackageReference Include="SenseNet.AI.Text.SemanticKernel" Version="0.0.1.3" />
<PackageReference Include="SenseNet.AI.Text.SemanticKernel" Version="0.0.1.5" />
<PackageReference Include="SenseNet.AI.Vision.Azure" Version="0.0.0.2" />
<PackageReference Include="SenseNet.Search.Lucene29.Local" Version="7.4.10.1" />
<PackageReference Include="SenseNet.Security.EFCSecurityStore" Version="3.1.0.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.9" />
<PackageReference Include="SenseNet.AI.Text.SemanticKernel" Version="0.0.1.3" />
<PackageReference Include="SenseNet.AI.Text.SemanticKernel" Version="0.0.1.5" />
<PackageReference Include="SenseNet.AI.Vision.Azure" Version="0.0.0.2" />
<PackageReference Include="SenseNet.Messaging.RabbitMQ" Version="1.1.2" />
<PackageReference Include="SenseNet.Search.Lucene29.Centralized.GrpcClient" Version="0.0.12.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.9" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.17.0" />
<PackageReference Include="SenseNet.AI.Text.SemanticKernel" Version="0.0.1.3" />
<PackageReference Include="SenseNet.AI.Text.SemanticKernel" Version="0.0.1.5" />
<PackageReference Include="SenseNet.AI.Vision.Azure" Version="0.0.0.2" />
<PackageReference Include="SenseNet.Preview.Aspose" Version="7.3.6" />
<PackageReference Include="SenseNet.Messaging.RabbitMQ" Version="1.1.2" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.9" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.17.0" />
<PackageReference Include="SenseNet.AI.Text.SemanticKernel" Version="0.0.1.3" />
<PackageReference Include="SenseNet.AI.Text.SemanticKernel" Version="0.0.1.5" />
<PackageReference Include="SenseNet.AI.Vision.Azure" Version="0.0.0.2" />
<PackageReference Include="SenseNet.Messaging.RabbitMQ" Version="1.1.2" />
<PackageReference Include="SenseNet.Search.Lucene29.Centralized.GrpcClient" Version="0.0.12.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.9" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.17.0" />
<PackageReference Include="SenseNet.AI.Text.SemanticKernel" Version="0.0.1.3" />
<PackageReference Include="SenseNet.AI.Text.SemanticKernel" Version="0.0.1.5" />
<PackageReference Include="SenseNet.AI.Vision.Azure" Version="0.0.0.2" />
<PackageReference Include="SenseNet.Preview.Aspose" Version="7.3.6" />
<PackageReference Include="SenseNet.Search.Lucene29.Local" Version="7.4.10.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.9" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.17.0" />
<PackageReference Include="SenseNet.AI.Text.SemanticKernel" Version="0.0.1.3" />
<PackageReference Include="SenseNet.AI.Text.SemanticKernel" Version="0.0.1.5" />
<PackageReference Include="SenseNet.AI.Vision.Azure" Version="0.0.0.2" />
<PackageReference Include="SenseNet.Search.Lucene29.Local" Version="7.4.10.1" />
<PackageReference Include="SenseNet.Security.EFCSecurityStore" Version="3.1.0.1" />
Expand Down

0 comments on commit dd5daf1

Please sign in to comment.