💄 Optimize the AI agent experience

This commit is contained in:
2025-10-26 12:10:10 +08:00
parent cf6e534d02
commit 92cd6b5f7e
7 changed files with 136 additions and 26 deletions

View File

@@ -26,13 +26,14 @@ public class ThoughtController(ThoughtProvider provider, ThoughtService service)
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();
var accountId = Guid.Parse(currentUser.Id);
// Generate topic if creating new sequence
// Generate a topic if creating a new sequence
string? topic = null;
if (!request.SequenceId.HasValue)
{
// Use AI to summarize topic from user message
// Use AI to summarize a topic from a user message
var summaryHistory = new ChatHistory(
"You are a helpful assistant. Summarize the following user message into a concise topic title (max 100 characters). Direct give the topic you summerized, do not add extra preifx / suffix."
"You are a helpful assistant. Summarize the following user message into a concise topic title (max 100 characters).\n" +
"Direct give the topic you summerized, do not add extra prefix / suffix."
);
summaryHistory.AddUserMessage(request.UserMessage);
@@ -105,7 +106,7 @@ public class ThoughtController(ThoughtProvider provider, ThoughtService service)
))
{
// Write each chunk to the HTTP response as SSE
var data = chunk.Content ?? "";
var data = chunk.ToString();
accumulatedContent.Append(data);
if (string.IsNullOrEmpty(data)) continue;
@@ -121,14 +122,15 @@ public class ThoughtController(ThoughtProvider provider, ThoughtService service)
// Write the topic if it was newly set, then the thought object as JSON to the stream
using (var streamBuilder = new MemoryStream())
{
await streamBuilder.WriteAsync("\n"u8.ToArray());
await streamBuilder.WriteAsync("\n\ndata: "u8.ToArray());
if (topic != null)
{
await streamBuilder.WriteAsync(Encoding.UTF8.GetBytes($"<topic>{sequence.Topic ?? ""}</topic>\n"));
var topicJson = JsonSerializer.Serialize(new { type = "topic", data = sequence.Topic ?? "" });
await streamBuilder.WriteAsync(Encoding.UTF8.GetBytes($"{topicJson}\n\n"));
}
await streamBuilder.WriteAsync(
Encoding.UTF8.GetBytes(JsonSerializer.Serialize(savedThought, GrpcTypeHelper.SerializerOptions)));
var thoughtJson = JsonSerializer.Serialize(new { type = "thought", data = savedThought }, GrpcTypeHelper.SerializerOptions);
await streamBuilder.WriteAsync(Encoding.UTF8.GetBytes($"data: {thoughtJson}\n\n"));
var outputBytes = streamBuilder.ToArray();
await Response.Body.WriteAsync(outputBytes);
await Response.Body.FlushAsync();

View File

@@ -1,22 +1,25 @@
using System.ClientModel;
using System.Text.Json;
using DysonNetwork.Shared.Models;
using DysonNetwork.Shared.Proto;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.Ollama;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using OpenAI;
using PostPinMode = DysonNetwork.Shared.Proto.PostPinMode;
using PostType = DysonNetwork.Shared.Proto.PostType;
namespace DysonNetwork.Insight.Thought;
public class ThoughtProvider
{
private readonly Kernel _kernel;
private readonly PostService.PostServiceClient _postClient;
private readonly AccountService.AccountServiceClient _accountClient;
public Kernel Kernel => _kernel;
public string? ModelProviderType { get; private set; }
public string? ModelDefault { get; private set; }
public Kernel Kernel { get; }
private string? ModelProviderType { get; set; }
private string? ModelDefault { get; set; }
public ThoughtProvider(
IConfiguration configuration,
@@ -27,7 +30,7 @@ public class ThoughtProvider
_postClient = postClient;
_accountClient = accountClient;
_kernel = InitializeThinkingProvider(configuration);
Kernel = InitializeThinkingProvider(configuration);
InitializeHelperFunctions();
}
@@ -63,7 +66,7 @@ public class ThoughtProvider
private void InitializeHelperFunctions()
{
// Add Solar Network tools plugin
_kernel.ImportPluginFromFunctions("helper_functions", [
Kernel.ImportPluginFromFunctions("helper_functions", [
KernelFunctionFactory.CreateFromMethod(async (string userId) =>
{
var request = new GetAccountRequest { Id = userId };
@@ -131,7 +134,7 @@ public class ThoughtProvider
if (tags != null) request.Tags.AddRange(tags);
if (types != null) request.Types_.AddRange(types.Select(t => (PostType)t));
var response = await _postClient.ListPostsAsync(request);
return JsonSerializer.Serialize(response.Posts, GrpcTypeHelper.SerializerOptions);
return JsonSerializer.Serialize(response.Posts.Select(SnPost.FromProtoValue), GrpcTypeHelper.SerializerOptions);
}, "list_posts", "Get posts from the Solar Network with customizable filters.")
]);
}