Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
53e56134d4 | ||
|
|
0d2f2cb826 | ||
|
|
f90105ebb4 | ||
|
|
67ed1ac1e3 | ||
|
|
69b84f6613 | ||
|
|
433d616b9b | ||
|
|
53aa575ad4 | ||
|
|
571df74c43 | ||
|
|
b7847c7e7d | ||
|
|
94eb41996e | ||
|
|
cefde6848d | ||
|
|
381b712b25 | ||
|
|
c319b0b4e4 | ||
|
|
1a32fa9e20 | ||
|
|
909406238c | ||
|
|
54a1d2a66f |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -278,3 +278,5 @@ database_backup
|
||||
/Yi.Abp.Net8/src/Yi.Abp.Web/yi-abp-dev.db
|
||||
|
||||
package-lock.json
|
||||
|
||||
.claude
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<SatelliteResourceLanguages>en;zh-CN</SatelliteResourceLanguages>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<Version>1.0.0</Version>
|
||||
<NoWarn>$(NoWarn);CS1591;CS8618;CS1998;CS8604;CS8620;CS8600;CS8602</NoWarn>
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.Model;
|
||||
|
||||
/// <summary>
|
||||
/// API类型选项
|
||||
/// </summary>
|
||||
public class ModelApiTypeOption
|
||||
{
|
||||
/// <summary>
|
||||
/// 显示名称
|
||||
/// </summary>
|
||||
public string Label { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 枚举值
|
||||
/// </summary>
|
||||
public int Value { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
using Yi.Framework.AiHub.Domain.Shared.Enums;
|
||||
|
||||
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.Model;
|
||||
|
||||
/// <summary>
|
||||
/// 模型库展示数据
|
||||
/// </summary>
|
||||
public class ModelLibraryDto
|
||||
{
|
||||
/// <summary>
|
||||
/// 模型ID
|
||||
/// </summary>
|
||||
public string ModelId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 模型名称
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 模型描述
|
||||
/// </summary>
|
||||
public string? Description { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 模型类型
|
||||
/// </summary>
|
||||
public ModelTypeEnum ModelType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 模型类型名称
|
||||
/// </summary>
|
||||
public string ModelTypeName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 模型API类型
|
||||
/// </summary>
|
||||
public ModelApiTypeEnum ModelApiType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 模型API类型名称
|
||||
/// </summary>
|
||||
public string ModelApiTypeName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 模型显示倍率
|
||||
/// </summary>
|
||||
public decimal MultiplierShow { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 供应商分组名称
|
||||
/// </summary>
|
||||
public string? ProviderName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 模型图标URL
|
||||
/// </summary>
|
||||
public string? IconUrl { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否为尊享模型(PremiumChat类型)
|
||||
/// </summary>
|
||||
public bool IsPremium { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using Yi.Framework.AiHub.Domain.Shared.Enums;
|
||||
using Yi.Framework.Ddd.Application.Contracts;
|
||||
|
||||
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.Model;
|
||||
|
||||
/// <summary>
|
||||
/// 获取模型库列表查询参数
|
||||
/// </summary>
|
||||
public class ModelLibraryGetListInput : PagedAllResultRequestDto
|
||||
{
|
||||
/// <summary>
|
||||
/// 搜索关键词(搜索模型名称、模型ID)
|
||||
/// </summary>
|
||||
public string? SearchKey { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 供应商名称筛选
|
||||
/// </summary>
|
||||
public List<string>? ProviderNames { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 模型类型筛选
|
||||
/// </summary>
|
||||
public List<ModelTypeEnum>? ModelTypes { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// API类型筛选
|
||||
/// </summary>
|
||||
public List<ModelApiTypeEnum>? ModelApiTypes { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否只显示尊享模型
|
||||
/// </summary>
|
||||
public bool? IsPremiumOnly { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.Model;
|
||||
|
||||
/// <summary>
|
||||
/// 模型类型选项
|
||||
/// </summary>
|
||||
public class ModelTypeOption
|
||||
{
|
||||
/// <summary>
|
||||
/// 显示名称
|
||||
/// </summary>
|
||||
public string Label { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 枚举值
|
||||
/// </summary>
|
||||
public int Value { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using Volo.Abp.Application.Dtos;
|
||||
using Yi.Framework.AiHub.Application.Contracts.Dtos.Model;
|
||||
|
||||
namespace Yi.Framework.AiHub.Application.Contracts.IServices;
|
||||
|
||||
/// <summary>
|
||||
/// 模型服务接口
|
||||
/// </summary>
|
||||
public interface IModelService
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取模型库列表(公开接口,无需登录)
|
||||
/// </summary>
|
||||
/// <param name="input">查询参数</param>
|
||||
/// <returns>分页模型列表</returns>
|
||||
Task<PagedResultDto<ModelLibraryDto>> GetListAsync(ModelLibraryGetListInput input);
|
||||
|
||||
/// <summary>
|
||||
/// 获取供应商列表(公开接口,无需登录)
|
||||
/// </summary>
|
||||
/// <returns>供应商列表</returns>
|
||||
Task<List<string>> GetProviderListAsync();
|
||||
|
||||
/// <summary>
|
||||
/// 获取模型类型选项列表(公开接口,无需登录)
|
||||
/// </summary>
|
||||
/// <returns>模型类型选项</returns>
|
||||
Task<List<ModelTypeOption>> GetModelTypeOptionsAsync();
|
||||
|
||||
/// <summary>
|
||||
/// 获取API类型选项列表(公开接口,无需登录)
|
||||
/// </summary>
|
||||
/// <returns>API类型选项</returns>
|
||||
Task<List<ModelApiTypeOption>> GetApiTypeOptionsAsync();
|
||||
}
|
||||
@@ -73,6 +73,7 @@ public class AiChatService : ApplicationService
|
||||
{
|
||||
var output = await _aiModelRepository._DbQueryable
|
||||
.Where(x => x.ModelType == ModelTypeEnum.Chat)
|
||||
.Where(x=>x.ModelApiType==ModelApiTypeEnum.OpenAi)
|
||||
.OrderByDescending(x => x.OrderNum)
|
||||
.Select(x => new ModelGetListOutput
|
||||
{
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
using Mapster;
|
||||
using SqlSugar;
|
||||
using Volo.Abp.Application.Dtos;
|
||||
using Volo.Abp.Application.Services;
|
||||
using Yi.Framework.AiHub.Application.Contracts.Dtos.Model;
|
||||
using Yi.Framework.AiHub.Application.Contracts.IServices;
|
||||
using Yi.Framework.AiHub.Domain.Entities.Model;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Consts;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Enums;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Extensions;
|
||||
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||
|
||||
namespace Yi.Framework.AiHub.Application.Services.Chat;
|
||||
|
||||
/// <summary>
|
||||
/// 模型服务
|
||||
/// </summary>
|
||||
public class ModelService : ApplicationService, IModelService
|
||||
{
|
||||
private readonly ISqlSugarRepository<AiModelEntity, Guid> _modelRepository;
|
||||
|
||||
public ModelService(ISqlSugarRepository<AiModelEntity, Guid> modelRepository)
|
||||
{
|
||||
_modelRepository = modelRepository;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取模型库列表(公开接口,无需登录)
|
||||
/// </summary>
|
||||
public async Task<PagedResultDto<ModelLibraryDto>> GetListAsync(ModelLibraryGetListInput input)
|
||||
{
|
||||
RefAsync<int> total = 0;
|
||||
|
||||
// 查询所有未删除的模型,使用WhereIF动态添加筛选条件
|
||||
var models = await _modelRepository._DbQueryable
|
||||
.Where(x => !x.IsDeleted)
|
||||
.WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), x =>
|
||||
x.Name.Contains(input.SearchKey) || x.ModelId.Contains(input.SearchKey))
|
||||
.WhereIF(input.ProviderNames is not null, x =>
|
||||
input.ProviderNames.Contains(x.ProviderName))
|
||||
.WhereIF(input.ModelTypes is not null, x =>
|
||||
input.ModelTypes.Contains(x.ModelType))
|
||||
.WhereIF(input.ModelApiTypes is not null, x =>
|
||||
input.ModelApiTypes.Contains(x.ModelApiType))
|
||||
.WhereIF(input.IsPremiumOnly == true, x =>
|
||||
PremiumPackageConst.ModeIds.Contains(x.ModelId))
|
||||
.OrderBy(x => x.OrderNum)
|
||||
.OrderBy(x => x.Name)
|
||||
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
|
||||
|
||||
// 转换为DTO
|
||||
var result = models.Select(model => new ModelLibraryDto
|
||||
{
|
||||
ModelId = model.ModelId,
|
||||
Name = model.Name,
|
||||
Description = model.Description,
|
||||
ModelType = model.ModelType,
|
||||
ModelTypeName = model.ModelType.GetDescription(),
|
||||
ModelApiType = model.ModelApiType,
|
||||
ModelApiTypeName = model.ModelApiType.GetDescription(),
|
||||
MultiplierShow = model.MultiplierShow,
|
||||
ProviderName = model.ProviderName,
|
||||
IconUrl = model.IconUrl,
|
||||
IsPremium = PremiumPackageConst.ModeIds.Contains(model.ModelId)
|
||||
}).ToList();
|
||||
|
||||
return new PagedResultDto<ModelLibraryDto>(total, result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取供应商列表(公开接口,无需登录)
|
||||
/// </summary>
|
||||
public async Task<List<string>> GetProviderListAsync()
|
||||
{
|
||||
var providers = await _modelRepository._DbQueryable
|
||||
.Where(x => !x.IsDeleted)
|
||||
.Where(x => !string.IsNullOrEmpty(x.ProviderName))
|
||||
.GroupBy(x => x.ProviderName)
|
||||
.OrderBy(x => x.ProviderName)
|
||||
.Select(x => x.ProviderName)
|
||||
.ToListAsync();
|
||||
|
||||
return providers;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取模型类型选项列表(公开接口,无需登录)
|
||||
/// </summary>
|
||||
public Task<List<ModelTypeOption>> GetModelTypeOptionsAsync()
|
||||
{
|
||||
var options = Enum.GetValues<ModelTypeEnum>()
|
||||
.Select(e => new ModelTypeOption
|
||||
{
|
||||
Label = e.GetDescription(),
|
||||
Value = (int)e
|
||||
})
|
||||
.ToList();
|
||||
|
||||
return Task.FromResult(options);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取API类型选项列表(公开接口,无需登录)
|
||||
/// </summary>
|
||||
public Task<List<ModelApiTypeOption>> GetApiTypeOptionsAsync()
|
||||
{
|
||||
var options = Enum.GetValues<ModelApiTypeEnum>()
|
||||
.Select(e => new ModelApiTypeOption
|
||||
{
|
||||
Label = e.GetDescription(),
|
||||
Value = (int)e
|
||||
})
|
||||
.ToList();
|
||||
|
||||
return Task.FromResult(options);
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ using Yi.Framework.AiHub.Domain.Shared.Dtos.Anthropic;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi.Embeddings;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi.Images;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi.Responses;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Enums;
|
||||
using Yi.Framework.Rbac.Application.Contracts.IServices;
|
||||
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||
@@ -85,6 +86,7 @@ public class OpenApiService : ApplicationService
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 图片生成
|
||||
/// </summary>
|
||||
@@ -102,6 +104,7 @@ public class OpenApiService : ApplicationService
|
||||
await _aiGateWayManager.CreateImageForStatisticsAsync(httpContext, userId, null, input, tokenId);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 向量生成
|
||||
/// </summary>
|
||||
@@ -185,12 +188,66 @@ public class OpenApiService : ApplicationService
|
||||
//ai网关代理httpcontext
|
||||
if (input.Stream)
|
||||
{
|
||||
await _aiGateWayManager.AnthropicCompleteChatStreamForStatisticsAsync(_httpContextAccessor.HttpContext, input,
|
||||
await _aiGateWayManager.AnthropicCompleteChatStreamForStatisticsAsync(_httpContextAccessor.HttpContext,
|
||||
input,
|
||||
userId, null, tokenId, cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
await _aiGateWayManager.AnthropicCompleteChatForStatisticsAsync(_httpContextAccessor.HttpContext, input, userId,
|
||||
await _aiGateWayManager.AnthropicCompleteChatForStatisticsAsync(_httpContextAccessor.HttpContext, input,
|
||||
userId,
|
||||
null, tokenId,
|
||||
cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 响应-Openai新规范 (尊享服务专用)
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
[HttpPost("openApi/v1/responses")]
|
||||
public async Task ResponsesAsync([FromBody] OpenAiResponsesInput input, CancellationToken cancellationToken)
|
||||
{
|
||||
//前面都是校验,后面才是真正的调用
|
||||
var httpContext = this._httpContextAccessor.HttpContext;
|
||||
var tokenValidation = await _tokenManager.ValidateTokenAsync(GetTokenByHttpContext(httpContext), input.Model);
|
||||
var userId = tokenValidation.UserId;
|
||||
var tokenId = tokenValidation.TokenId;
|
||||
await _aiBlacklistManager.VerifiyAiBlacklist(userId);
|
||||
|
||||
// 验证用户是否为VIP
|
||||
var userInfo = await _accountService.GetAsync(null, null, userId);
|
||||
if (userInfo == null)
|
||||
{
|
||||
throw new UserFriendlyException("用户信息不存在");
|
||||
}
|
||||
|
||||
// 检查是否为VIP(使用RoleCodes判断)
|
||||
if (!userInfo.RoleCodes.Contains(AiHubConst.VipRole) && userInfo.User.UserName != "cc")
|
||||
{
|
||||
throw new UserFriendlyException("该接口为尊享服务专用,需要VIP权限才能使用");
|
||||
}
|
||||
|
||||
// 检查尊享token包用量
|
||||
var availableTokens = await _premiumPackageManager.GetAvailableTokensAsync(userId);
|
||||
if (availableTokens <= 0)
|
||||
{
|
||||
throw new UserFriendlyException("尊享token包用量不足,请先购买尊享token包");
|
||||
}
|
||||
|
||||
//ai网关代理httpcontext
|
||||
if (input.Stream == true)
|
||||
{
|
||||
await _aiGateWayManager.OpenAiResponsesStreamForStatisticsAsync(_httpContextAccessor.HttpContext,
|
||||
input,
|
||||
userId, null, tokenId, cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
await _aiGateWayManager.OpenAiResponsesAsyncForStatisticsAsync(_httpContextAccessor.HttpContext, input,
|
||||
userId,
|
||||
null, tokenId,
|
||||
cancellationToken);
|
||||
}
|
||||
@@ -210,7 +267,8 @@ public class OpenApiService : ApplicationService
|
||||
|
||||
// 再检查 Authorization 头
|
||||
string authHeader = httpContext.Request.Headers["Authorization"];
|
||||
if (!string.IsNullOrWhiteSpace(authHeader) && authHeader.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase))
|
||||
if (!string.IsNullOrWhiteSpace(authHeader) &&
|
||||
authHeader.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return authHeader.Substring("Bearer ".Length).Trim();
|
||||
}
|
||||
@@ -227,5 +285,4 @@ public class OpenApiService : ApplicationService
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
@@ -12,7 +12,7 @@ public sealed class AnthropicInput
|
||||
|
||||
[JsonPropertyName("max_tokens")] public int? MaxTokens { get; set; }
|
||||
|
||||
[JsonPropertyName("messages")] public IList<AnthropicMessageInput> Messages { get; set; }
|
||||
[JsonPropertyName("messages")] public JsonElement? Messages { get; set; }
|
||||
|
||||
[JsonPropertyName("tools")] public IList<AnthropicMessageTool>? Tools { get; set; }
|
||||
|
||||
|
||||
@@ -1,648 +0,0 @@
|
||||
using System.Text.Json;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
|
||||
|
||||
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.Anthropic;
|
||||
|
||||
public static class AnthropicToOpenAi
|
||||
{
|
||||
/// <summary>
|
||||
/// 将AnthropicInput转换为ThorChatCompletionsRequest
|
||||
/// </summary>
|
||||
public static ThorChatCompletionsRequest ConvertAnthropicToOpenAi(AnthropicInput anthropicInput)
|
||||
{
|
||||
var openAiRequest = new ThorChatCompletionsRequest
|
||||
{
|
||||
Model = anthropicInput.Model,
|
||||
MaxTokens = anthropicInput.MaxTokens,
|
||||
Stream = anthropicInput.Stream,
|
||||
Messages = new List<ThorChatMessage>(anthropicInput.Messages.Count)
|
||||
};
|
||||
|
||||
// high medium minimal low
|
||||
if (openAiRequest.Model.EndsWith("-high") ||
|
||||
openAiRequest.Model.EndsWith("-medium") ||
|
||||
openAiRequest.Model.EndsWith("-minimal") ||
|
||||
openAiRequest.Model.EndsWith("-low"))
|
||||
{
|
||||
openAiRequest.ReasoningEffort = openAiRequest.Model switch
|
||||
{
|
||||
var model when model.EndsWith("-high") => "high",
|
||||
var model when model.EndsWith("-medium") => "medium",
|
||||
var model when model.EndsWith("-minimal") => "minimal",
|
||||
var model when model.EndsWith("-low") => "low",
|
||||
_ => "medium"
|
||||
};
|
||||
|
||||
openAiRequest.Model = openAiRequest.Model.Replace("-high", "")
|
||||
.Replace("-medium", "")
|
||||
.Replace("-minimal", "")
|
||||
.Replace("-low", "");
|
||||
}
|
||||
|
||||
if (anthropicInput.Thinking != null &&
|
||||
anthropicInput.Thinking.Type.Equals("enabled", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
openAiRequest.Thinking = new ThorChatClaudeThinking()
|
||||
{
|
||||
BudgetToken = anthropicInput.Thinking.BudgetTokens,
|
||||
Type = "enabled",
|
||||
};
|
||||
openAiRequest.EnableThinking = true;
|
||||
}
|
||||
|
||||
if (openAiRequest.Model.EndsWith("-thinking"))
|
||||
{
|
||||
openAiRequest.EnableThinking = true;
|
||||
openAiRequest.Model = openAiRequest.Model.Replace("-thinking", "");
|
||||
}
|
||||
|
||||
if (openAiRequest.Stream == true)
|
||||
{
|
||||
openAiRequest.StreamOptions = new ThorStreamOptions()
|
||||
{
|
||||
IncludeUsage = true,
|
||||
};
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(anthropicInput.System))
|
||||
{
|
||||
openAiRequest.Messages.Add(ThorChatMessage.CreateSystemMessage(anthropicInput.System));
|
||||
}
|
||||
|
||||
if (anthropicInput.Systems?.Count > 0)
|
||||
{
|
||||
foreach (var systemContent in anthropicInput.Systems)
|
||||
{
|
||||
openAiRequest.Messages.Add(ThorChatMessage.CreateSystemMessage(systemContent.Text ?? string.Empty));
|
||||
}
|
||||
}
|
||||
|
||||
// 处理messages
|
||||
if (anthropicInput.Messages != null)
|
||||
{
|
||||
foreach (var message in anthropicInput.Messages)
|
||||
{
|
||||
var thorMessages = ConvertAnthropicMessageToThor(message);
|
||||
// 需要过滤 空消息
|
||||
if (thorMessages.Count == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
openAiRequest.Messages.AddRange(thorMessages);
|
||||
}
|
||||
|
||||
openAiRequest.Messages = openAiRequest.Messages
|
||||
.Where(m => !string.IsNullOrEmpty(m.Content) || m.Contents?.Count > 0 || m.ToolCalls?.Count > 0 ||
|
||||
!string.IsNullOrEmpty(m.ToolCallId))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
// 处理tools
|
||||
if (anthropicInput.Tools is { Count: > 0 })
|
||||
{
|
||||
openAiRequest.Tools = anthropicInput.Tools.Where(x => x.name != "web_search")
|
||||
.Select(ConvertAnthropicToolToThor).ToList();
|
||||
}
|
||||
|
||||
// 判断是否存在web_search
|
||||
if (anthropicInput.Tools?.Any(x => x.name == "web_search") == true)
|
||||
{
|
||||
openAiRequest.WebSearchOptions = new ThorChatWebSearchOptions()
|
||||
{
|
||||
};
|
||||
}
|
||||
|
||||
// 处理tool_choice
|
||||
if (anthropicInput.ToolChoice != null)
|
||||
{
|
||||
openAiRequest.ToolChoice = ConvertAnthropicToolChoiceToThor(anthropicInput.ToolChoice);
|
||||
}
|
||||
|
||||
return openAiRequest;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据最后的内容块类型和OpenAI的完成原因确定Claude的停止原因
|
||||
/// </summary>
|
||||
public static string GetStopReasonByLastContentType(string? openAiFinishReason, string lastContentBlockType)
|
||||
{
|
||||
// 如果最后一个内容块是工具调用,优先返回tool_use
|
||||
if (lastContentBlockType == "tool_use")
|
||||
{
|
||||
return "tool_use";
|
||||
}
|
||||
|
||||
// 否则使用标准的转换逻辑
|
||||
return GetClaudeStopReason(openAiFinishReason);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建message_start事件
|
||||
/// </summary>
|
||||
public static AnthropicStreamDto CreateMessageStartEvent(string messageId, string model)
|
||||
{
|
||||
return new AnthropicStreamDto
|
||||
{
|
||||
Type = "message_start",
|
||||
Message = new AnthropicChatCompletionDto
|
||||
{
|
||||
id = messageId,
|
||||
type = "message",
|
||||
role = "assistant",
|
||||
model = model,
|
||||
content = new AnthropicChatCompletionDtoContent[0],
|
||||
Usage = new AnthropicCompletionDtoUsage
|
||||
{
|
||||
InputTokens = 0,
|
||||
OutputTokens = 0,
|
||||
CacheCreationInputTokens = 0,
|
||||
CacheReadInputTokens = 0
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建content_block_start事件
|
||||
/// </summary>
|
||||
public static AnthropicStreamDto CreateContentBlockStartEvent()
|
||||
{
|
||||
return new AnthropicStreamDto
|
||||
{
|
||||
Type = "content_block_start",
|
||||
Index = 0,
|
||||
ContentBlock = new AnthropicChatCompletionDtoContentBlock
|
||||
{
|
||||
Type = "text",
|
||||
Id = null,
|
||||
Name = null
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建thinking block start事件
|
||||
/// </summary>
|
||||
public static AnthropicStreamDto CreateThinkingBlockStartEvent()
|
||||
{
|
||||
return new AnthropicStreamDto
|
||||
{
|
||||
Type = "content_block_start",
|
||||
Index = 0,
|
||||
ContentBlock = new AnthropicChatCompletionDtoContentBlock
|
||||
{
|
||||
Type = "thinking",
|
||||
Id = null,
|
||||
Name = null
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建content_block_delta事件
|
||||
/// </summary>
|
||||
public static AnthropicStreamDto CreateContentBlockDeltaEvent(string text)
|
||||
{
|
||||
return new AnthropicStreamDto
|
||||
{
|
||||
Type = "content_block_delta",
|
||||
Index = 0,
|
||||
Delta = new AnthropicChatCompletionDtoDelta
|
||||
{
|
||||
Type = "text_delta",
|
||||
Text = text
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建thinking delta事件
|
||||
/// </summary>
|
||||
public static AnthropicStreamDto CreateThinkingBlockDeltaEvent(string thinking)
|
||||
{
|
||||
return new AnthropicStreamDto
|
||||
{
|
||||
Type = "content_block_delta",
|
||||
Index = 0,
|
||||
Delta = new AnthropicChatCompletionDtoDelta
|
||||
{
|
||||
Type = "thinking",
|
||||
Thinking = thinking
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建content_block_stop事件
|
||||
/// </summary>
|
||||
public static AnthropicStreamDto CreateContentBlockStopEvent()
|
||||
{
|
||||
return new AnthropicStreamDto
|
||||
{
|
||||
Type = "content_block_stop",
|
||||
Index = 0
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建message_delta事件
|
||||
/// </summary>
|
||||
public static AnthropicStreamDto CreateMessageDeltaEvent(string finishReason, AnthropicCompletionDtoUsage usage)
|
||||
{
|
||||
return new AnthropicStreamDto
|
||||
{
|
||||
Type = "message_delta",
|
||||
Usage = usage,
|
||||
Delta = new AnthropicChatCompletionDtoDelta
|
||||
{
|
||||
StopReason = finishReason
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建message_stop事件
|
||||
/// </summary>
|
||||
public static AnthropicStreamDto CreateMessageStopEvent()
|
||||
{
|
||||
return new AnthropicStreamDto
|
||||
{
|
||||
Type = "message_stop"
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建tool block start事件
|
||||
/// </summary>
|
||||
public static AnthropicStreamDto CreateToolBlockStartEvent(string? toolId, string? toolName)
|
||||
{
|
||||
return new AnthropicStreamDto
|
||||
{
|
||||
Type = "content_block_start",
|
||||
Index = 0,
|
||||
ContentBlock = new AnthropicChatCompletionDtoContentBlock
|
||||
{
|
||||
Type = "tool_use",
|
||||
Id = toolId,
|
||||
Name = toolName
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建tool delta事件
|
||||
/// </summary>
|
||||
public static AnthropicStreamDto CreateToolBlockDeltaEvent(string partialJson)
|
||||
{
|
||||
return new AnthropicStreamDto
|
||||
{
|
||||
Type = "content_block_delta",
|
||||
Index = 0,
|
||||
Delta = new AnthropicChatCompletionDtoDelta
|
||||
{
|
||||
Type = "input_json_delta",
|
||||
PartialJson = partialJson
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换Anthropic消息为Thor消息列表
|
||||
/// </summary>
|
||||
public static List<ThorChatMessage> ConvertAnthropicMessageToThor(AnthropicMessageInput anthropicMessage)
|
||||
{
|
||||
var results = new List<ThorChatMessage>();
|
||||
|
||||
// 处理简单的字符串内容
|
||||
if (anthropicMessage.Content != null)
|
||||
{
|
||||
var thorMessage = new ThorChatMessage
|
||||
{
|
||||
Role = anthropicMessage.Role,
|
||||
Content = anthropicMessage.Content
|
||||
};
|
||||
results.Add(thorMessage);
|
||||
return results;
|
||||
}
|
||||
|
||||
// 处理多模态内容
|
||||
if (anthropicMessage.Contents is { Count: > 0 })
|
||||
{
|
||||
var currentContents = new List<ThorChatMessageContent>();
|
||||
var currentToolCalls = new List<ThorToolCall>();
|
||||
|
||||
foreach (var content in anthropicMessage.Contents)
|
||||
{
|
||||
switch (content.Type)
|
||||
{
|
||||
case "text":
|
||||
currentContents.Add(ThorChatMessageContent.CreateTextContent(content.Text ?? string.Empty));
|
||||
break;
|
||||
case "thinking" when !string.IsNullOrEmpty(content.Thinking):
|
||||
results.Add(new ThorChatMessage()
|
||||
{
|
||||
ReasoningContent = content.Thinking
|
||||
});
|
||||
break;
|
||||
case "image":
|
||||
{
|
||||
if (content.Source != null)
|
||||
{
|
||||
var imageUrl = content.Source.Type == "base64"
|
||||
? $"data:{content.Source.MediaType};base64,{content.Source.Data}"
|
||||
: content.Source.Data;
|
||||
currentContents.Add(ThorChatMessageContent.CreateImageUrlContent(imageUrl ?? string.Empty));
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case "tool_use":
|
||||
{
|
||||
// 如果有普通内容,先创建内容消息
|
||||
if (currentContents.Count > 0)
|
||||
{
|
||||
if (currentContents.Count == 1 && currentContents.Any(x => x.Type == "text"))
|
||||
{
|
||||
var contentMessage = new ThorChatMessage
|
||||
{
|
||||
Role = anthropicMessage.Role,
|
||||
ContentCalculated = currentContents.FirstOrDefault()?.Text ?? string.Empty
|
||||
};
|
||||
results.Add(contentMessage);
|
||||
}
|
||||
else
|
||||
{
|
||||
var contentMessage = new ThorChatMessage
|
||||
{
|
||||
Role = anthropicMessage.Role,
|
||||
Contents = currentContents
|
||||
};
|
||||
results.Add(contentMessage);
|
||||
}
|
||||
|
||||
currentContents = new List<ThorChatMessageContent>();
|
||||
}
|
||||
|
||||
// 收集工具调用
|
||||
var toolCall = new ThorToolCall
|
||||
{
|
||||
Id = content.Id,
|
||||
Type = "function",
|
||||
Function = new ThorChatMessageFunction
|
||||
{
|
||||
Name = content.Name,
|
||||
Arguments = JsonSerializer.Serialize(content.Input)
|
||||
}
|
||||
};
|
||||
currentToolCalls.Add(toolCall);
|
||||
break;
|
||||
}
|
||||
case "tool_result":
|
||||
{
|
||||
// 如果有普通内容,先创建内容消息
|
||||
if (currentContents.Count > 0)
|
||||
{
|
||||
var contentMessage = new ThorChatMessage
|
||||
{
|
||||
Role = anthropicMessage.Role,
|
||||
Contents = currentContents
|
||||
};
|
||||
results.Add(contentMessage);
|
||||
currentContents = [];
|
||||
}
|
||||
|
||||
// 如果有工具调用,先创建工具调用消息
|
||||
if (currentToolCalls.Count > 0)
|
||||
{
|
||||
var toolCallMessage = new ThorChatMessage
|
||||
{
|
||||
Role = anthropicMessage.Role,
|
||||
ToolCalls = currentToolCalls
|
||||
};
|
||||
results.Add(toolCallMessage);
|
||||
currentToolCalls = new List<ThorToolCall>();
|
||||
}
|
||||
|
||||
// 创建工具结果消息
|
||||
var toolMessage = new ThorChatMessage
|
||||
{
|
||||
Role = "tool",
|
||||
ToolCallId = content.ToolUseId,
|
||||
Content = content.Content?.ToString() ?? string.Empty
|
||||
};
|
||||
results.Add(toolMessage);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 处理剩余的内容
|
||||
if (currentContents.Count > 0)
|
||||
{
|
||||
var contentMessage = new ThorChatMessage
|
||||
{
|
||||
Role = anthropicMessage.Role,
|
||||
Contents = currentContents
|
||||
};
|
||||
results.Add(contentMessage);
|
||||
}
|
||||
|
||||
// 处理剩余的工具调用
|
||||
if (currentToolCalls.Count > 0)
|
||||
{
|
||||
var toolCallMessage = new ThorChatMessage
|
||||
{
|
||||
Role = anthropicMessage.Role,
|
||||
ToolCalls = currentToolCalls
|
||||
};
|
||||
results.Add(toolCallMessage);
|
||||
}
|
||||
}
|
||||
|
||||
// 如果没有任何内容,返回一个空的消息
|
||||
if (results.Count == 0)
|
||||
{
|
||||
results.Add(new ThorChatMessage
|
||||
{
|
||||
Role = anthropicMessage.Role,
|
||||
Content = string.Empty
|
||||
});
|
||||
}
|
||||
|
||||
// 如果只有一个text则使用content字段
|
||||
if (results is [{ Contents.Count: 1 }] &&
|
||||
results.FirstOrDefault()?.Contents?.FirstOrDefault()?.Type == "text" &&
|
||||
!string.IsNullOrEmpty(results.FirstOrDefault()?.Contents?.FirstOrDefault()?.Text))
|
||||
{
|
||||
return
|
||||
[
|
||||
new ThorChatMessage
|
||||
{
|
||||
Role = results[0].Role,
|
||||
Content = results.FirstOrDefault()?.Contents?.FirstOrDefault()?.Text ?? string.Empty
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换Anthropic工具为Thor工具
|
||||
/// </summary>
|
||||
public static ThorToolDefinition ConvertAnthropicToolToThor(AnthropicMessageTool anthropicTool)
|
||||
{
|
||||
IDictionary<string, ThorToolFunctionPropertyDefinition> values =
|
||||
new Dictionary<string, ThorToolFunctionPropertyDefinition>();
|
||||
|
||||
if (anthropicTool.InputSchema?.Properties != null)
|
||||
{
|
||||
foreach (var property in anthropicTool.InputSchema.Properties)
|
||||
{
|
||||
if (property.Value?.description != null)
|
||||
{
|
||||
var definitionType = new ThorToolFunctionPropertyDefinition()
|
||||
{
|
||||
Description = property.Value.description,
|
||||
Type = property.Value.type
|
||||
};
|
||||
if (property.Value?.items?.type != null)
|
||||
{
|
||||
definitionType.Items = new ThorToolFunctionPropertyDefinition()
|
||||
{
|
||||
Type = property.Value.items.type
|
||||
};
|
||||
}
|
||||
|
||||
values.Add(property.Key, definitionType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return new ThorToolDefinition
|
||||
{
|
||||
Type = "function",
|
||||
Function = new ThorToolFunctionDefinition
|
||||
{
|
||||
Name = anthropicTool.name,
|
||||
Description = anthropicTool.Description,
|
||||
Parameters = new ThorToolFunctionPropertyDefinition
|
||||
{
|
||||
Type = anthropicTool.InputSchema?.Type ?? "object",
|
||||
Properties = values,
|
||||
Required = anthropicTool.InputSchema?.Required
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将OpenAI的完成原因转换为Claude的停止原因
|
||||
/// </summary>
|
||||
public static string GetClaudeStopReason(string? openAIFinishReason)
|
||||
{
|
||||
return openAIFinishReason switch
|
||||
{
|
||||
"stop" => "end_turn",
|
||||
"length" => "max_tokens",
|
||||
"tool_calls" => "tool_use",
|
||||
"content_filter" => "stop_sequence",
|
||||
_ => "end_turn"
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将OpenAI响应转换为Claude响应格式
|
||||
/// </summary>
|
||||
public static AnthropicChatCompletionDto ConvertOpenAIToClaude(ThorChatCompletionsResponse openAIResponse,
|
||||
AnthropicInput originalRequest)
|
||||
{
|
||||
var claudeResponse = new AnthropicChatCompletionDto
|
||||
{
|
||||
id = openAIResponse.Id,
|
||||
type = "message",
|
||||
role = "assistant",
|
||||
model = openAIResponse.Model ?? originalRequest.Model,
|
||||
stop_reason = GetClaudeStopReason(openAIResponse.Choices?.FirstOrDefault()?.FinishReason),
|
||||
stop_sequence = "",
|
||||
content = []
|
||||
};
|
||||
|
||||
if (openAIResponse.Choices is { Count: > 0 })
|
||||
{
|
||||
var choice = openAIResponse.Choices.First();
|
||||
var contents = new List<AnthropicChatCompletionDtoContent>();
|
||||
|
||||
if (!string.IsNullOrEmpty(choice.Message.Content) && !string.IsNullOrEmpty(choice.Message.ReasoningContent))
|
||||
{
|
||||
contents.Add(new AnthropicChatCompletionDtoContent
|
||||
{
|
||||
type = "thinking",
|
||||
Thinking = choice.Message.ReasoningContent
|
||||
});
|
||||
|
||||
contents.Add(new AnthropicChatCompletionDtoContent
|
||||
{
|
||||
type = "text",
|
||||
text = choice.Message.Content
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
// 处理思维内容
|
||||
if (!string.IsNullOrEmpty(choice.Message.ReasoningContent))
|
||||
contents.Add(new AnthropicChatCompletionDtoContent
|
||||
{
|
||||
type = "thinking",
|
||||
Thinking = choice.Message.ReasoningContent
|
||||
});
|
||||
|
||||
// 处理文本内容
|
||||
if (!string.IsNullOrEmpty(choice.Message.Content))
|
||||
contents.Add(new AnthropicChatCompletionDtoContent
|
||||
{
|
||||
type = "text",
|
||||
text = choice.Message.Content
|
||||
});
|
||||
}
|
||||
|
||||
// 处理工具调用
|
||||
if (choice.Message.ToolCalls is { Count: > 0 })
|
||||
contents.AddRange(choice.Message.ToolCalls.Select(toolCall => new AnthropicChatCompletionDtoContent
|
||||
{
|
||||
type = "tool_use", id = toolCall.Id, name = toolCall.Function?.Name,
|
||||
input = JsonSerializer.Deserialize<object>(toolCall.Function?.Arguments ?? "{}")
|
||||
}));
|
||||
|
||||
claudeResponse.content = contents.ToArray();
|
||||
}
|
||||
|
||||
// 处理使用情况统计 - 确保始终提供Usage信息
|
||||
claudeResponse.Usage = new AnthropicCompletionDtoUsage
|
||||
{
|
||||
InputTokens = openAIResponse.Usage?.PromptTokens ?? 0,
|
||||
OutputTokens = (int?)(openAIResponse.Usage?.CompletionTokens ?? 0),
|
||||
CacheCreationInputTokens = openAIResponse.Usage?.PromptTokensDetails?.CachedTokens ?? 0,
|
||||
CacheReadInputTokens = openAIResponse.Usage?.PromptTokensDetails?.CachedTokens ?? 0
|
||||
};
|
||||
|
||||
return claudeResponse;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 转换Anthropic工具选择为Thor工具选择
|
||||
/// </summary>
|
||||
public static ThorToolChoice ConvertAnthropicToolChoiceToThor(AnthropicTooChoiceInput anthropicToolChoice)
|
||||
{
|
||||
return new ThorToolChoice
|
||||
{
|
||||
Type = anthropicToolChoice.Type ?? "auto",
|
||||
Function = anthropicToolChoice.Name != null
|
||||
? new ThorToolChoiceFunctionTool { Name = anthropicToolChoice.Name }
|
||||
: null
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi.Responses;
|
||||
|
||||
public class OpenAiResponsesInput
|
||||
{
|
||||
[JsonPropertyName("stream")] public bool? Stream { get; set; }
|
||||
|
||||
[JsonPropertyName("model")] public string Model { get; set; }
|
||||
[JsonPropertyName("input")] public JsonElement Input { get; set; }
|
||||
|
||||
[JsonPropertyName("max_output_tokens")]
|
||||
public int? MaxOutputTokens { get; set; }
|
||||
|
||||
[JsonPropertyName("max_tool_calls")] public JsonElement? MaxToolCalls { get; set; }
|
||||
[JsonPropertyName("instructions")] public string? Instructions { get; set; }
|
||||
[JsonPropertyName("metadata")] public JsonElement? Metadata { get; set; }
|
||||
|
||||
[JsonPropertyName("parallel_tool_calls")]
|
||||
public bool? ParallelToolCalls { get; set; }
|
||||
|
||||
[JsonPropertyName("previous_response_id")]
|
||||
public string? PreviousResponseId { get; set; }
|
||||
|
||||
[JsonPropertyName("prompt")] public JsonElement? Prompt { get; set; }
|
||||
[JsonPropertyName("prompt_cache_key")] public string? PromptCacheKey { get; set; }
|
||||
|
||||
[JsonPropertyName("prompt_cache_retention")]
|
||||
public string? PromptCacheRetention { get; set; }
|
||||
|
||||
[JsonPropertyName("reasoning")] public JsonElement? Reasoning { get; set; }
|
||||
|
||||
[JsonPropertyName("safety_identifier")]
|
||||
public string? SafetyIdentifier { get; set; }
|
||||
|
||||
[JsonPropertyName("service_tier")] public string? ServiceTier { get; set; }
|
||||
[JsonPropertyName("store")] public bool? Store { get; set; }
|
||||
[JsonPropertyName("stream_options")] public JsonElement? StreamOptions { get; set; }
|
||||
[JsonPropertyName("temperature")] public decimal? Temperature { get; set; }
|
||||
[JsonPropertyName("text")] public JsonElement? Text { get; set; }
|
||||
[JsonPropertyName("tool_choice")] public JsonElement? ToolChoice { get; set; }
|
||||
[JsonPropertyName("tools")] public JsonElement? Tools { get; set; }
|
||||
[JsonPropertyName("top_logprobs")] public int? TopLogprobs { get; set; }
|
||||
[JsonPropertyName("top_p")] public decimal? TopP { get; set; }
|
||||
[JsonPropertyName("truncation")] public string? Truncation { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi.Responses;
|
||||
|
||||
public class OpenAiResponsesOutput
|
||||
{
|
||||
[JsonPropertyName("id")]
|
||||
public string? Id { get; set; }
|
||||
[JsonPropertyName("object")]
|
||||
public string? Object { get; set; }
|
||||
[JsonPropertyName("created_at")]
|
||||
public long CreatedAt { get; set; }
|
||||
[JsonPropertyName("status")]
|
||||
public string? Status { get; set; }
|
||||
[JsonPropertyName("error")]
|
||||
public dynamic? Error { get; set; }
|
||||
[JsonPropertyName("incomplete_details")]
|
||||
public dynamic? IncompleteDetails { get; set; }
|
||||
[JsonPropertyName("instructions")]
|
||||
public dynamic? Instructions { get; set; }
|
||||
[JsonPropertyName("max_output_tokens")]
|
||||
public dynamic? MaxOutputTokens { get; set; }
|
||||
[JsonPropertyName("model")]
|
||||
public string? Model { get; set; }
|
||||
// output 是复杂对象
|
||||
[JsonPropertyName("output")]
|
||||
public List<dynamic>? Output { get; set; }
|
||||
[JsonPropertyName("parallel_tool_calls")]
|
||||
public bool ParallelToolCalls { get; set; }
|
||||
[JsonPropertyName("previous_response_id")]
|
||||
public dynamic? PreviousResponseId { get; set; }
|
||||
[JsonPropertyName("reasoning")]
|
||||
public dynamic? Reasoning { get; set; }
|
||||
[JsonPropertyName("store")]
|
||||
public bool Store { get; set; }
|
||||
[JsonPropertyName("temperature")]
|
||||
public double Temperature { get; set; }
|
||||
[JsonPropertyName("text")]
|
||||
public dynamic? Text { get; set; }
|
||||
[JsonPropertyName("tool_choice")]
|
||||
public string? ToolChoice { get; set; }
|
||||
[JsonPropertyName("tools")]
|
||||
public List<dynamic>? Tools { get; set; }
|
||||
[JsonPropertyName("top_p")]
|
||||
public double TopP { get; set; }
|
||||
[JsonPropertyName("truncation")]
|
||||
public string? Truncation { get; set; }
|
||||
// usage 为唯一强类型
|
||||
[JsonPropertyName("usage")]
|
||||
public OpenAiResponsesUsageOutput? Usage { get; set; }
|
||||
[JsonPropertyName("user")]
|
||||
public dynamic? User { get; set; }
|
||||
[JsonPropertyName("metadata")]
|
||||
public dynamic? Metadata { get; set; }
|
||||
|
||||
public void SupplementalMultiplier(decimal multiplier)
|
||||
{
|
||||
if (this.Usage is not null)
|
||||
{
|
||||
this.Usage.InputTokens =
|
||||
(int)Math.Round((this.Usage?.InputTokens ?? 0) * multiplier);
|
||||
|
||||
this.Usage.OutputTokens =
|
||||
(int)Math.Round((this.Usage?.OutputTokens ?? 0) * multiplier);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class OpenAiResponsesUsageOutput
|
||||
{
|
||||
[JsonPropertyName("input_tokens")]
|
||||
public int InputTokens { get; set; }
|
||||
[JsonPropertyName("input_tokens_details")]
|
||||
public OpenAiResponsesInputTokensDetails? InputTokensDetails { get; set; }
|
||||
[JsonPropertyName("output_tokens")]
|
||||
public int OutputTokens { get; set; }
|
||||
[JsonPropertyName("output_tokens_details")]
|
||||
public OpenAiResponsesOutputTokensDetails? OutputTokensDetails { get; set; }
|
||||
[JsonPropertyName("total_tokens")]
|
||||
public int TotalTokens { get; set; }
|
||||
}
|
||||
public class OpenAiResponsesInputTokensDetails
|
||||
{
|
||||
[JsonPropertyName("cached_tokens")]
|
||||
public int CachedTokens { get; set; }
|
||||
}
|
||||
public class OpenAiResponsesOutputTokensDetails
|
||||
{
|
||||
[JsonPropertyName("reasoning_tokens")]
|
||||
public int ReasoningTokens { get; set; }
|
||||
}
|
||||
@@ -1,7 +1,15 @@
|
||||
namespace Yi.Framework.AiHub.Domain.Shared.Enums;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace Yi.Framework.AiHub.Domain.Shared.Enums;
|
||||
|
||||
public enum ModelApiTypeEnum
|
||||
{
|
||||
[Description("OpenAI")]
|
||||
OpenAi,
|
||||
Claude
|
||||
|
||||
[Description("Claude")]
|
||||
Claude,
|
||||
|
||||
[Description("Response")]
|
||||
Response
|
||||
}
|
||||
@@ -1,9 +1,15 @@
|
||||
namespace Yi.Framework.AiHub.Domain.Shared.Enums;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace Yi.Framework.AiHub.Domain.Shared.Enums;
|
||||
|
||||
public enum ModelTypeEnum
|
||||
{
|
||||
[Description("聊天")]
|
||||
Chat = 0,
|
||||
|
||||
[Description("图片")]
|
||||
Image = 1,
|
||||
Embedding = 2,
|
||||
PremiumChat = 3
|
||||
|
||||
[Description("嵌入")]
|
||||
Embedding = 2
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using System.ComponentModel;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Yi.Framework.AiHub.Domain.Shared.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// 枚举扩展方法
|
||||
/// </summary>
|
||||
public static class EnumExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取枚举的Description特性值
|
||||
/// </summary>
|
||||
/// <param name="value">枚举值</param>
|
||||
/// <returns>Description特性值,如果没有则返回枚举名称</returns>
|
||||
public static string GetDescription(this Enum value)
|
||||
{
|
||||
var field = value.GetType().GetField(value.ToString());
|
||||
if (field == null)
|
||||
{
|
||||
return value.ToString();
|
||||
}
|
||||
|
||||
var attribute = field.GetCustomAttribute<DescriptionAttribute>();
|
||||
return attribute?.Description ?? value.ToString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,279 @@
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Yi.Framework.AiHub.Domain.Shared.Extensions;
|
||||
|
||||
public static class JsonElementExtensions
|
||||
{
|
||||
#region 路径访问
|
||||
|
||||
/// <summary>
|
||||
/// 链式获取深层属性,支持对象属性和数组索引
|
||||
/// </summary>
|
||||
/// <example>
|
||||
/// root.GetPath("user", "addresses", 0, "city")
|
||||
/// </example>
|
||||
public static JsonElement? GetPath(this JsonElement element, params object[] path)
|
||||
{
|
||||
JsonElement current = element;
|
||||
|
||||
foreach (var key in path)
|
||||
{
|
||||
switch (key)
|
||||
{
|
||||
case string propertyName:
|
||||
if (current.ValueKind != JsonValueKind.Object ||
|
||||
!current.TryGetProperty(propertyName, out current))
|
||||
return null;
|
||||
break;
|
||||
|
||||
case int index:
|
||||
if (current.ValueKind != JsonValueKind.Array ||
|
||||
index < 0 || index >= current.GetArrayLength())
|
||||
return null;
|
||||
current = current[index];
|
||||
break;
|
||||
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return current;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 安全获取对象属性
|
||||
/// </summary>
|
||||
public static JsonElement? Get(this JsonElement element, string propertyName)
|
||||
{
|
||||
if (element.ValueKind == JsonValueKind.Object &&
|
||||
element.TryGetProperty(propertyName, out var value))
|
||||
return value;
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 安全获取数组元素
|
||||
/// </summary>
|
||||
public static JsonElement? Get(this JsonElement element, int index)
|
||||
{
|
||||
if (element.ValueKind == JsonValueKind.Array &&
|
||||
index >= 0 && index < element.GetArrayLength())
|
||||
return element[index];
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 链式安全获取对象属性
|
||||
/// </summary>
|
||||
public static JsonElement? Get(this JsonElement? element, string propertyName)
|
||||
=> element?.Get(propertyName);
|
||||
|
||||
/// <summary>
|
||||
/// 链式安全获取数组元素
|
||||
/// </summary>
|
||||
public static JsonElement? Get(this JsonElement? element, int index)
|
||||
=> element?.Get(index);
|
||||
|
||||
#endregion
|
||||
|
||||
#region 取值方法(带默认值)
|
||||
|
||||
public static string? GetString(this JsonElement? element, string? defaultValue = null)
|
||||
=> element?.ValueKind == JsonValueKind.String ? element.Value.GetString() : defaultValue;
|
||||
|
||||
public static int GetInt(this JsonElement? element, int defaultValue = 0)
|
||||
=> element?.ValueKind == JsonValueKind.Number ? element.Value.GetInt32() : defaultValue;
|
||||
|
||||
public static long GetLong(this JsonElement? element, long defaultValue = 0)
|
||||
=> element?.ValueKind == JsonValueKind.Number ? element.Value.GetInt64() : defaultValue;
|
||||
|
||||
public static double GetDouble(this JsonElement? element, double defaultValue = 0)
|
||||
=> element?.ValueKind == JsonValueKind.Number ? element.Value.GetDouble() : defaultValue;
|
||||
|
||||
public static decimal GetDecimal(this JsonElement? element, decimal defaultValue = 0)
|
||||
=> element?.ValueKind == JsonValueKind.Number ? element.Value.GetDecimal() : defaultValue;
|
||||
|
||||
public static bool GetBool(this JsonElement? element, bool defaultValue = false)
|
||||
=> element?.ValueKind is JsonValueKind.True or JsonValueKind.False
|
||||
? element.Value.GetBoolean()
|
||||
: defaultValue;
|
||||
|
||||
public static DateTime GetDateTime(this JsonElement? element, DateTime defaultValue = default)
|
||||
=> element?.ValueKind == JsonValueKind.String && element.Value.TryGetDateTime(out var dt)
|
||||
? dt
|
||||
: defaultValue;
|
||||
|
||||
public static Guid GetGuid(this JsonElement? element, Guid defaultValue = default)
|
||||
=> element?.ValueKind == JsonValueKind.String && element.Value.TryGetGuid(out var guid)
|
||||
? guid
|
||||
: defaultValue;
|
||||
|
||||
#endregion
|
||||
|
||||
#region 可空取值方法
|
||||
|
||||
public static int? GetIntOrNull(this JsonElement? element)
|
||||
=> element?.ValueKind == JsonValueKind.Number ? element.Value.GetInt32() : null;
|
||||
|
||||
public static long? GetLongOrNull(this JsonElement? element)
|
||||
=> element?.ValueKind == JsonValueKind.Number ? element.Value.GetInt64() : null;
|
||||
|
||||
public static double? GetDoubleOrNull(this JsonElement? element)
|
||||
=> element?.ValueKind == JsonValueKind.Number ? element.Value.GetDouble() : null;
|
||||
|
||||
public static decimal? GetDecimalOrNull(this JsonElement? element)
|
||||
=> element?.ValueKind == JsonValueKind.Number ? element.Value.GetDecimal() : null;
|
||||
|
||||
public static bool? GetBoolOrNull(this JsonElement? element)
|
||||
=> element?.ValueKind is JsonValueKind.True or JsonValueKind.False
|
||||
? element.Value.GetBoolean()
|
||||
: null;
|
||||
|
||||
public static DateTime? GetDateTimeOrNull(this JsonElement? element)
|
||||
=> element?.ValueKind == JsonValueKind.String && element.Value.TryGetDateTime(out var dt)
|
||||
? dt
|
||||
: null;
|
||||
|
||||
public static Guid? GetGuidOrNull(this JsonElement? element)
|
||||
=> element?.ValueKind == JsonValueKind.String && element.Value.TryGetGuid(out var guid)
|
||||
? guid
|
||||
: null;
|
||||
|
||||
#endregion
|
||||
|
||||
#region 数组操作
|
||||
|
||||
/// <summary>
|
||||
/// 安全获取数组,不存在返回空数组
|
||||
/// </summary>
|
||||
public static IEnumerable<JsonElement> GetArray(this JsonElement? element)
|
||||
{
|
||||
if (element?.ValueKind == JsonValueKind.Array)
|
||||
{
|
||||
foreach (var item in element.Value.EnumerateArray())
|
||||
yield return item;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取数组长度
|
||||
/// </summary>
|
||||
public static int GetArrayLength(this JsonElement? element)
|
||||
=> element?.ValueKind == JsonValueKind.Array ? element.Value.GetArrayLength() : 0;
|
||||
|
||||
/// <summary>
|
||||
/// 数组转 List
|
||||
/// </summary>
|
||||
public static List<string?> ToStringList(this JsonElement? element)
|
||||
=> element.GetArray().Select(e => e.GetString()).ToList();
|
||||
|
||||
public static List<int> ToIntList(this JsonElement? element)
|
||||
=> element.GetArray()
|
||||
.Where(e => e.ValueKind == JsonValueKind.Number)
|
||||
.Select(e => e.GetInt32())
|
||||
.ToList();
|
||||
|
||||
#endregion
|
||||
|
||||
#region 对象操作
|
||||
|
||||
/// <summary>
|
||||
/// 安全枚举对象属性
|
||||
/// </summary>
|
||||
public static IEnumerable<JsonProperty> GetProperties(this JsonElement? element)
|
||||
{
|
||||
if (element?.ValueKind == JsonValueKind.Object)
|
||||
{
|
||||
foreach (var prop in element.Value.EnumerateObject())
|
||||
yield return prop;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有属性名
|
||||
/// </summary>
|
||||
public static IEnumerable<string> GetPropertyNames(this JsonElement? element)
|
||||
=> element.GetProperties().Select(p => p.Name);
|
||||
|
||||
/// <summary>
|
||||
/// 判断是否包含某属性
|
||||
/// </summary>
|
||||
public static bool HasProperty(this JsonElement? element, string propertyName)
|
||||
=> element?.ValueKind == JsonValueKind.Object &&
|
||||
element.Value.TryGetProperty(propertyName, out _);
|
||||
|
||||
#endregion
|
||||
|
||||
#region 类型判断
|
||||
|
||||
public static bool IsNull(this JsonElement? element)
|
||||
=> element == null || element.Value.ValueKind == JsonValueKind.Null;
|
||||
|
||||
public static bool IsNullOrUndefined(this JsonElement? element)
|
||||
=> element == null || element.Value.ValueKind is JsonValueKind.Null or JsonValueKind.Undefined;
|
||||
|
||||
public static bool IsObject(this JsonElement? element)
|
||||
=> element?.ValueKind == JsonValueKind.Object;
|
||||
|
||||
public static bool IsArray(this JsonElement? element)
|
||||
=> element?.ValueKind == JsonValueKind.Array;
|
||||
|
||||
public static bool IsString(this JsonElement? element)
|
||||
=> element?.ValueKind == JsonValueKind.String;
|
||||
|
||||
public static bool IsNumber(this JsonElement? element)
|
||||
=> element?.ValueKind == JsonValueKind.Number;
|
||||
|
||||
public static bool IsBool(this JsonElement? element)
|
||||
=> element?.ValueKind is JsonValueKind.True or JsonValueKind.False;
|
||||
|
||||
public static bool Exists(this JsonElement? element)
|
||||
=> element != null && element.Value.ValueKind != JsonValueKind.Undefined;
|
||||
|
||||
#endregion
|
||||
|
||||
#region 反序列化
|
||||
|
||||
/// <summary>
|
||||
/// 反序列化为指定类型
|
||||
/// </summary>
|
||||
public static T? Deserialize<T>(this JsonElement? element, JsonSerializerOptions? options = null)
|
||||
=> element.HasValue ? element.Value.Deserialize<T>(options) : default;
|
||||
|
||||
/// <summary>
|
||||
/// 反序列化为指定类型,带默认值
|
||||
/// </summary>
|
||||
public static T Deserialize<T>(this JsonElement? element, T defaultValue, JsonSerializerOptions? options = null)
|
||||
=> element.HasValue ? element.Value.Deserialize<T>(options) ?? defaultValue : defaultValue;
|
||||
|
||||
#endregion
|
||||
|
||||
#region 转换为字典/动态类型
|
||||
|
||||
/// <summary>
|
||||
/// 转换为 Dictionary
|
||||
/// </summary>
|
||||
public static Dictionary<string, JsonElement>? ToDictionary(this JsonElement? element)
|
||||
{
|
||||
if (element?.ValueKind != JsonValueKind.Object)
|
||||
return null;
|
||||
|
||||
var dict = new Dictionary<string, JsonElement>();
|
||||
foreach (var prop in element.Value.EnumerateObject())
|
||||
dict[prop.Name] = prop.Value;
|
||||
return dict;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 原始值
|
||||
|
||||
/// <summary>
|
||||
/// 获取原始 JSON 字符串
|
||||
/// </summary>
|
||||
public static string? GetRawText(this JsonElement? element)
|
||||
=> element?.GetRawText();
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using System.Text.Json;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Dtos;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi.Responses;
|
||||
|
||||
namespace Yi.Framework.AiHub.Domain.AiGateWay;
|
||||
|
||||
public interface IOpenAiResponseService
|
||||
{
|
||||
/// <summary>
|
||||
/// 响应-流式
|
||||
/// </summary>
|
||||
/// <param name="aiModelDescribe"></param>
|
||||
/// <param name="input"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public IAsyncEnumerable<(string, JsonElement?)> ResponsesStreamAsync(AiModelDescribe aiModelDescribe,
|
||||
OpenAiResponsesInput input,
|
||||
CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// 响应-非流式
|
||||
/// </summary>
|
||||
/// <param name="aiModelDescribe"></param>
|
||||
/// <param name="input"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public Task<OpenAiResponsesOutput> ResponsesAsync(AiModelDescribe aiModelDescribe,
|
||||
OpenAiResponsesInput input,
|
||||
CancellationToken cancellationToken);
|
||||
}
|
||||
@@ -1,313 +0,0 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Volo.Abp.DependencyInjection;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Dtos;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Dtos.Anthropic;
|
||||
|
||||
namespace Yi.Framework.AiHub.Domain.AiGateWay.Impl.ThorCustomOpenAI.Chats;
|
||||
|
||||
/// <summary>
|
||||
/// OpenAI到Claude适配器服务
|
||||
/// 将Claude格式的请求转换为OpenAI格式,然后将OpenAI的响应转换为Claude格式
|
||||
/// </summary>
|
||||
public class CustomOpenAIAnthropicChatCompletionsService(
|
||||
IAbpLazyServiceProvider serviceProvider,
|
||||
ILogger<CustomOpenAIAnthropicChatCompletionsService> logger)
|
||||
: IAnthropicChatCompletionService
|
||||
{
|
||||
private IChatCompletionService GetChatCompletionService()
|
||||
{
|
||||
return serviceProvider.GetRequiredKeyedService<IChatCompletionService>(nameof(OpenAiChatCompletionsService));
|
||||
}
|
||||
|
||||
public async Task<AnthropicChatCompletionDto> ChatCompletionsAsync(AiModelDescribe aiModelDescribe,
|
||||
AnthropicInput request,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
// 转换请求格式:Claude -> OpenAI
|
||||
var openAIRequest = AnthropicToOpenAi.ConvertAnthropicToOpenAi(request);
|
||||
|
||||
if (openAIRequest.Model.StartsWith("gpt-5"))
|
||||
{
|
||||
openAIRequest.MaxCompletionTokens = request.MaxTokens;
|
||||
openAIRequest.MaxTokens = null;
|
||||
}
|
||||
else if (openAIRequest.Model.StartsWith("o3-mini") || openAIRequest.Model.StartsWith("o4-mini"))
|
||||
{
|
||||
openAIRequest.MaxCompletionTokens = request.MaxTokens;
|
||||
openAIRequest.MaxTokens = null;
|
||||
openAIRequest.Temperature = null;
|
||||
}
|
||||
|
||||
// 调用OpenAI服务
|
||||
var openAIResponse =
|
||||
await GetChatCompletionService().CompleteChatAsync(aiModelDescribe,openAIRequest, cancellationToken);
|
||||
|
||||
// 转换响应格式:OpenAI -> Claude
|
||||
var claudeResponse = AnthropicToOpenAi.ConvertOpenAIToClaude(openAIResponse, request);
|
||||
|
||||
return claudeResponse;
|
||||
}
|
||||
|
||||
public async IAsyncEnumerable<(string, AnthropicStreamDto?)> StreamChatCompletionsAsync(AiModelDescribe aiModelDescribe,
|
||||
AnthropicInput request,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var openAIRequest = AnthropicToOpenAi.ConvertAnthropicToOpenAi(request);
|
||||
openAIRequest.Stream = true;
|
||||
|
||||
if (openAIRequest.Model.StartsWith("gpt-5"))
|
||||
{
|
||||
openAIRequest.MaxCompletionTokens = request.MaxTokens;
|
||||
openAIRequest.MaxTokens = null;
|
||||
}
|
||||
else if (openAIRequest.Model.StartsWith("o3-mini") || openAIRequest.Model.StartsWith("o4-mini"))
|
||||
{
|
||||
openAIRequest.MaxCompletionTokens = request.MaxTokens;
|
||||
openAIRequest.MaxTokens = null;
|
||||
openAIRequest.Temperature = null;
|
||||
}
|
||||
|
||||
var messageId = Guid.NewGuid().ToString();
|
||||
var hasStarted = false;
|
||||
var hasTextContentBlockStarted = false;
|
||||
var hasThinkingContentBlockStarted = false;
|
||||
var toolBlocksStarted = new Dictionary<int, bool>(); // 使用索引而不是ID
|
||||
var toolCallIds = new Dictionary<int, string>(); // 存储每个索引对应的ID
|
||||
var toolCallIndexToBlockIndex = new Dictionary<int, int>(); // 工具调用索引到块索引的映射
|
||||
var accumulatedUsage = new AnthropicCompletionDtoUsage();
|
||||
var isFinished = false;
|
||||
var currentContentBlockType = ""; // 跟踪当前内容块类型
|
||||
var currentBlockIndex = 0; // 跟踪当前块索引
|
||||
var lastContentBlockType = ""; // 跟踪最后一个内容块类型,用于确定停止原因
|
||||
|
||||
await foreach (var openAIResponse in GetChatCompletionService().CompleteChatStreamAsync(aiModelDescribe,openAIRequest,
|
||||
cancellationToken))
|
||||
{
|
||||
// 发送message_start事件
|
||||
if (!hasStarted && openAIResponse.Choices?.Count > 0 &&
|
||||
openAIResponse.Choices.Any(x => x.Delta.ToolCalls?.Count > 0) == false)
|
||||
{
|
||||
hasStarted = true;
|
||||
var messageStartEvent = AnthropicToOpenAi.CreateMessageStartEvent(messageId, request.Model);
|
||||
yield return ("message_start", messageStartEvent);
|
||||
}
|
||||
|
||||
// 更新使用情况统计
|
||||
if (openAIResponse.Usage != null)
|
||||
{
|
||||
// 使用最新的token计数(OpenAI通常在最后的响应中提供完整的统计)
|
||||
if (openAIResponse.Usage.PromptTokens.HasValue)
|
||||
{
|
||||
accumulatedUsage.InputTokens = openAIResponse.Usage.PromptTokens.Value;
|
||||
}
|
||||
|
||||
if (openAIResponse.Usage.CompletionTokens.HasValue)
|
||||
{
|
||||
accumulatedUsage.OutputTokens = (int)openAIResponse.Usage.CompletionTokens.Value;
|
||||
}
|
||||
|
||||
if (openAIResponse.Usage.PromptTokensDetails?.CachedTokens.HasValue == true)
|
||||
{
|
||||
accumulatedUsage.CacheReadInputTokens =
|
||||
openAIResponse.Usage.PromptTokensDetails.CachedTokens.Value;
|
||||
}
|
||||
|
||||
// 记录调试信息
|
||||
logger.LogDebug("OpenAI Usage更新: Input={InputTokens}, Output={OutputTokens}, CacheRead={CacheRead}",
|
||||
accumulatedUsage.InputTokens, accumulatedUsage.OutputTokens,
|
||||
accumulatedUsage.CacheReadInputTokens);
|
||||
}
|
||||
|
||||
if (openAIResponse.Choices is { Count: > 0 })
|
||||
{
|
||||
var choice = openAIResponse.Choices.First();
|
||||
|
||||
// 处理内容
|
||||
if (!string.IsNullOrEmpty(choice.Delta?.Content))
|
||||
{
|
||||
// 如果当前有其他类型的内容块在运行,先结束它们
|
||||
if (currentContentBlockType != "text" && !string.IsNullOrEmpty(currentContentBlockType))
|
||||
{
|
||||
var stopEvent = AnthropicToOpenAi.CreateContentBlockStopEvent();
|
||||
stopEvent.Index = currentBlockIndex;
|
||||
yield return ("content_block_stop", stopEvent);
|
||||
currentBlockIndex++; // 切换内容块时增加索引
|
||||
currentContentBlockType = "";
|
||||
}
|
||||
|
||||
// 发送content_block_start事件(仅第一次)
|
||||
if (!hasTextContentBlockStarted || currentContentBlockType != "text")
|
||||
{
|
||||
hasTextContentBlockStarted = true;
|
||||
currentContentBlockType = "text";
|
||||
lastContentBlockType = "text";
|
||||
var contentBlockStartEvent = AnthropicToOpenAi.CreateContentBlockStartEvent();
|
||||
contentBlockStartEvent.Index = currentBlockIndex;
|
||||
yield return ("content_block_start",
|
||||
contentBlockStartEvent);
|
||||
}
|
||||
|
||||
// 发送content_block_delta事件
|
||||
var contentDeltaEvent = AnthropicToOpenAi.CreateContentBlockDeltaEvent(choice.Delta.Content);
|
||||
contentDeltaEvent.Index = currentBlockIndex;
|
||||
yield return ("content_block_delta",
|
||||
contentDeltaEvent);
|
||||
}
|
||||
|
||||
// 处理工具调用
|
||||
if (choice.Delta?.ToolCalls is { Count: > 0 })
|
||||
{
|
||||
foreach (var toolCall in choice.Delta.ToolCalls)
|
||||
{
|
||||
var toolCallIndex = toolCall.Index; // 使用索引来标识工具调用
|
||||
|
||||
// 发送tool_use content_block_start事件
|
||||
if (toolBlocksStarted.TryAdd(toolCallIndex, true))
|
||||
{
|
||||
// 如果当前有文本或thinking内容块在运行,先结束它们
|
||||
if (currentContentBlockType == "text" || currentContentBlockType == "thinking")
|
||||
{
|
||||
var stopEvent = AnthropicToOpenAi.CreateContentBlockStopEvent();
|
||||
stopEvent.Index = currentBlockIndex;
|
||||
yield return ("content_block_stop", stopEvent);
|
||||
currentBlockIndex++; // 增加块索引
|
||||
}
|
||||
// 如果当前有其他工具调用在运行,也需要结束它们
|
||||
else if (currentContentBlockType == "tool_use")
|
||||
{
|
||||
var stopEvent = AnthropicToOpenAi.CreateContentBlockStopEvent();
|
||||
stopEvent.Index = currentBlockIndex;
|
||||
yield return ("content_block_stop", stopEvent);
|
||||
currentBlockIndex++; // 增加块索引
|
||||
}
|
||||
|
||||
currentContentBlockType = "tool_use";
|
||||
lastContentBlockType = "tool_use";
|
||||
|
||||
// 为此工具调用分配一个新的块索引
|
||||
toolCallIndexToBlockIndex[toolCallIndex] = currentBlockIndex;
|
||||
|
||||
// 保存工具调用的ID(如果有的话)
|
||||
if (!string.IsNullOrEmpty(toolCall.Id))
|
||||
{
|
||||
toolCallIds[toolCallIndex] = toolCall.Id;
|
||||
}
|
||||
else if (!toolCallIds.ContainsKey(toolCallIndex))
|
||||
{
|
||||
// 如果没有ID且之前也没有保存过,生成一个新的ID
|
||||
toolCallIds[toolCallIndex] = Guid.NewGuid().ToString();
|
||||
}
|
||||
|
||||
var toolBlockStartEvent = AnthropicToOpenAi.CreateToolBlockStartEvent(
|
||||
toolCallIds[toolCallIndex],
|
||||
toolCall.Function?.Name);
|
||||
toolBlockStartEvent.Index = currentBlockIndex;
|
||||
yield return ("content_block_start",
|
||||
toolBlockStartEvent);
|
||||
}
|
||||
|
||||
// 如果有增量的参数,发送content_block_delta事件
|
||||
if (!string.IsNullOrEmpty(toolCall.Function?.Arguments))
|
||||
{
|
||||
var toolDeltaEvent =
|
||||
AnthropicToOpenAi.CreateToolBlockDeltaEvent(toolCall.Function.Arguments);
|
||||
// 使用该工具调用对应的块索引
|
||||
toolDeltaEvent.Index = toolCallIndexToBlockIndex[toolCallIndex];
|
||||
yield return ("content_block_delta",
|
||||
toolDeltaEvent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 处理推理内容
|
||||
if (!string.IsNullOrEmpty(choice.Delta?.ReasoningContent))
|
||||
{
|
||||
// 如果当前有其他类型的内容块在运行,先结束它们
|
||||
if (currentContentBlockType != "thinking" && !string.IsNullOrEmpty(currentContentBlockType))
|
||||
{
|
||||
var stopEvent = AnthropicToOpenAi.CreateContentBlockStopEvent();
|
||||
stopEvent.Index = currentBlockIndex;
|
||||
yield return ("content_block_stop", stopEvent);
|
||||
currentBlockIndex++; // 增加块索引
|
||||
currentContentBlockType = "";
|
||||
}
|
||||
|
||||
// 对于推理内容,也需要发送对应的事件
|
||||
if (!hasThinkingContentBlockStarted || currentContentBlockType != "thinking")
|
||||
{
|
||||
hasThinkingContentBlockStarted = true;
|
||||
currentContentBlockType = "thinking";
|
||||
lastContentBlockType = "thinking";
|
||||
var thinkingBlockStartEvent = AnthropicToOpenAi.CreateThinkingBlockStartEvent();
|
||||
thinkingBlockStartEvent.Index = currentBlockIndex;
|
||||
yield return ("content_block_start",
|
||||
thinkingBlockStartEvent);
|
||||
}
|
||||
|
||||
var thinkingDeltaEvent =
|
||||
AnthropicToOpenAi.CreateThinkingBlockDeltaEvent(choice.Delta.ReasoningContent);
|
||||
thinkingDeltaEvent.Index = currentBlockIndex;
|
||||
yield return ("content_block_delta",
|
||||
thinkingDeltaEvent);
|
||||
}
|
||||
|
||||
// 处理结束
|
||||
if (!string.IsNullOrEmpty(choice.FinishReason) && !isFinished)
|
||||
{
|
||||
isFinished = true;
|
||||
|
||||
// 发送content_block_stop事件(如果有活跃的内容块)
|
||||
if (!string.IsNullOrEmpty(currentContentBlockType))
|
||||
{
|
||||
var contentBlockStopEvent = AnthropicToOpenAi.CreateContentBlockStopEvent();
|
||||
contentBlockStopEvent.Index = currentBlockIndex;
|
||||
yield return ("content_block_stop",
|
||||
contentBlockStopEvent);
|
||||
}
|
||||
|
||||
// 发送message_delta事件
|
||||
var messageDeltaEvent = AnthropicToOpenAi.CreateMessageDeltaEvent(
|
||||
AnthropicToOpenAi.GetStopReasonByLastContentType(choice.FinishReason, lastContentBlockType),
|
||||
accumulatedUsage);
|
||||
|
||||
// 记录最终Usage统计
|
||||
logger.LogDebug(
|
||||
"流式响应结束,最终Usage: Input={InputTokens}, Output={OutputTokens}, CacheRead={CacheRead}",
|
||||
accumulatedUsage.InputTokens, accumulatedUsage.OutputTokens,
|
||||
accumulatedUsage.CacheReadInputTokens);
|
||||
|
||||
yield return ("message_delta",
|
||||
messageDeltaEvent);
|
||||
|
||||
// 发送message_stop事件
|
||||
var messageStopEvent = AnthropicToOpenAi.CreateMessageStopEvent();
|
||||
yield return ("message_stop",
|
||||
messageStopEvent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 确保流正确结束
|
||||
if (!isFinished)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(currentContentBlockType))
|
||||
{
|
||||
var contentBlockStopEvent = AnthropicToOpenAi.CreateContentBlockStopEvent();
|
||||
contentBlockStopEvent.Index = currentBlockIndex;
|
||||
yield return ("content_block_stop",
|
||||
contentBlockStopEvent);
|
||||
}
|
||||
|
||||
var messageDeltaEvent =
|
||||
AnthropicToOpenAi.CreateMessageDeltaEvent(
|
||||
AnthropicToOpenAi.GetStopReasonByLastContentType("end_turn", lastContentBlockType),
|
||||
accumulatedUsage);
|
||||
yield return ("message_delta", messageDeltaEvent);
|
||||
|
||||
var messageStopEvent = AnthropicToOpenAi.CreateMessageStopEvent();
|
||||
yield return ("message_stop",
|
||||
messageStopEvent);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -130,7 +130,7 @@ public sealed class OpenAiChatCompletionsService(ILogger<OpenAiChatCompletionsSe
|
||||
using var openai =
|
||||
Activity.Current?.Source.StartActivity("OpenAI 对话补全");
|
||||
|
||||
var response = await HttpClientFactory.GetHttpClient(options.Endpoint).PostJsonAsync(
|
||||
var response = await httpClientFactory.CreateClient().PostJsonAsync(
|
||||
options?.Endpoint.TrimEnd('/') + "/chat/completions",
|
||||
chatCompletionCreate, options.ApiKey).ConfigureAwait(false);
|
||||
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
using System.Diagnostics;
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text.Json;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Yi.Framework.AiHub.Domain.AiGateWay.Exceptions;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Dtos;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi.Responses;
|
||||
|
||||
namespace Yi.Framework.AiHub.Domain.AiGateWay.Impl.ThorCustomOpenAI.Chats;
|
||||
|
||||
public class OpenAiResponseService(ILogger<OpenAiResponseService> logger,IHttpClientFactory httpClientFactory):IOpenAiResponseService
|
||||
{
|
||||
|
||||
public async IAsyncEnumerable<(string, JsonElement?)> ResponsesStreamAsync(AiModelDescribe options, OpenAiResponsesInput input,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
using var openai =
|
||||
Activity.Current?.Source.StartActivity("OpenAi 响应");
|
||||
|
||||
|
||||
var client = httpClientFactory.CreateClient();
|
||||
|
||||
var response = await client.HttpRequestRaw(options.Endpoint.TrimEnd('/') + "/responses", input, options.ApiKey);
|
||||
|
||||
openai?.SetTag("Model", input.Model);
|
||||
openai?.SetTag("Response", response.StatusCode.ToString());
|
||||
|
||||
// 大于等于400的状态码都认为是异常
|
||||
if (response.StatusCode >= HttpStatusCode.BadRequest)
|
||||
{
|
||||
var error = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
|
||||
logger.LogError("OpenAI响应异常 请求地址:{Address}, StatusCode: {StatusCode} Response: {Response}",
|
||||
options.Endpoint,
|
||||
response.StatusCode, error);
|
||||
|
||||
throw new Exception("OpenAI响应异常" + response.StatusCode);
|
||||
}
|
||||
|
||||
using var stream = new StreamReader(await response.Content.ReadAsStreamAsync(cancellationToken));
|
||||
|
||||
using StreamReader reader = new(await response.Content.ReadAsStreamAsync(cancellationToken));
|
||||
string? line = string.Empty;
|
||||
|
||||
string? data = null;
|
||||
string eventType = string.Empty;
|
||||
|
||||
while ((line = await reader.ReadLineAsync(cancellationToken).ConfigureAwait(false)) != null)
|
||||
{
|
||||
line += Environment.NewLine;
|
||||
|
||||
if (line.StartsWith('{'))
|
||||
{
|
||||
logger.LogInformation("OpenAI响应异常 , StatusCode: {StatusCode} Response: {Response}", response.StatusCode,
|
||||
line);
|
||||
|
||||
throw new Exception("OpenAI响应异常" + line);
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(line))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (line.StartsWith("event:"))
|
||||
{
|
||||
eventType = line;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!line.StartsWith(OpenAIConstant.Data)) continue;
|
||||
|
||||
data = line[OpenAIConstant.Data.Length..].Trim();
|
||||
|
||||
var result = JsonSerializer.Deserialize<JsonElement>(data,
|
||||
ThorJsonSerializer.DefaultOptions);
|
||||
|
||||
yield return (eventType, result);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<OpenAiResponsesOutput> ResponsesAsync(AiModelDescribe options, OpenAiResponsesInput chatCompletionCreate,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
using var openai =
|
||||
Activity.Current?.Source.StartActivity("OpenAI 响应");
|
||||
|
||||
var response = await httpClientFactory.CreateClient().PostJsonAsync(
|
||||
options?.Endpoint.TrimEnd('/') + "/responses",
|
||||
chatCompletionCreate, options.ApiKey).ConfigureAwait(false);
|
||||
|
||||
openai?.SetTag("Model", chatCompletionCreate.Model);
|
||||
openai?.SetTag("Response", response.StatusCode.ToString());
|
||||
|
||||
if (response.StatusCode == HttpStatusCode.Unauthorized)
|
||||
{
|
||||
throw new BusinessException("渠道未登录,请联系管理人员", "401");
|
||||
}
|
||||
|
||||
// 如果限流则抛出限流异常
|
||||
if (response.StatusCode == HttpStatusCode.TooManyRequests)
|
||||
{
|
||||
throw new ThorRateLimitException();
|
||||
}
|
||||
|
||||
// 大于等于400的状态码都认为是异常
|
||||
if (response.StatusCode >= HttpStatusCode.BadRequest)
|
||||
{
|
||||
var error = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
|
||||
logger.LogError("OpenAI 响应异常 请求地址:{Address}, StatusCode: {StatusCode} Response: {Response}", options.Endpoint,
|
||||
response.StatusCode, error);
|
||||
|
||||
throw new BusinessException("OpenAI响应异常", response.StatusCode.ToString());
|
||||
}
|
||||
|
||||
var result =
|
||||
await response.Content.ReadFromJsonAsync<OpenAiResponsesOutput>(
|
||||
cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -65,4 +65,19 @@ public class AiModelEntity : Entity<Guid>, IOrderNum, ISoftDelete
|
||||
/// 模型倍率
|
||||
/// </summary>
|
||||
public decimal Multiplier { get; set; } = 1;
|
||||
|
||||
/// <summary>
|
||||
/// 模型显示倍率
|
||||
/// </summary>
|
||||
public decimal MultiplierShow { get; set; } = 1;
|
||||
|
||||
/// <summary>
|
||||
/// 供应商分组名称(如:OpenAI、Anthropic、Google等)
|
||||
/// </summary>
|
||||
public string? ProviderName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 模型图标URL
|
||||
/// </summary>
|
||||
public string? IconUrl { get; set; }
|
||||
}
|
||||
@@ -7,6 +7,7 @@ using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using Volo.Abp.Domain.Services;
|
||||
using Yi.Framework.AiHub.Domain.AiGateWay;
|
||||
@@ -18,7 +19,9 @@ using Yi.Framework.AiHub.Domain.Shared.Dtos.Anthropic;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi.Embeddings;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi.Images;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi.Responses;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Enums;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Extensions;
|
||||
using Yi.Framework.Core.Extensions;
|
||||
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||
using JsonSerializer = System.Text.Json.JsonSerializer;
|
||||
@@ -90,29 +93,6 @@ public class AiGateWayManager : DomainService
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 聊天完成-流式
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async IAsyncEnumerable<ThorChatCompletionsResponse> CompleteChatStreamAsync(
|
||||
ThorChatCompletionsRequest request,
|
||||
[EnumeratorCancellation] CancellationToken cancellationToken)
|
||||
{
|
||||
_specialCompatible.Compatible(request);
|
||||
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.OpenAi, request.Model);
|
||||
var chatService =
|
||||
LazyServiceProvider.GetRequiredKeyedService<IChatCompletionService>(modelDescribe.HandlerName);
|
||||
|
||||
await foreach (var result in chatService.CompleteChatStreamAsync(modelDescribe, request, cancellationToken))
|
||||
{
|
||||
result.SupplementalMultiplier(modelDescribe.Multiplier);
|
||||
yield return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 聊天完成-非流式
|
||||
/// </summary>
|
||||
@@ -174,6 +154,7 @@ public class AiGateWayManager : DomainService
|
||||
await response.WriteAsJsonAsync(data, cancellationToken);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 聊天完成-缓存处理
|
||||
/// </summary>
|
||||
@@ -199,8 +180,12 @@ public class AiGateWayManager : DomainService
|
||||
response.Headers.TryAdd("Connection", "keep-alive");
|
||||
|
||||
|
||||
var gateWay = LazyServiceProvider.GetRequiredService<AiGateWayManager>();
|
||||
var completeChatResponse = gateWay.CompleteChatStreamAsync(request, cancellationToken);
|
||||
_specialCompatible.Compatible(request);
|
||||
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.OpenAi, request.Model);
|
||||
var chatService =
|
||||
LazyServiceProvider.GetRequiredKeyedService<IChatCompletionService>(modelDescribe.HandlerName);
|
||||
|
||||
var completeChatResponse = chatService.CompleteChatStreamAsync(modelDescribe,request, cancellationToken);
|
||||
var tokenUsage = new ThorUsageResponse();
|
||||
|
||||
//缓存队列算法
|
||||
@@ -242,6 +227,7 @@ public class AiGateWayManager : DomainService
|
||||
{
|
||||
await foreach (var data in completeChatResponse)
|
||||
{
|
||||
data.SupplementalMultiplier(modelDescribe.Multiplier);
|
||||
if (data.Usage is not null && (data.Usage.CompletionTokens > 0 || data.Usage.OutputTokens > 0))
|
||||
{
|
||||
tokenUsage = data.Usage;
|
||||
@@ -498,29 +484,6 @@ public class AiGateWayManager : DomainService
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Anthropic聊天完成-流式
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async IAsyncEnumerable<(string, AnthropicStreamDto?)> AnthropicCompleteChatStreamAsync(
|
||||
AnthropicInput request,
|
||||
[EnumeratorCancellation] CancellationToken cancellationToken)
|
||||
{
|
||||
_specialCompatible.AnthropicCompatible(request);
|
||||
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.Claude, request.Model);
|
||||
var chatService =
|
||||
LazyServiceProvider.GetRequiredKeyedService<IAnthropicChatCompletionService>(modelDescribe.HandlerName);
|
||||
|
||||
await foreach (var result in chatService.StreamChatCompletionsAsync(modelDescribe, request, cancellationToken))
|
||||
{
|
||||
result.Item2.SupplementalMultiplier(modelDescribe.Multiplier);
|
||||
yield return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Anthropic聊天完成-非流式
|
||||
/// </summary>
|
||||
@@ -554,7 +517,7 @@ public class AiGateWayManager : DomainService
|
||||
await _aiMessageManager.CreateUserMessageAsync(userId.Value, sessionId,
|
||||
new MessageInputDto
|
||||
{
|
||||
Content = sessionId is null ? "不予存储" : request.Messages?.FirstOrDefault()?.Content ?? string.Empty,
|
||||
Content = "不予存储",
|
||||
ModelId = request.Model,
|
||||
TokenUsage = data.TokenUsage,
|
||||
}, tokenId);
|
||||
@@ -562,7 +525,7 @@ public class AiGateWayManager : DomainService
|
||||
await _aiMessageManager.CreateSystemMessageAsync(userId.Value, sessionId,
|
||||
new MessageInputDto
|
||||
{
|
||||
Content = sessionId is null ? "不予存储" : data.content?.FirstOrDefault()?.text,
|
||||
Content = "不予存储",
|
||||
ModelId = request.Model,
|
||||
TokenUsage = data.TokenUsage
|
||||
}, tokenId);
|
||||
@@ -580,6 +543,7 @@ public class AiGateWayManager : DomainService
|
||||
await response.WriteAsJsonAsync(data, cancellationToken);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Anthropic聊天完成-缓存处理
|
||||
/// </summary>
|
||||
@@ -604,15 +568,19 @@ public class AiGateWayManager : DomainService
|
||||
response.Headers.TryAdd("Cache-Control", "no-cache");
|
||||
response.Headers.TryAdd("Connection", "keep-alive");
|
||||
|
||||
_specialCompatible.AnthropicCompatible(request);
|
||||
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.Claude, request.Model);
|
||||
var chatService =
|
||||
LazyServiceProvider.GetRequiredKeyedService<IAnthropicChatCompletionService>(modelDescribe.HandlerName);
|
||||
|
||||
var gateWay = LazyServiceProvider.GetRequiredService<AiGateWayManager>();
|
||||
var completeChatResponse = gateWay.AnthropicCompleteChatStreamAsync(request, cancellationToken);
|
||||
var completeChatResponse = chatService.StreamChatCompletionsAsync(modelDescribe,request, cancellationToken);
|
||||
ThorUsageResponse? tokenUsage = null;
|
||||
StringBuilder backupSystemContent = new StringBuilder();
|
||||
try
|
||||
{
|
||||
await foreach (var responseResult in completeChatResponse)
|
||||
{
|
||||
responseResult.Item2.SupplementalMultiplier(modelDescribe.Multiplier);
|
||||
//message_start是为了保底机制
|
||||
if (responseResult.Item1.Contains("message_delta") || responseResult.Item1.Contains("message_start"))
|
||||
{
|
||||
@@ -634,7 +602,7 @@ public class AiGateWayManager : DomainService
|
||||
await _aiMessageManager.CreateUserMessageAsync(userId, sessionId,
|
||||
new MessageInputDto
|
||||
{
|
||||
Content = sessionId is null ? "不予存储" : request.Messages?.LastOrDefault()?.Content ?? string.Empty,
|
||||
Content = "不予存储",
|
||||
ModelId = request.Model,
|
||||
TokenUsage = tokenUsage,
|
||||
}, tokenId);
|
||||
@@ -642,7 +610,7 @@ public class AiGateWayManager : DomainService
|
||||
await _aiMessageManager.CreateSystemMessageAsync(userId, sessionId,
|
||||
new MessageInputDto
|
||||
{
|
||||
Content = sessionId is null ? "不予存储" : backupSystemContent.ToString(),
|
||||
Content = "不予存储",
|
||||
ModelId = request.Model,
|
||||
TokenUsage = tokenUsage
|
||||
}, tokenId);
|
||||
@@ -660,7 +628,167 @@ public class AiGateWayManager : DomainService
|
||||
}
|
||||
}
|
||||
|
||||
#region Anthropic格式Http响应
|
||||
|
||||
/// <summary>
|
||||
/// OpenAi 响应-非流式-缓存处理
|
||||
/// </summary>
|
||||
/// <param name="httpContext"></param>
|
||||
/// <param name="request"></param>
|
||||
/// <param name="userId"></param>
|
||||
/// <param name="sessionId"></param>
|
||||
/// <param name="tokenId"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
public async Task OpenAiResponsesAsyncForStatisticsAsync(HttpContext httpContext,
|
||||
OpenAiResponsesInput request,
|
||||
Guid? userId = null,
|
||||
Guid? sessionId = null,
|
||||
Guid? tokenId = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
// _specialCompatible.AnthropicCompatible(request);
|
||||
var response = httpContext.Response;
|
||||
// 设置响应头,声明是 json
|
||||
//response.ContentType = "application/json; charset=UTF-8";
|
||||
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.Response, request.Model);
|
||||
|
||||
var chatService =
|
||||
LazyServiceProvider.GetRequiredKeyedService<IOpenAiResponseService>(modelDescribe.HandlerName);
|
||||
var data = await chatService.ResponsesAsync(modelDescribe, request, cancellationToken);
|
||||
|
||||
data.SupplementalMultiplier(modelDescribe.Multiplier);
|
||||
|
||||
var tokenUsage= new ThorUsageResponse
|
||||
{
|
||||
InputTokens = data.Usage.InputTokens,
|
||||
OutputTokens = data.Usage.OutputTokens,
|
||||
TotalTokens = data.Usage.InputTokens + data.Usage.OutputTokens,
|
||||
};
|
||||
if (userId is not null)
|
||||
{
|
||||
await _aiMessageManager.CreateUserMessageAsync(userId.Value, sessionId,
|
||||
new MessageInputDto
|
||||
{
|
||||
Content = "不予存储",
|
||||
ModelId = request.Model,
|
||||
TokenUsage = tokenUsage,
|
||||
}, tokenId);
|
||||
|
||||
await _aiMessageManager.CreateSystemMessageAsync(userId.Value, sessionId,
|
||||
new MessageInputDto
|
||||
{
|
||||
Content = "不予存储",
|
||||
ModelId = request.Model,
|
||||
TokenUsage = tokenUsage
|
||||
}, tokenId);
|
||||
|
||||
await _usageStatisticsManager.SetUsageAsync(userId.Value, request.Model, tokenUsage, tokenId);
|
||||
|
||||
// 扣减尊享token包用量
|
||||
var totalTokens = tokenUsage.TotalTokens ?? 0;
|
||||
if (totalTokens > 0)
|
||||
{
|
||||
await PremiumPackageManager.TryConsumeTokensAsync(userId.Value, totalTokens);
|
||||
}
|
||||
}
|
||||
|
||||
await response.WriteAsJsonAsync(data, cancellationToken);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// OpenAi响应-流式-缓存处理
|
||||
/// </summary>
|
||||
/// <param name="httpContext"></param>
|
||||
/// <param name="request"></param>
|
||||
/// <param name="userId"></param>
|
||||
/// <param name="sessionId"></param>
|
||||
/// <param name="tokenId">Token Id(Web端传null或Guid.Empty)</param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task OpenAiResponsesStreamForStatisticsAsync(
|
||||
HttpContext httpContext,
|
||||
OpenAiResponsesInput request,
|
||||
Guid? userId = null,
|
||||
Guid? sessionId = null,
|
||||
Guid? tokenId = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var response = httpContext.Response;
|
||||
// 设置响应头,声明是 SSE 流
|
||||
response.ContentType = "text/event-stream;charset=utf-8;";
|
||||
response.Headers.TryAdd("Cache-Control", "no-cache");
|
||||
response.Headers.TryAdd("Connection", "keep-alive");
|
||||
|
||||
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.Response, request.Model);
|
||||
var chatService =
|
||||
LazyServiceProvider.GetRequiredKeyedService<IOpenAiResponseService>(modelDescribe.HandlerName);
|
||||
|
||||
var completeChatResponse = chatService.ResponsesStreamAsync(modelDescribe,request, cancellationToken);
|
||||
ThorUsageResponse? tokenUsage = null;
|
||||
try
|
||||
{
|
||||
await foreach (var responseResult in completeChatResponse)
|
||||
{
|
||||
//message_start是为了保底机制
|
||||
if (responseResult.Item1.Contains("response.completed"))
|
||||
{
|
||||
var obj = responseResult.Item2!.Value;
|
||||
int inputTokens = obj.GetPath("response","usage","input_tokens").GetInt();
|
||||
int outputTokens = obj.GetPath("response","usage","output_tokens").GetInt();
|
||||
inputTokens=Convert.ToInt32(inputTokens * modelDescribe.Multiplier);
|
||||
outputTokens=Convert.ToInt32(outputTokens * modelDescribe.Multiplier);
|
||||
tokenUsage = new ThorUsageResponse
|
||||
{
|
||||
PromptTokens =inputTokens,
|
||||
InputTokens = inputTokens,
|
||||
OutputTokens = outputTokens,
|
||||
CompletionTokens = outputTokens,
|
||||
TotalTokens = inputTokens+outputTokens,
|
||||
};
|
||||
}
|
||||
|
||||
await WriteAsEventStreamDataAsync(httpContext, responseResult.Item1, responseResult.Item2,
|
||||
cancellationToken);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, $"Ai响应异常");
|
||||
var errorContent = $"响应Ai异常,异常信息:\n当前Ai模型:{request.Model}\n异常信息:{e.Message}\n异常堆栈:{e}";
|
||||
throw new UserFriendlyException(errorContent);
|
||||
}
|
||||
|
||||
await _aiMessageManager.CreateUserMessageAsync(userId, sessionId,
|
||||
new MessageInputDto
|
||||
{
|
||||
Content = "不予存储" ,
|
||||
ModelId = request.Model,
|
||||
TokenUsage = tokenUsage,
|
||||
}, tokenId);
|
||||
|
||||
await _aiMessageManager.CreateSystemMessageAsync(userId, sessionId,
|
||||
new MessageInputDto
|
||||
{
|
||||
Content = "不予存储" ,
|
||||
ModelId = request.Model,
|
||||
TokenUsage = tokenUsage
|
||||
}, tokenId);
|
||||
|
||||
await _usageStatisticsManager.SetUsageAsync(userId, request.Model, tokenUsage, tokenId);
|
||||
|
||||
// 扣减尊享token包用量
|
||||
if (userId.HasValue && tokenUsage is not null)
|
||||
{
|
||||
var totalTokens = tokenUsage.TotalTokens ?? 0;
|
||||
if (tokenUsage.TotalTokens > 0)
|
||||
{
|
||||
await PremiumPackageManager.TryConsumeTokensAsync(userId.Value, totalTokens);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#region 流式传输格式Http响应
|
||||
|
||||
private static readonly byte[] EventPrefix = "event: "u8.ToArray();
|
||||
private static readonly byte[] DataPrefix = "data: "u8.ToArray();
|
||||
@@ -675,7 +803,6 @@ public class AiGateWayManager : DomainService
|
||||
string @event,
|
||||
T value,
|
||||
CancellationToken cancellationToken = default)
|
||||
where T : class
|
||||
{
|
||||
var response = context.Response;
|
||||
var bodyStream = response.Body;
|
||||
|
||||
@@ -49,13 +49,17 @@ namespace Yi.Framework.AiHub.Domain
|
||||
|
||||
#region Anthropic ChatCompletion
|
||||
|
||||
services.AddKeyedTransient<IAnthropicChatCompletionService, CustomOpenAIAnthropicChatCompletionsService>(
|
||||
nameof(CustomOpenAIAnthropicChatCompletionsService));
|
||||
services.AddKeyedTransient<IAnthropicChatCompletionService, AnthropicChatCompletionsService>(
|
||||
nameof(AnthropicChatCompletionsService));
|
||||
|
||||
#endregion
|
||||
|
||||
#region OpenAi Response
|
||||
|
||||
services.AddKeyedTransient<IOpenAiResponseService, OpenAiResponseService>(
|
||||
nameof(OpenAiResponseService));
|
||||
|
||||
#endregion
|
||||
|
||||
#region Image
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ using Mapster;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SqlSugar;
|
||||
using TencentCloud.Pds.V20210701.Models;
|
||||
using Volo.Abp;
|
||||
using Volo.Abp.Application.Dtos;
|
||||
using Volo.Abp.EventBus.Local;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using TencentCloud.Tbm.V20180129.Models;
|
||||
using Volo.Abp.DependencyInjection;
|
||||
using Volo.Abp.DependencyInjection;
|
||||
using Volo.Abp.Domain.Entities.Events;
|
||||
using Volo.Abp.EventBus;
|
||||
using Volo.Abp.EventBus.Local;
|
||||
|
||||
@@ -3,7 +3,6 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.VisualBasic;
|
||||
using TencentCloud.Mna.V20210119.Models;
|
||||
using Volo.Abp.Application.Services;
|
||||
using Volo.Abp.Caching;
|
||||
using Volo.Abp.DependencyInjection;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SqlSugar;
|
||||
using TencentCloud.Tcr.V20190924.Models;
|
||||
using Volo.Abp;
|
||||
using Volo.Abp.Application.Dtos;
|
||||
using Volo.Abp.Caching;
|
||||
|
||||
@@ -1,59 +1,59 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TencentCloud.Common.Profile;
|
||||
using TencentCloud.Common;
|
||||
using TencentCloud.Sms.V20210111.Models;
|
||||
using TencentCloud.Sms.V20210111;
|
||||
using Volo.Abp.Domain.Services;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Yi.Framework.Rbac.Domain.Managers
|
||||
{
|
||||
public class TencentCloudManager : DomainService
|
||||
{
|
||||
private ILogger<TencentCloudManager> _logger;
|
||||
public TencentCloudManager(ILogger<TencentCloudManager> logger)
|
||||
{
|
||||
_logger= logger;
|
||||
}
|
||||
|
||||
public async Task SendSmsAsync()
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
// 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
|
||||
// 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305
|
||||
// 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
|
||||
Credential cred = new Credential
|
||||
{
|
||||
SecretId = "SecretId",
|
||||
SecretKey = "SecretKey"
|
||||
};
|
||||
// 实例化一个client选项,可选的,没有特殊需求可以跳过
|
||||
ClientProfile clientProfile = new ClientProfile();
|
||||
// 实例化一个http选项,可选的,没有特殊需求可以跳过
|
||||
HttpProfile httpProfile = new HttpProfile();
|
||||
httpProfile.Endpoint = ("sms.tencentcloudapi.com");
|
||||
clientProfile.HttpProfile = httpProfile;
|
||||
|
||||
// 实例化要请求产品的client对象,clientProfile是可选的
|
||||
SmsClient client = new SmsClient(cred, "", clientProfile);
|
||||
// 实例化一个请求对象,每个接口都会对应一个request对象
|
||||
SendSmsRequest req = new SendSmsRequest();
|
||||
|
||||
// 返回的resp是一个SendSmsResponse的实例,与请求对象对应
|
||||
SendSmsResponse resp = await client.SendSms(req);
|
||||
// 输出json格式的字符串回包
|
||||
_logger.LogInformation("腾讯云Sms返回:"+AbstractModel.ToJsonString(resp));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e,e.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// using System;
|
||||
// using System.Collections.Generic;
|
||||
// using System.Linq;
|
||||
// using System.Text;
|
||||
// using System.Threading.Tasks;
|
||||
// using TencentCloud.Common.Profile;
|
||||
// using TencentCloud.Common;
|
||||
// using TencentCloud.Sms.V20210111.Models;
|
||||
// using TencentCloud.Sms.V20210111;
|
||||
// using Volo.Abp.Domain.Services;
|
||||
// using Microsoft.Extensions.Logging;
|
||||
//
|
||||
// namespace Yi.Framework.Rbac.Domain.Managers
|
||||
// {
|
||||
// public class TencentCloudManager : DomainService
|
||||
// {
|
||||
// private ILogger<TencentCloudManager> _logger;
|
||||
// public TencentCloudManager(ILogger<TencentCloudManager> logger)
|
||||
// {
|
||||
// _logger= logger;
|
||||
// }
|
||||
//
|
||||
// public async Task SendSmsAsync()
|
||||
// {
|
||||
//
|
||||
// try
|
||||
// {
|
||||
// // 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
|
||||
// // 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305
|
||||
// // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
|
||||
// Credential cred = new Credential
|
||||
// {
|
||||
// SecretId = "SecretId",
|
||||
// SecretKey = "SecretKey"
|
||||
// };
|
||||
// // 实例化一个client选项,可选的,没有特殊需求可以跳过
|
||||
// ClientProfile clientProfile = new ClientProfile();
|
||||
// // 实例化一个http选项,可选的,没有特殊需求可以跳过
|
||||
// HttpProfile httpProfile = new HttpProfile();
|
||||
// httpProfile.Endpoint = ("sms.tencentcloudapi.com");
|
||||
// clientProfile.HttpProfile = httpProfile;
|
||||
//
|
||||
// // 实例化要请求产品的client对象,clientProfile是可选的
|
||||
// SmsClient client = new SmsClient(cred, "", clientProfile);
|
||||
// // 实例化一个请求对象,每个接口都会对应一个request对象
|
||||
// SendSmsRequest req = new SendSmsRequest();
|
||||
//
|
||||
// // 返回的resp是一个SendSmsResponse的实例,与请求对象对应
|
||||
// SendSmsResponse resp = await client.SendSms(req);
|
||||
// // 输出json格式的字符串回包
|
||||
// _logger.LogInformation("腾讯云Sms返回:"+AbstractModel.ToJsonString(resp));
|
||||
// }
|
||||
// catch (Exception e)
|
||||
// {
|
||||
// _logger.LogError(e,e.ToString());
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
<PackageReference Include="IPTools.China" Version="1.6.0" />
|
||||
|
||||
<PackageReference Include="TencentCloudSDK" Version="3.0.966" />
|
||||
<!-- <PackageReference Include="TencentCloudSDK" Version="3.0.966" />-->
|
||||
|
||||
<PackageReference Include="UAParser" Version="3.1.47" />
|
||||
|
||||
|
||||
@@ -32,6 +32,7 @@ using Yi.Framework.AiHub.Application;
|
||||
using Yi.Framework.AiHub.Application.Services;
|
||||
using Yi.Framework.AiHub.Domain.Entities;
|
||||
using Yi.Framework.AiHub.Domain.Entities.Chat;
|
||||
using Yi.Framework.AiHub.Domain.Entities.Model;
|
||||
using Yi.Framework.AiHub.Domain.Entities.OpenApi;
|
||||
using Yi.Framework.AspNetCore;
|
||||
using Yi.Framework.AspNetCore.Authentication.OAuth;
|
||||
@@ -287,22 +288,14 @@ namespace Yi.Abp.Web
|
||||
};
|
||||
options.Events = new JwtBearerEvents
|
||||
{
|
||||
OnMessageReceived = context =>
|
||||
OnMessageReceived = messageContext =>
|
||||
{
|
||||
//优先Query中获取,再去cookies中获取
|
||||
var accessToken = context.Request.Query["access_token"];
|
||||
//优先Query中获取
|
||||
var accessToken = messageContext.Request.Query["access_token"];
|
||||
if (!string.IsNullOrEmpty(accessToken))
|
||||
{
|
||||
context.Token = accessToken;
|
||||
messageContext.Token = accessToken;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (context.Request.Cookies.TryGetValue("Token", out var cookiesToken))
|
||||
{
|
||||
context.Token = cookiesToken;
|
||||
}
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
};
|
||||
@@ -320,21 +313,13 @@ namespace Yi.Abp.Web
|
||||
};
|
||||
options.Events = new JwtBearerEvents
|
||||
{
|
||||
OnMessageReceived = context =>
|
||||
OnMessageReceived = messageContext =>
|
||||
{
|
||||
var refresh_token = context.Request.Headers["refresh_token"];
|
||||
if (!string.IsNullOrEmpty(refresh_token))
|
||||
{
|
||||
context.Token = refresh_token;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
var refreshToken = context.Request.Query["refresh_token"];
|
||||
var refreshToken = messageContext.Request.Query["refresh_token"];
|
||||
if (!string.IsNullOrEmpty(refreshToken))
|
||||
{
|
||||
context.Token = refreshToken;
|
||||
messageContext.Token = refreshToken;
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
};
|
||||
@@ -357,7 +342,7 @@ namespace Yi.Abp.Web
|
||||
var app = context.GetApplicationBuilder();
|
||||
app.UseRouting();
|
||||
|
||||
// app.ApplicationServices.GetRequiredService<ISqlSugarDbContext>().SqlSugarClient.CodeFirst.InitTables<MessageAggregateRoot>();
|
||||
// app.ApplicationServices.GetRequiredService<ISqlSugarDbContext>().SqlSugarClient.CodeFirst.InitTables<AiModelEntity>();
|
||||
// app.ApplicationServices.GetRequiredService<ISqlSugarDbContext>().SqlSugarClient.CodeFirst.InitTables<TokenAggregateRoot>();
|
||||
// app.ApplicationServices.GetRequiredService<ISqlSugarDbContext>().SqlSugarClient.CodeFirst.InitTables<UsageStatisticsAggregateRoot>();
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Shouldly;
|
||||
using TencentCloud.Ame.V20190916.Models;
|
||||
using TencentCloud.Tiw.V20190919.Models;
|
||||
using Volo.Abp.Domain.Repositories;
|
||||
using Xunit;
|
||||
using Yi.Framework.Rbac.Application.Contracts.Dtos.User;
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
{
|
||||
"permissions": {
|
||||
"allow": [
|
||||
"Bash(npx vue-tsc --noEmit)"
|
||||
"Bash(npx vue-tsc --noEmit)",
|
||||
"Bash(timeout 60 npx vue-tsc:*)"
|
||||
],
|
||||
"deny": [],
|
||||
"ask": []
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
"ComputedRef": true,
|
||||
"DirectiveBinding": true,
|
||||
"EffectScope": true,
|
||||
"ElMessage": true,
|
||||
"ElMessageBox": true,
|
||||
"ExtractDefaultPropTypes": true,
|
||||
"ExtractPropTypes": true,
|
||||
"ExtractPublicPropTypes": true,
|
||||
|
||||
@@ -112,7 +112,7 @@
|
||||
<body>
|
||||
<!-- 加载动画容器 -->
|
||||
<div id="yixinai-loader" class="loader-container">
|
||||
<div class="loader-title">意心Ai 2.5</div>
|
||||
<div class="loader-title">意心Ai 2.6</div>
|
||||
<div class="loader-subtitle">海外地址,仅首次访问预计加载约10秒</div>
|
||||
<div class="loader-logo">
|
||||
<div class="pulse-box"></div>
|
||||
|
||||
@@ -1,10 +1,56 @@
|
||||
import type { GetSessionListVO } from './types';
|
||||
import type { GetSessionListVO, ModelApiTypeOption, ModelLibraryDto, ModelLibraryGetListInput, ModelTypeOption, PagedResultDto } from './types';
|
||||
import { del, get, post, put } from '@/utils/request';
|
||||
|
||||
// 获取当前用户的模型列表
|
||||
export function getModelList() {
|
||||
return get<GetSessionListVO[]>('/ai-chat/model').json();
|
||||
}
|
||||
|
||||
// 获取模型库列表(公开接口,无需登录)
|
||||
export function getModelLibraryList(params?: ModelLibraryGetListInput) {
|
||||
const queryParams = new URLSearchParams();
|
||||
if (params?.searchKey) {
|
||||
queryParams.append('SearchKey', params.searchKey);
|
||||
}
|
||||
if (params?.providerNames && params.providerNames.length > 0) {
|
||||
params.providerNames.forEach(name => queryParams.append('ProviderNames', name));
|
||||
}
|
||||
if (params?.modelTypes && params.modelTypes.length > 0) {
|
||||
params.modelTypes.forEach(type => queryParams.append('ModelTypes', type.toString()));
|
||||
}
|
||||
if (params?.modelApiTypes && params.modelApiTypes.length > 0) {
|
||||
params.modelApiTypes.forEach(type => queryParams.append('ModelApiTypes', type.toString()));
|
||||
}
|
||||
if (params?.isPremiumOnly !== undefined) {
|
||||
queryParams.append('IsPremiumOnly', params.isPremiumOnly.toString());
|
||||
}
|
||||
if (params?.skipCount !== undefined) {
|
||||
queryParams.append('SkipCount', params.skipCount.toString());
|
||||
}
|
||||
if (params?.maxResultCount !== undefined) {
|
||||
queryParams.append('MaxResultCount', params.maxResultCount.toString());
|
||||
}
|
||||
|
||||
const queryString = queryParams.toString();
|
||||
const url = queryString ? `/model?${queryString}` : '/model';
|
||||
|
||||
return get<PagedResultDto<ModelLibraryDto>>(url).json();
|
||||
}
|
||||
|
||||
// 获取供应商列表(公开接口,无需登录)
|
||||
export function getProviderList() {
|
||||
return get<string[]>('/model/provider-list').json();
|
||||
}
|
||||
|
||||
// 获取模型类型选项列表(公开接口,无需登录)
|
||||
export function getModelTypeOptions() {
|
||||
return get<ModelTypeOption[]>('/model/model-type-options').json();
|
||||
}
|
||||
|
||||
// 获取API类型选项列表(公开接口,无需登录)
|
||||
export function getApiTypeOptions() {
|
||||
return get<ModelApiTypeOption[]>('/model/api-type-options').json();
|
||||
}
|
||||
// 申请ApiKey
|
||||
export function applyApiKey() {
|
||||
return post<any>('/token').json();
|
||||
|
||||
@@ -13,3 +13,61 @@ export interface GetSessionListVO {
|
||||
remark?: string;
|
||||
modelId?: string;
|
||||
}
|
||||
|
||||
// 模型类型枚举
|
||||
export enum ModelTypeEnum {
|
||||
Chat = 0,
|
||||
Image = 1,
|
||||
Embedding = 2,
|
||||
PremiumChat = 3,
|
||||
}
|
||||
|
||||
// 模型API类型枚举
|
||||
export enum ModelApiTypeEnum {
|
||||
OpenAi = 0,
|
||||
Claude = 1,
|
||||
}
|
||||
|
||||
// 模型库展示数据
|
||||
export interface ModelLibraryDto {
|
||||
modelId: string;
|
||||
name: string;
|
||||
description?: string;
|
||||
modelType: ModelTypeEnum;
|
||||
modelTypeName: string;
|
||||
modelApiType: ModelApiTypeEnum;
|
||||
modelApiTypeName: string;
|
||||
multiplierShow: number;
|
||||
providerName?: string;
|
||||
iconUrl?: string;
|
||||
isPremium: boolean;
|
||||
}
|
||||
|
||||
// 获取模型库列表查询参数
|
||||
export interface ModelLibraryGetListInput {
|
||||
searchKey?: string;
|
||||
providerNames?: string[];
|
||||
modelTypes?: ModelTypeEnum[];
|
||||
modelApiTypes?: ModelApiTypeEnum[];
|
||||
isPremiumOnly?: boolean;
|
||||
skipCount?: number;
|
||||
maxResultCount?: number;
|
||||
}
|
||||
|
||||
// 分页结果
|
||||
export interface PagedResultDto<T> {
|
||||
items: T[];
|
||||
totalCount: number;
|
||||
}
|
||||
|
||||
// 模型类型选项
|
||||
export interface ModelTypeOption {
|
||||
label: string;
|
||||
value: number;
|
||||
}
|
||||
|
||||
// API类型选项
|
||||
export interface ModelApiTypeOption {
|
||||
label: string;
|
||||
value: number;
|
||||
}
|
||||
|
||||
@@ -12,4 +12,4 @@ export const COLLAPSE_THRESHOLD: number = 600;
|
||||
export const SIDE_BAR_WIDTH: number = 280;
|
||||
|
||||
// 路由白名单地址[本地存在的路由 staticRouter.ts 中]
|
||||
export const ROUTER_WHITE_LIST: string[] = ['/chat', '/chat/not_login', '/products', '/403', '/404'];
|
||||
export const ROUTER_WHITE_LIST: string[] = ['/chat', '/chat/not_login', '/products', '/model-library', '/403', '/404'];
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
.layout-blank{
|
||||
height: 100vh;
|
||||
overflow: auto;
|
||||
margin: 20px ;
|
||||
//margin: 20px ;
|
||||
}
|
||||
/* 无样式 */
|
||||
</style>
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
<script setup lang="ts">
|
||||
import { useRouter } from 'vue-router';
|
||||
|
||||
const router = useRouter();
|
||||
|
||||
function goToModelLibrary() {
|
||||
router.push('/model-library');
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="model-library-btn-container" data-tour="model-library-btn">
|
||||
<div
|
||||
class="model-library-btn"
|
||||
title="查看模型库"
|
||||
@click="goToModelLibrary"
|
||||
>
|
||||
<!-- PC端显示文字 -->
|
||||
<span class="pc-text">模型库</span>
|
||||
<!-- 移动端显示图标 -->
|
||||
<svg
|
||||
class="mobile-icon"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
width="20"
|
||||
height="20"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
stroke-width="2"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
>
|
||||
<rect x="3" y="3" width="7" height="7" />
|
||||
<rect x="14" y="3" width="7" height="7" />
|
||||
<rect x="14" y="14" width="7" height="7" />
|
||||
<rect x="3" y="14" width="7" height="7" />
|
||||
</svg>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped lang="scss">
|
||||
.model-library-btn-container {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
|
||||
.model-library-btn {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
cursor: pointer;
|
||||
font-size: 1.2rem;
|
||||
font-weight: bold;
|
||||
color: #606266;
|
||||
transition: all 0.2s;
|
||||
|
||||
&:hover {
|
||||
color: #606266;
|
||||
transform: translateY(-1px);
|
||||
}
|
||||
|
||||
// PC端显示文字,隐藏图标
|
||||
.pc-text {
|
||||
display: inline;
|
||||
margin: 0 12px;
|
||||
}
|
||||
|
||||
.mobile-icon {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 移动端显示图标,隐藏文字
|
||||
@media (max-width: 768px) {
|
||||
.model-library-btn-container {
|
||||
.model-library-btn {
|
||||
.pc-text {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.mobile-icon {
|
||||
display: inline;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -10,6 +10,7 @@ import Avatar from './components/Avatar.vue';
|
||||
import Collapse from './components/Collapse.vue';
|
||||
import CreateChat from './components/CreateChat.vue';
|
||||
import LoginBtn from './components/LoginBtn.vue';
|
||||
import ModelLibraryBtn from './components/ModelLibraryBtn.vue';
|
||||
import TitleEditing from './components/TitleEditing.vue';
|
||||
|
||||
const userStore = useUserStore();
|
||||
@@ -72,6 +73,7 @@ onKeyStroke(event => event.ctrlKey && event.key.toLowerCase() === 'k', handleCtr
|
||||
<!-- 右边 -->
|
||||
<div class="right-box flex h-full items-center pr-20px flex-shrink-0 mr-auto flex-row">
|
||||
<AnnouncementBtn />
|
||||
<ModelLibraryBtn />
|
||||
<AiTutorialBtn />
|
||||
<Avatar v-show="userStore.userInfo" />
|
||||
<LoginBtn v-show="!userStore.userInfo" />
|
||||
|
||||
98
Yi.Ai.Vue3/src/pages/modelLibrary/element.text
Normal file
98
Yi.Ai.Vue3/src/pages/modelLibrary/element.text
Normal file
@@ -0,0 +1,98 @@
|
||||
<template>
|
||||
<div class="flex gap-2">
|
||||
<el-tag v-for="tag in tags" :key="tag.name" closable :type="tag.type">
|
||||
{{ tag.name }}
|
||||
</el-tag>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { ref } from 'vue'
|
||||
|
||||
import type { TagProps } from 'element-plus'
|
||||
|
||||
interface TagsItem {
|
||||
name: string
|
||||
type: TagProps['type']
|
||||
}
|
||||
|
||||
const tags = ref<TagsItem[]>([
|
||||
{ name: 'Tag 1', type: 'primary' },
|
||||
{ name: 'Tag 2', type: 'success' },
|
||||
{ name: 'Tag 3', type: 'info' },
|
||||
{ name: 'Tag 4', type: 'warning' },
|
||||
{ name: 'Tag 5', type: 'danger' },
|
||||
])
|
||||
</script>
|
||||
<template>
|
||||
<div class="flex gap-2">
|
||||
<el-check-tag checked>Checked</el-check-tag>
|
||||
<el-check-tag :checked="checked" @change="onChange">Toggle me</el-check-tag>
|
||||
<el-check-tag disabled>Disabled</el-check-tag>
|
||||
</div>
|
||||
<div class="flex gap-2 mt-4">
|
||||
<el-check-tag :checked="checked1" type="primary" @change="onChange1">
|
||||
Tag 1
|
||||
</el-check-tag>
|
||||
<el-check-tag :checked="checked2" type="success" @change="onChange2">
|
||||
Tag 2
|
||||
</el-check-tag>
|
||||
<el-check-tag :checked="checked3" type="info" @change="onChange3">
|
||||
Tag 3
|
||||
</el-check-tag>
|
||||
<el-check-tag :checked="checked4" type="warning" @change="onChange4">
|
||||
Tag 4
|
||||
</el-check-tag>
|
||||
<el-check-tag :checked="checked5" type="danger" @change="onChange5">
|
||||
Tag 5
|
||||
</el-check-tag>
|
||||
<el-check-tag
|
||||
:checked="checked6"
|
||||
disabled
|
||||
type="success"
|
||||
@change="onChange6"
|
||||
>
|
||||
Tag 6
|
||||
</el-check-tag>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { ref } from 'vue'
|
||||
|
||||
const checked = ref(false)
|
||||
const checked1 = ref(true)
|
||||
const checked2 = ref(true)
|
||||
const checked3 = ref(true)
|
||||
const checked4 = ref(true)
|
||||
const checked5 = ref(true)
|
||||
const checked6 = ref(true)
|
||||
|
||||
const onChange = (status: boolean) => {
|
||||
checked.value = status
|
||||
}
|
||||
|
||||
const onChange1 = (status: boolean) => {
|
||||
checked1.value = status
|
||||
}
|
||||
|
||||
const onChange2 = (status: boolean) => {
|
||||
checked2.value = status
|
||||
}
|
||||
|
||||
const onChange3 = (status: boolean) => {
|
||||
checked3.value = status
|
||||
}
|
||||
|
||||
const onChange4 = (status: boolean) => {
|
||||
checked4.value = status
|
||||
}
|
||||
|
||||
const onChange5 = (status: boolean) => {
|
||||
checked5.value = status
|
||||
}
|
||||
|
||||
const onChange6 = (status: boolean) => {
|
||||
checked6.value = status
|
||||
}
|
||||
</script>
|
||||
1070
Yi.Ai.Vue3/src/pages/modelLibrary/index.vue
Normal file
1070
Yi.Ai.Vue3/src/pages/modelLibrary/index.vue
Normal file
File diff suppressed because it is too large
Load Diff
@@ -37,12 +37,23 @@ export const layoutRouter: RouteRecordRaw[] = [
|
||||
component: () => import('@/pages/products/index.vue'),
|
||||
meta: {
|
||||
title: '产品页面',
|
||||
keepAlive: true, // 如果需要缓存
|
||||
isDefaultChat: false, // 根据实际情况设置
|
||||
layout: 'blankPage', // 如果需要自定义布局
|
||||
keepAlive: true,
|
||||
isDefaultChat: false,
|
||||
layout: 'blankPage',
|
||||
},
|
||||
|
||||
},
|
||||
{
|
||||
path: '/model-library',
|
||||
name: 'modelLibrary',
|
||||
component: () => import('@/pages/modelLibrary/index.vue'),
|
||||
meta: {
|
||||
title: '模型库',
|
||||
keepAlive: true,
|
||||
isDefaultChat: false,
|
||||
layout: 'blankPage',
|
||||
},
|
||||
},
|
||||
{
|
||||
path: '/pay-result',
|
||||
name: 'payResult',
|
||||
|
||||
43
Yi.Ai.Vue3/types/components.d.ts
vendored
43
Yi.Ai.Vue3/types/components.d.ts
vendored
@@ -13,6 +13,46 @@ declare module 'vue' {
|
||||
CardFlipActivity: typeof import('./../src/components/userPersonalCenter/components/CardFlipActivity.vue')['default']
|
||||
DailyTask: typeof import('./../src/components/userPersonalCenter/components/DailyTask.vue')['default']
|
||||
DeepThinking: typeof import('./../src/components/DeepThinking/index.vue')['default']
|
||||
ElAlert: typeof import('element-plus/es')['ElAlert']
|
||||
ElAvatar: typeof import('element-plus/es')['ElAvatar']
|
||||
ElBadge: typeof import('element-plus/es')['ElBadge']
|
||||
ElButton: typeof import('element-plus/es')['ElButton']
|
||||
ElCard: typeof import('element-plus/es')['ElCard']
|
||||
ElCheckbox: typeof import('element-plus/es')['ElCheckbox']
|
||||
ElCheckTag: typeof import('element-plus/es')['ElCheckTag']
|
||||
ElCol: typeof import('element-plus/es')['ElCol']
|
||||
ElCollapse: typeof import('element-plus/es')['ElCollapse']
|
||||
ElCollapseItem: typeof import('element-plus/es')['ElCollapseItem']
|
||||
ElContainer: typeof import('element-plus/es')['ElContainer']
|
||||
ElDatePicker: typeof import('element-plus/es')['ElDatePicker']
|
||||
ElDialog: typeof import('element-plus/es')['ElDialog']
|
||||
ElDivider: typeof import('element-plus/es')['ElDivider']
|
||||
ElEmpty: typeof import('element-plus/es')['ElEmpty']
|
||||
ElForm: typeof import('element-plus/es')['ElForm']
|
||||
ElFormItem: typeof import('element-plus/es')['ElFormItem']
|
||||
ElHeader: typeof import('element-plus/es')['ElHeader']
|
||||
ElIcon: typeof import('element-plus/es')['ElIcon']
|
||||
ElImage: typeof import('element-plus/es')['ElImage']
|
||||
ElInput: typeof import('element-plus/es')['ElInput']
|
||||
ElInputNumber: typeof import('element-plus/es')['ElInputNumber']
|
||||
ElMain: typeof import('element-plus/es')['ElMain']
|
||||
ElMenu: typeof import('element-plus/es')['ElMenu']
|
||||
ElMenuItem: typeof import('element-plus/es')['ElMenuItem']
|
||||
ElOption: typeof import('element-plus/es')['ElOption']
|
||||
ElPagination: typeof import('element-plus/es')['ElPagination']
|
||||
ElProgress: typeof import('element-plus/es')['ElProgress']
|
||||
ElRow: typeof import('element-plus/es')['ElRow']
|
||||
ElSelect: typeof import('element-plus/es')['ElSelect']
|
||||
ElSkeleton: typeof import('element-plus/es')['ElSkeleton']
|
||||
ElSwitch: typeof import('element-plus/es')['ElSwitch']
|
||||
ElTable: typeof import('element-plus/es')['ElTable']
|
||||
ElTableColumn: typeof import('element-plus/es')['ElTableColumn']
|
||||
ElTabPane: typeof import('element-plus/es')['ElTabPane']
|
||||
ElTabs: typeof import('element-plus/es')['ElTabs']
|
||||
ElTag: typeof import('element-plus/es')['ElTag']
|
||||
ElTimeline: typeof import('element-plus/es')['ElTimeline']
|
||||
ElTimelineItem: typeof import('element-plus/es')['ElTimelineItem']
|
||||
ElTooltip: typeof import('element-plus/es')['ElTooltip']
|
||||
FilesSelect: typeof import('./../src/components/FilesSelect/index.vue')['default']
|
||||
IconSelect: typeof import('./../src/components/IconSelect/index.vue')['default']
|
||||
Indexl: typeof import('./../src/components/SupportModelProducts/indexl.vue')['default']
|
||||
@@ -39,4 +79,7 @@ declare module 'vue' {
|
||||
VerificationCode: typeof import('./../src/components/LoginDialog/components/FormLogin/VerificationCode.vue')['default']
|
||||
WelecomeText: typeof import('./../src/components/WelecomeText/index.vue')['default']
|
||||
}
|
||||
export interface GlobalDirectives {
|
||||
vLoading: typeof import('element-plus/es')['ElLoadingDirective']
|
||||
}
|
||||
}
|
||||
|
||||
1
Yi.Ai.Vue3/types/import_meta.d.ts
vendored
1
Yi.Ai.Vue3/types/import_meta.d.ts
vendored
@@ -6,7 +6,6 @@ interface ImportMetaEnv {
|
||||
readonly VITE_WEB_ENV: string;
|
||||
readonly VITE_WEB_BASE_API: string;
|
||||
readonly VITE_API_URL: string;
|
||||
readonly VITE_BUILD_COMPRESS: string;
|
||||
readonly VITE_SSO_SEVER_URL: string;
|
||||
readonly VITE_APP_VERSION: string;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user