add file handler
This commit is contained in:
parent
74862eb029
commit
3893a24a96
11
BotSharp.sln
11
BotSharp.sln
|
|
@ -97,6 +97,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.OpenAI", "s
|
|||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.EmailHandler", "src\Plugins\BotSharp.Plugin.EmailHandler\BotSharp.Plugin.EmailHandler.csproj", "{A72B3BEB-E14B-4917-BE44-97EAE4E122D2}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.FileHandler", "src\Plugins\BotSharp.Plugin.FileReader\BotSharp.Plugin.FileHandler.csproj", "{D0C5055E-4849-467F-A078-FB01F8DEC55A}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
|
@ -393,6 +395,14 @@ Global
|
|||
{A72B3BEB-E14B-4917-BE44-97EAE4E122D2}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{A72B3BEB-E14B-4917-BE44-97EAE4E122D2}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{A72B3BEB-E14B-4917-BE44-97EAE4E122D2}.Release|x64.Build.0 = Release|Any CPU
|
||||
{D0C5055E-4849-467F-A078-FB01F8DEC55A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{D0C5055E-4849-467F-A078-FB01F8DEC55A}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{D0C5055E-4849-467F-A078-FB01F8DEC55A}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{D0C5055E-4849-467F-A078-FB01F8DEC55A}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{D0C5055E-4849-467F-A078-FB01F8DEC55A}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{D0C5055E-4849-467F-A078-FB01F8DEC55A}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{D0C5055E-4849-467F-A078-FB01F8DEC55A}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{D0C5055E-4849-467F-A078-FB01F8DEC55A}.Release|x64.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
|
@ -439,6 +449,7 @@ Global
|
|||
{806A0B0E-FEFF-420E-B5B2-C9FCBF890A8C} = {D5293208-2BEF-42FC-A64C-5954F61720BA}
|
||||
{6507D336-3A4D-41D4-81C0-2B900173A5FE} = {D5293208-2BEF-42FC-A64C-5954F61720BA}
|
||||
{A72B3BEB-E14B-4917-BE44-97EAE4E122D2} = {51AFE054-AE99-497D-A593-69BAEFB5106F}
|
||||
{D0C5055E-4849-467F-A078-FB01F8DEC55A} = {51AFE054-AE99-497D-A593-69BAEFB5106F}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {A9969D89-C98B-40A5-A12B-FC87E55B3A19}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ using System.Threading.Tasks;
|
|||
|
||||
namespace BotSharp.Plugin.EmailHandler.Enums
|
||||
{
|
||||
public class Utility
|
||||
public class UtilityName
|
||||
{
|
||||
public const string EmailHandler = "email-handler";
|
||||
}
|
||||
|
|
@ -26,7 +26,7 @@ public class EmailHandlerHook : AgentHookBase
|
|||
{
|
||||
var conv = _services.GetRequiredService<IConversationService>();
|
||||
var isConvMode = conv.IsConversationMode();
|
||||
var isEnabled = !agent.Utilities.IsNullOrEmpty() && agent.Utilities.Contains(Utility.EmailHandler);
|
||||
var isEnabled = !agent.Utilities.IsNullOrEmpty() && agent.Utilities.Contains(UtilityName.EmailHandler);
|
||||
|
||||
if (isConvMode && isEnabled)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ namespace BotSharp.Plugin.EmailHandler.Hooks
|
|||
{
|
||||
public void AddUtilities(List<string> utilities)
|
||||
{
|
||||
utilities.Add(Utility.EmailHandler);
|
||||
utilities.Add(UtilityName.EmailHandler);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,23 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>$(TargetFramework)</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<LangVersion>$(LangVersion)</LangVersion>
|
||||
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
|
||||
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
|
||||
<GenerateDocumentationFile>$(GenerateDocumentationFile)</GenerateDocumentationFile>
|
||||
<OutputPath>$(SolutionDir)packages</OutputPath>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Infrastructure\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Functions\" />
|
||||
<Folder Include="data\" />
|
||||
<Folder Include="LlmContexts\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
namespace BotSharp.Plugin.FileHandler.Enums;
|
||||
|
||||
public class UtilityName
|
||||
{
|
||||
public const string ImageReader = "image-reader";
|
||||
public const string PdfReader = "pdf-reader";
|
||||
}
|
||||
25
src/Plugins/BotSharp.Plugin.FileReader/FileHandlerPlugin.cs
Normal file
25
src/Plugins/BotSharp.Plugin.FileReader/FileHandlerPlugin.cs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
using BotSharp.Abstraction.Settings;
|
||||
using BotSharp.Plugin.FileHandler.Hooks;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace BotSharp.Plugin.FileReader;
|
||||
|
||||
public class FileHandlerPlugin : IBotSharpPlugin
|
||||
{
|
||||
public string Id => "65be5aee-48df-4ff8-a50a-05c8bcd2a793";
|
||||
public string Name => "File Handler";
|
||||
public string Description => "AI reads files, such as image, pdf, excel";
|
||||
public string IconUrl => "https://lirp.cdn-website.com/6f8d6d8a/dms3rep/multi/opt/API_Icon-640w.png";
|
||||
public string[] AgentIds => [];
|
||||
|
||||
public void RegisterDI(IServiceCollection services, IConfiguration config)
|
||||
{
|
||||
services.AddScoped(provider =>
|
||||
{
|
||||
var settingService = provider.GetRequiredService<ISettingService>();
|
||||
return settingService.Bind<FileHandlerSettings>("FileHandler");
|
||||
});
|
||||
|
||||
services.AddScoped<IAgentHook, HttpHandlerHook>();
|
||||
services.AddScoped<IAgentUtilityHook, FileHandlerUtilityHook>();
|
||||
}
|
||||
|
|
@ -0,0 +1,69 @@
|
|||
using BotSharp.Abstraction.Agents.Enums;
|
||||
using BotSharp.Abstraction.Agents.Settings;
|
||||
using BotSharp.Abstraction.Functions.Models;
|
||||
using BotSharp.Abstraction.Repositories;
|
||||
|
||||
namespace BotSharp.Plugin.FileHandler.Hooks;
|
||||
|
||||
public class FileHandlerHook : AgentHookBase, IAgentHook
|
||||
{
|
||||
private const string IMAGE_READER_FN = "image_reader";
|
||||
private const string PDF_READER_FN = "pdf_reader";
|
||||
|
||||
public override string SelfId => string.Empty;
|
||||
|
||||
public FileHandlerHook(IServiceProvider services, AgentSettings settings) : base(services, settings)
|
||||
{
|
||||
}
|
||||
|
||||
public override void OnAgentLoaded(Agent agent)
|
||||
{
|
||||
var conv = _services.GetRequiredService<IConversationService>();
|
||||
var isConvMode = conv.IsConversationMode();
|
||||
|
||||
if (isConvMode)
|
||||
{
|
||||
AddUtility(agent, UtilityName.ImageReader, IMAGE_READER_FN);
|
||||
AddUtility(agent, UtilityName.PdfReader, PDF_READER_FN);
|
||||
}
|
||||
|
||||
base.OnAgentLoaded(agent);
|
||||
}
|
||||
|
||||
private bool IsEnableUtility(Agent agent, string utility)
|
||||
{
|
||||
return !agent.Utilities.IsNullOrEmpty() && agent.Utilities.Contains(utility);
|
||||
}
|
||||
|
||||
private (string, FunctionDef?) GetPromptAndFunction(string functionName)
|
||||
{
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
var agent = db.GetAgent(BuiltInAgentId.UtilityAssistant);
|
||||
var prompt = agent?.Templates?.FirstOrDefault(x => x.Name.IsEqualTo($"{functionName}.fn"))?.Content ?? string.Empty;
|
||||
var loadAttachmentFn = agent?.Functions?.FirstOrDefault(x => x.Name.IsEqualTo(functionName));
|
||||
return (prompt, loadAttachmentFn);
|
||||
}
|
||||
|
||||
private void AddUtility(Agent agent, string utility, string functionName)
|
||||
{
|
||||
if (!IsEnableUtility(agent, utility)) return;
|
||||
|
||||
var (prompt, fn) = GetPromptAndFunction(functionName);
|
||||
if (fn != null)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(prompt))
|
||||
{
|
||||
agent.Instruction += $"\r\n\r\n{prompt}\r\n\r\n";
|
||||
}
|
||||
|
||||
if (agent.Functions == null)
|
||||
{
|
||||
agent.Functions = new List<FunctionDef> { fn };
|
||||
}
|
||||
else
|
||||
{
|
||||
agent.Functions.Add(fn);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
namespace BotSharp.Plugin.FileHandler.Hooks;
|
||||
|
||||
public class FileHandlerUtilityHook : IAgentUtilityHook
|
||||
{
|
||||
public void AddUtilities(List<string> utilities)
|
||||
{
|
||||
utilities.Add(UtilityName.ImageReader);
|
||||
utilities.Add(UtilityName.PdfReader);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
namespace BotSharp.Plugin.FileHandler.Settings;
|
||||
|
||||
public class FileHandlerSettings
|
||||
{
|
||||
}
|
||||
20
src/Plugins/BotSharp.Plugin.FileReader/Using.cs
Normal file
20
src/Plugins/BotSharp.Plugin.FileReader/Using.cs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
global using System;
|
||||
global using System.Collections.Generic;
|
||||
global using System.Text;
|
||||
global using System.Linq;
|
||||
global using System.Text.Json;
|
||||
global using System.Threading.Tasks;
|
||||
global using BotSharp.Abstraction.Agents;
|
||||
global using BotSharp.Abstraction.Conversations;
|
||||
global using BotSharp.Abstraction.Plugins;
|
||||
global using BotSharp.Abstraction.Conversations.Models;
|
||||
global using BotSharp.Abstraction.Functions;
|
||||
global using BotSharp.Abstraction.Agents.Models;
|
||||
global using BotSharp.Abstraction.Templating;
|
||||
global using Microsoft.Extensions.DependencyInjection;
|
||||
global using BotSharp.Abstraction.Utilities;
|
||||
global using BotSharp.Abstraction.Messaging;
|
||||
global using BotSharp.Abstraction.Messaging.Models.RichContent;
|
||||
global using BotSharp.Abstraction.Options;
|
||||
global using BotSharp.Plugin.FileHandler.Enums;
|
||||
global using BotSharp.Plugin.FileHandler.Settings;
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
namespace BotSharp.Plugin.HttpHandler.Enums;
|
||||
|
||||
public class Utility
|
||||
public class UtilityName
|
||||
{
|
||||
public const string HttpHandler = "http-handler";
|
||||
}
|
||||
|
|
@ -40,7 +40,6 @@ public class HandleHttpRequestFn : IFunctionCallback
|
|||
var response = await SendHttpRequest(url, method, content);
|
||||
var responseContent = await HandleHttpResponse(response);
|
||||
message.Content = responseContent;
|
||||
message.StopCompletion = true;
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
|
@ -48,7 +47,6 @@ public class HandleHttpRequestFn : IFunctionCallback
|
|||
var msg = $"Fail when sending http request. Url: {url}, method: {method}, content: {content}";
|
||||
_logger.LogWarning($"{msg}\n(Error: {ex.Message})");
|
||||
message.Content = msg;
|
||||
message.StopCompletion = true;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,11 +21,11 @@ public class HttpHandlerHook : AgentHookBase
|
|||
{
|
||||
var conv = _services.GetRequiredService<IConversationService>();
|
||||
var isConvMode = conv.IsConversationMode();
|
||||
var isEnabled = !agent.Utilities.IsNullOrEmpty() && agent.Utilities.Contains(Utility.HttpHandler);
|
||||
var isEnabled = !agent.Utilities.IsNullOrEmpty() && agent.Utilities.Contains(UtilityName.HttpHandler);
|
||||
|
||||
if (isConvMode && isEnabled)
|
||||
{
|
||||
var (prompt, fn) = GetPromptAndFunction();
|
||||
var (prompt, fn) = GetPromptAndFunction(FUNCTION_NAME);
|
||||
if (fn != null)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(prompt))
|
||||
|
|
@ -47,12 +47,12 @@ public class HttpHandlerHook : AgentHookBase
|
|||
base.OnAgentLoaded(agent);
|
||||
}
|
||||
|
||||
private (string, FunctionDef?) GetPromptAndFunction()
|
||||
private (string, FunctionDef?) GetPromptAndFunction(string functionName)
|
||||
{
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
var agent = db.GetAgent(BuiltInAgentId.UtilityAssistant);
|
||||
var prompt = agent?.Templates?.FirstOrDefault(x => x.Name.IsEqualTo($"{FUNCTION_NAME}.fn"))?.Content ?? string.Empty;
|
||||
var loadAttachmentFn = agent?.Functions?.FirstOrDefault(x => x.Name.IsEqualTo(FUNCTION_NAME));
|
||||
var prompt = agent?.Templates?.FirstOrDefault(x => x.Name.IsEqualTo($"{functionName}.fn"))?.Content ?? string.Empty;
|
||||
var loadAttachmentFn = agent?.Functions?.FirstOrDefault(x => x.Name.IsEqualTo(functionName));
|
||||
return (prompt, loadAttachmentFn);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,6 @@ public class HttpHandlerUtilityHook : IAgentUtilityHook
|
|||
{
|
||||
public void AddUtilities(List<string> utilities)
|
||||
{
|
||||
utilities.Add(Utility.HttpHandler);
|
||||
utilities.Add(UtilityName.HttpHandler);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,16 +1,16 @@
|
|||
global using System;
|
||||
global using System.Collections.Generic;
|
||||
global using System.Text;
|
||||
global using System.Linq;
|
||||
global using System.Text.Json;
|
||||
global using System.Threading.Tasks;
|
||||
global using BotSharp.Abstraction.Conversations;
|
||||
global using BotSharp.Abstraction.Plugins;
|
||||
global using System.Text.Json;
|
||||
global using BotSharp.Abstraction.Conversations.Models;
|
||||
global using System.Threading.Tasks;
|
||||
global using BotSharp.Abstraction.Functions;
|
||||
global using BotSharp.Abstraction.Agents.Models;
|
||||
global using BotSharp.Abstraction.Templating;
|
||||
global using Microsoft.Extensions.DependencyInjection;
|
||||
global using System.Linq;
|
||||
global using BotSharp.Abstraction.Utilities;
|
||||
global using BotSharp.Abstraction.Messaging;
|
||||
global using BotSharp.Abstraction.Messaging.Models.RichContent;
|
||||
|
|
|
|||
|
|
@ -29,6 +29,7 @@
|
|||
<ProjectReference Include="..\..\tests\BotSharp.Plugin.PizzaBot\BotSharp.Plugin.PizzaBot.csproj" />
|
||||
<ProjectReference Include="..\BotSharp.ServiceDefaults\BotSharp.ServiceDefaults.csproj" />
|
||||
<ProjectReference Include="..\Plugins\BotSharp.Plugin.Dashboard\BotSharp.Plugin.Dashboard.csproj" />
|
||||
<ProjectReference Include="..\Plugins\BotSharp.Plugin.FileReader\BotSharp.Plugin.FileHandler.csproj" />
|
||||
<ProjectReference Include="..\Plugins\BotSharp.Plugin.MetaGLM\BotSharp.Plugin.MetaGLM.csproj" />
|
||||
<ProjectReference Include="..\Plugins\BotSharp.Plugin.SparkDesk\BotSharp.Plugin.SparkDesk.csproj" />
|
||||
</ItemGroup>
|
||||
|
|
|
|||
|
|
@ -295,7 +295,8 @@
|
|||
"BotSharp.Plugin.LLamaSharp",
|
||||
"BotSharp.Plugin.SparkDesk",
|
||||
"BotSharp.Plugin.MetaGLM",
|
||||
"BotSharp.Plugin.HttpHandler"
|
||||
"BotSharp.Plugin.HttpHandler",
|
||||
"BotSharp.Plugin.FileHandler"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue