feat: 合并知识库目录与内容获取接口
将原有“目录查询”和“按目录获取内容”两个工具合并为单一接口,一次性返回所有目录及对应内容,简化调用方式;新增统一的知识库项模型,并补充异常与失败场景的日志与兜底处理。
This commit is contained in:
@@ -26,64 +26,80 @@ public class YxaiKnowledgeTool : ISingletonDependency
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[YiAgentTool("意心Ai平台知识库目录"), DisplayName("YxaiKnowledgeDirectory"),
|
||||
Description("获取意心AI相关内容的知识库目录列表")]
|
||||
public async Task<List<YxaiKnowledgeDirectoryItem>> YxaiKnowledgeDirectory()
|
||||
[YiAgentTool("意心Ai平台知识库"), DisplayName("YxaiKnowledge"),
|
||||
Description("获取意心AI相关内容的知识库目录及内容列表")]
|
||||
public async Task<List<YxaiKnowledgeItem>> YxaiKnowledge()
|
||||
{
|
||||
try
|
||||
{
|
||||
var client = _httpClientFactory.CreateClient();
|
||||
var response = await client.GetAsync(DirectoryUrl);
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
// 1. 先获取目录列表
|
||||
var directoryResponse = await client.GetAsync(DirectoryUrl);
|
||||
if (!directoryResponse.IsSuccessStatusCode)
|
||||
{
|
||||
_logger.LogError("意心知识库目录接口调用失败: {StatusCode}", response.StatusCode);
|
||||
return new List<YxaiKnowledgeDirectoryItem>();
|
||||
_logger.LogError("意心知识库目录接口调用失败: {StatusCode}", directoryResponse.StatusCode);
|
||||
return new List<YxaiKnowledgeItem>();
|
||||
}
|
||||
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
var result = JsonSerializer.Deserialize(json, YxaiKnowledgeJsonContext.Default.ListYxaiKnowledgeDirectoryItem);
|
||||
var directoryJson = await directoryResponse.Content.ReadAsStringAsync();
|
||||
var directories = JsonSerializer.Deserialize(directoryJson,
|
||||
YxaiKnowledgeJsonContext.Default.ListYxaiKnowledgeDirectoryItem);
|
||||
|
||||
return result ?? new List<YxaiKnowledgeDirectoryItem>();
|
||||
if (directories == null || directories.Count == 0)
|
||||
{
|
||||
return new List<YxaiKnowledgeItem>();
|
||||
}
|
||||
|
||||
// 2. 循环调用内容接口获取每个目录的内容
|
||||
var result = new List<YxaiKnowledgeItem>();
|
||||
foreach (var directory in directories)
|
||||
{
|
||||
try
|
||||
{
|
||||
var contentUrl = string.Format(ContentUrlTemplate, directory.Id);
|
||||
var contentResponse = await client.GetAsync(contentUrl);
|
||||
|
||||
if (contentResponse.IsSuccessStatusCode)
|
||||
{
|
||||
var contentJson = await contentResponse.Content.ReadAsStringAsync();
|
||||
var contentResult = JsonSerializer.Deserialize(contentJson,
|
||||
YxaiKnowledgeJsonContext.Default.YxaiKnowledgeContentResponse);
|
||||
|
||||
result.Add(new YxaiKnowledgeItem
|
||||
{
|
||||
Name = directory.Name,
|
||||
Content = contentResult?.Content ?? ""
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogWarning("获取知识库内容失败: {StatusCode}, DirectoryId: {DirectoryId}",
|
||||
contentResponse.StatusCode, directory.Id);
|
||||
result.Add(new YxaiKnowledgeItem
|
||||
{
|
||||
Name = directory.Name,
|
||||
Content = $"获取内容失败: {contentResponse.StatusCode}"
|
||||
});
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "获取知识库内容发生异常, DirectoryId: {DirectoryId}", directory.Id);
|
||||
result.Add(new YxaiKnowledgeItem
|
||||
{
|
||||
Name = directory.Name,
|
||||
Content = "获取内容发生异常"
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "获取意心知识库目录发生异常");
|
||||
return new List<YxaiKnowledgeDirectoryItem>();
|
||||
}
|
||||
}
|
||||
|
||||
[YiAgentTool("意心Ai平台知识库内容"), DisplayName("YxaiKnowledge"),
|
||||
Description("根据目录ID获取意心AI知识库的具体内容")]
|
||||
public async Task<string> YxaiKnowledge([Description("知识库目录ID")] string directoryId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(directoryId))
|
||||
{
|
||||
return "目录ID不能为空";
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var client = _httpClientFactory.CreateClient();
|
||||
var url = string.Format(ContentUrlTemplate, directoryId);
|
||||
var response = await client.GetAsync(url);
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
_logger.LogError("意心知识库内容接口调用失败: {StatusCode}, DirectoryId: {DirectoryId}",
|
||||
response.StatusCode, directoryId);
|
||||
return "获取知识库内容失败";
|
||||
}
|
||||
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
var result = JsonSerializer.Deserialize(json, YxaiKnowledgeJsonContext.Default.YxaiKnowledgeContentResponse);
|
||||
|
||||
return result?.Content ?? "未找到相关内容";
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "获取意心知识库内容发生异常, DirectoryId: {DirectoryId}", directoryId);
|
||||
return "获取知识库内容发生异常";
|
||||
_logger.LogError(ex, "获取意心知识库发生异常");
|
||||
return new List<YxaiKnowledgeItem>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -102,6 +118,22 @@ public class YxaiKnowledgeContentResponse
|
||||
[JsonPropertyName("content")] public string? Content { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 合并后的知识库项,包含目录和内容
|
||||
/// </summary>
|
||||
public class YxaiKnowledgeItem
|
||||
{
|
||||
/// <summary>
|
||||
/// 目录名称
|
||||
/// </summary>
|
||||
public string Name { get; set; } = "";
|
||||
|
||||
/// <summary>
|
||||
/// 知识库内容
|
||||
/// </summary>
|
||||
public string Content { get; set; } = "";
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region JSON 序列化上下文
|
||||
@@ -112,4 +144,4 @@ internal partial class YxaiKnowledgeJsonContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
|
||||
#endregion
|
||||
#endregion
|
||||
Reference in New Issue
Block a user