-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add AI project reference and GetSummary operation. * Update tools and ai library reference. * Add features operation. * Fix parameter order. * Let signed in users call the summary method. * Cache feature list. * Add semantic kernel reference to all web apps. * Update to the newest sn AI libraries.
- Loading branch information
Showing
20 changed files
with
199 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using SenseNet.AI.Text; | ||
using SenseNet.ApplicationModel; | ||
using SenseNet.ContentRepository; | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace SenseNet.Services.Core.Operations; | ||
|
||
// ReSharper disable once InconsistentNaming | ||
public static class AIOperations | ||
{ | ||
/// <summary> | ||
/// Gets the summary of a long text using AI. | ||
/// </summary> | ||
/// <snCategory>AI</snCategory> | ||
/// <param name="content"></param> | ||
/// <param name="maxWordCount">Maximum number of words in the summary.</param> | ||
/// <param name="maxSentenceCount">Maximum number of sentences in the summary.</param> | ||
/// <param name="context"></param> | ||
/// <param name="text">A long text to create summary from.</param> | ||
/// <returns>An object containing the summary.</returns> | ||
[ODataAction] | ||
[ContentTypes(N.CT.PortalRoot)] | ||
[AllowedRoles(N.R.Everyone)] | ||
public static async Task<object> GetSummary(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 summary = await summaryProvider.GetSummary(text, maxWordCount, maxSentenceCount, | ||
context.RequestAborted).ConfigureAwait(false); | ||
|
||
return new | ||
{ | ||
summary | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Caching.Memory; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using SenseNet.ApplicationModel; | ||
using SenseNet.ContentRepository; | ||
using SenseNet.Tools.Features; | ||
using Task = System.Threading.Tasks.Task; | ||
|
||
namespace SenseNet.Services.Core.Operations; | ||
|
||
// ReSharper disable once InconsistentNaming | ||
public static class FeatureOperations | ||
{ | ||
#region Helper classes | ||
|
||
/// <summary> | ||
/// View object for feature information. | ||
/// </summary> | ||
internal class FeatureInfo | ||
{ | ||
public string Name { get; set; } | ||
public string DisplayName { get; set; } | ||
public FeatureAvailabilityInfo State { get; set; } | ||
} | ||
|
||
/// <summary> | ||
/// View object for feature availability. | ||
/// </summary> | ||
internal class FeatureAvailabilityInfo | ||
{ | ||
/// <summary> | ||
/// State of the feature. | ||
/// </summary> | ||
public string State { get; private set; } | ||
|
||
/// <summary> | ||
/// Reason if the feature is not available. | ||
/// </summary> | ||
public string Reason { get; private set; } | ||
|
||
/// <summary> | ||
/// Last time the feature was available. | ||
/// </summary> | ||
public DateTime? LastAvailable { get; private set; } | ||
|
||
internal static FeatureAvailabilityInfo FromFeatureAvailability(FeatureAvailability featureAvailability) | ||
{ | ||
return new FeatureAvailabilityInfo | ||
{ | ||
State = featureAvailability.State.ToString(), | ||
Reason = featureAvailability.Reason, | ||
LastAvailable = featureAvailability.LastAvailable | ||
}; | ||
} | ||
} | ||
|
||
#endregion | ||
|
||
// private in-memory cache for the feature list | ||
private static readonly IMemoryCache FeatureCache = new MemoryCache(new MemoryCacheOptions()); | ||
|
||
/// <summary> | ||
/// Gets the list of registered features. | ||
/// </summary> | ||
/// <returns></returns> | ||
[ODataFunction] | ||
[ContentTypes(N.CT.PortalRoot)] | ||
[AllowedRoles(N.R.Everyone)] | ||
public static async Task<object> GetFeatures(Content content, HttpContext context) | ||
{ | ||
const string cacheKey = "features"; | ||
|
||
if (FeatureCache.TryGetValue(cacheKey, out var cachedFeatures)) | ||
return cachedFeatures; | ||
|
||
var logger = context.RequestServices.GetRequiredService<ILogger<FeatureInfo>>(); | ||
var snFeatures = context.RequestServices.GetServices<ISnFeature>(); | ||
|
||
logger.LogTrace("Collecting available features."); | ||
|
||
var features = await Task.WhenAll(snFeatures.Select(async snFeature => | ||
{ | ||
var state = await snFeature.GetStateAsync(context.RequestAborted).ConfigureAwait(false); | ||
var featureInfo = new FeatureInfo | ||
{ | ||
Name = snFeature.Name, | ||
DisplayName = snFeature.DisplayName, | ||
State = FeatureAvailabilityInfo.FromFeatureAvailability(state) | ||
}; | ||
//TODO: permission check for certain features | ||
return featureInfo; | ||
})); | ||
|
||
logger.LogTrace("Adding feature list with {count} items to the cache.", features.Length); | ||
|
||
FeatureCache.Set(cacheKey, features, TimeSpan.FromSeconds(30)); | ||
|
||
return new | ||
{ | ||
features | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters