✨ Provide real user and posts data for the thinking
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using DysonNetwork.Insight;
|
||||
using DysonNetwork.Insight.Startup;
|
||||
using DysonNetwork.Shared.Http;
|
||||
using DysonNetwork.Shared.Registry;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
@@ -10,10 +11,13 @@ builder.AddServiceDefaults();
|
||||
builder.ConfigureAppKestrel(builder.Configuration);
|
||||
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddAppServices(builder.Configuration);
|
||||
builder.Services.AddAppServices();
|
||||
builder.Services.AddAppAuthentication();
|
||||
builder.Services.AddAppFlushHandlers();
|
||||
builder.Services.AddAppBusinessServices();
|
||||
|
||||
builder.Services.AddAccountService();
|
||||
builder.Services.AddSphereService();
|
||||
builder.Services.AddThinkingServices(builder.Configuration);
|
||||
|
||||
builder.AddSwaggerManifest(
|
||||
|
||||
@@ -10,7 +10,7 @@ namespace DysonNetwork.Insight.Startup;
|
||||
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
public static IServiceCollection AddAppServices(this IServiceCollection services, IConfiguration configuration)
|
||||
public static IServiceCollection AddAppServices(this IServiceCollection services)
|
||||
{
|
||||
services.AddDbContext<AppDatabase>();
|
||||
services.AddSingleton<IClock>(SystemClock.Instance);
|
||||
@@ -65,8 +65,7 @@ public static class ServiceCollectionExtensions
|
||||
|
||||
public static IServiceCollection AddThinkingServices(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
var thinkingProvider = new ThinkingProvider(configuration);
|
||||
services.AddSingleton(thinkingProvider);
|
||||
services.AddSingleton<ThinkingProvider>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
@@ -1,14 +1,34 @@
|
||||
using DysonNetwork.Shared.Proto;
|
||||
using Microsoft.SemanticKernel;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace DysonNetwork.Insight.Thinking;
|
||||
|
||||
public class ThinkingProvider
|
||||
{
|
||||
public readonly Kernel Kernel;
|
||||
public readonly string? ModelProviderType;
|
||||
public readonly string? ModelDefault;
|
||||
private readonly Kernel _kernel;
|
||||
private readonly PostService.PostServiceClient _postClient;
|
||||
private readonly AccountService.AccountServiceClient _accountClient;
|
||||
|
||||
public ThinkingProvider(IConfiguration configuration)
|
||||
public Kernel Kernel => _kernel;
|
||||
public string? ModelProviderType { get; private set; }
|
||||
public string? ModelDefault { get; private set; }
|
||||
|
||||
public ThinkingProvider(
|
||||
IConfiguration configuration,
|
||||
PostService.PostServiceClient postClient,
|
||||
AccountService.AccountServiceClient accountClient
|
||||
)
|
||||
{
|
||||
_postClient = postClient;
|
||||
_accountClient = accountClient;
|
||||
|
||||
_kernel = InitializeThinkingProvider(configuration);
|
||||
InitializeHelperFunctions();
|
||||
}
|
||||
|
||||
private Kernel InitializeThinkingProvider(IConfiguration configuration)
|
||||
{
|
||||
var cfg = configuration.GetSection("Thinking");
|
||||
ModelProviderType = cfg.GetValue<string>("Provider")?.ToLower();
|
||||
@@ -26,23 +46,37 @@ public class ThinkingProvider
|
||||
throw new IndexOutOfRangeException("Unknown thinking provider: " + ModelProviderType);
|
||||
}
|
||||
|
||||
Kernel = builder.Build();
|
||||
return builder.Build();
|
||||
}
|
||||
|
||||
private void InitializeHelperFunctions()
|
||||
{
|
||||
// Add Solar Network tools plugin
|
||||
Kernel.ImportPluginFromFunctions("helper_functions", [
|
||||
_kernel.ImportPluginFromFunctions("helper_functions", [
|
||||
KernelFunctionFactory.CreateFromMethod(async (string userId) =>
|
||||
{
|
||||
// MOCK: simulate fetching user profile
|
||||
await Task.Delay(100);
|
||||
return $"{{\"userId\":\"{userId}\",\"name\":\"MockUser\",\"bio\":\"Loves music and tech.\"}}";
|
||||
var request = new GetAccountRequest { Id = userId };
|
||||
var response = await _accountClient.GetAccountAsync(request);
|
||||
return JsonSerializer.Serialize(response, GrpcTypeHelper.SerializerOptions);
|
||||
}, "get_user_profile", "Get a user profile from the Solar Network."),
|
||||
KernelFunctionFactory.CreateFromMethod(async (string topic) =>
|
||||
KernelFunctionFactory.CreateFromMethod(async (string postId) =>
|
||||
{
|
||||
// MOCK: simulate fetching recent posts
|
||||
await Task.Delay(200);
|
||||
return
|
||||
$"[{{\"postId\":\"p1\",\"topic\":\"{topic}\",\"content\":\"Mock post content 1.\"}}, {{\"postId\":\"p2\",\"topic\":\"{topic}\",\"content\":\"Mock post content 2.\"}}]";
|
||||
var request = new GetPostRequest { Id = postId };
|
||||
var response = await _postClient.GetPostAsync(request);
|
||||
return JsonSerializer.Serialize(response, GrpcTypeHelper.SerializerOptions);
|
||||
}, "get_post", "Get a single post by ID from the Solar Network."),
|
||||
KernelFunctionFactory.CreateFromMethod(async (string query) =>
|
||||
{
|
||||
var request = new SearchPostsRequest { Query = query, PageSize = 10 };
|
||||
var response = await _postClient.SearchPostsAsync(request);
|
||||
return JsonSerializer.Serialize(response.Posts, GrpcTypeHelper.SerializerOptions);
|
||||
}, "search_posts", "Search posts by query from the Solar Network."),
|
||||
KernelFunctionFactory.CreateFromMethod(async () =>
|
||||
{
|
||||
var request = new ListPostsRequest { PageSize = 10 };
|
||||
var response = await _postClient.ListPostsAsync(request);
|
||||
return JsonSerializer.Serialize(response.Posts, GrpcTypeHelper.SerializerOptions);
|
||||
}, "get_recent_posts", "Get recent posts from the Solar Network.")
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user