Skip to content
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
18 changes: 17 additions & 1 deletion src/Infrastructure/BotSharp.Abstraction/Graph/IGraphDb.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
using BotSharp.Abstraction.Graph.Models;
using BotSharp.Abstraction.Graph.Options;
using BotSharp.Abstraction.Graph.Requests;
using BotSharp.Abstraction.Graph.Responses;

namespace BotSharp.Abstraction.Graph;

public interface IGraphDb
{
public string Provider { get; }

Task<GraphSearchData> Search(string query, GraphSearchOptions options);
Task<GraphSearchResult> SearchAsync(string query, GraphSearchOptions? options = null)
=> throw new NotImplementedException();

#region Node
Task<GraphNode?> GetNodeAsync(string graphId, string nodeId)
=> throw new NotImplementedException();
Task<GraphNode> CreateNodeAsync(string graphId, GraphNodeCreationModel node)
=> throw new NotImplementedException();
Task<GraphNode> UpdateNodeAsync(string graphId, string nodeId, GraphNodeUpdateModel node)
=> throw new NotImplementedException();
Task<GraphNode> UpsertNodeAsync(string graphId, string nodeId, GraphNodeUpdateModel node)
=> throw new NotImplementedException();
Task<GraphNodeDeleteResponse?> DeleteNodeAsync(string graphId, string nodeId)
=> throw new NotImplementedException();
#endregion
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using BotSharp.Abstraction.Graph.Models;
using BotSharp.Abstraction.Graph.Options;
using BotSharp.Abstraction.Graph.Requests;
using BotSharp.Abstraction.Graph.Responses;

namespace BotSharp.Abstraction.Graph;

public interface IGraphKnowledgeService
{
Task<GraphSearchResult> SearchAsync(string query, GraphSearchOptions? options = null);

#region Node
Task<GraphNode?> GetNodeAsync(string graphId, string nodeId, GraphNodeOptions? options = null);
Task<GraphNode> CreateNodeAsync(string graphId, GraphNodeCreationModel node, GraphNodeOptions? options = null);
Task<GraphNode> UpdateNodeAsync(string graphId, string nodeId, GraphNodeUpdateModel node, GraphNodeOptions? options = null);
Task<GraphNode> UpsertNodeAsync(string graphId, string nodeId, GraphNodeUpdateModel node, GraphNodeOptions? options = null);
Task<GraphNodeDeleteResponse?> DeleteNodeAsync(string graphId, string nodeId, GraphNodeOptions? options = null);
#endregion
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace BotSharp.Abstraction.Graph.Models;

public class GraphEdge
{
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
namespace BotSharp.Plugin.Membase.Models;
namespace BotSharp.Abstraction.Graph.Models;

public class Node
public class GraphNode
{
public string Id { get; set; } = string.Empty;
public List<string> Labels { get; set; } = new();
public object Properties { get; set; } = new();
public EmbeddingInfo? Embedding { get; set; }
public DateTime Time { get; set; } = DateTime.UtcNow;
public DateTime? Time { get; set; } = DateTime.UtcNow;

public override string ToString()
{
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ namespace BotSharp.Abstraction.Graph.Models;

public class GraphSearchResult
{
public string Result { get; set; }
public string Result { get; set; } = string.Empty;
public string[] Keys { get; set; } = [];
public Dictionary<string, object?>[] Values { get; set; } = [];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace BotSharp.Abstraction.Graph.Options;

public class GraphNodeOptions
{
public string? Provider { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ namespace BotSharp.Abstraction.Graph.Options;

public class GraphSearchOptions
{
public string Method { get; set; }
public string? Provider { get; set; }
public string? GraphId { get; set; }
public Dictionary<string, object>? Arguments { get; set; }
public string? Method { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace BotSharp.Abstraction.Graph.Requests;

public class GraphNodeCreationModel
{
public string? Id { get; set; }
public string[]? Labels { get; set; }
public object? Properties { get; set; }
public GraphNodeEmbedding? Embedding { get; set; }
public DateTime? Time { get; set; }
}

public class GraphNodeEmbedding
{
public string Model { get; set; }
public float[] Vector { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace BotSharp.Abstraction.Graph.Requests;

public class GraphNodeUpdateModel
{
public string Id { get; set; } = null!;
public string[]? Labels { get; set; }
public object? Properties { get; set; }
public GraphNodeEmbedding? Embedding { get; set; }
public DateTime? Time { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace BotSharp.Abstraction.Graph.Responses;

public class GraphNodeDeleteResponse : ResponseBase
{
public string? Message { get; set; }
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ public interface IKnowledgeService
Task<bool> UpsertVectorCollectionData(string collectionName, VectorUpdateModel update);
#endregion

#region Graph
Task<GraphSearchResult> SearchGraphKnowledge(string query, GraphSearchOptions options);
#endregion

#region Document
/// <summary>
/// Save documents and their contents to knowledgebase
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace BotSharp.Abstraction.Utilities;
using BotSharp.Abstraction.Shared.Options;

namespace BotSharp.Abstraction.Shared;

/// <summary>
/// Service for repairing malformed JSON using LLM.
Expand All @@ -10,14 +12,16 @@ public interface IJsonRepairService
/// </summary>
/// <typeparam name="T">Target type</typeparam>
/// <param name="malformedJson">The malformed JSON string</param>
/// <param name="options">The options to fix malformed JSON string</param>
/// <returns>Deserialized object or default if repair fails</returns>
Task<T?> RepairAndDeserialize<T>(string malformedJson);
Task<T?> RepairAndDeserializeAsync<T>(string malformedJson, JsonRepairOptions? options = null);

/// <summary>
/// Repair malformed JSON string.
/// </summary>
/// <param name="malformedJson">The malformed JSON string</param>
/// <param name="options">The options to fix malformed JSON string</param>
/// <returns>Repaired JSON string</returns>
Task<string> Repair(string malformedJson);
Task<string> RepairAsync(string malformedJson, JsonRepairOptions? options = null);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace BotSharp.Abstraction.Shared.Options;

public class JsonRepairOptions : LlmConfigBase
{
/// <summary>
/// Agent id to get instruction
/// </summary>
[JsonPropertyName("agent_id")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? AgentId { get; set; }

/// <summary>
/// Template (prompt) name
/// </summary>
[JsonPropertyName("template_name")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? TemplateName { get; set; }

/// <summary>
/// Data that can be used to fill in the prompt
/// </summary>
[JsonPropertyName("data")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public Dictionary<string, object>? Data { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,14 @@ public static string CleanStr(this string? str)
return str.Replace(" ", "").Replace("\t", "").Replace("\n", "").Replace("\r", "");
}

[GeneratedRegex(@"[^\u0000-\u007F]")]
private static partial Regex NonAsciiCharactersRegex();

public static string CleanJsonStr(this string? str)
{
if (string.IsNullOrWhiteSpace(str)) return string.Empty;

str = str.Replace("```json", string.Empty).Replace("```", string.Empty).Trim();
if (string.IsNullOrWhiteSpace(str))
{
return string.Empty;
}

return NonAsciiCharactersRegex().Replace(str, "");
return str.Replace("```json", string.Empty).Replace("```", string.Empty).Trim();
}

public static T? Json<T>(this string text)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ public string RenderTemplate(Agent agent, string templateName, IDictionary<strin

public ResponseFormatType GetTemplateResponseFormat(Agent agent, string templateName)
{
if(string.IsNullOrEmpty(templateName)) return ResponseFormatType.Text;
if (string.IsNullOrEmpty(templateName))
{
return ResponseFormatType.Text;
}

var template = agent.Templates.FirstOrDefault(x => x.Name == templateName)?.Content ?? string.Empty;
return template.Contains(JsonFormat) ? ResponseFormatType.Json : ResponseFormatType.Text;
Expand Down
2 changes: 1 addition & 1 deletion src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>$(TargetFramework)</TargetFramework>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using BotSharp.Abstraction.Instructs.Settings;
using BotSharp.Abstraction.Plugins.Models;
using BotSharp.Abstraction.Settings;
using BotSharp.Abstraction.Shared;
using BotSharp.Core.Instructs.Hooks;
using BotSharp.Core.Shared;
using Microsoft.Extensions.Configuration;

namespace BotSharp.Core.Instructs;
Expand All @@ -20,6 +22,7 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
return settingService.Bind<InstructionSettings>("Instruction");
});

services.AddScoped<IJsonRepairService, JsonRepairService>();
services.AddScoped<IAgentUtilityHook, InstructUtilityHook>();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using BotSharp.Abstraction.Instructs.Options;
using BotSharp.Abstraction.MLTasks;
using BotSharp.Abstraction.Models;
using BotSharp.Abstraction.Shared;

namespace BotSharp.Core.Instructs;

Expand Down Expand Up @@ -55,7 +56,6 @@ public async Task<InstructResult> Execute(
}

response = await RunLlm(agent, message, instruction, templateName, files, fileOptions, responseFormat);

return response;
}

Expand Down Expand Up @@ -294,12 +294,13 @@ private async Task<InstructResult> RunLlm(
{
result = await GetChatCompletion(chatCompleter, agent, instruction, prompt, message.MessageId, files);
}

// Repair JSON format if needed
responseFormat = responseFormat ?? agentService.GetTemplateResponseFormat(agent, templateName);
responseFormat ??= agentService.GetTemplateResponseFormat(agent, templateName);
if (responseFormat == ResponseFormatType.Json)
{
var jsonRepairService = _services.GetRequiredService<IJsonRepairService>();
result = await jsonRepairService.Repair(result);
result = await jsonRepairService.RepairAsync(result);
}
response.Text = result;
}
Expand Down
19 changes: 0 additions & 19 deletions src/Infrastructure/BotSharp.Core/JsonRepair/JsonRepairPlugin.cs

This file was deleted.

Loading
Loading