Thanks to visit codestin.com
Credit goes to github.com

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
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ public class DialogMetaData
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? ToolCallId { get; set; }

[JsonPropertyName("meta_data")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public Dictionary<string, string?>? MetaData { get; set; }

[JsonPropertyName("sender_id")]
public string? SenderId { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,18 @@ public class RoleDialogModel : ITrackableMessage
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? FunctionName { get; set; }

[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? ToolCallId { get; set; }

[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? PostbackFunctionName { get; set; }

[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? FunctionArgs { get; set; }

[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? ToolCallId { get; set; }

[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public Dictionary<string, string?>? MetaData { get; set; }

/// <summary>
/// Set this flag is in OnFunctionExecuting, if true, it won't be executed by InvokeFunction.
/// </summary>
Expand Down Expand Up @@ -189,9 +192,9 @@ public static RoleDialogModel From(RoleDialogModel source,
MessageId = source.MessageId,
MessageType = source.MessageType,
MessageLabel = source.MessageLabel,
FunctionArgs = source.FunctionArgs,
FunctionName = source.FunctionName,
ToolCallId = source.ToolCallId,
FunctionName = source.FunctionName,
FunctionArgs = source.FunctionArgs,
Indication = source.Indication,
PostbackFunctionName = source.PostbackFunctionName,
RichContent = source.RichContent,
Expand All @@ -200,7 +203,8 @@ public static RoleDialogModel From(RoleDialogModel source,
Instruction = source.Instruction,
Data = source.Data,
IsStreaming = source.IsStreaming,
Annotations = source.Annotations
Annotations = source.Annotations,
MetaData = source.MetaData != null ? new(source.MetaData) : null
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ public List<RoleDialogModel> GetDialogs(string conversationId)
MessageLabel = meta?.MessageLabel,
CreatedAt = meta?.CreatedTime ?? default,
SenderId = senderId,
ToolCallId = meta?.ToolCallId,
FunctionName = meta?.FunctionName,
FunctionArgs = meta?.FunctionArgs,
ToolCallId = meta?.ToolCallId,
MetaData = meta?.MetaData,
RichContent = richContent,
SecondaryContent = secondaryContent,
SecondaryRichContent = secondaryRichContent,
Expand Down Expand Up @@ -110,9 +111,10 @@ public List<RoleDialogModel> GetDialogs(string conversationId)
MessageId = dialog.MessageId,
MessageType = dialog.MessageType,
MessageLabel = dialog.MessageLabel,
ToolCallId = dialog.ToolCallId,
FunctionName = dialog.FunctionName,
FunctionArgs = dialog.FunctionArgs,
ToolCallId = dialog.ToolCallId,
MetaData = dialog.MetaData,
CreatedTime = dialog.CreatedAt
};

Expand All @@ -139,6 +141,7 @@ public List<RoleDialogModel> GetDialogs(string conversationId)
MessageLabel = dialog.MessageLabel,
SenderId = dialog.SenderId,
FunctionName = dialog.FunctionName,
MetaData = dialog.MetaData,
CreatedTime = dialog.CreatedAt
};

Expand Down
13 changes: 10 additions & 3 deletions src/Infrastructure/BotSharp.Core/Plugins/PluginLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ public PluginDef UpdatePluginStatus(IServiceProvider services, string id, bool e
foreach (var agentId in dependentAgentIds)
{
var agent = agentService.LoadAgent(agentId).ConfigureAwait(false).GetAwaiter().GetResult();
if (agent == null)
{
continue;
}

agent.Disabled = false;
agentService.UpdateAgent(agent, AgentField.Disabled);

Expand All @@ -184,11 +189,13 @@ public PluginDef UpdatePluginStatus(IServiceProvider services, string id, bool e
foreach (var agentId in plugin.AgentIds)
{
var agent = agentService.LoadAgent(agentId).ConfigureAwait(false).GetAwaiter().GetResult();
if (agent != null)
if (agent == null)
{
agent.Disabled = true;
agentService.UpdateAgent(agent, AgentField.Disabled);
continue;
}

agent.Disabled = true;
agentService.UpdateAgent(agent, AgentField.Disabled);
}
}
return plugin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public async Task<bool> InvokeAgent(
message.ToolCallId = response.ToolCallId;
message.FunctionName = response.FunctionName;
message.FunctionArgs = response.FunctionArgs;
message.MetaData = response.MetaData != null ? new(response.MetaData) : null;
message.Indication = response.Indication;
message.CurrentAgentId = agent.Id;
message.IsStreaming = response.IsStreaming;
Expand All @@ -74,6 +75,7 @@ public async Task<bool> InvokeAgent(

message = RoleDialogModel.From(message, role: AgentRole.Assistant, content: response.Content);
message.CurrentAgentId = agent.Id;
message.MetaData = response.MetaData != null ? new(response.MetaData) : null;
message.IsStreaming = response.IsStreaming;
message.MessageLabel = response.MessageLabel;
dialogs.Add(message);
Expand Down
6 changes: 6 additions & 0 deletions src/Plugins/BotSharp.Plugin.GoogleAI/Constants/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace BotSharp.Plugin.GoogleAI.Constants;

internal static class Constants
{
internal const string ThoughtSignature = "thought_signature";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using GenerativeAI.Types;

namespace BotSharp.Plugin.GoogleAI.Models.Chat;

internal class ChatThoughtModel
{
internal FunctionCall? ToolCall { get; set; }
internal string? ThoughtSignature { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using Azure;
using BotSharp.Abstraction.Files;
using BotSharp.Abstraction.Files.Models;
using BotSharp.Abstraction.Files.Utilities;
using BotSharp.Abstraction.Hooks;
using BotSharp.Abstraction.MessageHub.Models;
using BotSharp.Core.Infrastructures.Streams;
using BotSharp.Core.MessageHub;
using Fluid;
using GenerativeAI;
using GenerativeAI.Core;
using GenerativeAI.Types;
Expand All @@ -26,6 +24,7 @@ public class ChatCompletionProvider : IChatCompletion
public string Model => _model;

private GoogleAiSettings _settings;

public ChatCompletionProvider(
IServiceProvider services,
GoogleAiSettings googleSettings,
Expand Down Expand Up @@ -58,13 +57,18 @@ public async Task<RoleDialogModel> GetChatCompletions(Agent agent, List<RoleDial
RoleDialogModel responseMessage;
if (response.GetFunction() != null)
{
var toolCall = response.GetFunction();
responseMessage = new RoleDialogModel(AgentRole.Function, text)
{
CurrentAgentId = agent.Id,
MessageId = conversations.LastOrDefault()?.MessageId ?? string.Empty,
ToolCallId = part.FunctionCall.Name,
FunctionName = part.FunctionCall.Name,
FunctionArgs = part.FunctionCall.Args?.ToJsonString(),
ToolCallId = toolCall?.Id,
FunctionName = toolCall?.Name,
FunctionArgs = toolCall?.Args?.ToJsonString(),
MetaData = new Dictionary<string, string?>
{
[Constants.ThoughtSignature] = part?.ThoughtSignature
},
RenderedInstruction = string.Join("\r\n", renderedInstructions)
};
}
Expand All @@ -74,6 +78,10 @@ public async Task<RoleDialogModel> GetChatCompletions(Agent agent, List<RoleDial
{
CurrentAgentId = agent.Id,
MessageId = conversations.LastOrDefault()?.MessageId ?? string.Empty,
MetaData = new Dictionary<string, string?>
{
[Constants.ThoughtSignature] = part?.ThoughtSignature
},
RenderedInstruction = string.Join("\r\n", renderedInstructions)
};
}
Expand Down Expand Up @@ -117,6 +125,10 @@ public async Task<bool> GetChatCompletionsAsync(Agent agent, List<RoleDialogMode
var msg = new RoleDialogModel(AgentRole.Assistant, text)
{
CurrentAgentId = agent.Id,
MetaData = new Dictionary<string, string?>
{
[Constants.ThoughtSignature] = part?.ThoughtSignature
},
RenderedInstruction = string.Join("\r\n", renderedInstructions)
};

Expand Down Expand Up @@ -145,6 +157,10 @@ public async Task<bool> GetChatCompletionsAsync(Agent agent, List<RoleDialogMode
ToolCallId = toolCall?.Id,
FunctionName = toolCall?.Name,
FunctionArgs = toolCall?.Args?.ToJsonString(),
MetaData = new Dictionary<string, string?>
{
[Constants.ThoughtSignature] = part?.ThoughtSignature
},
RenderedInstruction = string.Join("\r\n", renderedInstructions)
};

Expand Down Expand Up @@ -195,6 +211,7 @@ public async Task<RoleDialogModel> GetChatCompletionsStreamingAsync(Agent agent,
});

using var textStream = new RealtimeTextStream();
ChatThoughtModel? thoughtModel = null;
UsageMetadata? tokenUsage = null;

var responseMessage = new RoleDialogModel(AgentRole.Assistant, string.Empty)
Expand All @@ -212,36 +229,47 @@ public async Task<RoleDialogModel> GetChatCompletionsStreamingAsync(Agent agent,
}

var part = candidate?.Content?.Parts?.FirstOrDefault();
thoughtModel = part?.FunctionCall != null
? new() { ToolCall = part.FunctionCall, ThoughtSignature = part.ThoughtSignature }
: thoughtModel;

if (!string.IsNullOrEmpty(part?.Text))
{
var text = part.Text;
textStream.Collect(text);

var content = new RoleDialogModel(AgentRole.Assistant, text)
{
CurrentAgentId = agent.Id,
MessageId = messageId
};
hub.Push(new()
{
EventName = ChatEvent.OnReceiveLlmStreamMessage,
RefId = conv.ConversationId,
Data = content
Data = new RoleDialogModel(AgentRole.Assistant, text)
{
CurrentAgentId = agent.Id,
MessageId = messageId
}
});
}

if (candidate.FinishReason == FinishReason.STOP)
if (candidate!.FinishReason == FinishReason.STOP)
{
if (part?.FunctionCall != null)
var thought = part?.FunctionCall != null
? new() { ToolCall = part.FunctionCall, ThoughtSignature = part.ThoughtSignature }
: thoughtModel;
var functionCall = thought?.ToolCall;

if (functionCall != null)
{
var functionCall = part.FunctionCall;
responseMessage = new RoleDialogModel(AgentRole.Function, string.Empty)
{
CurrentAgentId = agent.Id,
MessageId = messageId,
ToolCallId = functionCall.Id,
FunctionName = functionCall.Name,
FunctionArgs = functionCall.Args?.ToString() ?? string.Empty
FunctionArgs = functionCall.Args?.ToJsonString(),
MetaData = new Dictionary<string, string?>
{
[Constants.ThoughtSignature] = thought?.ThoughtSignature
}
};

#if DEBUG
Expand All @@ -259,7 +287,11 @@ public async Task<RoleDialogModel> GetChatCompletionsStreamingAsync(Agent agent,
{
CurrentAgentId = agent.Id,
MessageId = messageId,
IsStreaming = true
IsStreaming = true,
MetaData = new Dictionary<string, string?>
{
[Constants.ThoughtSignature] = part?.ThoughtSignature
}
};
}

Expand All @@ -272,7 +304,11 @@ public async Task<RoleDialogModel> GetChatCompletionsStreamingAsync(Agent agent,
{
CurrentAgentId = agent.Id,
MessageId = messageId,
IsStreaming = true
IsStreaming = true,
MetaData = new Dictionary<string, string?>
{
[Constants.ThoughtSignature] = part?.ThoughtSignature
}
};

tokenUsage = response?.UsageMetadata;
Expand Down Expand Up @@ -381,6 +417,7 @@ public void SetModelName(string model)
contents.Add(new Content([
new Part()
{
ThoughtSignature = message.MetaData?.GetValueOrDefault(Constants.ThoughtSignature, null),
FunctionCall = new FunctionCall
{
Id = message.ToolCallId,
Expand All @@ -393,6 +430,7 @@ public void SetModelName(string model)
contents.Add(new Content([
new Part()
{
ThoughtSignature = message.MetaData?.GetValueOrDefault(Constants.ThoughtSignature, null),
FunctionResponse = new FunctionResponse
{
Id = message.ToolCallId,
Expand All @@ -410,7 +448,14 @@ public void SetModelName(string model)
else if (message.Role == AgentRole.User)
{
var text = message.LlmContent;
var contentParts = new List<Part> { new() { Text = text } };
var contentParts = new List<Part>
{
new()
{
Text = text,
ThoughtSignature = message.MetaData?.GetValueOrDefault(Constants.ThoughtSignature, null)
}
};

if (allowMultiModal && !message.Files.IsNullOrEmpty())
{
Expand All @@ -422,7 +467,14 @@ public void SetModelName(string model)
else if (message.Role == AgentRole.Assistant)
{
var text = message.LlmContent;
var contentParts = new List<Part> { new() { Text = text } };
var contentParts = new List<Part>
{
new()
{
Text = text,
ThoughtSignature = message.MetaData?.GetValueOrDefault(Constants.ThoughtSignature, null)
}
};

if (allowMultiModal && !message.Files.IsNullOrEmpty())
{
Expand Down
2 changes: 2 additions & 0 deletions src/Plugins/BotSharp.Plugin.GoogleAI/Using.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@
global using BotSharp.Abstraction.Functions.Models;
global using BotSharp.Abstraction.Loggers;

global using BotSharp.Plugin.GoogleAI.Constants;
global using BotSharp.Plugin.GoogleAI.Models.Chat;
global using BotSharp.Plugin.GoogleAi.Settings;
global using BotSharp.Plugin.GoogleAi.Providers.Chat;
Loading
Loading