Merge pull request #103 from hchen2020/master

Add GetVectors batch interface.
This commit is contained in:
Haiping 2023-08-15 12:58:08 -05:00 committed by GitHub
commit 5c4f2214f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 50 additions and 37 deletions

View file

@ -27,21 +27,6 @@ public interface IConversationService
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);
}

View file

@ -11,5 +11,6 @@ public interface IConversationStateService
ConversationState Load();
string GetState(string name);
void SetState(string name, string value);
void CleanState();
void Save();
}

View file

@ -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

View file

@ -4,4 +4,5 @@ public interface ITextEmbedding
{
int Dimension { get; }
float[] GetVector(string text);
List<float[]> GetVectors(List<string> texts);
}

View file

@ -1,4 +1,3 @@
using BotSharp.Abstraction.Agents.Enums;
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.Functions;
using BotSharp.Abstraction.MLTasks;
@ -79,18 +78,6 @@ public class ConversationService : IConversationService
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
@ -109,11 +96,11 @@ public class ConversationService : IConversationService
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)
{
@ -162,6 +149,12 @@ public class ConversationService : IConversationService
await onMessageReceived(msg);
}
// Clean conversation
if (msg.IsConversationEnd)
{
stateService.CleanState();
}
});
return result;

View file

@ -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;
@ -89,6 +87,11 @@ public class ConversationStateService : IConversationStateService, IDisposable
_logger.LogInformation($"Saved state {_conversationId}");
}
public void CleanState()
{
File.Delete(_file);
}
private string GetStorageFile(string conversationId)
{
var dir = Path.Combine(_dbSettings.FileRepository, "conversations", conversationId);

View file

@ -26,4 +26,9 @@ public class TextEmbeddingProvider : ITextEmbedding
return _embedder.GetEmbeddings(text);
}
public List<float[]> GetVectors(List<string> texts)
{
throw new NotImplementedException();
}
}

View file

@ -124,6 +124,15 @@ public class ChatCompletionProvider : IChatCompletion
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)
{

View file

@ -16,6 +16,8 @@ using Microsoft.Extensions.DependencyInjection;
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;
@ -60,15 +62,16 @@ public class ChatbotUiController : ControllerBase, IApiAdapter
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

View file

@ -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;
@ -42,4 +43,14 @@ public class fastTextEmbeddingProvider : ITextEmbedding
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;
}
}