Merge pull request #871 from iceljc/master

add max output token
This commit is contained in:
iceljc 2025-02-05 17:51:05 -06:00 committed by GitHub
commit 52f7a976ca
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 57 additions and 10 deletions

View file

@ -0,0 +1,6 @@
namespace BotSharp.Abstraction.Agents.Constants;
public static class LlmConstant
{
public const int DEFAULT_MAX_OUTPUT_TOKEN = 1024;
}

View file

@ -22,6 +22,16 @@ public class AgentLlmConfig
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? Model { get; set; }
/// <summary>
/// Max recursion depth
/// </summary>
[JsonPropertyName("max_recursion_depth")]
public int MaxRecursionDepth { get; set; } = 3;
/// <summary>
/// Max output token
/// </summary>
[JsonPropertyName("max_output_tokens")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public int? MaxOutputTokens { get; set; }
}

View file

@ -52,7 +52,7 @@ public class BotSharpStats
public static (DateTime, DateTime) BuildTimeInterval(DateTime recordTime, StatsInterval interval)
{
DateTime startTime = recordTime;
DateTime endTime = DateTime.UtcNow;
DateTime endTime = startTime;
switch (interval)
{
@ -70,6 +70,7 @@ public class BotSharpStats
break;
}
endTime = endTime.AddSeconds(-1);
startTime = DateTime.SpecifyKind(startTime, DateTimeKind.Utc);
endTime = DateTime.SpecifyKind(endTime, DateTimeKind.Utc);
return (startTime, endTime);

View file

@ -80,7 +80,7 @@ public class AgentCreationModel
MaxMessageCount = MaxMessageCount,
Profiles = Profiles,
Labels = Labels,
LlmConfig = LlmConfig,
LlmConfig = LlmConfig ?? new(),
KnowledgeBases = KnowledgeBases,
Rules = Rules,
RoutingRules = RoutingRules?.Select(x => RoutingRuleUpdateModel.ToDomainElement(x))?.ToList() ?? [],

View file

@ -105,7 +105,7 @@ public class AgentUpdateModel
Utilities = Utilities ?? [],
KnowledgeBases = KnowledgeBases ?? [],
Rules = Rules ?? [],
LlmConfig = LlmConfig
LlmConfig = LlmConfig ?? new()
};
return agent;

View file

