Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,6 @@ Task<bool> SendMessage(string agentId,
/// <param name="excludedStates"></param>
/// <returns></returns>
Task UpdateBreakpoint(bool resetStates = false, string? reason = null, params string[] excludedStates);

Task<string> GetConversationSummary(string conversationId);
}
6 changes: 5 additions & 1 deletion src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
Expand Down Expand Up @@ -56,6 +56,7 @@
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\agent.json" />
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\instruction.liquid" />
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\.welcome.liquid" />
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\conversation.summary.liquid" />
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.hf.liquid" />
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.naive.liquid" />
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.sequential.get_remaining_task.liquid" />
Expand Down Expand Up @@ -142,6 +143,9 @@
<Content Include="data\agents\dfd9b46d-d00c-40af-8a75-3fbdc2b89869\templates\instruction.reviewer.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\conversation.summary.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\plugins\config.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using BotSharp.Abstraction.MLTasks;
using BotSharp.Abstraction.Templating;

namespace BotSharp.Core.Conversations.Services;

public partial class ConversationService
{
public async Task<string> GetConversationSummary(string conversationId)
{
if (string.IsNullOrEmpty(conversationId)) return string.Empty;

var routing = _services.GetRequiredService<IRoutingService>();
var agentService = _services.GetRequiredService<IAgentService>();

var dialogs = _storage.GetDialogs(conversationId);
if (dialogs.IsNullOrEmpty()) return string.Empty;

var router = await agentService.LoadAgent(AIAssistant);
var prompt = GetPrompt(router);
var summary = await Summarize(router, prompt, dialogs);

return summary;
}

private string GetPrompt(Agent agent)
{
var template = agent.Templates.First(x => x.Name == "conversation.summary").Content;
var render = _services.GetRequiredService<ITemplateRender>();
return render.Render(template, new Dictionary<string, object> { });
}

private async Task<string> Summarize(Agent agent, string prompt, List<RoleDialogModel> dialogs)
{
var provider = "openai";
string? model;

var providerService = _services.GetRequiredService<ILlmProviderService>();
var modelSettings = providerService.GetProviderModels(provider);
var modelSetting = modelSettings.FirstOrDefault(x => x.Name.IsEqualTo("gpt4-turbo") || x.Name.IsEqualTo("gpt-4o"));

if (modelSetting != null)
{
model = modelSetting.Name;
}
else
{
provider = agent?.LlmConfig?.Provider;
model = agent?.LlmConfig?.Model;
if (provider == null || model == null)
{
var agentSettings = _services.GetRequiredService<AgentSettings>();
provider = agentSettings.LlmConfig.Provider;
model = agentSettings.LlmConfig.Model;
}
}

var chatCompletion = CompletionProvider.GetChatCompletion(_services, provider, model);
var response = await chatCompletion.GetChatCompletions(new Agent
{
Id = agent.Id,
Name = agent.Name,
Instruction = prompt
}, dialogs);

return response.Content;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public partial class ConversationService : IConversationService
private readonly IConversationStorage _storage;
private readonly IConversationStateService _state;
private string _conversationId;
private const string AIAssistant = "01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a";

public string ConversationId => _conversationId;

public IConversationStateService States => _state;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Please summarize the conversation.

*** Super Important! Please consider the entire conversation. Do not only consider the recent sentences. ***
** Please do not respond to the latest conversation.
** If there are different topics in the conversation, please summarize each topic in different sentences and list them with bullets.
* Please use concise sentences to summarize each topic.
* Please do not include excessive details in the summaries.
* Please use 'user' instead of 'you', 'he' or 'she'.
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ public async Task<ConversationViewModel> GetConversation([FromRoute] string conv
return result;
}

[HttpGet("/conversation/{conversationId}/summary")]
public async Task<string> GetConversationSummary([FromRoute] string conversationId)
{
var service = _services.GetRequiredService<IConversationService>();
return await service.GetConversationSummary(conversationId);
}

[HttpGet("/conversation/{conversationId}/user")]
public async Task<UserViewModel> GetConversationUser([FromRoute] string conversationId)
{
Expand Down