fix: 修复用量统计线程问题并完善搜索与Token计算逻辑
- OnlineSearch 增加 daysAgo 非法值保护,避免无效时间范围 - 修复 UsageStatistics 中 Prompt/Completion Token 为 0 时的统计异常 - 引入独立 UnitOfWork,解决流式处理下的并发与事务问题 - 确保用量统计、系统消息与尊享包扣减的原子性 - 补充前端 Element Plus 组件类型声明 - 统一并优化部分代码格式,不影响业务逻辑
This commit is contained in:
@@ -29,8 +29,14 @@ public class OnlineSearchTool : ISingletonDependency
|
||||
}
|
||||
|
||||
[YiAgentTool("联网搜索"), DisplayName("OnlineSearch"), Description("进行在线搜索,获取最新的网络信息,近期信息是7天,实时信息是1天")]
|
||||
public async Task<string> OnlineSearch([Description("搜索关键字")]string keyword, [Description("距离现在多久天")]int? daysAgo = null)
|
||||
public async Task<string> OnlineSearch([Description("搜索关键字")] string keyword,
|
||||
[Description("距离现在多久天")] int? daysAgo = null)
|
||||
{
|
||||
if (daysAgo <= 0)
|
||||
{
|
||||
daysAgo = 1;
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(keyword))
|
||||
{
|
||||
return "搜索关键词不能为空";
|
||||
@@ -149,8 +155,7 @@ public class OnlineSearchTool : ISingletonDependency
|
||||
/// </summary>
|
||||
public class BaiduSearchRequest
|
||||
{
|
||||
[JsonPropertyName("messages")]
|
||||
public List<BaiduSearchMessage> Messages { get; set; } = new();
|
||||
[JsonPropertyName("messages")] public List<BaiduSearchMessage> Messages { get; set; } = new();
|
||||
|
||||
[JsonPropertyName("search_filter")]
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
@@ -162,8 +167,7 @@ public class BaiduSearchRequest
|
||||
/// </summary>
|
||||
public class BaiduSearchFilter
|
||||
{
|
||||
[JsonPropertyName("range")]
|
||||
public BaiduSearchRange? Range { get; set; }
|
||||
[JsonPropertyName("range")] public BaiduSearchRange? Range { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -171,8 +175,7 @@ public class BaiduSearchFilter
|
||||
/// </summary>
|
||||
public class BaiduSearchRange
|
||||
{
|
||||
[JsonPropertyName("page_time")]
|
||||
public BaiduSearchPageTime? PageTime { get; set; }
|
||||
[JsonPropertyName("page_time")] public BaiduSearchPageTime? PageTime { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -180,11 +183,9 @@ public class BaiduSearchRange
|
||||
/// </summary>
|
||||
public class BaiduSearchPageTime
|
||||
{
|
||||
[JsonPropertyName("gte")]
|
||||
public string? Gte { get; set; }
|
||||
[JsonPropertyName("gte")] public string? Gte { get; set; }
|
||||
|
||||
[JsonPropertyName("lte")]
|
||||
public string? Lte { get; set; }
|
||||
[JsonPropertyName("lte")] public string? Lte { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -192,11 +193,9 @@ public class BaiduSearchPageTime
|
||||
/// </summary>
|
||||
public class BaiduSearchMessage
|
||||
{
|
||||
[JsonPropertyName("role")]
|
||||
public string Role { get; set; } = "user";
|
||||
[JsonPropertyName("role")] public string Role { get; set; } = "user";
|
||||
|
||||
[JsonPropertyName("content")]
|
||||
public string Content { get; set; } = "";
|
||||
[JsonPropertyName("content")] public string Content { get; set; } = "";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -204,11 +203,9 @@ public class BaiduSearchMessage
|
||||
/// </summary>
|
||||
public class BaiduSearchResponse
|
||||
{
|
||||
[JsonPropertyName("request_id")]
|
||||
public string? RequestId { get; set; }
|
||||
[JsonPropertyName("request_id")] public string? RequestId { get; set; }
|
||||
|
||||
[JsonPropertyName("references")]
|
||||
public List<BaiduSearchReference>? References { get; set; }
|
||||
[JsonPropertyName("references")] public List<BaiduSearchReference>? References { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -216,23 +213,17 @@ public class BaiduSearchResponse
|
||||
/// </summary>
|
||||
public class BaiduSearchReference
|
||||
{
|
||||
[JsonPropertyName("id")]
|
||||
public int Id { get; set; }
|
||||
[JsonPropertyName("id")] public int Id { get; set; }
|
||||
|
||||
[JsonPropertyName("url")]
|
||||
public string? Url { get; set; }
|
||||
[JsonPropertyName("url")] public string? Url { get; set; }
|
||||
|
||||
[JsonPropertyName("title")]
|
||||
public string? Title { get; set; }
|
||||
[JsonPropertyName("title")] public string? Title { get; set; }
|
||||
|
||||
[JsonPropertyName("date")]
|
||||
public string? Date { get; set; }
|
||||
[JsonPropertyName("date")] public string? Date { get; set; }
|
||||
|
||||
[JsonPropertyName("snippet")]
|
||||
public string? Snippet { get; set; }
|
||||
[JsonPropertyName("snippet")] public string? Snippet { get; set; }
|
||||
|
||||
[JsonPropertyName("website")]
|
||||
public string? Website { get; set; }
|
||||
[JsonPropertyName("website")] public string? Website { get; set; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
Reference in New Issue
Block a user