Skip to content
Open
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 @@ -46,6 +46,10 @@ public class ChatResponseDto : InstructResult
[JsonPropertyName("is_streaming")]
public bool IsStreaming { get; set; }

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

[JsonPropertyName("meta_data")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public Dictionary<string, string?>? MetaData { get; set; }
Expand Down
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("thought")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public Dictionary<string, string?>? Thought { get; set; }

[JsonPropertyName("meta_data")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public Dictionary<string, string?>? MetaData { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public class RoleDialogModel : ITrackableMessage
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? ToolCallId { get; set; }

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

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

Expand Down Expand Up @@ -136,7 +139,6 @@ public class RoleDialogModel : ITrackableMessage
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
public bool IsStreaming { get; set; }


[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
public bool IsFromUser => Role == AgentRole.User;

Expand Down Expand Up @@ -204,6 +206,7 @@ public static RoleDialogModel From(RoleDialogModel source,
Data = source.Data,
IsStreaming = source.IsStreaming,
Annotations = source.Annotations,
Thought = source.Thought != null ? new(source.Thought) : null,
MetaData = source.MetaData != null ? new(source.MetaData) : null
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ public class ReasoningSetting
public class WebSearchSetting
{
public bool IsDefault { get; set; }

[Obsolete("Set SearchContextSize in Parameters")]
public string? SearchContextSize { get; set; }
public Dictionary<string, ModelParamSetting>? Parameters { get; set; }
}
#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public async Task<List<RoleDialogModel>> GetDialogs(string conversationId, Conve
ToolCallId = meta?.ToolCallId,
FunctionName = meta?.FunctionName,
FunctionArgs = meta?.FunctionArgs,
Thought = meta?.Thought,
MetaData = meta?.MetaData,
RichContent = richContent,
SecondaryContent = secondaryContent,
Expand Down Expand Up @@ -119,6 +120,7 @@ public async Task<List<RoleDialogModel>> GetDialogs(string conversationId, Conve
ToolCallId = dialog.ToolCallId,
FunctionName = dialog.FunctionName,
FunctionArgs = dialog.FunctionArgs,
Thought = dialog.Thought,
MetaData = dialog.MetaData,
CreatedTime = dialog.CreatedAt
};
Expand Down Expand Up @@ -146,6 +148,7 @@ public async Task<List<RoleDialogModel>> GetDialogs(string conversationId, Conve
MessageLabel = dialog.MessageLabel,
SenderId = dialog.SenderId,
FunctionName = dialog.FunctionName,
Thought = dialog.Thought,
MetaData = dialog.MetaData,
CreatedTime = dialog.CreatedAt
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public async Task<bool> InvokeAgent(
message.ToolCallId = response.ToolCallId;
message.FunctionName = response.FunctionName;
message.FunctionArgs = response.FunctionArgs;
message.Thought = response.Thought != null ? new(response.Thought) : null;
message.MetaData = response.MetaData != null ? new(response.MetaData) : null;
message.Indication = response.Indication;
message.CurrentAgentId = agent.Id;
Expand All @@ -73,6 +74,7 @@ public async Task<bool> InvokeAgent(

message = RoleDialogModel.From(message, role: AgentRole.Assistant, content: response.Content);
message.CurrentAgentId = agent.Id;
message.Thought = response.Thought != null ? new(response.Thought) : null;
message.MetaData = response.MetaData != null ? new(response.MetaData) : null;
message.IsStreaming = response.IsStreaming;
message.MessageLabel = response.MessageLabel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public async Task<IEnumerable<ChatResponseModel>> GetDialogs(
Data = message.Data,
Sender = UserDto.FromUser(user),
Payload = message.Payload,
Thought = message.Thought,
MetaData = message.MetaData,
HasMessageFiles = files.Any(x => x.MessageId.IsEqualTo(message.MessageId) && x.FileSource == FileSource.User)
});
Expand All @@ -147,6 +148,7 @@ public async Task<IEnumerable<ChatResponseModel>> GetDialogs(
Text = !string.IsNullOrEmpty(message.SecondaryContent) ? message.SecondaryContent : message.Content,
Function = message.FunctionName,
Data = message.Data,
Thought = message.Thought,
MetaData = message.MetaData,
Sender = new()
{
Expand Down Expand Up @@ -419,6 +421,8 @@ await conv.SendMessage(agentId, inputMsg,
response.RichContent = msg.SecondaryRichContent ?? msg.RichContent;
response.Instruction = msg.Instruction;
response.Data = msg.Data;
response.Thought = msg.Thought;
response.MetaData = msg.MetaData;
});
}
catch (OperationCanceledException) when (input.IsStreamingMessage)
Expand Down Expand Up @@ -485,6 +489,8 @@ await conv.SendMessage(agentId, inputMsg,
response.RichContent = msg.SecondaryRichContent ?? msg.RichContent;
response.Instruction = msg.Instruction;
response.Data = msg.Data;
response.Thought = msg.Thought;
response.MetaData = msg.MetaData;
response.States = state.GetStates();

await OnChunkReceived(Response, response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public override async Task OnResponseGenerated(RoleDialogModel message)
Function = message.FunctionName,
RichContent = message.SecondaryRichContent ?? message.RichContent,
Data = message.Data,
Thought = message.Thought,
MetaData = message.MetaData,
States = state.GetStates(),
IsStreaming = message.IsStreaming,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public override void OnNext(HubObserveData<RoleDialogModel> value)
Function = message.FunctionName,
RichContent = message.SecondaryRichContent ?? message.RichContent,
Data = message.Data,
Thought = message.Thought,
MetaData = message.MetaData,
Sender = new()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ private async Task<List<RoleDialogModel>> AssembleFiles(string conversationId, L
{
ContentType = x.ContentType,
FileUrl = x.FileUrl,
FileStorageUrl = x.FileStorageUrl
FileStorageUrl = x.FileStorageUrl,
FileName = x.FileName,
FileExtension = x.FileExtension
}).ToList();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace BotSharp.Plugin.GoogleAI.Constants;
namespace BotSharp.Plugin.GoogleAi;

internal static class Constants
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class ChatCompletionProvider : IChatCompletion
private readonly IServiceProvider _services;
private readonly ILogger<ChatCompletionProvider> _logger;
private readonly IConversationStateService _state;
private readonly IFileStorageService _fileStorage;

private List<string> renderedInstructions = [];

private string _model;
Expand All @@ -31,12 +33,14 @@ public ChatCompletionProvider(
IServiceProvider services,
GoogleAiSettings googleSettings,
ILogger<ChatCompletionProvider> logger,
IConversationStateService state)
IConversationStateService state,
IFileStorageService fileStorage)
{
_settings = googleSettings;
_services = services;
_logger = logger;
_state = state;
_fileStorage = fileStorage;
}

public async Task<RoleDialogModel> GetChatCompletions(Agent agent, List<RoleDialogModel> conversations)
Expand Down Expand Up @@ -75,7 +79,7 @@ public async Task<RoleDialogModel> GetChatCompletions(Agent agent, List<RoleDial
ToolCallId = toolCall?.Id,
FunctionName = toolCall?.Name,
FunctionArgs = toolCall?.Args?.ToJsonString(),
MetaData = new Dictionary<string, string?>
Thought = new Dictionary<string, string?>
{
[Constants.ThoughtSignature] = thoughtSignature
},
Expand All @@ -99,18 +103,18 @@ public async Task<RoleDialogModel> GetChatCompletions(Agent agent, List<RoleDial
{
CurrentAgentId = agent.Id,
MessageId = conversations.LastOrDefault()?.MessageId ?? string.Empty,
MetaData = new Dictionary<string, string?>
Thought = new Dictionary<string, string?>
{
[Constants.ThoughtSignature] = thoughtSignature
},
RenderedInstruction = string.Join("\r\n", renderedInstructions)
};
}

if (responseMessage != null && thoughtPart != null)
if (thoughtPart != null)
{
responseMessage.MetaData ??= [];
responseMessage.MetaData[Constants.ThinkingText] = thoughtPart.Text;
responseMessage.Thought ??= [];
responseMessage.Thought[Constants.ThinkingText] = thoughtPart.Text;
}

// After chat completion hook
Expand Down Expand Up @@ -158,7 +162,7 @@ public async Task<bool> GetChatCompletionsAsync(Agent agent, List<RoleDialogMode
var msg = new RoleDialogModel(AgentRole.Assistant, text)
{
CurrentAgentId = agent.Id,
MetaData = new Dictionary<string, string?>
Thought = new Dictionary<string, string?>
{
[Constants.ThoughtSignature] = thoughtSignature
},
Expand All @@ -167,7 +171,7 @@ public async Task<bool> GetChatCompletionsAsync(Agent agent, List<RoleDialogMode

if (thoughtPart != null)
{
msg.MetaData[Constants.ThinkingText] = thoughtPart.Text;
msg.Thought[Constants.ThinkingText] = thoughtPart.Text;
}

// After chat completion hook
Expand Down Expand Up @@ -195,7 +199,7 @@ public async Task<bool> GetChatCompletionsAsync(Agent agent, List<RoleDialogMode
ToolCallId = toolCall?.Id,
FunctionName = toolCall?.Name,
FunctionArgs = toolCall?.Args?.ToJsonString(),
MetaData = new Dictionary<string, string?>
Thought = new Dictionary<string, string?>
{
[Constants.ThoughtSignature] = thoughtSignature
},
Expand All @@ -217,7 +221,7 @@ public async Task<bool> GetChatCompletionsAsync(Agent agent, List<RoleDialogMode
CurrentAgentId = agent.Id,
MessageId = conversations.LastOrDefault()?.MessageId ?? string.Empty,
StopCompletion = true,
MetaData = new Dictionary<string, string?>
Thought = new Dictionary<string, string?>
{
[Constants.ThoughtSignature] = thoughtSignature
},
Expand Down Expand Up @@ -263,7 +267,7 @@ public async Task<RoleDialogModel> GetChatCompletionsStreamingAsync(Agent agent,
});

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

Expand Down Expand Up @@ -301,7 +305,7 @@ public async Task<RoleDialogModel> GetChatCompletionsStreamingAsync(Agent agent,
if (!string.IsNullOrEmpty(thoughtPart?.Text))
{
var text = thoughtPart.Text;
thinkingTextStream.Collect(text);
thinkingStream.Collect(text);
hub.Push(new()
{
EventName = ChatEvent.OnReceiveLlmStreamMessage,
Expand All @@ -310,7 +314,7 @@ public async Task<RoleDialogModel> GetChatCompletionsStreamingAsync(Agent agent,
{
CurrentAgentId = agent.Id,
MessageId = messageId,
MetaData = new()
Thought = new()
{
[Constants.ThinkingText] = text
}
Expand Down Expand Up @@ -352,7 +356,7 @@ public async Task<RoleDialogModel> GetChatCompletionsStreamingAsync(Agent agent,
ToolCallId = functionCall.Id,
FunctionName = functionCall.Name,
FunctionArgs = functionCall.Args?.ToJsonString(),
MetaData = new Dictionary<string, string?>
Thought = new Dictionary<string, string?>
{
[Constants.ThoughtSignature] = thought?.ThoughtSignature
}
Expand All @@ -374,7 +378,7 @@ public async Task<RoleDialogModel> GetChatCompletionsStreamingAsync(Agent agent,
CurrentAgentId = agent.Id,
MessageId = messageId,
IsStreaming = true,
MetaData = new Dictionary<string, string?>
Thought = new Dictionary<string, string?>
{
[Constants.ThoughtSignature] = thoughtSignature
}
Expand All @@ -391,7 +395,7 @@ public async Task<RoleDialogModel> GetChatCompletionsStreamingAsync(Agent agent,
CurrentAgentId = agent.Id,
MessageId = messageId,
IsStreaming = true,
MetaData = new Dictionary<string, string?>
Thought = new Dictionary<string, string?>
{
[Constants.ThoughtSignature] = part?.ThoughtSignature
}
Expand All @@ -418,12 +422,12 @@ public async Task<RoleDialogModel> GetChatCompletionsStreamingAsync(Agent agent,
};
}

// Set thinking text in metadata
var thinkingText = thinkingTextStream.GetText();
// Set thinking text in thought and metadata
var thinkingText = thinkingStream.GetText();
if (!string.IsNullOrEmpty(thinkingText))
{
responseMessage.MetaData ??= [];
responseMessage.MetaData[Constants.ThinkingText] = thinkingText;
responseMessage.Thought ??= [];
responseMessage.Thought[Constants.ThinkingText] = thinkingText;
}

hub.Push(new()
Expand Down Expand Up @@ -458,7 +462,6 @@ public void SetModelName(string model)
{
var agentService = _services.GetRequiredService<IAgentService>();
var googleSettings = _services.GetRequiredService<GoogleAiSettings>();
var fileStorage = _services.GetRequiredService<IFileStorageService>();
var settingsService = _services.GetRequiredService<ILlmProviderService>();
var settings = settingsService.GetSetting(Provider, _model);
var allowMultiModal = settings != null && settings.MultiModal;
Expand Down Expand Up @@ -528,7 +531,7 @@ public void SetModelName(string model)
contents.Add(new Content([
new Part()
{
ThoughtSignature = message.MetaData?.GetValueOrDefault(Constants.ThoughtSignature, null),
ThoughtSignature = message.Thought?.GetValueOrDefault(Constants.ThoughtSignature, null),
FunctionCall = new FunctionCall
{
Id = message.ToolCallId,
Expand All @@ -541,7 +544,7 @@ public void SetModelName(string model)
contents.Add(new Content([
new Part()
{
ThoughtSignature = message.MetaData?.GetValueOrDefault(Constants.ThoughtSignature, null),
ThoughtSignature = message.Thought?.GetValueOrDefault(Constants.ThoughtSignature, null),
FunctionResponse = new FunctionResponse
{
Id = message.ToolCallId,
Expand All @@ -564,7 +567,7 @@ public void SetModelName(string model)
new()
{
Text = text,
ThoughtSignature = message.MetaData?.GetValueOrDefault(Constants.ThoughtSignature, null)
ThoughtSignature = message.Thought?.GetValueOrDefault(Constants.ThoughtSignature, null)
}
};

Expand All @@ -583,7 +586,7 @@ public void SetModelName(string model)
new()
{
Text = text,
ThoughtSignature = message.MetaData?.GetValueOrDefault(Constants.ThoughtSignature, null)
ThoughtSignature = message.Thought?.GetValueOrDefault(Constants.ThoughtSignature, null)
}
};

Expand Down Expand Up @@ -627,8 +630,6 @@ public void SetModelName(string model)

private void CollectMessageContentParts(List<Part> contentParts, List<BotSharpFile> files)
{
var fileStorage = _services.GetRequiredService<IFileStorageService>();

foreach (var file in files)
{
if (!string.IsNullOrEmpty(file.FileData))
Expand All @@ -646,7 +647,7 @@ private void CollectMessageContentParts(List<Part> contentParts, List<BotSharpFi
else if (!string.IsNullOrEmpty(file.FileStorageUrl))
{
var contentType = FileUtility.GetFileContentType(file.FileStorageUrl);
var binary = fileStorage.GetFileBytes(file.FileStorageUrl);
var binary = _fileStorage.GetFileBytes(file.FileStorageUrl);
contentParts.Add(new Part()
{
InlineData = new()
Expand Down
Loading
Loading