@ -170,12 +170,14 @@ public class ChatCompletionProvider : IChatCompletion
var state = _services.GetRequiredService<IConversationStateService>();
var temperature = decimal.Parse(state.GetState("temperature", "0.0"));
var maxToken = int.Parse(state.GetState("max_tokens", "512"));
var maxTokens = int.TryParse(state.GetState("max_tokens"), out var tokens)
? tokens
: agent.LlmConfig?.MaxOutputTokens ?? LlmConstant.DEFAULT_MAX_OUTPUT_TOKEN;
var parameters = new MessageParameters()
{
Messages = messages,
MaxTokens = maxToken,
MaxTokens = maxTokens,
Model = settings.Name,
Stream = false,
Temperature = temperature,

View file

@ -8,6 +8,7 @@ global using Anthropic.SDK;
global using Anthropic.SDK.Constants;
global using Anthropic.SDK.Messaging;
global using BotSharp.Abstraction.Agents;
global using BotSharp.Abstraction.Agents.Constants;
global using BotSharp.Abstraction.Agents.Enums;
global using BotSharp.Abstraction.Agents.Models;
global using BotSharp.Abstraction.Conversations.Models;

View file

@ -225,7 +225,10 @@ public class ChatCompletionProvider : IChatCompletion
var messages = new List<ChatMessage>();
var temperature = float.Parse(state.GetState("temperature", "0.0"));
var maxTokens = int.Parse(state.GetState("max_tokens", "1024"));
var maxTokens = int.TryParse(state.GetState("max_tokens"), out var tokens)
? tokens
: agent.LlmConfig?.MaxOutputTokens ?? LlmConstant.DEFAULT_MAX_OUTPUT_TOKEN;
var options = new ChatCompletionOptions()
{
Temperature = temperature,

View file

@ -5,6 +5,7 @@ global using System.IO;
global using System.Threading.Tasks;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.Logging;
global using BotSharp.Abstraction.Agents.Constants;
global using BotSharp.Abstraction.Agents.Enums;
global using BotSharp.Abstraction.Agents.Models;
global using BotSharp.Abstraction.Conversations;

View file

@ -201,7 +201,9 @@ public class ChatCompletionProvider : IChatCompletion
var messages = new List<ChatMessage>();
var temperature = float.Parse(state.GetState("temperature", "0.0"));
var maxTokens = int.Parse(state.GetState("max_tokens", "1024"));
var maxTokens = int.TryParse(state.GetState("max_tokens"), out var tokens)
? tokens
: agent.LlmConfig?.MaxOutputTokens ?? LlmConstant.DEFAULT_MAX_OUTPUT_TOKEN;
var options = new ChatCompletionOptions()
{
Temperature = temperature,

View file

@ -10,6 +10,7 @@ global using BotSharp.Abstraction.Conversations.Models;
global using BotSharp.Abstraction.Agents.Models;
global using BotSharp.Abstraction.MLTasks;
global using BotSharp.Abstraction.Agents;
global using BotSharp.Abstraction.Agents.Constants;
global using BotSharp.Abstraction.Agents.Enums;
global using BotSharp.Abstraction.Conversations;
global using BotSharp.Abstraction.Loggers;

View file

@ -1,5 +1,6 @@
using BotSharp.Abstraction.Agents;
using BotSharp.Abstraction.Agents.Enums;
using BotSharp.Abstraction.Conversations;
using BotSharp.Abstraction.Loggers;
using Google.Protobuf.WellKnownTypes;
using Microsoft.Extensions.Logging;
@ -188,10 +189,20 @@ public class GeminiChatCompletionProvider : IChatCompletion
}
}
var state = _services.GetRequiredService<IConversationStateService>();
var temperature = float.Parse(state.GetState("temperature", "0.0"));
var maxTokens = int.TryParse(state.GetState("max_tokens"), out var tokens)
? tokens
: agent.LlmConfig?.MaxOutputTokens ?? LlmConstant.DEFAULT_MAX_OUTPUT_TOKEN;
var request = new GenerateContentRequest
{
Contents = contents,
Tools = tools
Tools = tools,
GenerationConfig = new()
{
Temperature = temperature,
MaxOutputTokens = maxTokens
}
};
var prompt = GetPrompt(systemPrompts, funcPrompts, convPrompts);

View file

@ -5,6 +5,7 @@ global using System.Threading.Tasks;
global using System.Linq;
global using System.Text.Json;
global using BotSharp.Abstraction.Conversations.Models;
global using BotSharp.Abstraction.Agents.Constants;
global using BotSharp.Abstraction.Agents.Models;
global using BotSharp.Abstraction.MLTasks;
global using Microsoft.Extensions.Configuration;

View file

@ -8,6 +8,7 @@ public class AgentLlmConfigMongoElement
public string? Model { get; set; }
public bool IsInherit { get; set; }
public int MaxRecursionDepth { get; set; }
public int? MaxOutputTokens { get; set; }
public static AgentLlmConfigMongoElement? ToMongoElement(AgentLlmConfig? config)
{
@ -19,6 +20,7 @@ public class AgentLlmConfigMongoElement
Model = config.Model,
IsInherit = config.IsInherit,
MaxRecursionDepth = config.MaxRecursionDepth,
MaxOutputTokens = config.MaxOutputTokens,
};
}
@ -32,6 +34,7 @@ public class AgentLlmConfigMongoElement
Model = config.Model,
IsInherit = config.IsInherit,
MaxRecursionDepth = config.MaxRecursionDepth,
MaxOutputTokens = config.MaxOutputTokens,
};
}
}

View file

@ -202,7 +202,9 @@ public class ChatCompletionProvider : IChatCompletion
var messages = new List<ChatMessage>();
var temperature = float.Parse(state.GetState("temperature", "0.0"));
var maxTokens = int.Parse(state.GetState("max_tokens", "1024"));
var maxTokens = int.TryParse(state.GetState("max_tokens"), out var tokens)
? tokens
: agent.LlmConfig?.MaxOutputTokens ?? LlmConstant.DEFAULT_MAX_OUTPUT_TOKEN;
var options = new ChatCompletionOptions()
{
Temperature = temperature,

View file

@ -70,7 +70,9 @@ public class RealTimeCompletionProvider : IRealTimeCompletion
var messages = new List<ChatMessage>();
var temperature = float.Parse(state.GetState("temperature", "0.0"));
var maxTokens = int.Parse(state.GetState("max_tokens", "1024"));
var maxTokens = int.TryParse(state.GetState("max_tokens"), out var tokens)
? tokens
: agent.LlmConfig?.MaxOutputTokens ?? LlmConstant.DEFAULT_MAX_OUTPUT_TOKEN;
var options = new ChatCompletionOptions()
{
ToolChoice = ChatToolChoice.CreateAutoChoice(),

View file

@ -6,6 +6,7 @@ global using System.Threading.Tasks;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.Logging;
global using BotSharp.Abstraction.Agents.Enums;
global using BotSharp.Abstraction.Agents.Constants;
global using BotSharp.Abstraction.Agents.Models;
global using BotSharp.Abstraction.Conversations;
global using BotSharp.Abstraction.Conversations.Models;