Skip to content

Add GetVectors batch interface. #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 15, 2023
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 @@ -27,21 +27,6 @@ Task<bool> SendMessage(string agentId,
Func<RoleDialogModel, Task> onMessageReceived,
Func<RoleDialogModel, Task> onFunctionExecuting);

/// <summary>
/// Send message to LLM if frontend passed over the dialog history
/// </summary>
/// <param name="agentId"></param>
/// <param name="conversationId"></param>
/// <param name="wholeDialogs"></param>
/// <param name="onMessageReceived"></param>
/// <param name="onFunctionExecuting">This delegate is useful when you want to report progress on UI</param>
/// <returns></returns>
Task<bool> SendMessage(string agentId,
string conversationId,
List<RoleDialogModel> wholeDialogs,
Func<RoleDialogModel, Task> onMessageReceived,
Func<RoleDialogModel, Task> onFunctionExecuting);

List<RoleDialogModel> GetDialogHistory(string conversationId, int lastCount = 20);
Task CleanHistory(string agentId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public interface IConversationStateService
ConversationState Load();
string GetState(string name);
void SetState(string name, string value);
void CleanState();
void Save();
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public class RoleDialogModel
/// When function callback has been executed, system will pass result to LLM again,
/// Set this property to True to stop calling LLM.
/// </summary>
public bool StopSubsequentInteraction { get;set; }
public bool StopSubsequentInteraction { get; set; }

public bool IsConversationEnd { get; set; }

/// <summary>
/// Channel name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ public interface ITextEmbedding
{
int Dimension { get; }
float[] GetVector(string text);
List<float[]> GetVectors(List<string> texts);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using BotSharp.Abstraction.Agents.Enums;
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.Functions;
using BotSharp.Abstraction.MLTasks;
Expand Down Expand Up @@ -79,18 +78,6 @@ public async Task<bool> SendMessage(string agentId, string conversationId,

var wholeDialogs = GetDialogHistory(conversationId);

var response = await SendMessage(agentId, conversationId, wholeDialogs,
onMessageReceived: onMessageReceived,
onFunctionExecuting: onFunctionExecuting);

return response;
}

public async Task<bool> SendMessage(string agentId, string conversationId,
List<RoleDialogModel> wholeDialogs,
Func<RoleDialogModel, Task> onMessageReceived,
Func<RoleDialogModel, Task> onFunctionExecuting)
{
var converation = await GetConversation(conversationId);

// Create conversation if this conversation not exists
Expand All @@ -109,11 +96,11 @@ public async Task<bool> SendMessage(string agentId, string conversationId,
stateService.SetConversation(conversationId);
stateService.Load();
stateService.SetState("agentId", agentId);

// load agent
var agentService = _services.GetRequiredService<IAgentService>();
var agent = await agentService.LoadAgent(agentId);

// Get relevant domain knowledge
/*if (_settings.EnableKnowledgeBase)
{
Expand Down Expand Up @@ -162,6 +149,12 @@ public async Task<bool> SendMessage(string agentId, string conversationId,

await onMessageReceived(msg);
}

// Clean conversation
if (msg.IsConversationEnd)
{
stateService.CleanState();
}
});

return result;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using BotSharp.Abstraction.Conversations.Models;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.Extensions.Logging;
using System.IO;

namespace BotSharp.Core.Conversations.Services;
Expand Down Expand Up @@ -89,6 +87,11 @@ public void Save()
_logger.LogInformation($"Saved state {_conversationId}");
}

public void CleanState()
{
File.Delete(_file);
}

private string GetStorageFile(string conversationId)
{
var dir = Path.Combine(_dbSettings.FileRepository, "conversations", conversationId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ public float[] GetVector(string text)

return _embedder.GetEmbeddings(text);
}

public List<float[]> GetVectors(List<string> texts)
{
throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ public async Task<bool> GetChatCompletionsAsync(Agent agent, List<RoleDialogMode
return true;
}

if (funcContextIn.IsConversationEnd)
{
await onMessageReceived(new RoleDialogModel(ChatRole.Assistant.ToString(), funcContextIn.Content)
{
IsConversationEnd = true
});
return true;
}

// After function is executed, pass the result to LLM
chatCompletionsOptions.Messages.Add(new ChatMessage(ChatRole.Function, funcContextIn.ExecutionResult)
{
Expand Down
11 changes: 7 additions & 4 deletions src/Plugins/BotSharp.Plugin.ChatbotUI/ChatbotUiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
using BotSharp.Abstraction.Conversations;
using BotSharp.Abstraction.Conversations.Models;
using Microsoft.AspNetCore.Authorization;
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Agents.Enums;

namespace BotSharp.Plugin.ChatbotUI.Controllers;

Expand Down Expand Up @@ -60,15 +62,16 @@ public async Task SendMessage([FromBody] OpenAiMessageInput input)
Response.Headers.Add(HeaderNames.Connection, "keep-alive");
var outputStream = Response.Body;

var conversations = input.Messages
var conversation = input.Messages
.Where(x => x.Role == AgentRole.User)
.Select(x => new RoleDialogModel(x.Role, x.Content))
.ToList();
.Last();

var conversationService = _services.GetRequiredService<IConversationService>();

var result = await conversationService.SendMessage(input.AgentId,
input.ConversationId,
conversations,
input.ConversationId,
conversation,
async msg =>
await OnChunkReceived(outputStream, msg),
async fn
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using BotSharp.Abstraction.MLTasks;
using BotSharp.Plugin.MetaAI.Settings;
using FastText.NetWrapper;
using System.Collections.Generic;
using System.IO;

namespace BotSharp.Plugin.MetaAI.Providers;
Expand Down Expand Up @@ -42,4 +43,14 @@ public float[] GetVector(string text)

return _fastText.GetSentenceVector(text);
}

public List<float[]> GetVectors(List<string> texts)
{
var vectors = new List<float[]>();
for (int i = 0; i < texts.Count; i++)
{
vectors.Add(GetVector(texts[i]));
}
return vectors;
}
}