Skip to content

Change to read_file #528

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 8 commits into from
Jul 7, 2024
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 @@ -2,6 +2,6 @@ namespace BotSharp.Abstraction.Agents.Enums;

public class AgentUtility
{
public const string FileAnalyzer = "file-analyzer";
public const string FileReader = "file-reader";
public const string ImageGenerator = "image-generator";
}
8 changes: 4 additions & 4 deletions src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@
<ItemGroup>
<None Remove="data\agents\00000000-0000-0000-0000-000000000000\agent.json" />
<None Remove="data\agents\00000000-0000-0000-0000-000000000000\functions\generate_image.json" />
<None Remove="data\agents\00000000-0000-0000-0000-000000000000\functions\read_file.json" />
<None Remove="data\agents\00000000-0000-0000-0000-000000000000\instruction.liquid" />
<None Remove="data\agents\00000000-0000-0000-0000-000000000000\functions\load_attachment.json" />
<None Remove="data\agents\00000000-0000-0000-0000-000000000000\templates\generate_image.fn.liquid" />
<None Remove="data\agents\00000000-0000-0000-0000-000000000000\templates\load_attachment.fn.liquid" />
<None Remove="data\agents\00000000-0000-0000-0000-000000000000\templates\read_file.fn.liquid" />
<None Remove="data\agents\01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b\agent.json" />
<None Remove="data\agents\01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b\functions.json" />
<None Remove="data\agents\01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b\functions\human_intervention_needed.json" />
Expand Down Expand Up @@ -159,10 +159,10 @@
<Content Include="data\agents\00000000-0000-0000-0000-000000000000\instruction.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\00000000-0000-0000-0000-000000000000\functions\load_attachment.json">
<Content Include="data\agents\00000000-0000-0000-0000-000000000000\functions\read_file.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\00000000-0000-0000-0000-000000000000\templates\load_attachment.fn.liquid">
<Content Include="data\agents\00000000-0000-0000-0000-000000000000\templates\read_file.fn.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\00000000-0000-0000-0000-000000000000\functions\generate_image.json">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public async Task<bool> SendMessage(string agentId,

var content = $"Received [{agent.Name}] {message.Role}: {message.Content}";
#if DEBUG
Console.WriteLine(content, Color.GreenYellow);
Console.WriteLine(content);
#else
_logger.LogInformation(content);
#endif
Expand Down Expand Up @@ -103,7 +103,7 @@ private async Task HandleAssistantMessage(RoleDialogModel response, Func<RoleDia
response.Role = AgentRole.Assistant;
var text = $"Sending [{agentName}] {response.Role}: {response.Content}";
#if DEBUG
Console.WriteLine(text, Color.Yellow);
Console.WriteLine(text);
#else
_logger.LogInformation(text);
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void PrintStatistics()
}
var stats = $"Token Usage: {_promptTokenCount} prompt + {_completionTokenCount} completion = {Total} total tokens ({_timer.ElapsedMilliseconds / 1000f:f2}s). One-Way cost: {Cost:C4}, accumulated cost: {AccumulatedCost:C4}. [{_model}]";
#if DEBUG
Console.WriteLine(stats, Color.DarkGray);
Console.WriteLine(stats);
#else
_logger.LogInformation(stats);
#endif
Expand Down
4 changes: 2 additions & 2 deletions src/Infrastructure/BotSharp.Core/Files/FilePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
{
services.AddScoped<IBotSharpFileService, BotSharpFileService>();

services.AddScoped<IAgentHook, FileAnalyzerHook>();
services.AddScoped<IAgentUtilityHook, FileAnalyzerUtilityHook>();
services.AddScoped<IAgentHook, FileReaderHook>();
services.AddScoped<IAgentUtilityHook, FileReaderUtilityHook>();
services.AddScoped<IAgentHook, ImageGeneratorHook>();
services.AddScoped<IAgentUtilityHook, ImageGeneratorUtilityHook>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@

namespace BotSharp.Core.Files.Functions;

public class LoadAttachmentFn : IFunctionCallback
public class ReadFileFn : IFunctionCallback
{
public string Name => "load_attachment";
public string Indication => "Analyzing files";
public string Name => "read_file";
public string Indication => "Reading files";

private readonly IServiceProvider _services;
private readonly ILogger<LoadAttachmentFn> _logger;
private readonly ILogger<ReadFileFn> _logger;
private readonly IEnumerable<string> _imageTypes = new List<string> { "image", "images", "png", "jpg", "jpeg" };
private readonly IEnumerable<string> _pdfTypes = new List<string> { "pdf" };
private static string UTILITY_ASSISTANT = Guid.Empty.ToString();

public LoadAttachmentFn(
public ReadFileFn(
IServiceProvider services,
ILogger<LoadAttachmentFn> logger)
ILogger<ReadFileFn> logger)
{
_services = services;
_logger = logger;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
namespace BotSharp.Core.Files.Hooks;

public class FileAnalyzerHook : AgentHookBase
public class FileReaderHook : AgentHookBase
{
private static string UTILITY_ASSISTANT = Guid.Empty.ToString();
private static string FUNCTION_NAME = "load_attachment";
private static string FUNCTION_NAME = "read_file";

public override string SelfId => string.Empty;

public FileAnalyzerHook(IServiceProvider services, AgentSettings settings)
public FileReaderHook(IServiceProvider services, AgentSettings settings)
: base(services, settings)
{
}
Expand All @@ -16,7 +16,7 @@ public override void OnAgentLoaded(Agent agent)
{
var conv = _services.GetRequiredService<IConversationService>();
var isConvMode = conv.IsConversationMode();
var isEnabled = !agent.Utilities.IsNullOrEmpty() && agent.Utilities.Contains(AgentUtility.FileAnalyzer);
var isEnabled = !agent.Utilities.IsNullOrEmpty() && agent.Utilities.Contains(AgentUtility.FileReader);

if (isConvMode && isEnabled)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace BotSharp.Core.Files.Hooks;

public class FileAnalyzerUtilityHook : IAgentUtilityHook
public class FileReaderUtilityHook : IAgentUtilityHook
{
public void AddUtilities(List<string> utilities)
{
utilities.Add(AgentUtility.FileAnalyzer);
utilities.Add(AgentUtility.FileReader);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "load_attachment",
"name": "read_file",
"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": {
"type": "object",
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Please call read_file if user wants to describe files, such as images, pdf.
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@ private int GetDimension()
{
var state = _services.GetRequiredService<IConversationStateService>();
var stateDimension = state.GetState("embedding_dimension");
var defaultDimension = Dimension > 0 ? Dimension : DEFAULT_DIMENSION;

if (int.TryParse(stateDimension, out var dimension))
{
return dimension > 0 ? dimension :(Dimension > 0 ? Dimension: DEFAULT_DIMENSION);
return dimension > 0 ? dimension : defaultDimension;
}
return Dimension > 0 ? Dimension : DEFAULT_DIMENSION;
return defaultDimension;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private GeneratedImageStyle GetImageStyle(string style)
GeneratedImageStyle retStyle;
switch (value)
{
case "standard":
case "natural":
retStyle = GeneratedImageStyle.Natural;
break;
case "vivid":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@

namespace BotSharp.Plugin.HttpHandler.Functions;

public class HandleHttpRequest : IFunctionCallback
public class HandleHttpRequestFn : IFunctionCallback
{
public string Name => "handle_http_request";
public string Indication => "Handling http request";

private readonly IServiceProvider _services;
private readonly ILogger<HandleHttpRequest> _logger;
private readonly ILogger<HandleHttpRequestFn> _logger;
private readonly IHttpClientFactory _httpClientFactory;
private readonly IHttpContextAccessor _context;
private readonly BotSharpOptions _options;

public HandleHttpRequest(IServiceProvider services,
ILogger<HandleHttpRequest> logger,
public HandleHttpRequestFn(IServiceProvider services,
ILogger<HandleHttpRequestFn> logger,
IHttpClientFactory httpClientFactory,
IHttpContextAccessor context,
BotSharpOptions options)
Expand All @@ -41,14 +41,16 @@ public async Task<bool> Execute(RoleDialogModel message)
var response = await SendHttpRequest(url, method, content);
var responseContent = await HandleHttpResponse(response);
message.RichContent = BuildRichContent(responseContent);
return await Task.FromResult(true);
message.StopCompletion = true;
return true;
}
catch (Exception ex)
{
var msg = $"Fail when sending http request. Url: {url}, method: {method}, content: {content}";
_logger.LogWarning($"{msg}\n(Error: {ex.Message})");
message.RichContent = BuildRichContent($"{msg}");
return await Task.FromResult(false);
message.StopCompletion = true;
return false;
}
}

Expand Down