From dd5daf10685e4d696e14ea29b2256eb0228df13a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikl=C3=B3s=20T=C3=B3th?= Date: Mon, 15 Jan 2024 17:51:22 +0100 Subject: [PATCH] Add content query generator feature. (#2013) --- src/Services.Core/Operations/AIOperations.cs | 40 +++++++++++++++---- .../SenseNet.Services.Core.csproj | 2 +- src/Tests/WebAppTests/ServicesTests.cs | 36 +++++++++++------ .../SnWebApplication.Api.InMem.Admin.csproj | 2 +- ...nWebApplication.Api.InMem.TokenAuth.csproj | 2 +- .../SnWebApplication.Api.Sql.Admin.csproj | 2 +- ...ication.Api.Sql.SearchService.Admin.csproj | 2 +- ...Sql.SearchService.TokenAuth.Preview.csproj | 2 +- ...ion.Api.Sql.SearchService.TokenAuth.csproj | 2 +- ...plication.Api.Sql.TokenAuth.Preview.csproj | 2 +- .../SnWebApplication.Api.Sql.TokenAuth.csproj | 2 +- 11 files changed, 65 insertions(+), 29 deletions(-) diff --git a/src/Services.Core/Operations/AIOperations.cs b/src/Services.Core/Operations/AIOperations.cs index d8635b1fd..30b45c9f9 100644 --- a/src/Services.Core/Operations/AIOperations.cs +++ b/src/Services.Core/Operations/AIOperations.cs @@ -13,7 +13,7 @@ namespace SenseNet.Services.Core.Operations; public static class AIOperations { /// - /// Gets the summary of a long text using AI. + /// Generates the summary of a long text using AI. /// /// AI /// @@ -25,16 +25,13 @@ public static class AIOperations [ODataAction] [ContentTypes(N.CT.PortalRoot)] [AllowedRoles(N.R.AITextUsers)] - public static async Task GetSummary(Content content, HttpContext context, + public static async Task 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() ?? - throw new InvalidOperationException("AI summary provider is not available."); + var summaryGenerator = context.RequestServices.GetService() ?? + 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 @@ -43,6 +40,33 @@ public static async Task GetSummary(Content content, HttpContext context }; } + /// + /// Generates a content query from natural language text using AI. + /// + /// AI + /// + /// + /// A natural language text to generate a from. + /// Optional thread id to chain requests. + /// An object containing the generated query and a thread ID. + [ODataAction] + [ContentTypes(N.CT.PortalRoot)] + [AllowedRoles(N.R.AITextUsers)] + public static async Task 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() ?? + throw new InvalidOperationException("AI content query generator is not available."); + + var queryData = await queryGenerator.GenerateQueryAsync(text, threadId, context.RequestAborted).ConfigureAwait(false); + + return new + { + queryData + }; + } + /// /// Generates an image from the provided text. /// diff --git a/src/Services.Core/SenseNet.Services.Core.csproj b/src/Services.Core/SenseNet.Services.Core.csproj index ed7b9f791..883d8d34e 100644 --- a/src/Services.Core/SenseNet.Services.Core.csproj +++ b/src/Services.Core/SenseNet.Services.Core.csproj @@ -29,7 +29,7 @@ - + diff --git a/src/Tests/WebAppTests/ServicesTests.cs b/src/Tests/WebAppTests/ServicesTests.cs index 46225d263..cff3e6c2b 100644 --- a/src/Tests/WebAppTests/ServicesTests.cs +++ b/src/Tests/WebAppTests/ServicesTests.cs @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, diff --git a/src/WebApps/SnWebApplication.Api.InMem.Admin/SnWebApplication.Api.InMem.Admin.csproj b/src/WebApps/SnWebApplication.Api.InMem.Admin/SnWebApplication.Api.InMem.Admin.csproj index bca892efb..d2eccff33 100644 --- a/src/WebApps/SnWebApplication.Api.InMem.Admin/SnWebApplication.Api.InMem.Admin.csproj +++ b/src/WebApps/SnWebApplication.Api.InMem.Admin/SnWebApplication.Api.InMem.Admin.csproj @@ -7,7 +7,7 @@ - + diff --git a/src/WebApps/SnWebApplication.Api.InMem.TokenAuth/SnWebApplication.Api.InMem.TokenAuth.csproj b/src/WebApps/SnWebApplication.Api.InMem.TokenAuth/SnWebApplication.Api.InMem.TokenAuth.csproj index ef681e6e9..e6fbd4410 100644 --- a/src/WebApps/SnWebApplication.Api.InMem.TokenAuth/SnWebApplication.Api.InMem.TokenAuth.csproj +++ b/src/WebApps/SnWebApplication.Api.InMem.TokenAuth/SnWebApplication.Api.InMem.TokenAuth.csproj @@ -12,7 +12,7 @@ - + diff --git a/src/WebApps/SnWebApplication.Api.Sql.Admin/SnWebApplication.Api.Sql.Admin.csproj b/src/WebApps/SnWebApplication.Api.Sql.Admin/SnWebApplication.Api.Sql.Admin.csproj index eb5e7e015..28a9c8109 100644 --- a/src/WebApps/SnWebApplication.Api.Sql.Admin/SnWebApplication.Api.Sql.Admin.csproj +++ b/src/WebApps/SnWebApplication.Api.Sql.Admin/SnWebApplication.Api.Sql.Admin.csproj @@ -8,7 +8,7 @@ - + diff --git a/src/WebApps/SnWebApplication.Api.Sql.SearchService.Admin/SnWebApplication.Api.Sql.SearchService.Admin.csproj b/src/WebApps/SnWebApplication.Api.Sql.SearchService.Admin/SnWebApplication.Api.Sql.SearchService.Admin.csproj index 6b565dedc..752758c7f 100644 --- a/src/WebApps/SnWebApplication.Api.Sql.SearchService.Admin/SnWebApplication.Api.Sql.SearchService.Admin.csproj +++ b/src/WebApps/SnWebApplication.Api.Sql.SearchService.Admin/SnWebApplication.Api.Sql.SearchService.Admin.csproj @@ -8,7 +8,7 @@ - + diff --git a/src/WebApps/SnWebApplication.Api.Sql.SearchService.TokenAuth.Preview/SnWebApplication.Api.Sql.SearchService.TokenAuth.Preview.csproj b/src/WebApps/SnWebApplication.Api.Sql.SearchService.TokenAuth.Preview/SnWebApplication.Api.Sql.SearchService.TokenAuth.Preview.csproj index 756e982f8..39adad6de 100644 --- a/src/WebApps/SnWebApplication.Api.Sql.SearchService.TokenAuth.Preview/SnWebApplication.Api.Sql.SearchService.TokenAuth.Preview.csproj +++ b/src/WebApps/SnWebApplication.Api.Sql.SearchService.TokenAuth.Preview/SnWebApplication.Api.Sql.SearchService.TokenAuth.Preview.csproj @@ -12,7 +12,7 @@ - + diff --git a/src/WebApps/SnWebApplication.Api.Sql.SearchService.TokenAuth/SnWebApplication.Api.Sql.SearchService.TokenAuth.csproj b/src/WebApps/SnWebApplication.Api.Sql.SearchService.TokenAuth/SnWebApplication.Api.Sql.SearchService.TokenAuth.csproj index 438775546..3dea48df2 100644 --- a/src/WebApps/SnWebApplication.Api.Sql.SearchService.TokenAuth/SnWebApplication.Api.Sql.SearchService.TokenAuth.csproj +++ b/src/WebApps/SnWebApplication.Api.Sql.SearchService.TokenAuth/SnWebApplication.Api.Sql.SearchService.TokenAuth.csproj @@ -12,7 +12,7 @@ - + diff --git a/src/WebApps/SnWebApplication.Api.Sql.TokenAuth.Preview/SnWebApplication.Api.Sql.TokenAuth.Preview.csproj b/src/WebApps/SnWebApplication.Api.Sql.TokenAuth.Preview/SnWebApplication.Api.Sql.TokenAuth.Preview.csproj index 4bcd6860f..1b71fc7b5 100644 --- a/src/WebApps/SnWebApplication.Api.Sql.TokenAuth.Preview/SnWebApplication.Api.Sql.TokenAuth.Preview.csproj +++ b/src/WebApps/SnWebApplication.Api.Sql.TokenAuth.Preview/SnWebApplication.Api.Sql.TokenAuth.Preview.csproj @@ -12,7 +12,7 @@ - + diff --git a/src/WebApps/SnWebApplication.Api.Sql.TokenAuth/SnWebApplication.Api.Sql.TokenAuth.csproj b/src/WebApps/SnWebApplication.Api.Sql.TokenAuth/SnWebApplication.Api.Sql.TokenAuth.csproj index 7cf1ed6f6..f3d59ec79 100644 --- a/src/WebApps/SnWebApplication.Api.Sql.TokenAuth/SnWebApplication.Api.Sql.TokenAuth.csproj +++ b/src/WebApps/SnWebApplication.Api.Sql.TokenAuth/SnWebApplication.Api.Sql.TokenAuth.csproj @@ -12,7 +12,7 @@ - +