feat: 联网搜索支持按时间范围过滤
- OnlineSearch 方法新增 daysAgo 参数,支持按最近天数筛选搜索结果 - 百度搜索请求增加 search_filter 时间范围(gte/lte) - 补充相关模型与 JSON 源生成配置 - 更新工具描述,明确近期与实时信息范围
This commit is contained in:
@@ -28,8 +28,8 @@ public class OnlineSearchTool : ISingletonDependency
|
||||
_baiduApiKey = configuration["BaiduSearch:ApiKey"] ?? "";
|
||||
}
|
||||
|
||||
[YiAgentTool("联网搜索"), DisplayName("OnlineSearch"), Description("进行在线搜索,获取最新的网络信息")]
|
||||
public async Task<string> OnlineSearch(string keyword)
|
||||
[YiAgentTool("联网搜索"), DisplayName("OnlineSearch"), Description("进行在线搜索,获取最新的网络信息,近期信息是7天,实时信息是1天")]
|
||||
public async Task<string> OnlineSearch([Description("搜索关键字")]string keyword, [Description("距离现在多久天")]int? daysAgo = null)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(keyword))
|
||||
{
|
||||
@@ -49,6 +49,22 @@ public class OnlineSearchTool : ISingletonDependency
|
||||
}
|
||||
};
|
||||
|
||||
// 设置时间范围过滤
|
||||
if (daysAgo.HasValue)
|
||||
{
|
||||
requestBody.SearchFilter = new BaiduSearchFilter
|
||||
{
|
||||
Range = new BaiduSearchRange
|
||||
{
|
||||
PageTime = new BaiduSearchPageTime
|
||||
{
|
||||
Gte = $"now-{daysAgo.Value}d/d",
|
||||
Lte = "now"
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
var jsonContent = JsonSerializer.Serialize(requestBody, BaiduJsonContext.Default.BaiduSearchRequest);
|
||||
var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
|
||||
|
||||
@@ -135,6 +151,40 @@ public class BaiduSearchRequest
|
||||
{
|
||||
[JsonPropertyName("messages")]
|
||||
public List<BaiduSearchMessage> Messages { get; set; } = new();
|
||||
|
||||
[JsonPropertyName("search_filter")]
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
public BaiduSearchFilter? SearchFilter { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 百度搜索过滤器
|
||||
/// </summary>
|
||||
public class BaiduSearchFilter
|
||||
{
|
||||
[JsonPropertyName("range")]
|
||||
public BaiduSearchRange? Range { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 百度搜索范围
|
||||
/// </summary>
|
||||
public class BaiduSearchRange
|
||||
{
|
||||
[JsonPropertyName("page_time")]
|
||||
public BaiduSearchPageTime? PageTime { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 百度搜索时间范围
|
||||
/// </summary>
|
||||
public class BaiduSearchPageTime
|
||||
{
|
||||
[JsonPropertyName("gte")]
|
||||
public string? Gte { get; set; }
|
||||
|
||||
[JsonPropertyName("lte")]
|
||||
public string? Lte { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -191,6 +241,9 @@ public class BaiduSearchReference
|
||||
|
||||
[JsonSerializable(typeof(BaiduSearchRequest))]
|
||||
[JsonSerializable(typeof(BaiduSearchResponse))]
|
||||
[JsonSerializable(typeof(BaiduSearchFilter))]
|
||||
[JsonSerializable(typeof(BaiduSearchRange))]
|
||||
[JsonSerializable(typeof(BaiduSearchPageTime))]
|
||||
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
|
||||
internal partial class BaiduJsonContext : JsonSerializerContext
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user