feat: add sparkdesk plugin

This commit is contained in:
geffzhang 2024-03-11 22:01:40 +08:00
parent 1d5a0a3b96
commit a3f8099b8d
7 changed files with 334 additions and 1 deletions

View file

@ -81,7 +81,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.HttpHandler
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.SqlDriver", "src\Plugins\BotSharp.Plugin.SqlDriver\BotSharp.Plugin.SqlDriver.csproj", "{D775DB67-A4B4-44E5-9144-522689590057}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.Dashboard", "src\Plugins\BotSharp.Plugin.Dashboard\BotSharp.Plugin.Dashboard.csproj", "{267998C1-55C2-4ADC-8361-2CDFA5EA6D6C}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.Dashboard", "src\Plugins\BotSharp.Plugin.Dashboard\BotSharp.Plugin.Dashboard.csproj", "{267998C1-55C2-4ADC-8361-2CDFA5EA6D6C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.SparkDesk", "src\Plugins\BotSharp.Plugin.SparkDesk\BotSharp.Plugin.SparkDesk.csproj", "{289E25C8-63F1-4D52-9909-207724DB40CB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -323,6 +325,14 @@ Global
{267998C1-55C2-4ADC-8361-2CDFA5EA6D6C}.Release|Any CPU.Build.0 = Release|Any CPU
{267998C1-55C2-4ADC-8361-2CDFA5EA6D6C}.Release|x64.ActiveCfg = Release|Any CPU
{267998C1-55C2-4ADC-8361-2CDFA5EA6D6C}.Release|x64.Build.0 = Release|Any CPU
{289E25C8-63F1-4D52-9909-207724DB40CB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{289E25C8-63F1-4D52-9909-207724DB40CB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{289E25C8-63F1-4D52-9909-207724DB40CB}.Debug|x64.ActiveCfg = Debug|Any CPU
{289E25C8-63F1-4D52-9909-207724DB40CB}.Debug|x64.Build.0 = Debug|Any CPU
{289E25C8-63F1-4D52-9909-207724DB40CB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{289E25C8-63F1-4D52-9909-207724DB40CB}.Release|Any CPU.Build.0 = Release|Any CPU
{289E25C8-63F1-4D52-9909-207724DB40CB}.Release|x64.ActiveCfg = Release|Any CPU
{289E25C8-63F1-4D52-9909-207724DB40CB}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -364,6 +374,7 @@ Global
{32D9E720-6FE6-4F29-94B1-B10B05BFAD75} = {51AFE054-AE99-497D-A593-69BAEFB5106F}
{D775DB67-A4B4-44E5-9144-522689590057} = {51AFE054-AE99-497D-A593-69BAEFB5106F}
{267998C1-55C2-4ADC-8361-2CDFA5EA6D6C} = {51AFE054-AE99-497D-A593-69BAEFB5106F}
{289E25C8-63F1-4D52-9909-207724DB40CB} = {D5293208-2BEF-42FC-A64C-5954F61720BA}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A9969D89-C98B-40A5-A12B-FC87E55B3A19}

View file

@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>$(LangVersion)</LangVersion>
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
<GenerateDocumentationFile>$(GenerateDocumentationFile)</GenerateDocumentationFile>
<OutputPath>$(SolutionDir)packages</OutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Sdcb.SparkDesk" Version="3.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Infrastructure\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,244 @@
using BotSharp.Abstraction.Agents;
using BotSharp.Abstraction.Agents.Enums;
using BotSharp.Abstraction.Loggers;
using BotSharp.Abstraction.Routing;
namespace BotSharp.Plugin.SparkDesk.Providers;
public class ChatCompletionProvider : IChatCompletion
{
public string Provider => "sparkdesk";
private readonly SparkDeskSettings _settings;
private readonly IServiceProvider _services;
private readonly ILogger _logger;
private string _model;
public ChatCompletionProvider(IServiceProvider services,
SparkDeskSettings settings,
ILogger<ChatCompletionProvider> logger)
{
_services = services;
_settings = settings;
_logger = logger;
_model = $"general{settings.ModelVersion.ToString()}";
}
public async Task<RoleDialogModel> GetChatCompletions(Agent agent, List<RoleDialogModel> conversations)
{
var contentHooks = _services.GetServices<IContentGeneratingHook>().ToList();
// Before chat completion hook
foreach (var hook in contentHooks)
{
await hook.BeforeGenerating(agent, conversations);
}
var client = new SparkDeskClient(appId: _settings.AppId, apiKey: _settings.ApiKey, apiSecret: _settings.ApiSecret);
var (prompt, messages, funcall) = PrepareOptions(agent, conversations);
var response = await client.ChatAsync(modelVersion:_settings.ModelVersion, messages,functions:funcall);
var responseMessage = new RoleDialogModel(AgentRole.Assistant, response.Text)
{
CurrentAgentId = agent.Id,
MessageId = conversations.Last().MessageId
};
if (response.FunctionCall != null)
{
responseMessage = new RoleDialogModel(AgentRole.Function, response.Text)
{
CurrentAgentId = agent.Id,
MessageId = conversations.Last().MessageId,
FunctionName = response.FunctionCall.Name,
FunctionArgs = response.FunctionCall.Arguments
};
// Somethings LLM will generate a function name with agent name.
if (!string.IsNullOrEmpty(responseMessage.FunctionName))
{
responseMessage.FunctionName = responseMessage.FunctionName.Split('.').Last();
}
}
// After chat completion hook
foreach (var hook in contentHooks)
{
await hook.AfterGenerated(responseMessage, new TokenStatsModel
{
Prompt = prompt,
Provider = Provider,
Model = _model,
PromptCount = response.Usage.PromptTokens,
CompletionCount = response.Usage.CompletionTokens
});
}
return responseMessage;
}
public async Task<bool> GetChatCompletionsAsync(Agent agent, List<RoleDialogModel> conversations, Func<RoleDialogModel, Task> onMessageReceived, Func<RoleDialogModel, Task> onFunctionExecuting)
{
var hooks = _services.GetServices<IContentGeneratingHook>().ToList();
// Before chat completion hook
foreach (var hook in hooks)
{
await hook.BeforeGenerating(agent, conversations);
}
var client = new SparkDeskClient(appId: _settings.AppId, apiKey: _settings.ApiKey, apiSecret: _settings.ApiSecret);
var (prompt, messages, funcall) = PrepareOptions(agent, conversations);
var response = await client.ChatAsync(modelVersion: _settings.ModelVersion, messages, functions: funcall);
var msg = new RoleDialogModel(AgentRole.Assistant, response.Text)
{
CurrentAgentId = agent.Id
};
// After chat completion hook
foreach (var hook in hooks)
{
await hook.AfterGenerated(msg, new TokenStatsModel
{
Prompt = prompt,
Provider = Provider,
Model = _model,
PromptCount = response.Usage.PromptTokens,
CompletionCount = response.Usage.CompletionTokens
});
}
if (response.FunctionCall != null)
{
_logger.LogInformation($"[{agent.Name}]: {response.FunctionCall.Name}({response.FunctionCall.Arguments})");
var funcContextIn = new RoleDialogModel(AgentRole.Function, response.Text)
{
CurrentAgentId = agent.Id,
FunctionName = response.FunctionCall.Name,
FunctionArgs = response.FunctionCall.Arguments
};
// Somethings LLM will generate a function name with agent name.
if (!string.IsNullOrEmpty(funcContextIn.FunctionName))
{
funcContextIn.FunctionName = funcContextIn.FunctionName.Split('.').Last();
}
// Execute functions
await onFunctionExecuting(funcContextIn);
}
else
{
// Text response received
await onMessageReceived(msg);
}
return true;
}
public async Task<bool> GetChatCompletionsStreamingAsync(Agent agent, List<RoleDialogModel> conversations, Func<RoleDialogModel, Task> onMessageReceived)
{
var client = new SparkDeskClient(appId: _settings.AppId, apiKey: _settings.ApiKey, apiSecret: _settings.ApiSecret);
var (prompt, messages, funcall) = PrepareOptions(agent, conversations);
await foreach (StreamedChatResponse response in client.ChatAsStreamAsync(modelVersion: _settings.ModelVersion, messages, functions: funcall))
{
if (response.FunctionCall !=null)
{
//Console.Write(choice.FunctionArgumentsUpdate);
await onMessageReceived(new RoleDialogModel(AgentRole.Function, response.Text)
{
CurrentAgentId = agent.Id,
FunctionName = response.FunctionCall.Name,
FunctionArgs = response.FunctionCall.Arguments
});
continue;
}
await onMessageReceived(new RoleDialogModel(AgentRole.Assistant, response.Text)
{
CurrentAgentId = agent.Id
});
}
return true;
}
public void SetModelName(string model)
{
_model = model;
}
private (string, ChatMessage[], FunctionDef[]?) PrepareOptions(Agent agent, List<RoleDialogModel> conversations)
{
var prompt = "";
var functions = new List<FunctionDef>();
var agentService = _services.GetRequiredService<IAgentService>();
if (!string.IsNullOrEmpty(agent.Instruction))
{
prompt += agentService.RenderedInstruction(agent);
}
var routing = _services.GetRequiredService<IRoutingService>();
var router = routing.Router;
var messages = conversations.Select(c => new ChatMessage(c.Role == AgentRole.User ? "user" : "AI", c.Content))
.ToList();
if (agent.Functions != null && agent.Functions.Count > 0)
{
foreach (var function in agent.Functions)
{
functions.Add(ConvertToFunctionDef(function));
}
prompt += "\r\n\r\n[Conversations]\r\n";
foreach (var dialog in conversations)
{
prompt += dialog.Role == AgentRole.Function ?
$"{dialog.Role}: {dialog.FunctionName} => {dialog.Content}\r\n" :
$"{dialog.Role}: {dialog.Content}\r\n";
}
prompt += "\r\n\r\n" + router.Templates.FirstOrDefault(x => x.Name == "response_with_function").Content;
return (prompt, new List<ChatMessage>
{
new ChatMessage("Which function should be used for the next step based on latest user or function response, output your response in JSON:", AgentRole.User),
}.ToArray(), functions.ToArray());
}
return (prompt, messages.ToArray(), null);
}
private FunctionDef ConvertToFunctionDef(BotSharp.Abstraction.Functions.Models.FunctionDef def)
{
var parameters = def.Parameters;
var funcParamsProperties = parameters.Properties;
var requiredList = parameters.Required;
var fundef = new List<FunctionParametersDef>();
if (funcParamsProperties != null)
{
var props = funcParamsProperties.RootElement.EnumerateObject();
while (props.MoveNext())
{
var prop = props.Current;
var name = prop.Name;
bool required = requiredList.Contains(name);
FunctionParametersDef parametersDef = new FunctionParametersDef(name, prop.Value.GetProperty("type").GetRawText(), prop.Value.GetProperty("description").GetRawText(), required);
fundef.Add(parametersDef);
}
}
FunctionDef functionDef = new FunctionDef(def.Name, def.Description, fundef.ToArray());
return functionDef;
}
}

View file

@ -0,0 +1,12 @@
namespace BotSharp.Plugin.SparkDesk;
public class SparkDeskSettings
{
public string AppId { get; set; }
public string ApiKey { get; set; }
public string ApiSecret { get; set; }
public ModelVersion ModelVersion { get; set; }
}

View file

@ -0,0 +1,27 @@
using BotSharp.Plugin.SparkDesk.Providers;
namespace BotSharp.Plugin.SparkDesk;
public class SparkDeskPlugin : IBotSharpPlugin
{
public string Id => "93ad041b-a99e-5766-946e-6a8cc2d4dd4f";
public string Name => "sparkdesk";
public string Description => "xfyun sparkdesk Service including text generation services.";
public SettingsMeta Settings => new SettingsMeta("SparkDesk");
public object GetNewSettingsInstance()
{
return new SparkDeskSettings();
}
public void RegisterDI(IServiceCollection services, IConfiguration config)
{
services.AddScoped(provider =>
{
var settingService = provider.GetRequiredService<ISettingService>();
return settingService.Bind<SparkDeskSettings>("SparkDesk");
});
services.AddScoped<IChatCompletion, ChatCompletionProvider>();
}
}

View file

@ -0,0 +1,17 @@
global using System;
global using System.Collections.Generic;
global using System.Text;
global using System.Threading.Tasks;
global using System.Linq;
global using System.Text.Json;
global using BotSharp.Abstraction.Conversations.Models;
global using BotSharp.Abstraction.Agents.Models;
global using BotSharp.Abstraction.MLTasks;
global using Microsoft.Extensions.Configuration;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.Logging;
global using System.Text.Json.Serialization;
global using BotSharp.Abstraction.Utilities;
global using BotSharp.Abstraction.Plugins;
global using BotSharp.Abstraction.Settings;
global using Sdcb.SparkDesk;

View file

@ -28,6 +28,7 @@
<ItemGroup>
<ProjectReference Include="..\..\tests\BotSharp.Plugin.PizzaBot\BotSharp.Plugin.PizzaBot.csproj" />
<ProjectReference Include="..\Plugins\BotSharp.Plugin.Dashboard\BotSharp.Plugin.Dashboard.csproj" />
<ProjectReference Include="..\Plugins\BotSharp.Plugin.SparkDesk\BotSharp.Plugin.SparkDesk.csproj" />
</ItemGroup>
<ItemGroup Condition="$(SolutionName)==BotSharp">