add reasoning effort level

This commit is contained in:
Jicheng Lu 2025-08-08 17:13:37 -05:00
parent 0e2fb847a7
commit bf56fa9d43
6 changed files with 49 additions and 5 deletions

View file

@ -46,7 +46,7 @@
<PackageVersion Include="Whisper.net.Runtime" Version="1.8.1" /> <PackageVersion Include="Whisper.net.Runtime" Version="1.8.1" />
<PackageVersion Include="NCrontab" Version="3.3.3" /> <PackageVersion Include="NCrontab" Version="3.3.3" />
<PackageVersion Include="Azure.AI.OpenAI" Version="2.2.0-beta.5" /> <PackageVersion Include="Azure.AI.OpenAI" Version="2.2.0-beta.5" />
<PackageVersion Include="OpenAI" Version="2.2.0" /> <PackageVersion Include="OpenAI" Version="2.2.0-beta.4" />
<PackageVersion Include="MailKit" Version="4.11.0" /> <PackageVersion Include="MailKit" Version="4.11.0" />
<PackageVersion Include="Microsoft.Data.Sqlite" Version="8.0.8" /> <PackageVersion Include="Microsoft.Data.Sqlite" Version="8.0.8" />
<PackageVersion Include="MySql.Data" Version="9.0.0" /> <PackageVersion Include="MySql.Data" Version="9.0.0" />

View file

@ -3,4 +3,5 @@ namespace BotSharp.Abstraction.Agents.Constants;
public static class LlmConstant public static class LlmConstant
{ {
public const int DEFAULT_MAX_OUTPUT_TOKEN = 1024; public const int DEFAULT_MAX_OUTPUT_TOKEN = 1024;
public const string DEFAULT_REASONING_EFFORT_LEVEL = "low";
} }

View file

@ -34,4 +34,11 @@ public class AgentLlmConfig
[JsonPropertyName("max_output_tokens")] [JsonPropertyName("max_output_tokens")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public int? MaxOutputTokens { get; set; } public int? MaxOutputTokens { get; set; }
/// <summary>
/// Reasoning effort level
/// </summary>
[JsonPropertyName("reasoning_effort_level")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? ReasoningEffortLevel { get; set; }
} }

View file

@ -14,8 +14,7 @@ public class AgentController : ControllerBase
public AgentController( public AgentController(
IAgentService agentService, IAgentService agentService,
IUserIdentity user, IUserIdentity user,
IServiceProvider services IServiceProvider services)
)
{ {
_agentService = agentService; _agentService = agentService;
_user = user; _user = user;

View file

@ -10,6 +10,7 @@ public class AgentLlmConfigMongoElement
public bool IsInherit { get; set; } public bool IsInherit { get; set; }
public int MaxRecursionDepth { get; set; } public int MaxRecursionDepth { get; set; }
public int? MaxOutputTokens { get; set; } public int? MaxOutputTokens { get; set; }
public string? ReasoningEffortLevel { get; set; }
public static AgentLlmConfigMongoElement? ToMongoElement(AgentLlmConfig? config) public static AgentLlmConfigMongoElement? ToMongoElement(AgentLlmConfig? config)
{ {
@ -22,6 +23,7 @@ public class AgentLlmConfigMongoElement
IsInherit = config.IsInherit, IsInherit = config.IsInherit,
MaxRecursionDepth = config.MaxRecursionDepth, MaxRecursionDepth = config.MaxRecursionDepth,
MaxOutputTokens = config.MaxOutputTokens, MaxOutputTokens = config.MaxOutputTokens,
ReasoningEffortLevel = config.ReasoningEffortLevel
}; };
} }
@ -36,6 +38,7 @@ public class AgentLlmConfigMongoElement
IsInherit = config.IsInherit, IsInherit = config.IsInherit,
MaxRecursionDepth = config.MaxRecursionDepth, MaxRecursionDepth = config.MaxRecursionDepth,
MaxOutputTokens = config.MaxOutputTokens, MaxOutputTokens = config.MaxOutputTokens,
ReasoningEffortLevel = config.ReasoningEffortLevel
}; };
} }
} }

View file

@ -17,7 +17,12 @@ public class ChatCompletionProvider : IChatCompletion
private readonly Dictionary<string, float> _defaultTemperature = new() private readonly Dictionary<string, float> _defaultTemperature = new()
{ {
{ "o4-mini", 1.0f } { "o3", 1.0f },
{ "o3-mini", 1.0f },
{ "o4-mini", 1.0f },
{ "gpt-5", 1.0f },
{ "gpt-5-mini", 1.0f },
{ "gpt-5-nano", 1.0f }
}; };
public virtual string Provider => "openai"; public virtual string Provider => "openai";
@ -499,13 +504,42 @@ public class ChatCompletionProvider : IChatCompletion
? tokens ? tokens
: agent.LlmConfig?.MaxOutputTokens ?? LlmConstant.DEFAULT_MAX_OUTPUT_TOKEN; : agent.LlmConfig?.MaxOutputTokens ?? LlmConstant.DEFAULT_MAX_OUTPUT_TOKEN;
var level = state.GetState("reasoning_effort_level")
.IfNullOrEmptyAs(agent?.LlmConfig?.ReasoningEffortLevel ?? string.Empty)
.IfNullOrEmptyAs(LlmConstant.DEFAULT_REASONING_EFFORT_LEVEL);
var reasoningEffortLevel = ParseReasoningEffortLevel(level);
return new ChatCompletionOptions() return new ChatCompletionOptions()
{ {
Temperature = temperature, Temperature = temperature,
MaxOutputTokenCount = maxTokens MaxOutputTokenCount = maxTokens,
ReasoningEffortLevel = reasoningEffortLevel
}; };
} }
private ChatReasoningEffortLevel? ParseReasoningEffortLevel(string? level)
{
if (string.IsNullOrWhiteSpace(level) || !_defaultTemperature.ContainsKey(_model))
{
return null;
}
var effortLevel = ChatReasoningEffortLevel.Low;
switch (level.ToLower())
{
case "medium":
effortLevel = ChatReasoningEffortLevel.Medium;
break;
case "high":
effortLevel = ChatReasoningEffortLevel.High;
break;
default:
break;
}
return effortLevel;
}
public void SetModelName(string model) public void SetModelName(string model)
{ {
_model = model; _model = model;