Merge pull request #504 from iceljc/bugfix/refine-attachment-load

refine load attachment
This commit is contained in:
C. Oceania 2024-06-19 09:08:00 -05:00 committed by GitHub
commit f12550a440
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 26 additions and 36 deletions

View file

@ -31,7 +31,7 @@ public class LoadAttachmentFn : IFunctionCallback
var wholeDialogs = conv.GetDialogHistory();
var fileTypes = args?.FileTypes?.Split(",")?.ToList() ?? new List<string>();
var dialogs = await AssembleFiles(conv.ConversationId, wholeDialogs, fileTypes);
var agent = await agentService.LoadAgent(AIAssistant);
var agent = await agentService.LoadAgent(!string.IsNullOrEmpty(message.CurrentAgentId) ? message.CurrentAgentId : AIAssistant);
var fileAgent = new Agent
{
Id = agent.Id,

View file

@ -1,10 +1,11 @@
using Microsoft.EntityFrameworkCore;
namespace BotSharp.Core.Files.Hooks;
public class AttachmentProcessingHook : AgentHookBase
{
private readonly IServiceProvider _services;
private readonly AgentSettings _agentSettings;
public override string SelfId => string.Empty;
@ -12,7 +13,6 @@ public class AttachmentProcessingHook : AgentHookBase
: base(services, settings)
{
_services = services;
_agentSettings = settings;
}
public override void OnAgentLoaded(Agent agent)
@ -23,39 +23,29 @@ public class AttachmentProcessingHook : AgentHookBase
if (hasConvFiles)
{
agent.Instruction += "\r\n\r\nIf user wants to describe images or pdf files, please call load_attachment.";
}
agent.Instruction += "\r\n\r\nPlease call load_attachment if user wants to describe files, such as images, pdf.\r\n\r\n";
base.OnAgentLoaded(agent);
}
public override bool OnFunctionsLoaded(List<FunctionDef> functions)
{
var fileService = _services.GetRequiredService<IBotSharpFileService>();
var conv = _services.GetRequiredService<IConversationService>();
var hasConvFiles = fileService.HasConversationUserFiles(conv.ConversationId);
if (hasConvFiles)
{
var json = JsonSerializer.Serialize(new
if (agent.Functions != null)
{
user_request = new
var json = JsonSerializer.Serialize(new
{
type = "string",
description = "The request posted by user, which is related to analyzing requested files. User can request for multiple files to process at one time."
},
file_types = new
{
type = "string",
description = "The file types requested by user to analyze, such as image, png, jpeg, and pdf. There can be multiple file types in a single request. An example output is, 'image,pdf'"
}
});
user_request = new
{
type = "string",
description = "The request posted by user, which is related to analyzing requested files. User can request for multiple files to process at one time."
},
file_types = new
{
type = "string",
description = "The file types requested by user to analyze, such as image, png, jpeg, and pdf. There can be multiple file types in a single request. An example output is, 'image,pdf'"
}
});
functions.Add(new FunctionDef
{
Name = "load_attachment",
Description = "If the user's request is related to analyzing files and/or images, you can call this function to analyze files and images.",
Parameters =
agent.Functions.Add(new FunctionDef
{
Name = "load_attachment",
Description = "If the user's request is related to analyzing files and/or images, you can call this function to analyze files and images.",
Parameters =
{
Properties = JsonSerializer.Deserialize<JsonDocument>(json),
Required = new List<string>
@ -64,8 +54,10 @@ public class AttachmentProcessingHook : AgentHookBase
"file_types"
}
}
});
});
}
}
return base.OnFunctionsLoaded(functions); ;
base.OnAgentLoaded(agent);
}
}

View file

@ -5,7 +5,6 @@ using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Conversations;
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.Files;
using BotSharp.Abstraction.Files.Models;
using BotSharp.Abstraction.Loggers;
using BotSharp.Abstraction.MLTasks;
using BotSharp.Abstraction.Utilities;
@ -16,7 +15,6 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.ComTypes;
using System.Threading.Tasks;
namespace BotSharp.Plugin.AzureOpenAI.Providers;