Compare commits

..

140 Commits

Author SHA1 Message Date
ccnetcore
a9e8b2b01f feat: 增加尊享包商品及折扣逻辑,完善VIP与尊享包相关接口和数据返回
- 新增尊享包商品类型,支持 5000W 和 10000W Tokens
- 增加尊享包折扣计算与折扣后价格获取方法
- PayService 新增获取商品列表接口,支持尊享包折扣展示
- PayManager 支持尊享包订单金额按折扣计算,并新增获取用户累计充值金额方法
- OpenApiService Anthropic接口增加VIP与尊享包用量校验
- AiGateWayManager 增加尊享包Token扣减逻辑
- AiAccountService 返回用户VIP状态、到期时间及尊享包Token统计信息
2025-10-12 20:07:58 +08:00
ccnetcore
4d09243efd feat: 完成尊享服务 2025-10-12 16:42:26 +08:00
ccnetcore
5934056fe6 fix: 修复Anthropic接口TokenUsage序列化及HttpClient创建方式问题 2025-10-12 14:38:26 +08:00
chenchun
2a81062fa3 perf: 优化 HttpClient 配置,增加 10 分钟超时设置 2025-10-12 00:02:34 +08:00
chenchun
fdc868323f fix: 修正 TokenUsage 计算逻辑,使用 CacheReadInputTokens 替代重复的 CacheCreationInputTokens 2025-10-11 23:36:26 +08:00
chenchun
593b3a4cdd fix: 修正消息与Anthropic返回的Token统计逻辑,避免零值覆盖并支持缓存Token计算 2025-10-11 23:27:46 +08:00
chenchun
2b12e18e6c fix: 为AnthropicHandles添加MaxTokens有效性校验 2025-10-11 20:32:41 +08:00
chenchun
345ed80ec8 feat: 新增claude接口转换支持 2025-10-11 15:25:43 +08:00
chenchun
29dc1ae250 fix: 限制 Azure OpenAI 请求最大 tokens 并优化响应处理空行格式 2025-10-10 15:16:16 +08:00
ccnetcore
9fdd41b134 fix: 更新会员套餐为8个月并调整价格信息 2025-10-08 21:24:29 +08:00
ccnetcore
31ee5e8ffb feat: 新增 YiXinVip 8 个月商品类型并移除 9 个月配置 2025-10-08 21:18:42 +08:00
ccnetcore
d7922bb71d fix: 修复 tokenUsage 为空时的空引用问题 2025-09-27 17:40:31 +08:00
chenchun
fa3ac91ba4 fix: 修正文件整理大师Vip数量限制提示错误 2025-09-18 16:17:10 +08:00
ccnetcore
e7c152e955 fix: 修复文件整理大师文件数量限制及模型名称错误 2025-09-13 21:19:54 +08:00
ccnetcore
0223b5c104 fix: 更新客服联系方式和产品价格信息
- 统一修改客服支持提示信息为"备注ai获取专属客服支持"
- 更新会员套餐价格和描述信息
- 替换模型排行榜iframe为openrouter链接
- 调整内容截断长度从2000到10000字符
2025-09-07 18:49:58 +08:00
ccnetcore
9e41a7c446 fix: 调整YiXinVip商品价格及有效期配置 2025-09-07 18:29:41 +08:00
ccnetcore
2ba0ffc1b7 Merge remote-tracking branch 'origin/ai-hub' into ai-hub 2025-09-07 17:16:13 +08:00
ccnetcore
7d038e1266 style: 优化样式1.3 2025-09-07 17:16:07 +08:00
Gsh
b98285f314 fix: 处理二维码接口过多调用问题 2025-09-07 13:30:03 +08:00
ccnetcore
73438da666 fix: 将 VerifyNext 接口由 GET 改为 POST 请求 2025-09-07 01:34:52 +08:00
ccnetcore
85f2e1b579 feat: 新增文件整理大师服务及校验与对话接口 2025-09-07 01:34:25 +08:00
ccnetcore
ece89ebad0 refactor: 移除自定义 HttpClientFactory,改用依赖注入的 IHttpClientFactory 2025-09-04 22:26:06 +08:00
Gsh
6e2dd39246 fix: 未登录支付按钮改登录 2025-09-03 11:43:40 +08:00
Gsh
a61286e534 fix: 非会员模型选择跳转修改 2025-09-03 10:56:44 +08:00
Gsh
4f944a5466 fix: 优化二维码加载错误显示 2025-08-31 23:56:42 +08:00
ccnetcore
d29aac088a feat: 全面优化ai-hub前端细节 2025-08-31 01:37:36 +08:00
Gsh
8abd122773 feat: 重新登录逻辑更改 2025-08-30 23:58:57 +08:00
Gsh
08084aa0bc feat: 默认展示二维码登录 2025-08-30 23:13:46 +08:00
Gsh
e69cd5a73c feat: 用户头像路径固定 2025-08-30 23:05:14 +08:00
Gsh
76aa3bdc64 feat: 用户头像路径固定 2025-08-30 22:39:27 +08:00
Gsh
93251104af Merge remote-tracking branch 'origin/ai-hub' into ai-hub 2025-08-30 22:29:21 +08:00
Gsh
3cae477f3e feat: 增加扫码登录功能 2025-08-30 22:28:38 +08:00
ccnetcore
25c736dc0a fix: 修复扫码回调在非等待状态下仍被处理的问题 2025-08-30 22:07:09 +08:00
ccnetcore
96a09d8980 fix: 修复绑定用户时返回值未包含用户ID的问题 2025-08-30 21:58:43 +08:00
ccnetcore
72387235a0 refactor: 移除分布式锁获取时的超时参数 2025-08-30 21:17:25 +08:00
ccnetcore
1b00e505b7 fix: 修复绑定微信二维码生成时未登录用户的异常提示 2025-08-30 18:02:46 +08:00
ccnetcore
1c54e47b9e feat: 新增AI账户服务及扩展用户信息获取功能,支持通过userId查询用户信息 2025-08-30 17:55:13 +08:00
ccnetcore
ba07e2c905 feat: 新增服务号注册授权页面并优化注册提示信息 2025-08-30 00:02:27 +08:00
ccnetcore
bde4611a50 fix: 修复服务号注册缓存时间及锁定范围问题 2025-08-29 23:32:40 +08:00
ccnetcore
e7326fea7b Merge remote-tracking branch 'origin/ai-hub' into ai-hub 2025-08-29 22:21:11 +08:00
ccnetcore
d13b23ad2e fix: 修复服务号扫码场景缓存未保存用户ID且场景结果错误的问题 2025-08-29 22:20:52 +08:00
Gsh
8b1830a711 Merge remote-tracking branch 'origin/ai-hub' into ai-hub 2025-08-29 22:02:14 +08:00
Gsh
b70c530754 fix: 增加扫码 2025-08-29 22:01:44 +08:00
ccnetcore
d90e24f9ed fix: 修复服务号回调处理返回用户ID及缓存更新逻辑 2025-08-29 21:55:07 +08:00
chenchun
1fbd521d1a fix: 注册接口加分布式锁防止并发重复注册 2025-08-29 14:33:35 +08:00
chenchun
2ae6183e7f fix: 防止重复注册意社区账号 2025-08-29 14:31:04 +08:00
chenchun
7905911624 feat: 注册用户时支持传入头像参数 2025-08-29 14:11:50 +08:00
chenchun
c5b6b33d8e feat: 注册公众号用户时保存额外用户信息到数据库 2025-08-29 13:54:02 +08:00
chenchun
5d29fd6d3b feat: 为微信服务号用户信息响应模型添加JsonPropertyName映射并移除无用字段 2025-08-29 13:41:50 +08:00
chenchun
ad8f48f36b feat: 服务号用户信息获取增加日志记录 2025-08-29 11:53:36 +08:00
chenchun
f9843c13d4 fix: 修复场景缓存为空时的处理逻辑并调整注册成功缓存写入方式 2025-08-29 11:34:57 +08:00
chenchun
6bd561b094 feat: 新增微信公众号扫码注册功能及幂等处理
- 新增 `FuwuhaoConst` 常量类,统一缓存 Key 前缀管理
- `FuwuhaoOptions` 增加 FromUser、RedirectUri、PicUrl 配置项
- `FuwuhaoManager` 新增 `BuildRegisterMessage` 方法,构建注册引导图文消息
- `FuwuhaoService`
  - 增加 OpenId 与 Scene 绑定缓存,支持扫码注册有效期管理
  - 回调处理支持注册场景,返回图文消息引导用户注册
  - 新增注册接口 `RegisterByCodeAsync`,根据微信授权信息自动注册账号并更新场景状态
- `AccountManager` 注册方法增加分布式锁,防止重复注册,并校验用户名唯一性
2025-08-29 11:01:09 +08:00
chenchun
d2c6238df1 feat: 启用AI股票生成与新闻生成任务并切换至OpenAI接口配置 2025-08-28 15:40:59 +08:00
chenchun
1d108983e8 feat: 增加服务号回调签名校验及扫码回调幂等处理
- `FuwuhaoManager` 新增 `ValidateCallback` 方法,用于校验微信回调签名
- `FuwuhaoOptions` 增加 `CallbackToken` 配置项
- `QrCodeResponse` 属性添加 `JsonPropertyName` 标注,支持 JSON 序列化映射
- `FuwuhaoService` 在回调接口中增加签名校验,并通过分布式锁实现幂等处理
- 调整场景值解析逻辑,过滤非扫码/关注事件
- 优化缓存过期时间设置
2025-08-28 15:20:15 +08:00
ccnetcore
b768bca638 feat: 完成支持微信扫码功能 2025-08-27 23:42:46 +08:00
chenchun
28fcd6c9ce feat: 新增服务号回调处理服务及数据模型 2025-08-27 17:46:39 +08:00
ccnetcore
10559a925c style: 优化样式1.2 2025-08-25 00:39:16 +08:00
ccnetcore
942e218a9e feat: 新增 FileMaster 发送消息接口 2025-08-23 13:15:28 +08:00
ccnetcore
f6af9edc38 feat: 删除文件 2025-08-23 12:23:17 +08:00
ccnetcore
e0f6331ec3 feat:构建 2025-08-23 12:21:16 +08:00
ccnetcore
06f0c6caa7 style: 整体调整 2025-08-23 12:14:09 +08:00
ccnetcore
56ec260e3a perf: 优化已完成状态下的输出等待时间,加快响应速度 2025-08-21 21:50:06 +08:00
ccnetcore
176cf84369 fix: 修复 AiGateWayManager 中可能的空引用异常 2025-08-21 01:16:57 +08:00
Gsh
6de3b722ed fix: 新增文件助手目录 2025-08-18 23:07:34 +08:00
Gsh
2cf6326764 fix: 更新组件库 2025-08-18 21:02:55 +08:00
Gsh
ec27ee58b4 fix: 充值成功与记录页面增加联系客服,apikey教程更改 2025-08-17 22:08:03 +08:00
ccnetcore
4e42e2202e refactor: 移除代码补全兼容逻辑并优化 DTO 可空类型处理 2025-08-16 19:20:58 +08:00
ccnetcore
e60d8eceb7 feat: 新增 YiXinVip 商品价格配置及前端构建压缩环境变量定义 2025-08-16 00:08:03 +08:00
Gsh
08fb939b38 fix: 动态支付响应页面 2025-08-15 23:49:18 +08:00
ccnetcore
482dd73afd feat: 支持创建订单时自定义支付宝回调地址 2025-08-15 23:41:01 +08:00
Gsh
f09a9fee75 fix: 产品相关页面样式优化 2025-08-15 23:28:09 +08:00
chenchun
9a31d14b41 feat: 优化CORS配置支持通配符和代码格式化
- 支持CORS配置使用通配符"*"允许所有来源访问
- 优化CORS策略配置逻辑,区分通配符和具体域名处理
- 格式化代码,移除多余空行和统一代码风格
- 修复Hangfire配置中的变量赋值格式
- 更新默认CORS配置为通配符模式
2025-08-14 17:04:27 +08:00
chenchun
2fd7f88f04 feat: 新增海外站点流量限制和CORS配置优化
- 新增yxai.chat域名到CORS白名单
- 为海外站点yxai.chat添加大流量接口访问限制
- 修复Azure OpenAI图像生成服务默认尺寸设置
2025-08-14 15:14:30 +08:00
chenchun
9d4cc802e9 fix: 添加CodeGeeX跨域支持
在CORS配置中新增http://codegeex域名,支持CodeGeeX工具的跨域访问请求
2025-08-14 14:24:03 +08:00
Gsh
ee6b4827fa feat: 增加支付宝在线支付、套餐订购弹窗、会员权益、支持模型展示等 2025-08-14 00:26:39 +08:00
ccnetcore
48d8c528f6 fix: 调整YiXinVip各档价格为0.01方便测试 2025-08-13 22:21:03 +08:00
ccnetcore
40c0a5ac64 feat: 支付完成后自动为用户充值VIP并支持按商品类型计算有效期 2025-08-13 22:19:31 +08:00
chenchun
3a60bcc174 refactor: 优化交易状态枚举处理方式
- 为TradeStatusEnum枚举添加Description特性标注
- 重构GetTradeStatusDescription方法,使用反射获取Description特性值
- 简化ParseTradeStatus方法,使用Enum.TryParse替代switch表达式
- 提高代码可维护性,避免硬编码状态描述
2025-08-13 18:30:56 +08:00
chenchun
2b3fad16fd feat: 优化支付宝回调通知记录功能
- 新增SignStr字段记录支付宝回调的原始签名字符串
- 修改日志记录格式,使用键值对形式记录回调通知数据
- 更新PayManager.RecordPayNoticeAsync方法支持记录原始签名字符串
- 移除AlipayManager中冗余的注释说明
2025-08-13 18:21:05 +08:00
chenchun
f0cf6bf5c8 fix: 修复支付宝支付功能相关问题
- 修复支付接口参数顺序错误,调整商品名称和订单号参数位置
- 修复支付页面HTML返回格式,直接返回Body内容而非序列化字符串
- 添加支付相关接口的权限控制,支付回调接口允许匿名访问
- 优化支付宝回调验签逻辑,保持原始参数顺序避免验签失败
- 增加回调格式错误的异常处理
- 修复商品类型枚举显示名称为英文,新增测试商品类型
- 修正Token服务提示文案中的错别字
- 移除订单更新时不必要的时间字段设置
2025-08-13 17:42:13 +08:00
chenchun
0ba4e3240b feat: 完成支付宝接入 2025-08-13 12:07:35 +08:00
ccnetcore
9332b17fc1 feat: 集成支付宝支付SDK并添加当面付测试调用,更新CORS配置支持capacitor 2025-08-13 08:26:45 +08:00
ccnetcore
4ec4023f40 feat: 增加EmbeddingResponse的object字段并完善AiGateWayManager的Usage统计,更新CORS配置 2025-08-11 20:24:48 +08:00
chenchun
d9971541f2 feat: 支持字符串类型的embedding输入参数
在AiGateWayManager中新增对JsonElement字符串类型的处理,确保embedding请求能够正确处理单个字符串输入参数。
2025-08-11 18:10:11 +08:00
chenchun
7b0e4fcc73 fix: 修复Embedding输入处理逻辑和字段可空性
- 优化Embedding输入类型判断逻辑,支持string和JsonElement数组类型
- 将EncodingFormat字段设置为可空类型,提高兼容性
- 注释知识库场景下的消息统计功能,避免不必要的数据记录
2025-08-11 18:05:33 +08:00
chenchun
cfde73d13a fix: 修复输出为空问题 2025-08-11 16:53:33 +08:00
chenchun
c17c9000a8 refactor: 移除AiHub Domain层对Application.Contracts的循环依赖
移除Yi.Framework.AiHub.Domain项目中对Yi.Framework.AiHub.Application.Contracts的项目引用,解决领域层和应用层之间的循环依赖问题,符合DDD架构分层原则。
2025-08-11 15:51:59 +08:00
chenchun
42d537a68b style: 调整架构引用 2025-08-11 15:31:11 +08:00
chenchun
25eebec8f7 feat: 新增向量嵌入服务支持
新增SiliconFlow向量嵌入服务实现,支持文本向量化功能:
- 新增ITextEmbeddingService接口和SiliconFlowTextEmbeddingService实现
- 新增EmbeddingCreateRequest/Response等向量相关DTO
- 在AiGateWayManager中新增EmbeddingForStatisticsAsync方法
- 在OpenApiService中新增向量生成API接口
- 扩展ModelTypeEnum枚举支持Embedding类型
- 优化ThorChatMessage的Content属性处理逻辑
2025-08-11 15:29:24 +08:00
Gsh
bbe5b01872 fix: 优化token图表,增加全屏显示 2025-08-10 15:34:53 +08:00
ccnetcore
6b31536de5 fix: 修复用户过期判断逻辑,按日期比较避免当天误判 2025-08-10 12:07:09 +08:00
ccnetcore
2e5db5500f Merge remote-tracking branch 'origin/ai-hub' into ai-hub 2025-08-10 11:53:52 +08:00
ccnetcore
7038d31c53 feat: 新增VIP充值接口并支持通过角色代码为用户分配角色 2025-08-10 11:53:28 +08:00
Gsh
3eb27c3d35 fix: 增加对话token显示,token消耗统计 2025-08-10 00:56:44 +08:00
ccnetcore
a9c3a1bcec fix: 修复统计中 Token 数量计算错误,将计数改为求和 2025-08-09 23:38:56 +08:00
ccnetcore
384926e73a feat: 新增用户数据导出功能 2025-08-09 22:55:26 +08:00
ccnetcore
4335c12659 chore: 注释掉生成新闻和股票价格的异步调用 2025-08-09 13:58:26 +08:00
ccnetcore
e6e4829164 feat: 新增VIP过期自动卸载功能
- 新增`AiRechargeManager`类,实现VIP过期用户的自动卸载逻辑。
- 新增`AiHubConst`常量类,统一管理角色名称。
- 在`IRoleService`中添加`RemoveUserRoleByRoleCodeAsync`方法,用于移除指定用户的角色。
- 在`RoleManager`中实现`RemoveUserRoleByRoleCodeAsync`方法。
- 优化`CurrentExtensions`中VIP角色判断逻辑,使用常量替代硬编码。
- 调整`YiAbpWebModule`中部分代码格式,提升可读性。
2025-08-09 13:14:15 +08:00
ccnetcore
f3c67cf598 fix: 修复统计数量偶发问题 2025-08-09 12:20:28 +08:00
ccnetcore
4681d468ce style: 优化验证码样式 2025-08-05 22:41:20 +08:00
chenchun
63e7d3d5f5 style: 更新主题2.2 2025-08-05 18:23:33 +08:00
chenchun
f47d8c8ce3 style: 优化2.1样式 2025-08-05 17:19:03 +08:00
chenchun
6f69f45ddc Merge branch 'bbs-sharpdance' into ai-hub 2025-08-05 14:11:16 +08:00
chenchun
e73678c788 style: 全部样式更新2.0 2025-08-05 14:09:39 +08:00
ccnetcore
09a2f91cbf style: 优化样式1.1 2025-08-04 23:55:48 +08:00
ccnetcore
29da7499a4 Merge branch 'bbs-sharpdance' into ai-hub 2025-08-04 23:37:11 +08:00
ccnetcore
5b024e9443 style: 重写ele 2025-08-04 23:34:13 +08:00
ccnetcore
225932eff1 style: 上线全局样式 2025-08-04 23:29:25 +08:00
Gsh
65d5f5ae86 fix: 加载优化、vip状态优化、apikey优化 2025-08-04 23:11:42 +08:00
ccnetcore
3e647ef14d style: 全局修改样式主题 2025-08-04 22:35:45 +08:00
chenchun
7cb3aea2e6 style: 调整样式 2025-08-04 18:27:18 +08:00
chenchun
7f4b8f1c8a feat: 添加暗色主题支持
- 在HTML根元素添加dark类名以启用暗色模式
- 引入Element Plus暗色主题CSS变量文件
- 格式化代码缩进和结构,提升代码可读性
2025-08-04 17:07:01 +08:00
ccnetcore
0a2710b865 feat: 支持图片生成 2025-08-04 01:03:47 +08:00
ccnetcore
2a301c4983 feat: 支持图片生成 2025-08-03 23:23:32 +08:00
Gsh
faa8131a1b fix: 未登录对话id逻辑玩优化 2025-08-03 21:56:51 +08:00
ccnetcore
71bd885bd0 fix: 支持router参数 2025-08-03 21:47:22 +08:00
ccnetcore
691a1e50f0 feat: 支持未登录用户统计 2025-08-03 21:32:54 +08:00
ccnetcore
ef6e9fd16d style: 优化提示词 2025-08-02 22:04:22 +08:00
chenchun
17f9ac6d54 style: 优化防抖样式 2025-08-01 17:58:07 +08:00
chenchun
3f8e6e48c0 Merge remote-tracking branch 'origin/ai-hub' into ai-hub 2025-07-28 14:39:09 +08:00
chenchun
bda4fdf69d feat: 兼容代码补全功能 2025-07-28 14:39:02 +08:00
Gsh
5c85ed13fd fix: 加载进度优化与登录弹窗优化 2025-07-28 13:43:46 +08:00
chenchun
1986901031 Merge remote-tracking branch 'origin/ai-hub' into ai-hub 2025-07-28 13:15:49 +08:00
chenchun
e1d3ec21e5 feat: 支持错误处理 2025-07-28 13:15:42 +08:00
Gsh
f45283dade Merge remote-tracking branch 'origin/ai-hub' into ai-hub 2025-07-28 12:59:42 +08:00
Gsh
31c44d8df7 fix: 登录弹窗超时功能取消 2025-07-28 12:59:07 +08:00
chenchun
bf443963c8 fix: 修复ThorChatCompletionsRequest中Messages属性的可空类型问题 2025-07-28 12:50:48 +08:00
chenchun
a0eb234539 feat: 兼容了用量使用显示 2025-07-22 10:40:23 +08:00
ccnetcore
b6d670c240 perf: 兼容deepseek格式 2025-07-21 22:03:55 +08:00
ccnetcore
b5fb2c42c6 feat: 兼容deepseek协议 2025-07-21 21:57:14 +08:00
ccnetcore
d72cc529ba perf: 优化流式输出 2025-07-21 21:15:02 +08:00
Gsh
660bd00cae fix: apikey加载状态 2025-07-20 22:12:48 +08:00
Gsh
b5489711ec fix: 加载优化 2025-07-20 21:01:41 +08:00
Gsh
76717c4f8a Merge remote-tracking branch 'origin/ai-hub' into ai-hub 2025-07-20 17:23:33 +08:00
ccnetcore
3d53d0bcd6 style: 完成进度条加载 2025-07-20 17:14:05 +08:00
ccnetcore
c7c9428b68 style: 完成进度条加载 2025-07-20 17:01:17 +08:00
ccnetcore
991a970d6a style: 完成进度条加载 2025-07-20 16:40:54 +08:00
ccnetcore
cbe93b9f7e style: 完成进度条加载 2025-07-20 15:15:05 +08:00
ccnetcore
5d7217b775 feat: 完成支持functioncall功能 2025-07-18 23:12:20 +08:00
ccnetcore
d6836b8bcf feat: 提交cicd产物 2025-07-18 21:08:51 +08:00
240 changed files with 11625 additions and 1126 deletions

View File

@@ -0,0 +1,36 @@
using Yi.Framework.Rbac.Domain.Shared.Dtos;
namespace Yi.Framework.AiHub.Application.Contracts.Dtos;
public class AiUserRoleMenuDto:UserRoleMenuDto
{
/// <summary>
/// 是否绑定服务号
/// </summary>
public bool IsBindFuwuhao { get; set; }
/// <summary>
/// 是否为VIP用户
/// </summary>
public bool IsVip { get; set; }
/// <summary>
/// VIP到期时间
/// </summary>
public DateTime? VipExpireTime { get; set; }
/// <summary>
/// 尊享包总Token数
/// </summary>
public long PremiumTotalTokens { get; set; }
/// <summary>
/// 尊享包已使用Token数
/// </summary>
public long PremiumUsedTokens { get; set; }
/// <summary>
/// 尊享包剩余Token数
/// </summary>
public long PremiumRemainingTokens { get; set; }
}

View File

@@ -0,0 +1,14 @@
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.FileMaster;
public class VerifyNextInput
{
/// <summary>
/// 文件数
/// </summary>
public int FileCount { get; set; }
/// <summary>
/// 文件夹数
/// </summary>
public int DirectoryCount { get; set; }
}

View File

@@ -0,0 +1,14 @@
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.Fuwuhao;
public class QrCodeOutput
{
/// <summary>
/// Qrcode url
/// </summary>
public string QrCodeUrl { get; set; }
/// <summary>
/// 场景值
/// </summary>
public string Scene { get; set; }
}

View File

@@ -0,0 +1,21 @@
using Yi.Framework.AiHub.Domain.Shared.Enums.Fuwuhao;
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.Fuwuhao;
public class QrCodeResultOutput
{
/// <summary>
/// 返回状态
/// </summary>
public SceneResultEnum SceneResult { get; set; } = SceneResultEnum.Wait;
/// <summary>
/// 如果是已登录返回token
/// </summary>
public string? Token { get; set; }
/// <summary>
/// 刷新token
/// </summary>
public string? RefreshToken { get; set; }
}

View File

@@ -0,0 +1,16 @@
using Yi.Framework.AiHub.Domain.Shared.Enums.Fuwuhao;
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.Fuwuhao;
public class SceneCacheDto
{
public SceneResultEnum SceneResult { get; set; } = SceneResultEnum.Wait;
public SceneTypeEnum SceneType { get; set; }
/// <summary>
/// 如果是绑定类型需要用户id
/// </summary>
public Guid? UserId { get; set; }
}

View File

@@ -0,0 +1,18 @@
using System.ComponentModel.DataAnnotations;
using Yi.Framework.AiHub.Domain.Shared.Enums;
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.Pay;
/// <summary>
/// 创建订单输入DTO
/// </summary>
public class CreateOrderInput
{
/// <summary>
/// 商品类型
/// </summary>
[Required]
public GoodsTypeEnum GoodsType { get; set; }
public string? ReturnUrl{ get; set; }
}

View File

@@ -0,0 +1,22 @@
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.Pay;
/// <summary>
/// 创建订单输出DTO
/// </summary>
public class CreateOrderOutput
{
/// <summary>
/// 订单ID
/// </summary>
public Guid OrderId { get; set; }
/// <summary>
/// 商家订单号
/// </summary>
public string OutTradeNo { get; set; }
/// <summary>
/// 支付页面HTML内容
/// </summary>
public object PaymentPageHtml { get; set; }
}

View File

@@ -0,0 +1,44 @@
using Yi.Framework.AiHub.Domain.Shared.Enums;
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.Pay;
/// <summary>
/// 商品列表输出DTO
/// </summary>
public class GoodsListOutput
{
/// <summary>
/// 商品名称
/// </summary>
public string GoodsName { get; set; }
/// <summary>
/// 商品原价
/// </summary>
public decimal OriginalPrice { get; set; }
/// <summary>
/// 商品实际价格(折扣后的价格)
/// </summary>
public decimal GoodsPrice { get; set; }
/// <summary>
/// 商品类型
/// </summary>
public GoodsTypeEnum GoodsType { get; set; }
/// <summary>
/// 商品备注
/// </summary>
public string Remark { get; set; }
/// <summary>
/// 折扣金额(仅尊享包)
/// </summary>
public decimal? DiscountAmount { get; set; }
/// <summary>
/// 折扣说明(仅尊享包)
/// </summary>
public string? DiscountDescription { get; set; }
}

View File

@@ -0,0 +1,12 @@
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.Pay;
/// <summary>
/// 查询订单状态输入DTO
/// </summary>
public class QueryOrderStatusInput
{
/// <summary>
/// 商家订单号
/// </summary>
public string OutTradeNo { get; set; }
}

View File

@@ -0,0 +1,59 @@
using Yi.Framework.AiHub.Domain.Shared.Enums;
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.Pay;
/// <summary>
/// 查询订单状态输出DTO
/// </summary>
public class QueryOrderStatusOutput
{
/// <summary>
/// 订单ID
/// </summary>
public Guid OrderId { get; set; }
/// <summary>
/// 商家订单号
/// </summary>
public string OutTradeNo { get; set; }
/// <summary>
/// 支付宝交易号
/// </summary>
public string? TradeNo { get; set; }
/// <summary>
/// 交易状态
/// </summary>
public TradeStatusEnum TradeStatus { get; set; }
/// <summary>
/// 交易状态描述
/// </summary>
public string TradeStatusDescription { get; set; }
/// <summary>
/// 订单金额
/// </summary>
public decimal TotalAmount { get; set; }
/// <summary>
/// 商品名称
/// </summary>
public string GoodsName { get; set; }
/// <summary>
/// 商品类型
/// </summary>
public GoodsTypeEnum GoodsType { get; set; }
/// <summary>
/// 创建时间
/// </summary>
public DateTime CreationTime { get; set; }
/// <summary>
/// 最后修改时间
/// </summary>
public DateTime? LastModificationTime { get; set; }
}

View File

@@ -0,0 +1,44 @@
using System.ComponentModel.DataAnnotations;
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.Recharge;
public class RechargeCreateInput
{
/// <summary>
/// 用户ID
/// </summary>
[Required]
public Guid UserId { get; set; }
/// <summary>
/// 充值金额
/// </summary>
[Required]
[Range(0.01, double.MaxValue, ErrorMessage = "充值金额必须大于0")]
public decimal RechargeAmount { get; set; }
/// <summary>
/// 充值内容
/// </summary>
[Required]
[StringLength(500, ErrorMessage = "充值内容不能超过500个字符")]
public string Content { get; set; } = string.Empty;
/// <summary>
/// VIP月数为空或0表示永久VIP
/// </summary>
[Range(0, int.MaxValue, ErrorMessage = "月数必须大于等于0")]
public int? Months { get; set; }
/// <summary>
/// 备注
/// </summary>
[StringLength(1000, ErrorMessage = "备注不能超过1000个字符")]
public string? Remark { get; set; }
/// <summary>
/// 联系方式
/// </summary>
[StringLength(200, ErrorMessage = "联系方式不能超过200个字符")]
public string? ContactInfo { get; set; }
}

View File

@@ -0,0 +1,17 @@
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.UsageStatistics;
/// <summary>
/// 每日Token使用量统计DTO
/// </summary>
public class DailyTokenUsageDto
{
/// <summary>
/// 日期
/// </summary>
public DateTime Date { get; set; }
/// <summary>
/// Token消耗量
/// </summary>
public long Tokens { get; set; }
}

View File

@@ -0,0 +1,22 @@
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.UsageStatistics;
/// <summary>
/// 模型Token使用量统计DTO
/// </summary>
public class ModelTokenUsageDto
{
/// <summary>
/// 模型ID
/// </summary>
public string Model { get; set; }
/// <summary>
/// 总消耗量
/// </summary>
public long Tokens { get; set; }
/// <summary>
/// 占比(百分比)
/// </summary>
public decimal Percentage { get; set; }
}

View File

@@ -0,0 +1,39 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.Application.Services;
using Yi.Framework.AiHub.Application.Contracts.Dtos.Pay;
namespace Yi.Framework.AiHub.Application.Contracts.IServices;
/// <summary>
/// 支付服务接口
/// </summary>
public interface IPayService : IApplicationService
{
/// <summary>
/// 创建订单并发起支付
/// </summary>
/// <param name="input">创建订单输入</param>
/// <returns>订单创建结果</returns>
Task<CreateOrderOutput> CreateOrderAsync(CreateOrderInput input);
/// <summary>
/// 支付宝异步通知处理
/// </summary>
/// <param name="form">表单数据</param>
/// <returns></returns>
Task<string> AlipayNotifyAsync([FromForm] IFormCollection form);
/// <summary>
/// 查询订单状态
/// </summary>
/// <param name="input">查询订单状态输入</param>
/// <returns>订单状态信息</returns>
Task<QueryOrderStatusOutput> QueryOrderStatusAsync([FromQuery] QueryOrderStatusInput input);
/// <summary>
/// 获取商品列表
/// </summary>
/// <returns>商品列表</returns>
Task<List<GoodsListOutput>> GetGoodsListAsync();
}

View File

@@ -0,0 +1,18 @@
using Yi.Framework.AiHub.Application.Contracts.Dtos.Recharge;
namespace Yi.Framework.AiHub.Application.Contracts.IServices;
public interface IRechargeService
{
/// <summary>
/// 移除用户vip及角色
/// </summary>
Task RemoveVipRoleByExpireAsync();
/// <summary>
/// 给用户充值VIP
/// </summary>
/// <param name="input">充值输入参数</param>
/// <returns></returns>
Task RechargeVipAsync(RechargeCreateInput input);
}

View File

@@ -0,0 +1,21 @@
using Yi.Framework.AiHub.Application.Contracts.Dtos.UsageStatistics;
namespace Yi.Framework.AiHub.Application.Contracts.IServices;
/// <summary>
/// 使用量统计服务接口
/// </summary>
public interface IUsageStatisticsService
{
/// <summary>
/// 获取当前用户近7天的Token消耗统计
/// </summary>
/// <returns>每日Token使用量列表</returns>
Task<List<DailyTokenUsageDto>> GetLast7DaysTokenUsageAsync();
/// <summary>
/// 获取当前用户各个模型的Token消耗量及占比
/// </summary>
/// <returns>模型Token使用量列表</returns>
Task<List<ModelTokenUsageDto>> GetModelTokenUsageAsync();
}

View File

@@ -6,8 +6,5 @@
<ProjectReference Include="..\..\rbac\Yi.Framework.Rbac.Application.Contracts\Yi.Framework.Rbac.Application.Contracts.csproj" />
<ProjectReference Include="..\Yi.Framework.AiHub.Domain.Shared\Yi.Framework.AiHub.Domain.Shared.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="IServices\" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,96 @@
using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.Application.Services;
using Volo.Abp.Users;
using Yi.Framework.AiHub.Application.Contracts.Dtos;
using Yi.Framework.AiHub.Domain.Entities;
using Yi.Framework.Rbac.Application.Contracts.IServices;
using Yi.Framework.Rbac.Domain.Shared.Dtos;
using Yi.Framework.SqlSugarCore.Abstractions;
using Yi.Framework.AiHub.Domain.Extensions;
using Yi.Framework.AiHub.Domain.Shared.Consts;
namespace Yi.Framework.AiHub.Application.Services;
public class AiAccountService : ApplicationService
{
private IAccountService _accountService;
private ISqlSugarRepository<AiUserExtraInfoEntity> _userRepository;
private ISqlSugarRepository<AiRechargeAggregateRoot> _rechargeRepository;
private ISqlSugarRepository<PremiumPackageAggregateRoot> _premiumPackageRepository;
public AiAccountService(
IAccountService accountService,
ISqlSugarRepository<AiUserExtraInfoEntity> userRepository,
ISqlSugarRepository<AiRechargeAggregateRoot> rechargeRepository,
ISqlSugarRepository<PremiumPackageAggregateRoot> premiumPackageRepository)
{
_accountService = accountService;
_userRepository = userRepository;
_rechargeRepository = rechargeRepository;
_premiumPackageRepository = premiumPackageRepository;
}
/// <summary>
/// 获取ai用户信息
/// </summary>
/// <returns></returns>
[Authorize]
[HttpGet("account/ai")]
public async Task<AiUserRoleMenuDto> GetAsync()
{
var userId = CurrentUser.GetId();
var userAccount = await _accountService.GetAsync(null, null, userId: CurrentUser.GetId());
var output = userAccount.Adapt<AiUserRoleMenuDto>();
// 是否绑定服务号
output.IsBindFuwuhao = await _userRepository.IsAnyAsync(x => userId == x.UserId);
// 是否为VIP用户
output.IsVip = CurrentUser.IsAiVip();
// 获取VIP到期时间
if (output.IsVip)
{
var recharges = await _rechargeRepository._DbQueryable
.Where(x => x.UserId == userId)
.ToListAsync();
if (recharges.Any())
{
// 如果有任何一个充值记录的过期时间为null说明是永久VIP
if (recharges.Any(x => !x.ExpireDateTime.HasValue))
{
output.VipExpireTime = null; // 永久VIP
}
else
{
// 取最大的过期时间
output.VipExpireTime = recharges
.Where(x => x.ExpireDateTime.HasValue)
.Max(x => x.ExpireDateTime);
}
}
}
// 获取尊享包Token信息
var premiumPackages = await _premiumPackageRepository._DbQueryable
.Where(x => x.UserId == userId && x.IsActive)
.ToListAsync();
if (premiumPackages.Any())
{
// 过滤掉已过期的包
var validPackages = premiumPackages
.Where(p => p.IsAvailable())
.ToList();
output.PremiumTotalTokens = validPackages.Sum(x => x.TotalTokens);
output.PremiumUsedTokens = validPackages.Sum(x => x.UsedTokens);
output.PremiumRemainingTokens = validPackages.Sum(x => x.RemainingTokens);
}
return output;
}
}

View File

@@ -12,12 +12,13 @@ using OpenAI.Chat;
using Volo.Abp.Application.Services;
using Volo.Abp.Users;
using Yi.Framework.AiHub.Application.Contracts.Dtos;
using Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi;
using Yi.Framework.AiHub.Domain.Entities;
using Yi.Framework.AiHub.Domain.Entities.Model;
using Yi.Framework.AiHub.Domain.Extensions;
using Yi.Framework.AiHub.Domain.Managers;
using Yi.Framework.AiHub.Domain.Shared.Dtos;
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
using Yi.Framework.AiHub.Domain.Shared.Enums;
using Yi.Framework.Rbac.Application.Contracts.IServices;
using Yi.Framework.Rbac.Domain.Shared.Dtos;
using Yi.Framework.SqlSugarCore.Abstractions;
@@ -68,6 +69,7 @@ public class AiChatService : ApplicationService
public async Task<List<ModelGetListOutput>> GetModelAsync()
{
var output = await _aiModelRepository._DbQueryable
.Where(x => x.ModelType == ModelTypeEnum.Chat)
.OrderByDescending(x => x.OrderNum)
.Select(x => new ModelGetListOutput
{
@@ -94,7 +96,8 @@ public class AiChatService : ApplicationService
/// <param name="input"></param>
/// <param name="sessionId"></param>
/// <param name="cancellationToken"></param>
public async Task PostSendAsync([FromBody] ThorChatCompletionsRequest input, [FromRoute] Guid sessionId,
[HttpPost("ai-chat/send")]
public async Task PostSendAsync([FromBody] ThorChatCompletionsRequest input, [FromQuery] Guid? sessionId,
CancellationToken cancellationToken)
{
//除了免费模型,其他的模型都要校验
@@ -114,8 +117,46 @@ public class AiChatService : ApplicationService
throw new UserFriendlyException("未登录用户只能使用未加速的DeepSeek-R1请登录后重试");
}
}
//ai网关代理httpcontext
await _aiGateWayManager.CompleteChatStreamForStatisticsAsync(_httpContextAccessor.HttpContext, input,
CurrentUser.Id, sessionId, cancellationToken);
}
/// <summary>
/// 发送消息
/// </summary>
/// <param name="input"></param>
/// <param name="cancellationToken"></param>
[HttpPost("ai-chat/FileMaster/send")]
public async Task PostFileMasterSendAsync([FromBody] ThorChatCompletionsRequest input,
CancellationToken cancellationToken)
{
if (!string.IsNullOrWhiteSpace(input.Model))
{
throw new BusinessException("当前接口不支持第三方使用");
}
if (CurrentUser.IsAuthenticated)
{
await _aiBlacklistManager.VerifiyAiBlacklist(CurrentUser.GetId());
if (CurrentUser.IsAiVip())
{
input.Model = "gpt-5-chat";
}
else
{
input.Model = "gpt-4.1-mini";
}
}
else
{
input.Model = "DeepSeek-R1-0528";
}
//ai网关代理httpcontext
await _aiGateWayManager.CompleteChatStreamForStatisticsAsync(_httpContextAccessor.HttpContext, input,
CurrentUser.Id, null, cancellationToken);
}
}

View File

@@ -0,0 +1,88 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.Application.Services;
using Volo.Abp.Users;
using Yi.Framework.AiHub.Application.Contracts.Dtos.FileMaster;
using Yi.Framework.AiHub.Domain.Managers;
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
namespace Yi.Framework.AiHub.Application.Services.FileMaster;
public class FileMasterService : ApplicationService
{
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly AiGateWayManager _aiGateWayManager;
private readonly AiBlacklistManager _aiBlacklistManager;
public FileMasterService(IHttpContextAccessor httpContextAccessor, AiGateWayManager aiGateWayManager,
AiBlacklistManager aiBlacklistManager)
{
_httpContextAccessor = httpContextAccessor;
_aiGateWayManager = aiGateWayManager;
_aiBlacklistManager = aiBlacklistManager;
}
/// <summary>
/// 校验下一步
/// </summary>
/// <returns></returns>
[HttpPost("FileMaster/VerifyNext")]
public Task<string> VerifyNextAsync(VerifyNextInput input)
{
if (!CurrentUser.IsAuthenticated)
{
if (input.DirectoryCount + input.FileCount >= 20)
{
throw new UserFriendlyException("未登录用户文件夹与文件数量不能大于20个请登录后解锁全部功能");
}
}
else
{
if (input.DirectoryCount + input.FileCount >= 100)
{
throw new UserFriendlyException("为防止无限制暴力使用当前文件整理大师Vip最多支持100文件与文件夹数量");
}
}
return Task.FromResult("success");
}
/// <summary>
/// 对话
/// </summary>
/// <param name="input"></param>
/// <param name="cancellationToken"></param>
[HttpPost("FileMaster/chat/completions")]
public async Task ChatCompletionsAsync([FromBody] ThorChatCompletionsRequest input,
CancellationToken cancellationToken)
{
if (CurrentUser.IsAuthenticated)
{
input.Model = "gpt-5-chat";
}
else
{
input.Model = "gpt-5-chat";
}
Guid? userId = CurrentUser.IsAuthenticated ? CurrentUser.GetId() : null;
if (userId is not null)
{
await _aiBlacklistManager.VerifiyAiBlacklist(userId.Value);
}
//ai网关代理httpcontext
if (input.Stream == true)
{
await _aiGateWayManager.CompleteChatStreamForStatisticsAsync(_httpContextAccessor.HttpContext, input,
userId, null, cancellationToken);
}
else
{
await _aiGateWayManager.CompleteChatForStatisticsAsync(_httpContextAccessor.HttpContext, input, userId,
null,
cancellationToken);
}
}
}

View File

@@ -0,0 +1,309 @@
using System.Text;
using System.Text.Json;
using System.Xml.Serialization;
using Medallion.Threading;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Logging;
using Volo.Abp.Application.Services;
using Volo.Abp.Caching;
using Volo.Abp.Users;
using Yi.Framework.AiHub.Application.Contracts.Dtos.Fuwuhao;
using Yi.Framework.AiHub.Domain.Entities;
using Yi.Framework.AiHub.Domain.Managers.Fuwuhao;
using Yi.Framework.AiHub.Domain.Shared.Consts;
using Yi.Framework.AiHub.Domain.Shared.Enums.Fuwuhao;
using Yi.Framework.Rbac.Application.Contracts.Dtos.Account;
using Yi.Framework.Rbac.Application.Contracts.IServices;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.AiHub.Application.Services;
/// <summary>
/// 服务号服务
/// </summary>
public class FuwuhaoService : ApplicationService
{
private readonly ILogger<FuwuhaoService> _logger;
private readonly IHttpContextAccessor _accessor;
private readonly FuwuhaoManager _fuwuhaoManager;
private IDistributedCache<SceneCacheDto> _sceneCache;
private IDistributedCache<string> _openIdToSceneCache;
private ISqlSugarRepository<AiUserExtraInfoEntity> _userRepository;
private IAccountService _accountService;
private IFileService _fileService;
public IDistributedLockProvider DistributedLock => LazyServiceProvider.LazyGetService<IDistributedLockProvider>();
public FuwuhaoService(ILogger<FuwuhaoService> logger, IHttpContextAccessor accessor, FuwuhaoManager fuwuhaoManager,
IDistributedCache<SceneCacheDto> sceneCache, IAccountService accountService, IFileService fileService,
IDistributedCache<string> openIdToSceneCache, ISqlSugarRepository<AiUserExtraInfoEntity> userRepositroy)
{
_logger = logger;
_accessor = accessor;
_fuwuhaoManager = fuwuhaoManager;
_sceneCache = sceneCache;
_accountService = accountService;
_fileService = fileService;
_openIdToSceneCache = openIdToSceneCache;
_userRepository = userRepositroy;
}
/// <summary>
/// 服务器号测试回调
/// </summary>
/// <returns></returns>
[HttpGet("fuwuhao/callback")]
public async Task<string> GetCallbackAsync([FromQuery] string signature, [FromQuery] string timestamp,
[FromQuery] string nonce, [FromQuery] string echostr)
{
return echostr;
}
/// <summary>
/// 服务号关注回调
/// </summary>
/// <param name="signature"></param>
/// <param name="timestamp"></param>
/// <param name="nonce"></param>
/// <returns></returns>
[HttpPost("fuwuhao/callback")]
public async Task<string> PostCallbackAsync([FromQuery] string signature, [FromQuery] string timestamp,
[FromQuery] string nonce)
{
_fuwuhaoManager.ValidateCallback(signature, timestamp, nonce);
var request = _accessor.HttpContext.Request;
// 1. 读取原始 XML 内容
using var reader = new StreamReader(request.Body, Encoding.UTF8);
var xmlString = await reader.ReadToEndAsync();
var serializer = new XmlSerializer(typeof(FuwuhaoCallModel));
using var stringReader = new StringReader(xmlString);
var body = (FuwuhaoCallModel)serializer.Deserialize(stringReader);
//获取场景值,后续通过场景值设置缓存状态,前端轮询这个场景值用户是否已操作即可
var scene = body.EventKey.Replace("qrscene_", "");
if (!(body.Event is "SCAN" or "subscribe"))
{
throw new UserFriendlyException("当前回调只处理扫码 与 关注");
}
if (scene is null)
{
throw new UserFriendlyException("服务号返回无场景值");
}
//制作幂等
await using (var handle =
await DistributedLock.TryAcquireLockAsync($"Yi:fuwuhao:callbacklock:{scene}"))
{
if (handle == null)
{
return "success"; // 跳过直接返回成功
}
var cache = await _sceneCache.GetAsync($"{FuwuhaoConst.SceneCacheKey}{scene}");
if (cache == null)
{
return "success"; // 跳过直接返回成功
}
if (cache.SceneResult != SceneResultEnum.Wait)
{
return "success"; // 跳过直接返回成功
}
//根据操作类型,进行业务处理,返回处理结果,再写入缓存,10s过去相当于用户10s扫完app后轮询要在10秒内完成
var scenResult =
await _fuwuhaoManager.CallBackHandlerAsync(cache.SceneType, body.FromUserName, cache.UserId);
cache.SceneResult = scenResult.SceneResult;
cache.UserId = scenResult.UserId;
await _sceneCache.SetAsync($"{FuwuhaoConst.SceneCacheKey}{scene}", cache,
new DistributedCacheEntryOptions() { AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(120) });
//如果是注册将OpenId与Scene进行绑定代表用户有30分钟进行注册
if (scenResult.SceneResult == SceneResultEnum.Register)
{
await _openIdToSceneCache.SetAsync($"{FuwuhaoConst.OpenIdToSceneCacheKey}{body.FromUserName}", scene,
new DistributedCacheEntryOptions() { AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30) });
var replyMessage =
_fuwuhaoManager.BuildRegisterMessage(body.FromUserName);
return replyMessage;
}
}
return "success";
}
/// <summary>
/// 创建带参数的二维码
/// </summary>
/// <returns>二维码URL</returns>
[HttpPost("fuwuhao/qrcode")]
public async Task<QrCodeOutput> GetQrCodeAsync([FromQuery] SceneTypeEnum sceneType)
{
if (sceneType == SceneTypeEnum.Bind && CurrentUser.Id is null)
{
throw new UserFriendlyException("绑定微信,需登录用户,请重新登录后重试");
}
//生成一个随机场景值
var scene = Guid.NewGuid().ToString("N");
var qrCodeUrl = await _fuwuhaoManager.CreateQrCodeAsync(scene);
await _sceneCache.SetAsync($"{FuwuhaoConst.SceneCacheKey}{scene}", new SceneCacheDto()
{
UserId = CurrentUser.IsAuthenticated ? CurrentUser.GetId() : null,
SceneType = sceneType
}, new DistributedCacheEntryOptions() { AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(10) });
return new QrCodeOutput()
{
QrCodeUrl = qrCodeUrl,
Scene = scene
};
}
/// <summary>
/// 扫码登录/注册/绑定,轮询接口
/// </summary>
/// <param name="scene"></param>
/// <returns></returns>
[HttpGet("fuwuhao/qrcode/result")]
public async Task<QrCodeResultOutput> GetQrCodeResultAsync([FromQuery] string scene)
{
var output = new QrCodeResultOutput();
var cache = await _sceneCache.GetAsync($"{FuwuhaoConst.SceneCacheKey}{scene}");
if (cache is null)
{
output.SceneResult = SceneResultEnum.Expired;
return output;
}
output.SceneResult = cache.SceneResult;
switch (output.SceneResult)
{
case SceneResultEnum.Login:
if (cache.UserId is null)
{
throw new ApplicationException("获取用户id异常请重试");
}
var loginInfo = await _accountService.PostLoginAsync(cache.UserId!.Value);
output.Token = loginInfo.Token;
output.RefreshToken = loginInfo.RefreshToken;
break;
}
return output;
}
/// <summary>
/// 注册账号需要微信code
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
[HttpGet("fuwuhao/register")]
public async Task<IActionResult> RegisterByCodeAsync([FromQuery] string code)
{
var message = await RegisterByCodeForMessageAsync(code);
//var message = "恭喜注册";
var filePath = Path.Combine("wwwroot", "aihub", "auth.html");
var html = await File.ReadAllTextAsync(filePath);
var result = new ContentResult
{
Content = html.Replace("{{message}}", message),
ContentType = "text/html",
StatusCode = 200
};
return result;
}
private async Task<string> RegisterByCodeForMessageAsync(string code)
{
//根据code获取到openid、微信用户昵称、头像
var userInfo = await _fuwuhaoManager.GetUserInfoByCodeAsync(code);
if (userInfo is null)
{
return "当前注册已经失效,请重新扫码注册!";
}
var scene = await _openIdToSceneCache.GetAsync($"{FuwuhaoConst.OpenIdToSceneCacheKey}{userInfo.OpenId}");
if (scene is null)
{
return "当前注册已经过期,请重新扫码注册!";
}
var files = new FormFileCollection();
// 下载头像并添加到系统文件中
if (!string.IsNullOrEmpty(userInfo.HeadImgUrl))
{
using var httpClient = new HttpClient();
var imageBytes = await httpClient.GetByteArrayAsync(userInfo.HeadImgUrl);
var imageStream = new MemoryStream(imageBytes);
// 从URL中提取文件扩展名默认为png
var fileName = $"avatar_{userInfo.OpenId}.png";
var formFile = new FormFile(imageStream, 0, imageBytes.Length, "avatar", fileName)
{
Headers = new HeaderDictionary(),
ContentType = "image/png"
};
files.Add(formFile);
}
var result = await _fileService.Post(files);
//由于存在查询/编辑在同一个事务操作,上锁防止并发
await using (await DistributedLock.AcquireLockAsync($"fuwuhao:RegisterLock:{userInfo.OpenId}",
TimeSpan.FromMinutes(1)))
{
if (await _userRepository.IsAnyAsync(x => x.FuwuhaoOpenId == userInfo.OpenId))
{
return "你已注册过意社区账号!";
}
Guid userId;
try
{
userId = await _accountService.PostSystemRegisterAsync(new RegisterDto
{
UserName = $"wx{Random.Shared.Next(100000, 999999)}",
Password = Guid.NewGuid().ToString("N"),
Phone = null,
Email = null,
Nick = userInfo.Nickname,
Icon = result.FirstOrDefault()?.Id.ToString()
});
}
catch (UserFriendlyException e)
{
return e.Message;
}
await _userRepository.InsertAsync(new AiUserExtraInfoEntity(userId, userInfo.OpenId));
await _sceneCache.SetAsync($"{FuwuhaoConst.SceneCacheKey}{scene}", new SceneCacheDto
{
UserId = userId,
SceneResult = SceneResultEnum.Login
},
new DistributedCacheEntryOptions() { AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(120) });
}
return "恭喜你成功注册意社区账号!";
}
}
[XmlRoot("xml")]
public class FuwuhaoCallModel
{
[XmlElement("ToUserName")] public string ToUserName { get; set; }
[XmlElement("FromUserName")] public string FromUserName { get; set; }
[XmlElement("CreateTime")] public string CreateTime { get; set; }
[XmlElement("MsgType")] public string MsgType { get; set; }
[XmlElement("Event")] public string Event { get; set; }
[XmlElement("EventKey")] public string EventKey { get; set; }
[XmlElement("Ticket")] public string Ticket { get; set; }
}

View File

@@ -2,10 +2,18 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Volo.Abp.Application.Services;
using Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi;
using Volo.Abp.Users;
using Yi.Framework.AiHub.Domain.Entities;
using Yi.Framework.AiHub.Domain.Entities.Model;
using Yi.Framework.AiHub.Domain.Extensions;
using Yi.Framework.AiHub.Domain.Managers;
using Yi.Framework.AiHub.Domain.Shared.Consts;
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.Enums;
using Yi.Framework.Rbac.Application.Contracts.IServices;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.AiHub.Application.Services;
@@ -17,16 +25,23 @@ public class OpenApiService : ApplicationService
private readonly TokenManager _tokenManager;
private readonly AiGateWayManager _aiGateWayManager;
private readonly ISqlSugarRepository<AiModelEntity> _aiModelRepository;
private readonly AiBlacklistManager _aiBlacklistManager;
private readonly IAccountService _accountService;
private readonly PremiumPackageManager _premiumPackageManager;
public OpenApiService(IHttpContextAccessor httpContextAccessor, ILogger<OpenApiService> logger,
TokenManager tokenManager, AiGateWayManager aiGateWayManager,
ISqlSugarRepository<AiModelEntity> aiModelRepository)
ISqlSugarRepository<AiModelEntity> aiModelRepository, AiBlacklistManager aiBlacklistManager,
IAccountService accountService, PremiumPackageManager premiumPackageManager)
{
_httpContextAccessor = httpContextAccessor;
_logger = logger;
_tokenManager = tokenManager;
_aiGateWayManager = aiGateWayManager;
_aiModelRepository = aiModelRepository;
_aiBlacklistManager = aiBlacklistManager;
_accountService = accountService;
_premiumPackageManager = premiumPackageManager;
}
/// <summary>
@@ -41,6 +56,7 @@ public class OpenApiService : ApplicationService
//前面都是校验,后面才是真正的调用
var httpContext = this._httpContextAccessor.HttpContext;
var userId = await _tokenManager.GetUserIdAsync(GetTokenByHttpContext(httpContext));
await _aiBlacklistManager.VerifiyAiBlacklist(userId);
//ai网关代理httpcontext
if (input.Stream == true)
{
@@ -55,6 +71,37 @@ public class OpenApiService : ApplicationService
}
}
/// <summary>
/// 图片生成
/// </summary>
/// <param name="input"></param>
/// <param name="cancellationToken"></param>
[HttpPost("openApi/v1/images/generations")]
public async Task ImagesGenerationsAsync([FromBody] ImageCreateRequest input, CancellationToken cancellationToken)
{
var httpContext = this._httpContextAccessor.HttpContext;
Intercept(httpContext);
var userId = await _tokenManager.GetUserIdAsync(GetTokenByHttpContext(httpContext));
await _aiBlacklistManager.VerifiyAiBlacklist(userId);
await _aiGateWayManager.CreateImageForStatisticsAsync(httpContext, userId, null, input);
}
/// <summary>
/// 向量生成
/// </summary>
/// <param name="input"></param>
/// <param name="cancellationToken"></param>
[HttpPost("openApi/v1/embeddings")]
public async Task EmbeddingAsync([FromBody] ThorEmbeddingInput input, CancellationToken cancellationToken)
{
var httpContext = this._httpContextAccessor.HttpContext;
Intercept(httpContext);
var userId = await _tokenManager.GetUserIdAsync(GetTokenByHttpContext(httpContext));
await _aiBlacklistManager.VerifiyAiBlacklist(userId);
await _aiGateWayManager.EmbeddingForStatisticsAsync(httpContext, userId, null, input);
}
/// <summary>
/// 获取模型列表
/// </summary>
@@ -63,6 +110,7 @@ public class OpenApiService : ApplicationService
public async Task<ModelsListDto> ModelsAsync()
{
var data = await _aiModelRepository._DbQueryable
.Where(x => x.ModelType == ModelTypeEnum.Chat)
.OrderByDescending(x => x.OrderNum)
.Select(x => new ModelsDataDto
{
@@ -79,6 +127,58 @@ public class OpenApiService : ApplicationService
};
}
/// <summary>
/// Anthropic对话尊享服务专用
/// </summary>
/// <param name="input"></param>
/// <param name="cancellationToken"></param>
[HttpPost("openApi/v1/messages")]
public async Task MessagesAsync([FromBody] AnthropicInput input,
CancellationToken cancellationToken)
{
//前面都是校验,后面才是真正的调用
var httpContext = this._httpContextAccessor.HttpContext;
var userId = await _tokenManager.GetUserIdAsync(GetTokenByHttpContext(httpContext));
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)
{
await _aiGateWayManager.AnthropicCompleteChatStreamForStatisticsAsync(_httpContextAccessor.HttpContext, input,
userId, null, cancellationToken);
}
else
{
await _aiGateWayManager.AnthropicCompleteChatForStatisticsAsync(_httpContextAccessor.HttpContext, input, userId,
null,
cancellationToken);
}
}
#region
private string? GetTokenByHttpContext(HttpContext httpContext)
{
// 获取Authorization头
@@ -92,4 +192,15 @@ public class OpenApiService : ApplicationService
return null;
}
private void Intercept(HttpContext httpContext)
{
if (httpContext.Request.Host.Value == "yxai.chat")
{
throw new UserFriendlyException("当前海外站点不支持大流量接口请使用转发站点https://ai.ccnetcore.com");
}
}
#endregion
}

View File

@@ -0,0 +1,293 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.Application.Services;
using Yi.Framework.AiHub.Domain.Alipay;
using Yi.Framework.AiHub.Domain.Managers;
using Yi.Framework.AiHub.Application.Contracts.Dtos.Pay;
using Yi.Framework.AiHub.Domain.Shared.Enums;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Yi.Framework.AiHub.Application.Contracts.IServices;
using Volo.Abp;
using Yi.Framework.AiHub.Domain.Entities.Pay;
using Yi.Framework.SqlSugarCore.Abstractions;
using System.ComponentModel;
using System.Reflection;
using Volo.Abp.Users;
using Yi.Framework.AiHub.Application.Contracts.Dtos.Recharge;
namespace Yi.Framework.AiHub.Application.Services;
/// <summary>
/// 支付服务
/// </summary>
public class PayService : ApplicationService, IPayService
{
private readonly AlipayManager _alipayManager;
private readonly PayManager _payManager;
private readonly ILogger<PayService> _logger;
private readonly ISqlSugarRepository<PayOrderAggregateRoot, Guid> _payOrderRepository;
private readonly IRechargeService _rechargeService;
private readonly PremiumPackageManager _premiumPackageManager;
public PayService(
AlipayManager alipayManager,
PayManager payManager,
ILogger<PayService> logger,
ISqlSugarRepository<PayOrderAggregateRoot, Guid> payOrderRepository,
IRechargeService rechargeService,
PremiumPackageManager premiumPackageManager)
{
_alipayManager = alipayManager;
_payManager = payManager;
_logger = logger;
_payOrderRepository = payOrderRepository;
_rechargeService = rechargeService;
_premiumPackageManager = premiumPackageManager;
}
/// <summary>
/// 创建订单并发起支付
/// </summary>
/// <param name="input">创建订单输入</param>
/// <returns>订单创建结果</returns>
[Authorize]
[HttpPost("pay/Order")]
public async Task<CreateOrderOutput> CreateOrderAsync(CreateOrderInput input)
{
// 1. 通过PayManager创建订单内部会验证VIP资格
var order = await _payManager.CreateOrderAsync(input.GoodsType);
// 2. 通过AlipayManager发起页面支付
var paymentPageHtml = await _alipayManager.PaymentPageAsync(
order.GoodsName,
order.OutTradeNo,
order.TotalAmount,
input.ReturnUrl);
// 3. 返回结果
return new CreateOrderOutput
{
OrderId = order.Id,
OutTradeNo = order.OutTradeNo,
PaymentPageHtml = paymentPageHtml.Body
};
}
/// <summary>
/// 支付宝异步通知处理
/// </summary>
/// <param name="form">表单数据</param>
/// <returns></returns>
[HttpPost("pay/AlipayNotify")]
[AllowAnonymous]
public async Task<string> AlipayNotifyAsync([FromForm] IFormCollection form)
{
// 1. 将表单数据转换为字典,保持原始顺序
var notifyData = new Dictionary<string, string>();
foreach (var item in form)
{
notifyData[item.Key] = item.Value.ToString();
}
var signStr = string.Join("&", notifyData.Select(kv => $"{kv.Key}={kv.Value}"));
_logger.LogInformation($"收到支付宝回调通知:{signStr}");
// 2. 验证签名
await _alipayManager.VerifyNotifyAsync(notifyData);
// 3. 记录支付通知
await _payManager.RecordPayNoticeAsync(notifyData, signStr);
// 4. 更新订单状态
var outTradeNo = notifyData.GetValueOrDefault("out_trade_no", string.Empty);
var tradeStatus = notifyData.GetValueOrDefault("trade_status", string.Empty);
var tradeNo = notifyData.GetValueOrDefault("trade_no", string.Empty);
if (!string.IsNullOrEmpty(outTradeNo) && !string.IsNullOrEmpty(tradeStatus))
{
var status = ParseTradeStatus(tradeStatus);
var order = await _payManager.UpdateOrderStatusAsync(outTradeNo, status, tradeNo);
_logger.LogInformation("订单状态更新成功,订单号:{OutTradeNo},状态:{TradeStatus}", outTradeNo, tradeStatus);
// 5. 根据商品类型进行不同的处理
if (order.GoodsType.IsPremiumPackage())
{
// 处理尊享包商品:创建尊享包记录
await _premiumPackageManager.CreatePremiumPackageAsync(
order.UserId,
order.GoodsType,
order.TotalAmount,
expireMonths: null // 尊享包不设置过期时间,或者可以根据需求设置
);
_logger.LogInformation(
$"用户 {order.UserId} 购买尊享包成功,订单号:{outTradeNo},商品:{order.GoodsName}");
}
else if (order.GoodsType.IsVipService())
{
// 处理VIP服务商品充值VIP
await _rechargeService.RechargeVipAsync(new RechargeCreateInput
{
UserId = order.UserId,
RechargeAmount = order.TotalAmount,
Content = order.GoodsName,
Months = order.GoodsType.GetValidMonths(),
Remark = "自助充值",
ContactInfo = null
});
_logger.LogInformation(
$"用户 {order.UserId} 充值VIP成功订单号{outTradeNo},月数:{order.GoodsType.GetValidMonths()}");
}
else
{
_logger.LogWarning($"未知的商品类型:{order.GoodsType},订单号:{outTradeNo}");
}
}
else
{
throw new AlipayException($"回调格式错误");
}
return "success";
}
/// <summary>
/// 查询订单状态
/// </summary>
/// <param name="input">查询订单状态输入</param>
/// <returns>订单状态信息</returns>
[HttpGet("pay/OrderStatus")]
[Authorize]
public async Task<QueryOrderStatusOutput> QueryOrderStatusAsync([FromQuery] QueryOrderStatusInput input)
{
// 通过PayManager查询订单
var order = await _payOrderRepository.GetFirstAsync(x => x.OutTradeNo == input.OutTradeNo);
if (order == null)
{
throw new UserFriendlyException($"订单不存在:{input.OutTradeNo}");
}
return new QueryOrderStatusOutput
{
OrderId = order.Id,
OutTradeNo = order.OutTradeNo,
TradeNo = order.TradeNo,
TradeStatus = order.TradeStatus,
TradeStatusDescription = GetTradeStatusDescription(order.TradeStatus),
TotalAmount = order.TotalAmount,
GoodsName = order.GoodsName,
GoodsType = order.GoodsType,
CreationTime = order.CreationTime,
LastModificationTime = order.LastModificationTime
};
}
/// <summary>
/// 获取商品列表
/// </summary>
/// <returns>商品列表</returns>
[HttpGet("pay/GoodsList")]
public async Task<List<GoodsListOutput>> GetGoodsListAsync()
{
var goodsList = new List<GoodsListOutput>();
// 获取当前用户的累加充值金额(仅已登录用户)
decimal totalRechargeAmount = 0m;
if (CurrentUser.IsAuthenticated)
{
totalRechargeAmount = await _payManager.GetUserTotalRechargeAmountAsync(CurrentUser.GetId());
}
// 遍历所有商品枚举
foreach (GoodsTypeEnum goodsType in Enum.GetValues(typeof(GoodsTypeEnum)))
{
var originalPrice = goodsType.GetTotalAmount();
decimal actualPrice = originalPrice;
decimal? discountAmount = null;
string? discountDescription = null;
// 如果是尊享包商品,计算折扣
if (goodsType.IsPremiumPackage() && CurrentUser.IsAuthenticated)
{
discountAmount = goodsType.CalculateDiscount(totalRechargeAmount);
actualPrice = goodsType.GetDiscountedPrice(totalRechargeAmount);
if (discountAmount > 0)
{
discountDescription = $"已优惠 ¥{discountAmount:F2}累计充值每10元减1元最多减20元";
}
else
{
discountDescription = "累计充值每10元可减1元最多减20元";
}
}
var goodsItem = new GoodsListOutput
{
GoodsName = goodsType.GetDisplayName(),
OriginalPrice = originalPrice,
GoodsPrice = actualPrice,
GoodsType = goodsType,
Remark = GetGoodsRemark(goodsType),
DiscountAmount = discountAmount,
DiscountDescription = discountDescription
};
goodsList.Add(goodsItem);
}
return goodsList;
}
/// <summary>
/// 获取商品备注信息
/// </summary>
/// <param name="goodsType">商品类型</param>
/// <returns>商品备注</returns>
private string GetGoodsRemark(GoodsTypeEnum goodsType)
{
if (goodsType.IsPremiumPackage())
{
var tokenAmount = goodsType.GetTokenAmount();
return $"尊享包服务,提供 {tokenAmount:N0} Tokens需要VIP资格";
}
else if (goodsType.IsVipService())
{
var validMonths = goodsType.GetValidMonths();
var monthlyPrice = goodsType.GetMonthlyPrice();
return $"VIP服务有效期 {validMonths} 个月,月均价 ¥{monthlyPrice:F2}";
}
return "未知商品类型";
}
/// <summary>
/// 获取交易状态描述
/// </summary>
/// <param name="tradeStatus">交易状态</param>
/// <returns>状态描述</returns>
private string GetTradeStatusDescription(TradeStatusEnum tradeStatus)
{
var fieldInfo = tradeStatus.GetType().GetField(tradeStatus.ToString());
var descriptionAttribute = fieldInfo?.GetCustomAttribute<DescriptionAttribute>();
return descriptionAttribute?.Description ?? "未知状态";
}
/// <summary>
/// 解析交易状态
/// </summary>
/// <param name="tradeStatus">状态字符串</param>
/// <returns></returns>
private TradeStatusEnum ParseTradeStatus(string tradeStatus)
{
if (Enum.TryParse<TradeStatusEnum>(tradeStatus, out var result))
{
return result;
}
return TradeStatusEnum.WAIT_TRADE;
}
}

View File

@@ -1,41 +1,112 @@
using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Application.Services;
using Volo.Abp.Users;
using Yi.Framework.AiHub.Application.Contracts.Dtos.Recharge;
using Yi.Framework.AiHub.Application.Contracts.IServices;
using Yi.Framework.AiHub.Domain.Entities;
using Yi.Framework.AiHub.Domain.Managers;
using Yi.Framework.AiHub.Domain.Shared.Consts;
using Yi.Framework.Rbac.Application.Contracts.IServices;
using Yi.Framework.Rbac.Domain.Shared.Dtos;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.AiHub.Application.Services;
/// <summary>
/// ai 充值表
/// </summary>
public class RechargeService : ApplicationService
namespace Yi.Framework.AiHub.Application.Services
{
private readonly ISqlSugarRepository<AiRechargeAggregateRoot> _repository;
public RechargeService(ISqlSugarRepository<AiRechargeAggregateRoot> repository)
public class RechargeService : ApplicationService, IRechargeService
{
_repository = repository;
}
private readonly ISqlSugarRepository<AiRechargeAggregateRoot> _repository;
private readonly ICurrentUser _currentUser;
private readonly IUserService _userService;
private readonly IRoleService _roleService;
private readonly AiRechargeManager _aiMessageManager;
/// <summary>
/// 查询已登录的账户充值记录
/// </summary>
/// <returns></returns>
[Route("recharge/account")]
[Authorize]
public async Task<List<RechargeGetListOutput>> GetListByAccountAsync()
{
var userId = CurrentUser.Id;
var entities = await _repository._DbQueryable.Where(x => x.UserId == userId)
.OrderByDescending(x => x.CreationTime)
.ToListAsync();
var output = entities.Adapt<List<RechargeGetListOutput>>();
return output;
public RechargeService(
ISqlSugarRepository<AiRechargeAggregateRoot> repository,
ICurrentUser currentUser,
IUserService userService, IRoleService roleService, AiRechargeManager aiMessageManager)
{
_repository = repository;
_currentUser = currentUser;
_userService = userService;
_roleService = roleService;
_aiMessageManager = aiMessageManager;
}
/// <summary>
/// 查询已登录的账户充值记录
/// </summary>
/// <returns></returns>
[Route("recharge/account")]
[Authorize]
public async Task<List<RechargeGetListOutput>> GetListByAccountAsync()
{
var userId = CurrentUser.Id;
var entities = await _repository._DbQueryable.Where(x => x.UserId == userId)
.OrderByDescending(x => x.CreationTime)
.ToListAsync();
var output = entities.Adapt<List<RechargeGetListOutput>>();
return output;
}
/// <summary>
/// 给用户充值VIP
/// </summary>
/// <param name="input">充值输入参数</param>
/// <returns></returns>
[HttpPost("recharge/vip")]
public async Task RechargeVipAsync(RechargeCreateInput input)
{
DateTime? expireDateTime = null;
// 如果传入了月数,计算过期时间
if (input.Months.HasValue && input.Months.Value > 0)
{
// 直接查询该用户最大的过期时间
var maxExpireTime = await _repository._DbQueryable
.Where(x => x.UserId == input.UserId && x.ExpireDateTime.HasValue)
.MaxAsync(x => x.ExpireDateTime);
// 如果最大过期时间大于现在时间,从最大过期时间开始计算
// 否则从当天开始计算
DateTime baseDateTime = maxExpireTime.HasValue && maxExpireTime.Value > DateTime.Now
? maxExpireTime.Value
: DateTime.Now;
// 计算新的过期时间
expireDateTime = baseDateTime.AddMonths(input.Months.Value);
}
// 如果月数为空或0表示永久VIPExpireDateTime保持为null
// 创建充值记录
var rechargeRecord = new AiRechargeAggregateRoot
{
UserId = input.UserId,
RechargeAmount = input.RechargeAmount,
Content = input.Content,
ExpireDateTime = expireDateTime,
Remark = input.Remark,
ContactInfo = input.ContactInfo
};
// 保存充值记录到数据库
await _repository.InsertAsync(rechargeRecord);
// 使用UserService给用户添加VIP角色
await _userService.AddUserRoleByRoleCodeAsync(input.UserId,
new List<string>() { AiHubConst.VipRole, "default" });
}
/// <summary>
/// 移除用户vip及角色
/// </summary>
[RemoteService(isEnabled: false)]
public async Task RemoveVipRoleByExpireAsync()
{
var expiredUserIds = await _aiMessageManager.RemoveVipByExpireAsync();
if (expiredUserIds is not null)
{
await _roleService.RemoveUserRoleByRoleCodeAsync(expiredUserIds, AiHubConst.VipRole);
}
}
}
}

View File

@@ -47,7 +47,7 @@ public class TokenService : ApplicationService
{
if (!CurrentUser.IsAiVip())
{
throw new UserFriendlyException("充值成为Vip第三方token服务");
throw new UserFriendlyException("充值成为Vip第三方token服务");
}
await _tokenManager.CreateAsync(CurrentUser.GetId());

View File

@@ -0,0 +1,105 @@
using Microsoft.AspNetCore.Authorization;
using SqlSugar;
using Volo.Abp.Application.Services;
using Volo.Abp.Users;
using Yi.Framework.AiHub.Application.Contracts.Dtos.UsageStatistics;
using Yi.Framework.AiHub.Application.Contracts.IServices;
using Yi.Framework.AiHub.Domain.Entities;
using Yi.Framework.AiHub.Domain.Entities.Chat;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.AiHub.Application.Services;
/// <summary>
/// 使用量统计服务
/// </summary>
[Authorize]
public class UsageStatisticsService : ApplicationService, IUsageStatisticsService
{
private readonly ISqlSugarRepository<MessageAggregateRoot> _messageRepository;
private readonly ISqlSugarRepository<UsageStatisticsAggregateRoot> _usageStatisticsRepository;
public UsageStatisticsService(
ISqlSugarRepository<MessageAggregateRoot> messageRepository,
ISqlSugarRepository<UsageStatisticsAggregateRoot> usageStatisticsRepository)
{
_messageRepository = messageRepository;
_usageStatisticsRepository = usageStatisticsRepository;
}
/// <summary>
/// 获取当前用户近7天的Token消耗统计
/// </summary>
/// <returns>每日Token使用量列表</returns>
public async Task<List<DailyTokenUsageDto>> GetLast7DaysTokenUsageAsync()
{
var userId = CurrentUser.GetId();
var endDate = DateTime.Today;
var startDate = endDate.AddDays(-6); // 近7天
// 从Message表统计近7天的token消耗
var dailyUsage = await _messageRepository._DbQueryable
.Where(x => x.UserId == userId)
.Where(x => x.CreationTime >= startDate && x.CreationTime < endDate.AddDays(1))
.GroupBy(x => x.CreationTime.Date)
.Select(g => new
{
Date = g.CreationTime.Date,
Tokens = SqlFunc.AggregateSum(g.TokenUsage.TotalTokenCount)
})
.ToListAsync();
// 生成完整的7天数据包括没有使用记录的日期
var result = new List<DailyTokenUsageDto>();
for (int i = 0; i < 7; i++)
{
var date = startDate.AddDays(i);
var usage = dailyUsage.FirstOrDefault(x => x.Date == date);
result.Add(new DailyTokenUsageDto
{
Date = date,
Tokens = usage?.Tokens ?? 0
});
}
return result.OrderBy(x => x.Date).ToList();
}
/// <summary>
/// 获取当前用户各个模型的Token消耗量及占比
/// </summary>
/// <returns>模型Token使用量列表</returns>
public async Task<List<ModelTokenUsageDto>> GetModelTokenUsageAsync()
{
var userId = CurrentUser.GetId();
// 从UsageStatistics表获取各模型的token消耗统计
var modelUsages = await _usageStatisticsRepository._DbQueryable
.Where(x => x.UserId == userId)
.Select(x => new
{
x.ModelId,
x.TotalTokenCount
})
.ToListAsync();
if (!modelUsages.Any())
{
return new List<ModelTokenUsageDto>();
}
// 计算总token数
var totalTokens = modelUsages.Sum(x => x.TotalTokenCount);
// 计算各模型占比
var result = modelUsages.Select(x => new ModelTokenUsageDto
{
Model = x.ModelId,
Tokens = x.TotalTokenCount,
Percentage = totalTokens > 0 ? Math.Round((decimal)x.TotalTokenCount / totalTokens * 100, 2) : 0
}).OrderByDescending(x => x.Tokens).ToList();
return result;
}
}

View File

@@ -0,0 +1,6 @@
namespace Yi.Framework.AiHub.Domain.Shared.Consts;
public class AiHubConst
{
public const string VipRole = "YiXinAi-Vip";
}

View File

@@ -0,0 +1,7 @@
namespace Yi.Framework.AiHub.Domain.Shared.Consts;
public class FuwuhaoConst
{
public const string SceneCacheKey = "fuwuhao:scene:";
public const string OpenIdToSceneCacheKey = "fuwuhao:OpenIdToScene:";
}

View File

@@ -0,0 +1,9 @@
using System.Text.Json.Serialization;
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.Anthropic;
public sealed class AnthropicCacheControl
{
[JsonPropertyName("type")]
public string Type { get; set; }
}

View File

@@ -0,0 +1,151 @@
using System.Text.Json.Serialization;
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.Anthropic;
public class AnthropicStreamDto
{
[JsonPropertyName("type")] public string? Type { get; set; }
[JsonPropertyName("index")] public int? Index { get; set; }
[JsonPropertyName("content_block")] public AnthropicChatCompletionDtoContentBlock? ContentBlock { get; set; }
[JsonPropertyName("delta")] public AnthropicChatCompletionDtoDelta? Delta { get; set; }
[JsonPropertyName("message")] public AnthropicChatCompletionDto? Message { get; set; }
[JsonPropertyName("usage")] public AnthropicCompletionDtoUsage? Usage { get; set; }
[JsonPropertyName("error")] public AnthropicStreamErrorDto? Error { get; set; }
[JsonIgnore]
public ThorUsageResponse TokenUsage => new ThorUsageResponse
{
PromptTokens = Usage?.InputTokens + Usage?.CacheCreationInputTokens + Usage?.CacheReadInputTokens,
InputTokens = Usage?.InputTokens + Usage?.CacheCreationInputTokens + Usage?.CacheReadInputTokens,
OutputTokens = Usage?.OutputTokens,
InputTokensDetails = null,
CompletionTokens = Usage?.OutputTokens,
TotalTokens = Usage?.InputTokens + Usage?.CacheCreationInputTokens + Usage?.CacheReadInputTokens +
Usage?.OutputTokens,
PromptTokensDetails = null,
CompletionTokensDetails = null
};
}
public class AnthropicStreamErrorDto
{
[JsonPropertyName("type")] public string? Type { get; set; }
[JsonPropertyName("message")] public string? Message { get; set; }
}
public class AnthropicChatCompletionDtoDelta
{
[JsonPropertyName("type")] public string? Type { get; set; }
[JsonPropertyName("text")] public string? Text { get; set; }
[JsonPropertyName("thinking")] public string? Thinking { get; set; }
[JsonPropertyName("partial_json")] public string? PartialJson { get; set; }
[JsonPropertyName("stop_reason")] public string? StopReason { get; set; }
}
public class AnthropicChatCompletionDtoContentBlock
{
[JsonPropertyName("type")] public string? Type { get; set; }
[JsonPropertyName("thinking")] public string? Thinking { get; set; }
[JsonPropertyName("signature")] public string? Signature { get; set; }
[JsonPropertyName("id")] public string? Id { get; set; }
[JsonPropertyName("name")] public string? Name { get; set; }
[JsonPropertyName("input")] public object? Input { get; set; }
[JsonPropertyName("server_name")] public string? ServerName { get; set; }
[JsonPropertyName("is_error")] public bool? IsError { get; set; }
[JsonPropertyName("tool_use_id")] public string? ToolUseId { get; set; }
[JsonPropertyName("content")] public object? Content { get; set; }
}
public class AnthropicChatCompletionDto
{
public string id { get; set; }
public string type { get; set; }
public string role { get; set; }
public AnthropicChatCompletionDtoContent[] content { get; set; }
public string model { get; set; }
public string stop_reason { get; set; }
public object stop_sequence { get; set; }
public AnthropicCompletionDtoUsage Usage { get; set; }
[JsonIgnore]
public ThorUsageResponse TokenUsage => new ThorUsageResponse
{
PromptTokens = Usage?.InputTokens + Usage?.CacheCreationInputTokens + Usage?.CacheReadInputTokens,
InputTokens = Usage?.InputTokens + Usage?.CacheCreationInputTokens + Usage?.CacheReadInputTokens,
OutputTokens = Usage?.OutputTokens,
InputTokensDetails = null,
CompletionTokens = Usage?.OutputTokens,
TotalTokens = Usage?.InputTokens + Usage?.CacheCreationInputTokens + Usage?.CacheReadInputTokens +
Usage?.OutputTokens,
PromptTokensDetails = null,
CompletionTokensDetails = null
};
}
public class AnthropicChatCompletionDtoContent
{
public string type { get; set; }
public string? text { get; set; }
public string? id { get; set; }
public string? name { get; set; }
public object? input { get; set; }
[JsonPropertyName("thinking")] public string? Thinking { get; set; }
[JsonPropertyName("partial_json")] public string? PartialJson { get; set; }
public string? signature { get; set; }
}
public class AnthropicCompletionDtoUsage
{
[JsonPropertyName("input_tokens")] public int? InputTokens { get; set; }
[JsonPropertyName("cache_creation_input_tokens")]
public int? CacheCreationInputTokens { get; set; }
[JsonPropertyName("cache_read_input_tokens")]
public int? CacheReadInputTokens { get; set; }
[JsonPropertyName("output_tokens")] public int? OutputTokens { get; set; }
[JsonPropertyName("server_tool_use")] public AnthropicServerToolUse? ServerToolUse { get; set; }
}
public class AnthropicServerToolUse
{
[JsonPropertyName("web_search_requests")]
public int? WebSearchRequests { get; set; }
}

View File

@@ -0,0 +1,153 @@
using System.ComponentModel.DataAnnotations;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.Anthropic;
public sealed class AnthropicInput
{
[JsonPropertyName("stream")] public bool Stream { get; set; }
[JsonPropertyName("model")] public string Model { get; set; }
[JsonPropertyName("max_tokens")] public int? MaxTokens { get; set; }
[JsonPropertyName("messages")] public IList<AnthropicMessageInput> Messages { get; set; }
[JsonPropertyName("tools")] public IList<AnthropicMessageTool>? Tools { get; set; }
[JsonPropertyName("tool_choice")]
public object? ToolChoiceCalculated
{
get
{
if (string.IsNullOrEmpty(ToolChoiceString))
{
return ToolChoiceString;
}
if (ToolChoice?.Type == "function")
{
return ToolChoice;
}
return ToolChoice?.Type;
}
set
{
if (value is JsonElement jsonElement)
{
if (jsonElement.ValueKind == JsonValueKind.String)
{
ToolChoiceString = jsonElement.GetString();
}
else if (jsonElement.ValueKind == JsonValueKind.Object)
{
ToolChoice = jsonElement.Deserialize<AnthropicTooChoiceInput>(ThorJsonSerializer.DefaultOptions);
}
}
else
{
ToolChoice = (AnthropicTooChoiceInput)value;
}
}
}
[JsonIgnore] public string? ToolChoiceString { get; set; }
[JsonIgnore] public AnthropicTooChoiceInput? ToolChoice { get; set; }
[JsonIgnore] public IList<AnthropicMessageContent>? Systems { get; set; }
[JsonIgnore] public string? System { get; set; }
[JsonPropertyName("system")]
public object? SystemCalculated
{
get
{
if (System is not null && Systems is not null)
{
throw new ValidationException("System 和 Systems 字段不能同时有值");
}
if (System is not null)
{
return System;
}
return Systems!;
}
set
{
if (value is JsonElement str)
{
if (str.ValueKind == JsonValueKind.String)
{
System = value?.ToString();
}
else if (str.ValueKind == JsonValueKind.Array)
{
Systems = JsonSerializer.Deserialize<IList<AnthropicMessageContent>>(value?.ToString(),
ThorJsonSerializer.DefaultOptions);
}
}
else
{
System = value?.ToString();
}
}
}
[JsonPropertyName("thinking")] public AnthropicThinkingInput? Thinking { get; set; }
[JsonPropertyName("temperature")] public double? Temperature { get; set; }
[JsonPropertyName("metadata")] public Dictionary<string, object>? Metadata { get; set; }
}
public class AnthropicThinkingInput
{
[JsonPropertyName("type")] public string Type { get; set; }
[JsonPropertyName("budget_tokens")] public int BudgetTokens { get; set; }
}
public class AnthropicTooChoiceInput
{
[JsonPropertyName("type")] public string? Type { get; set; }
[JsonPropertyName("name")] public string? Name { get; set; }
}
public class AnthropicMessageTool
{
[JsonPropertyName("name")] public string name { get; set; }
[JsonPropertyName("description")] public string? Description { get; set; }
[JsonPropertyName("input_schema")] public Input_schema InputSchema { get; set; }
}
public class Input_schema
{
[JsonPropertyName("type")] public string Type { get; set; }
[JsonPropertyName("properties")] public Dictionary<string, InputSchemaValue>? Properties { get; set; }
[JsonPropertyName("required")] public string[]? Required { get; set; }
}
public class InputSchemaValue
{
public string type { get; set; }
public string description { get; set; }
public InputSchemaValueItems? items { get; set; }
}
public class InputSchemaValueItems
{
public string? type { get; set; }
}

View File

@@ -0,0 +1,76 @@
using System.ComponentModel.DataAnnotations;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.Anthropic;
public class AnthropicMessageContent
{
[JsonPropertyName("cache_control")] public AnthropicCacheControl? CacheControl { get; set; }
[JsonPropertyName("type")] public string Type { get; set; }
[JsonPropertyName("text")] public string? Text { get; set; }
[JsonPropertyName("tool_use_id")] public string? ToolUseId { get; set; }
[JsonPropertyName("name")] public string? Name { get; set; }
[JsonPropertyName("id")] public string? Id { get; set; }
[JsonPropertyName("thinking")] public string? Thinking { get; set; }
[JsonPropertyName("input")] public object? Input { get; set; }
[JsonPropertyName("content")]
public object? Content
{
get
{
if (_content is not null && _contents is not null)
{
throw new ValidationException("Messages 中 Content 和 Contents 字段不能同时有值");
}
if (_content is not null)
{
return _content;
}
return _contents;
}
set
{
if (value is JsonElement str)
{
if (str.ValueKind == JsonValueKind.String)
{
_content = value?.ToString();
}
else if (str.ValueKind == JsonValueKind.Array)
{
_contents = JsonSerializer.Deserialize<List<AnthropicMessageContent>>(value?.ToString());
}
}
else
{
_content = value?.ToString();
}
}
}
private string? _content;
private List<AnthropicMessageContent>? _contents;
public class AnthropicMessageContentSource
{
[JsonPropertyName("type")] public string Type { get; set; }
[JsonPropertyName("media_type")] public string? MediaType { get; set; }
[JsonPropertyName("data")] public string? Data { get; set; }
}
[JsonPropertyName("source")] public AnthropicMessageContentSource? Source { get; set; }
}

View File

@@ -0,0 +1,54 @@
using System.ComponentModel.DataAnnotations;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.Anthropic;
public class AnthropicMessageInput
{
[JsonPropertyName("role")]
public string Role { get; set; }
[JsonIgnore]
public string? Content;
[JsonPropertyName("content")]
public object? ContentCalculated
{
get
{
if (Content is not null && Contents is not null)
{
throw new ValidationException("Messages 中 Content 和 Contents 字段不能同时有值");
}
if (Content is not null)
{
return Content;
}
return Contents!;
}
set
{
if (value is JsonElement str)
{
if (str.ValueKind == JsonValueKind.String)
{
Content = value?.ToString();
}
else if (str.ValueKind == JsonValueKind.Array)
{
Contents = JsonSerializer.Deserialize<IList<AnthropicMessageContent>>(value?.ToString(),ThorJsonSerializer.DefaultOptions);
}
}
else
{
Content = value?.ToString();
}
}
}
[JsonIgnore]
public IList<AnthropicMessageContent>? Contents;
}

View File

@@ -0,0 +1,648 @@
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
};
}
}

View File

@@ -0,0 +1,14 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.Anthropic;
public static class ThorJsonSerializer
{
public static JsonSerializerOptions DefaultOptions => new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
};
}

View File

@@ -1,8 +1,6 @@
using SqlSugar;
using Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi;
using Yi.Framework.AiHub.Domain.Shared.Dtos;
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
namespace Yi.Framework.AiHub.Application.Contracts.Dtos;
namespace Yi.Framework.AiHub.Domain.Shared.Dtos;
public class MessageInputDto
{

View File

@@ -0,0 +1,79 @@
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi.Embeddings;
//TODO add model validation
//TODO check what is string or array for prompt,..
public record EmbeddingCreateRequest
{
/// <summary>
/// Input text to get embeddings for, encoded as a string or array of tokens. To get embeddings for multiple inputs
/// in a single request, pass an array of strings or array of token arrays. Each input must not exceed 2048 tokens in
/// length.
/// Unless your are embedding code, we suggest replacing newlines (`\n`) in your input with a single space, as we have
/// observed inferior results when newlines are present.
/// </summary>
/// <see href="https://platform.openai.com/docs/api-reference/embeddings/create#embeddings/create-input" />
[JsonIgnore]
public List<string>? InputAsList { get; set; }
/// <summary>
/// Input text to get embeddings for, encoded as a string or array of tokens. To get embeddings for multiple inputs
/// in a single request, pass an array of strings or array of token arrays. Each input must not exceed 2048 tokens in
/// length.
/// Unless your are embedding code, we suggest replacing newlines (`\n`) in your input with a single space, as we have
/// observed inferior results when newlines are present.
/// </summary>
/// <see href="https://platform.openai.com/docs/api-reference/embeddings/create#embeddings/create-input" />
[JsonIgnore]
public string? Input { get; set; }
[JsonPropertyName("input")]
public IList<string>? InputCalculated
{
get
{
if (Input != null && InputAsList != null)
{
throw new ValidationException(
"Input and InputAsList can not be assigned at the same time. One of them is should be null.");
}
if (Input != null)
{
return new List<string> { Input };
}
return InputAsList;
}
}
/// <summary>
/// ID of the model to use. You can use the [List models](/docs/api-reference/models/list) API to see all of your
/// available models, or see our [Model overview](/docs/models/overview) for descriptions of them.
/// </summary>
/// <see href="https://platform.openai.com/docs/api-reference/embeddings/create#embeddings/create-model" />
[JsonPropertyName("model")]
public string? Model { get; set; }
/// <summary>
/// The number of dimensions the resulting output embeddings should have. Only supported in text-embedding-3 and later models.
/// </summary>
/// <see href="https://platform.openai.com/docs/api-reference/embeddings/create#embeddings-create-dimensions" />
[JsonPropertyName("dimensions")]
public int? Dimensions { get; set; }
/// <summary>
/// The format to return the embeddings in. Can be either float or base64.
/// </summary>
/// <returns></returns>
[JsonPropertyName("encoding_format")]
public string? EncodingFormat { get; set; }
public IEnumerable<ValidationResult> Validate()
{
throw new NotImplementedException();
}
}

View File

@@ -0,0 +1,113 @@
using System.Buffers;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi.Embeddings;
public record EmbeddingCreateResponse : ThorBaseResponse
{
[JsonPropertyName("model")] public string Model { get; set; }
[JsonPropertyName("data")] public List<EmbeddingResponse> Data { get; set; } = [];
/// <summary>
/// 类型转换如果类型是base64,则将float[]转换为base64,如果是空或是float和原始类型一样则不转换
/// </summary>
public void ConvertEmbeddingData(string? encodingFormat)
{
if (Data.Count == 0)
{
return;
}
switch (encodingFormat)
{
// 判断第一个是否是float[],如果是则不转换
case null or "float" when Data[0].Embedding is float[]:
return;
// 否则转换成float[]
case null or "float":
{
foreach (var embeddingResponse in Data)
{
if (embeddingResponse.Embedding is string base64)
{
embeddingResponse.Embedding = Convert.FromBase64String(base64);
}
}
return;
}
// 判断第一个是否是string如果是则不转换
case "base64" when Data[0].Embedding is string:
return;
// 否则转换成base64
case "base64":
{
foreach (var embeddingResponse in Data)
{
if (embeddingResponse.Embedding is JsonElement str)
{
if (str.ValueKind == JsonValueKind.Array)
{
var floats = str.EnumerateArray().Select(element => element.GetSingle()).ToArray();
embeddingResponse.Embedding = ConvertFloatArrayToBase64(floats);
}
}
else if (embeddingResponse.Embedding is IList<double> doubles)
{
embeddingResponse.Embedding = ConvertFloatArrayToBase64(doubles.ToArray());
}
}
break;
}
}
}
public static string ConvertFloatArrayToBase64(double[] floatArray)
{
// 将 float[] 转换成 byte[]
byte[] byteArray = ArrayPool<byte>.Shared.Rent(floatArray.Length * sizeof(float));
try
{
Buffer.BlockCopy(floatArray, 0, byteArray, 0, byteArray.Length);
// 将 byte[] 转换成 base64 字符串
return Convert.ToBase64String(byteArray);
}
finally
{
ArrayPool<byte>.Shared.Return(byteArray);
}
}
public static string ConvertFloatArrayToBase64(float[] floatArray)
{
// 将 float[] 转换成 byte[]
byte[] byteArray = ArrayPool<byte>.Shared.Rent(floatArray.Length * sizeof(float));
try
{
Buffer.BlockCopy(floatArray, 0, byteArray, 0, floatArray.Length);
// 将 byte[] 转换成 base64 字符串
return Convert.ToBase64String(byteArray);
}
finally
{
ArrayPool<byte>.Shared.Return(byteArray);
}
}
[JsonPropertyName("usage")] public ThorUsageResponse? Usage { get; set; }
}
public record EmbeddingResponse
{
[JsonPropertyName("object")] public string Object { get; set; } = "embedding";
[JsonPropertyName("index")] public int? Index { get; set; }
[JsonPropertyName("embedding")] public object Embedding { get; set; }
}

View File

@@ -0,0 +1,22 @@
using System.Text.Json.Serialization;
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi.Embeddings;
public sealed class ThorEmbeddingInput
{
[JsonPropertyName("model")]
public string Model { get; set; }
[JsonPropertyName("input")]
public object Input { get; set; }
[JsonPropertyName("encoding_format")]
public string? EncodingFormat { get; set; }
[JsonPropertyName("dimensions")]
public int? Dimensions { get; set; }
[JsonPropertyName("user")]
public string? User { get; set; }
}

View File

@@ -0,0 +1,54 @@
using System.Text.Json.Serialization;
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi.Images;
/// <summary>
/// Image Create Request Model
/// </summary>
public record ImageCreateRequest : SharedImageRequestBaseModel
{
public ImageCreateRequest()
{
}
public ImageCreateRequest(string prompt)
{
Prompt = prompt;
}
/// <summary>
/// A text description of the desired image(s). The maximum length is 1000 characters for dall-e-2 and 4000 characters for dall-e-3
/// </summary>
[JsonPropertyName("prompt")]
public string Prompt { get; set; }
/// <summary>
/// The quality of the image that will be generated. Possible values are 'standard' or 'hd' (default is 'standard').
/// Hd creates images with finer details and greater consistency across the image.
/// This param is only supported for dall-e-3 model.
/// <br /><br />Check <see cref="StaticValues.ImageStatics.Quality"/> for possible values
/// </summary>
[JsonPropertyName("quality")]
public string? Quality { get; set; }
/// <summary>
/// The style of the generated images. Must be one of vivid or natural.
/// Vivid causes the model to lean towards generating hyper-real and dramatic images.
/// Natural causes the model to produce more natural, less hyper-real looking images. This param is only supported for dall-e-3.
/// <br /><br />Check <see cref="StaticValues.ImageStatics.Style"/> for possible values
/// </summary>
[JsonPropertyName("style")]
public string? Style { get; set; }
[JsonPropertyName("background")]
public string? Background { get; set; }
[JsonPropertyName("moderation")]
public string? Moderation { get; set; }
[JsonPropertyName("output_compression")]
public string? OutputCompression { get; set; }
[JsonPropertyName("output_format")]
public string? OutputFormat { get; set; }
}

View File

@@ -0,0 +1,19 @@
using System.Text.Json.Serialization;
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi.Images;
public record ImageCreateResponse : ThorBaseResponse
{
[JsonPropertyName("data")] public List<ImageDataResult> Results { get; set; }
[JsonPropertyName("usage")] public ThorUsageResponse? Usage { get; set; } = new();
public record ImageDataResult
{
[JsonPropertyName("url")] public string Url { get; set; }
[JsonPropertyName("b64_json")] public string B64 { get; set; }
[JsonPropertyName("revised_prompt")] public string RevisedPrompt { get; set; }
}
}

View File

@@ -0,0 +1,51 @@
using System.Text.Json.Serialization;
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi.Images;
public record ImageEditCreateRequest : SharedImageRequestBaseModel
{
/// <summary>
/// The image to edit. Must be a valid PNG file, less than 4MB, and square.
/// </summary>
public byte[]? Image { get; set; }
/// <summary>
/// Image file name
/// </summary>
public string ImageName { get; set; }
/// <summary>
/// An additional image whose fully transparent areas (e.g. where alpha is zero) indicate where image should be edited.
/// Must be a valid PNG file, less than 4MB, and have the same dimensions as image.
/// </summary>
public byte[]? Mask { get; set; }
/// <summary>
/// Mask file name
/// </summary>
public string? MaskName { get; set; }
[JsonPropertyName("quality")]
public string Quality { get; set; }
/// <summary>
/// A text description of the desired image(s). The maximum length is 1000 characters.
/// </summary>
[JsonPropertyName("prompt")]
public string Prompt { get; set; }
[JsonPropertyName("background")]
public string? Background { get; set; }
[JsonPropertyName("moderation")]
public string? Moderation { get; set; }
[JsonPropertyName("output_compression")]
public string? OutputCompression { get; set; }
[JsonPropertyName("output_format")]
public string? OutputFormat { get; set; }
[JsonPropertyName("style")]
public string? Style { get; set; }
}

View File

@@ -0,0 +1,14 @@
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi.Images;
public record ImageVariationCreateRequest : SharedImageRequestBaseModel
{
/// <summary>
/// The image to edit. Must be a valid PNG file, less than 4MB, and square.
/// </summary>
public byte[] Image { get; set; }
/// <summary>
/// Image file name
/// </summary>
public string ImageName { get; set; }
}

View File

@@ -0,0 +1,42 @@
using System.Text.Json.Serialization;
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi.Images;
public record SharedImageRequestBaseModel
{
/// <summary>
/// The number of images to generate. Must be between 1 and 10.
/// For dall-e-3 model, only n=1 is supported.
/// </summary>
[JsonPropertyName("n")]
public int? N { get; set; }
/// <summary>
/// The size of the generated images.
/// Must be one of 256x256, 512x512, or 1024x1024 for dall-e-2.
/// Must be one of 1024x1024, 1792x1024, or 1024x1792 for dall-e-3 models.
/// <br /><br />Check <see cref="StaticValues.ImageStatics.Size"/> for possible values
/// </summary>
[JsonPropertyName("size")]
public string? Size { get; set; }
/// <summary>
/// The format in which the generated images are returned. Must be one of url or b64_json
/// </summary>
[JsonPropertyName("response_format")]
public string? ResponseFormat { get; set; }
/// <summary>
/// A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse.
/// <a href="https://platform.openai.com/docs/usage-policies/end-user-ids">Learn more</a>.
/// </summary>
[JsonPropertyName("user")]
public string? User { get; set; }
/// <summary>
/// The model to use for image generation. Must be one of dall-e-2 or dall-e-3
/// For ImageEditCreateRequest and for ImageVariationCreateRequest only dall-e-2 modell is supported at this time.
/// </summary>
[JsonPropertyName("model")]
public string? Model { get; set; }
}

View File

@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi;
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
public class ModelsListDto
{

View File

@@ -1,4 +1,4 @@
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi;
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
/// <summary>
/// OpenAI常量

View File

@@ -1,7 +1,6 @@
using System.Text.Json.Serialization;
using Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi;
namespace Yi.Framework.AiHub.Domain.AiGateWay;
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
public record ThorBaseResponse
{

View File

@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi;
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
public sealed class ThorChatAudioRequest
{

View File

@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi;
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
/// <summary>
/// 聊天完成选项列

View File

@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi;
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
public class ThorChatClaudeThinking
{

View File

@@ -2,20 +2,14 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi;
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
/// <summary>
/// 对话补全请求参数对象
/// </summary>
public class ThorChatCompletionsRequest
{
public ThorChatCompletionsRequest()
{
Messages = new List<ThorChatMessage>();
}
[JsonPropertyName("store")]
public bool? Store { get; set; }
[JsonPropertyName("store")] public bool? Store { get; set; }
/// <summary>
/// 表示对话中支持的模态类型数组。可以为 null。
@@ -26,13 +20,14 @@ public class ThorChatCompletionsRequest
/// <summary>
/// 表示对话中的音频请求参数。可以为 null。
/// </summary>
[JsonPropertyName("audio")] public ThorChatAudioRequest? Audio { get; set; }
[JsonPropertyName("audio")]
public ThorChatAudioRequest? Audio { get; set; }
/// <summary>
/// 包含迄今为止对话的消息列表
/// </summary>
[JsonPropertyName("messages")]
public List<ThorChatMessage> Messages { get; set; }
public List<ThorChatMessage>? Messages { get; set; }
/// <summary>
/// 模型唯一编码值,如 gpt-4gpt-3.5-turbo,moonshot-v1-8k看底层具体平台定义
@@ -229,18 +224,25 @@ public class ThorChatCompletionsRequest
{
if (value is JsonElement jsonElement)
{
if (jsonElement.ValueKind == JsonValueKind.String)
{
ToolChoice = new ThorToolChoice
{
Type = jsonElement.GetString()
};
}
else if (jsonElement.ValueKind == JsonValueKind.Object)
// if (jsonElement.ValueKind == JsonValueKind.String)
// {
// ToolChoice = new ThorToolChoice
// {
// Type = jsonElement.GetString()
// };
// }
if (jsonElement.ValueKind == JsonValueKind.Object)
{
ToolChoice = jsonElement.Deserialize<ThorToolChoice>();
}
}
else if (value is string text)
{
ToolChoice = new ThorToolChoice
{
Type = text
};
}
else
{
ToolChoice = (ThorToolChoice)value;
@@ -277,13 +279,10 @@ public class ThorChatCompletionsRequest
[JsonPropertyName("thinking")] public ThorChatClaudeThinking? Thinking { get; set; }
/// <summary>
/// 参数验证
/// </summary>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public IEnumerable<ValidationResult> Validate()
{
throw new NotImplementedException();
}
[JsonPropertyName("enable_thinking")] public bool? EnableThinking { get; set; }
[JsonPropertyName("web_search_options")]
public ThorChatWebSearchOptions? WebSearchOptions { get; set; } = null;
[JsonPropertyName("reasoning_effort")] public string? ReasoningEffort { get; set; }
}

View File

@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi;
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
/// <summary>
/// 对话补全服务返回结果

View File

@@ -2,7 +2,7 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi;
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
/// <summary>
/// 聊天消息体建议使用CreeateXXX系列方法构建内容
@@ -14,7 +14,6 @@ public class ThorChatMessage
/// </summary>
public ThorChatMessage()
{
}
/// <summary>
@@ -52,16 +51,17 @@ public class ThorChatMessage
/// <summary>
/// 发出的消息内容计算用于json序列号和反序列化Content 和 Contents 不能同时赋值,只能二选一
/// 如果是工具调用,还真可能为空
/// </summary>
[JsonPropertyName("content")]
public object ContentCalculated
public object? ContentCalculated
{
get
{
if (Content is not null && Contents is not null)
{
throw new ValidationException("Messages 中 Content 和 Contents 字段不能同时有值");
}
// if (Content is not null && Contents is not null)
// {
// throw new ValidationException("Messages 中 Content 和 Contents 字段不能同时有值");
// }
if (Content is not null)
{
@@ -87,7 +87,6 @@ public class ThorChatMessage
{
Content = value?.ToString();
}
}
}
@@ -108,15 +107,14 @@ public class ThorChatMessage
/// </summary>
[JsonPropertyName("function_call")]
public ThorChatMessageFunction? FunctionCall { get; set; }
/// <summary>
/// 【可选】推理内容
/// </summary>
[JsonPropertyName("reasoning_content")]
public string? ReasoningContent { get; set; }
[JsonPropertyName("id")]
public string? Id { get; set; }
[JsonPropertyName("id")] public string? Id { get; set; }
/// <summary>
/// 工具调用列表,模型生成的工具调用,例如函数调用。<br/>
@@ -164,14 +162,15 @@ public class ThorChatMessage
/// <param name="name">参与者的可选名称。提供模型信息以区分同一角色的参与者。</param>
/// <param name="toolCalls">工具调用参数列表</param>
/// <returns></returns>
public static ThorChatMessage CreateAssistantMessage(string content, string? name = null, List<ThorToolCall> toolCalls = null)
public static ThorChatMessage CreateAssistantMessage(string content, string? name = null,
List<ThorToolCall> toolCalls = null)
{
return new()
{
Role = ThorChatMessageRoleConst.Assistant,
Content = content,
Name = name,
ToolCalls=toolCalls,
ToolCalls = toolCalls,
};
}
@@ -187,7 +186,7 @@ public class ThorChatMessage
{
Role = ThorChatMessageRoleConst.Tool,
Content = content,
ToolCallId= toolCallId
ToolCallId = toolCallId
};
}
}

View File

@@ -1,6 +1,8 @@

using System.Text.Json.Serialization;
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
public sealed class ThorChatMessageAudioContent
{
[JsonPropertyName("data")]

View File

@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi;
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
/// <summary>
/// 发出的消息内容包含图文一般是一文一图一文多图两种情况请使用CreeateXXX系列方法构建内容

View File

@@ -1,7 +1,7 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi
{
/// <summary>
/// 模型调用的函数。

View File

@@ -1,4 +1,4 @@
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi
{
/// <summary>
/// 对话消息角色定义

View File

@@ -0,0 +1,35 @@
using System.Text.Json.Serialization;
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
public class ThorChatWebSearchOptions
{
[JsonPropertyName("search_context_size")]
public string? SearchContextSize { get; set; }
[JsonPropertyName("user_location")]
public ThorUserLocation? UserLocation { get; set; }
}
public sealed class ThorUserLocation
{
[JsonPropertyName("type")] public required string Type { get; set; }
[JsonPropertyName("approximate")]
public ThorUserLocationApproximate? Approximate { get; set; }
}
public sealed class ThorUserLocationApproximate
{
[JsonPropertyName("city")]
public string? City { get; set; }
[JsonPropertyName("country")]
public string? Country { get; set; }
[JsonPropertyName("region")]
public string? Region { get; set; }
[JsonPropertyName("timezone")]
public string? Timezone { get; set; }
}

View File

@@ -1,7 +1,7 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi
{
public class ThorError
{

View File

@@ -1,4 +1,4 @@
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi
{
/// <summary>
/// 支持图片识别的消息体内容类型

View File

@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi;
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
/// <summary>
/// 指定模型必须输出的格式的对象。用于启用JSON模式。

View File

@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi;
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
public class ThorResponseJsonSchema
{

View File

@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi
{
/// <summary>
/// 流响应选项。仅当您设置 stream: true 时才设置此项。

View File

@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi;
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
/// <summary>
/// 工具调用对象定义

View File

@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi;
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
/// <summary>
/// 工具

View File

@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi
{
public class ThorToolChoiceFunctionTool
{

View File

@@ -1,4 +1,4 @@
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi
{
public class ThorToolChoiceTypeConst
{

View File

@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi;
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
/// <summary>
/// 有效工具的定义。

View File

@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi;
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
/// <summary>
/// 有效函数调用的定义。
@@ -30,5 +30,5 @@ public class ThorToolFunctionDefinition
/// documentation about the format.
/// </summary>
[JsonPropertyName("parameters")]
public ThorToolFunctionPropertyDefinition Parameters { get; set; }
public ThorToolFunctionPropertyDefinition? Parameters { get; set; }
}

View File

@@ -1,6 +1,7 @@
using System.Text.Json.Serialization;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi;
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
/// <summary>
/// 函数参数是JSON格式对象
@@ -25,37 +26,77 @@ public class ThorToolFunctionPropertyDefinition
/// 表示字符串类型的函数对象
/// </summary>
String,
/// <summary>
/// 表示整数类型的函数对象
/// </summary>
Integer,
/// <summary>
/// 表示数字(包括浮点数等)类型的函数对象
/// </summary>
Number,
/// <summary>
/// 表示对象类型的函数对象
/// </summary>
Object,
/// <summary>
/// 表示数组类型的函数对象
/// </summary>
Array,
/// <summary>
/// 表示布尔类型的函数对象
/// </summary>
Boolean,
/// <summary>
/// 表示空值类型的函数对象
/// </summary>
Null
}
public string typeStr = "object";
public string[] Types;
/// <summary>
/// 必填的。函数参数对象类型。默认值为“object”。
/// </summary>
[JsonPropertyName("type")]
public string Type { get; set; } = "object";
public object Type
{
get
{
if (Types is { Length: > 0 })
{
return Types;
}
return typeStr;
}
set
{
if (value is JsonElement str)
{
switch (str.ValueKind)
{
case JsonValueKind.String:
typeStr = value?.ToString();
break;
case JsonValueKind.Array:
Types = JsonSerializer.Deserialize<string[]>(value?.ToString());
break;
}
}
else
{
typeStr = value?.ToString();
}
}
}
/// <summary>
/// 可选。“函数参数”列表,作为从参数名称映射的字典
@@ -68,13 +109,13 @@ public class ThorToolFunctionPropertyDefinition
/// 可选。列出必需的“function arguments”列表。
/// </summary>
[JsonPropertyName("required")]
public List<string>? Required { get; set; }
public string[]? Required { get; set; }
/// <summary>
/// 可选。是否允许附加属性。默认值为true。
/// </summary>
[JsonPropertyName("additionalProperties")]
public bool? AdditionalProperties { get; set; }
public object? AdditionalProperties { get; set; }
/// <summary>
/// 可选。参数描述。
@@ -219,11 +260,12 @@ public class ThorToolFunctionPropertyDefinition
/// <param name="description"></param>
/// <param name="enum"></param>
/// <returns></returns>
public static ThorToolFunctionPropertyDefinition DefineObject(IDictionary<string, ThorToolFunctionPropertyDefinition>? properties,
List<string>? required,
bool? additionalProperties,
string? description,
List<string>? @enum)
public static ThorToolFunctionPropertyDefinition DefineObject(
IDictionary<string, ThorToolFunctionPropertyDefinition>? properties,
string[]? required,
object? additionalProperties,
string? description,
List<string>? @enum)
{
return new ThorToolFunctionPropertyDefinition
{
@@ -242,7 +284,6 @@ public class ThorToolFunctionPropertyDefinition
/// </summary>
/// <param name="type">要转换的类型</param>
/// <returns>给定类型的字符串表示形式</returns>
public static string ConvertTypeToString(FunctionObjectTypes type)
{
return type switch

View File

@@ -1,4 +1,4 @@
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi
{
/// <summary>
/// 工具类型定义

View File

@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi;
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
/// <summary>
/// 统计信息模型

View File

@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi;
namespace Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
/// <summary>
/// 图片消息内容对象

View File

@@ -0,0 +1,29 @@
namespace Yi.Framework.AiHub.Domain.Shared.Enums.Fuwuhao;
public enum SceneResultEnum
{
/// <summary>
/// 等待,用户未扫码
/// </summary>
Wait = 0,
/// <summary>
/// 已扫码完成登录
/// </summary>
Login = 1,
/// <summary>
/// 已扫码完成注册
/// </summary>
Register = 2,
/// <summary>
/// 已扫码完成绑定
/// </summary>
Bind = 3,
/// <summary>
/// 已过期
/// </summary>
Expired = 10
}

View File

@@ -0,0 +1,7 @@
namespace Yi.Framework.AiHub.Domain.Shared.Enums.Fuwuhao;
public enum SceneTypeEnum
{
LoginOrRegister,
Bind
}

View File

@@ -0,0 +1,265 @@
using System;
using System.Reflection;
namespace Yi.Framework.AiHub.Domain.Shared.Enums;
/// <summary>
/// 价格特性
/// </summary>
[AttributeUsage(AttributeTargets.Field)]
public class PriceAttribute : Attribute
{
public decimal Price { get; }
public int ValidMonths { get; }
public PriceAttribute(double price, int validMonths)
{
Price = (decimal)price;
ValidMonths = validMonths;
}
}
/// <summary>
/// 显示名称特性
/// </summary>
[AttributeUsage(AttributeTargets.Field)]
public class DisplayNameAttribute : Attribute
{
public string DisplayName { get; }
public DisplayNameAttribute(string displayName)
{
DisplayName = displayName;
}
}
/// <summary>
/// 商品类型特性
/// 用于标识商品是VIP服务还是尊享包服务
/// </summary>
[AttributeUsage(AttributeTargets.Field)]
public class GoodsCategoryAttribute : Attribute
{
public GoodsCategoryType Category { get; }
public GoodsCategoryAttribute(GoodsCategoryType category)
{
Category = category;
}
}
/// <summary>
/// 商品类别类型
/// </summary>
public enum GoodsCategoryType
{
/// <summary>
/// VIP服务
/// </summary>
VipService = 1,
/// <summary>
/// 尊享包服务
/// </summary>
PremiumPackage = 2
}
/// <summary>
/// Token数量特性
/// 用于标识尊享包的token数量
/// </summary>
[AttributeUsage(AttributeTargets.Field)]
public class TokenAmountAttribute : Attribute
{
public long TokenAmount { get; }
public TokenAmountAttribute(long tokenAmount)
{
TokenAmount = tokenAmount;
}
}
/// <summary>
/// 商品枚举
/// </summary>
public enum GoodsTypeEnum
{
// VIP服务
[Price(29.9, 1)]
[DisplayName("YiXinVip 1 month")]
[GoodsCategory(GoodsCategoryType.VipService)]
YiXinVip1 = 1,
[Price(83.7, 3)]
[DisplayName("YiXinVip 3 month")]
[GoodsCategory(GoodsCategoryType.VipService)]
YiXinVip3 = 3,
[Price(155.4, 6)]
[DisplayName("YiXinVip 6 month")]
[GoodsCategory(GoodsCategoryType.VipService)]
YiXinVip6 = 6,
[Price(183.2, 8)]
[DisplayName("YiXinVip 8 month")]
[GoodsCategory(GoodsCategoryType.VipService)]
YiXinVip8 = 8,
// 尊享包服务 - 需要VIP资格才能购买
[Price(188.9, 0)]
[DisplayName("Premium Package 5000W Tokens")]
[GoodsCategory(GoodsCategoryType.PremiumPackage)]
[TokenAmount(5000)]
PremiumPackage5000W = 101,
[Price(248.9, 0)]
[DisplayName("Premium Package 10000W Tokens")]
[GoodsCategory(GoodsCategoryType.PremiumPackage)]
[TokenAmount(10000)]
PremiumPackage10000W = 102,
}
public static class GoodsTypeEnumExtensions
{
/// <summary>
/// 获取商品总金额
/// </summary>
/// <param name="goodsType">商品类型</param>
/// <returns>总金额</returns>
public static decimal GetTotalAmount(this GoodsTypeEnum goodsType)
{
var fieldInfo = goodsType.GetType().GetField(goodsType.ToString());
var priceAttribute = fieldInfo?.GetCustomAttribute<PriceAttribute>();
return priceAttribute?.Price ?? 0m;
}
/// <summary>
/// 获取商品价格描述
/// </summary>
/// <param name="goodsType">商品类型</param>
/// <returns>价格描述</returns>
public static string GetPriceDescription(this GoodsTypeEnum goodsType)
{
var price = goodsType.GetTotalAmount();
return $"¥{price:F1}";
}
/// <summary>
/// 获取商品名称
/// </summary>
/// <param name="goodsType">商品类型</param>
/// <returns>商品名称</returns>
public static string GetDisplayName(this GoodsTypeEnum goodsType)
{
var fieldInfo = goodsType.GetType().GetField(goodsType.ToString());
var displayNameAttribute = fieldInfo?.GetCustomAttribute<DisplayNameAttribute>();
return displayNameAttribute?.DisplayName ?? goodsType.ToString();
}
/// <summary>
/// 获取商品有效月份
/// </summary>
/// <param name="goodsType">商品类型</param>
/// <returns>有效月份</returns>
public static int GetValidMonths(this GoodsTypeEnum goodsType)
{
var fieldInfo = goodsType.GetType().GetField(goodsType.ToString());
var priceAttribute = fieldInfo?.GetCustomAttribute<PriceAttribute>();
return priceAttribute?.ValidMonths ?? 1;
}
/// <summary>
/// 获取商品月均价格
/// </summary>
/// <param name="goodsType">商品类型</param>
/// <returns>月均价格</returns>
public static decimal GetMonthlyPrice(this GoodsTypeEnum goodsType)
{
var totalPrice = goodsType.GetTotalAmount();
var validMonths = goodsType.GetValidMonths();
return validMonths > 0 ? totalPrice / validMonths : 0m;
}
/// <summary>
/// 获取商品类别
/// </summary>
/// <param name="goodsType">商品类型</param>
/// <returns>商品类别</returns>
public static GoodsCategoryType GetGoodsCategory(this GoodsTypeEnum goodsType)
{
var fieldInfo = goodsType.GetType().GetField(goodsType.ToString());
var categoryAttribute = fieldInfo?.GetCustomAttribute<GoodsCategoryAttribute>();
return categoryAttribute?.Category ?? GoodsCategoryType.VipService;
}
/// <summary>
/// 是否为尊享包商品
/// </summary>
/// <param name="goodsType">商品类型</param>
/// <returns>是否为尊享包</returns>
public static bool IsPremiumPackage(this GoodsTypeEnum goodsType)
{
return goodsType.GetGoodsCategory() == GoodsCategoryType.PremiumPackage;
}
/// <summary>
/// 是否为VIP服务商品
/// </summary>
/// <param name="goodsType">商品类型</param>
/// <returns>是否为VIP服务</returns>
public static bool IsVipService(this GoodsTypeEnum goodsType)
{
return goodsType.GetGoodsCategory() == GoodsCategoryType.VipService;
}
/// <summary>
/// 获取尊享包Token数量
/// </summary>
/// <param name="goodsType">商品类型</param>
/// <returns>Token数量</returns>
public static long GetTokenAmount(this GoodsTypeEnum goodsType)
{
var fieldInfo = goodsType.GetType().GetField(goodsType.ToString());
var tokenAttribute = fieldInfo?.GetCustomAttribute<TokenAmountAttribute>();
return tokenAttribute?.TokenAmount ?? 0;
}
/// <summary>
/// 计算折扣金额(仅用于尊享包)
/// 规则每累加充值10元减少1元最多减少20元
/// </summary>
/// <param name="goodsType">商品类型</param>
/// <param name="totalRechargeAmount">用户累加充值金额</param>
/// <returns>折扣金额</returns>
public static decimal CalculateDiscount(this GoodsTypeEnum goodsType, decimal totalRechargeAmount)
{
// 只有尊享包才有折扣
if (!goodsType.IsPremiumPackage())
{
return 0m;
}
// 每10元减1元
var discountAmount = Math.Floor(totalRechargeAmount / 10m);
// 最多减少20元
return Math.Min(discountAmount, 20m);
}
/// <summary>
/// 获取折扣后的价格(仅用于尊享包)
/// </summary>
/// <param name="goodsType">商品类型</param>
/// <param name="totalRechargeAmount">用户累加充值金额</param>
/// <returns>折扣后的价格</returns>
public static decimal GetDiscountedPrice(this GoodsTypeEnum goodsType, decimal totalRechargeAmount)
{
var originalPrice = goodsType.GetTotalAmount();
var discount = goodsType.CalculateDiscount(totalRechargeAmount);
var discountedPrice = originalPrice - discount;
// 确保价格不为负数至少为0.01元
return Math.Max(discountedPrice, 0.01m);
}
}

View File

@@ -0,0 +1,9 @@
namespace Yi.Framework.AiHub.Domain.Shared.Enums;
public enum ModelTypeEnum
{
Chat = 0,
Image = 1,
Embedding = 2,
PremiumChat = 3
}

View File

@@ -0,0 +1,36 @@
using System.ComponentModel;
namespace Yi.Framework.AiHub.Domain.Shared.Enums;
public enum TradeStatusEnum
{
/// <summary>
/// 准备发起
/// </summary>
[Description("准备发起")]
WAIT_TRADE = 0,
/// <summary>
/// 交易创建
/// </summary>
[Description("等待买家付款")]
WAIT_BUYER_PAY = 10,
/// <summary>
/// 交易关闭
/// </summary>
[Description("交易关闭")]
TRADE_CLOSED = 20,
/// <summary>
/// 交易成功
/// </summary>
[Description("交易成功")]
TRADE_SUCCESS = 100,
/// <summary>
/// 交易结束
/// </summary>
[Description("交易结束")]
TRADE_FINISHED = -10
}

View File

@@ -6,7 +6,6 @@
</ItemGroup>
<ItemGroup>
<Folder Include="Consts\" />
<Folder Include="Etos\" />
</ItemGroup>

View File

@@ -2,6 +2,7 @@ using System.Net.Http.Json;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
namespace Yi.Framework.AiHub.Domain.AiGateWay;

View File

@@ -39,6 +39,12 @@ public static class HttpClientFactory
private static readonly ConcurrentDictionary<string, Lazy<List<HttpClient>>> HttpClientPool = new();
/// <summary>
/// 高并发下有问题
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
[Obsolete]
public static HttpClient GetHttpClient(string key)
{
return HttpClientPool.GetOrAdd(key, k => new Lazy<List<HttpClient>>(() =>
@@ -70,4 +76,4 @@ public static class HttpClientFactory
return clients;
})).Value[new Random().Next(0, PoolSize)];
}
}
}

View File

@@ -0,0 +1,29 @@
using Yi.Framework.AiHub.Domain.Shared.Dtos;
using Yi.Framework.AiHub.Domain.Shared.Dtos.Anthropic;
namespace Yi.Framework.AiHub.Domain.AiGateWay;
public interface IAnthropicChatCompletionService
{
/// <summary>
/// 非流式对话补全
/// </summary>
/// <param name="request">对话补全请求参数对象</param>
/// <param name="aiModelDescribe">平台参数对象</param>
/// <param name="cancellationToken">取消令牌</param>
/// <returns></returns>
Task<AnthropicChatCompletionDto> ChatCompletionsAsync(AiModelDescribe aiModelDescribe,
AnthropicInput request,
CancellationToken cancellationToken = default);
/// <summary>
/// 流式对话补全
/// </summary>
/// <param name="request">对话补全请求参数对象</param>
/// <param name="aiModelDescribe">平台参数对象</param>
/// <param name="cancellationToken">取消令牌</param>
/// <returns></returns>
IAsyncEnumerable<(string, AnthropicStreamDto?)> StreamChatCompletionsAsync(AiModelDescribe aiModelDescribe,
AnthropicInput request,
CancellationToken cancellationToken = default);
}

View File

@@ -1,5 +1,5 @@
using Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi;
using Yi.Framework.AiHub.Domain.Shared.Dtos;
using Yi.Framework.AiHub.Domain.Shared.Dtos;
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
namespace Yi.Framework.AiHub.Domain.AiGateWay;

View File

@@ -0,0 +1,39 @@
using Yi.Framework.AiHub.Domain.Shared.Dtos;
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi.Images;
namespace Yi.Framework.AiHub.Domain.AiGateWay;
public interface IImageService
{
/// <summary>Creates an image given a prompt.</summary>
/// <param name="imageCreate"></param>
/// <param name="aiModelDescribe"></param>
/// <param name="cancellationToken">Propagates notification that operations should be canceled.</param>
/// <returns></returns>
Task<ImageCreateResponse> CreateImage(
ImageCreateRequest imageCreate,
AiModelDescribe? aiModelDescribe = null,
CancellationToken cancellationToken = default);
/// <summary>
/// Creates an edited or extended image given an original image and a prompt.
/// </summary>
/// <param name="imageEditCreateRequest"></param>
/// <param name="aiModelDescribe"></param>
/// <param name="cancellationToken">Propagates notification that operations should be canceled.</param>
/// <returns></returns>
Task<ImageCreateResponse> CreateImageEdit(
ImageEditCreateRequest imageEditCreateRequest,
AiModelDescribe? aiModelDescribe = null,
CancellationToken cancellationToken = default);
/// <summary>Creates a variation of a given image.</summary>
/// <param name="imageEditCreateRequest"></param>
/// <param name="aiModelDescribe"></param>
/// <param name="cancellationToken">Propagates notification that operations should be canceled.</param>
/// <returns></returns>
Task<ImageCreateResponse> CreateImageVariation(
ImageVariationCreateRequest imageEditCreateRequest,
AiModelDescribe? aiModelDescribe = null,
CancellationToken cancellationToken = default);
}

View File

@@ -1,8 +1,10 @@
using Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi;
using Yi.Framework.AiHub.Domain.Shared.Dtos.Anthropic;
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
namespace Yi.Framework.AiHub.Domain.AiGateWay;
public interface ISpecialCompatible
{
public void Compatible(ThorChatCompletionsRequest request);
public void AnthropicCompatible(AnthropicInput request);
}

View File

@@ -0,0 +1,19 @@
using Yi.Framework.AiHub.Domain.Shared.Dtos;
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi.Embeddings;
namespace Yi.Framework.AiHub.Domain.AiGateWay;
public interface ITextEmbeddingService
{
/// <summary>
///
/// </summary>
/// <param name="createEmbeddingModel"></param>
/// <param name="aiModelDescribe"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<EmbeddingCreateResponse> EmbeddingAsync(
EmbeddingCreateRequest createEmbeddingModel,
AiModelDescribe? aiModelDescribe = null,
CancellationToken cancellationToken = default);
}

View File

@@ -4,13 +4,13 @@ using System.Net.Http.Json;
using System.Runtime.CompilerServices;
using System.Text.Json;
using Microsoft.Extensions.Logging;
using Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi;
using Yi.Framework.AiHub.Domain.AiGateWay.Exceptions;
using Yi.Framework.AiHub.Domain.Shared.Dtos;
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
namespace Yi.Framework.AiHub.Domain.AiGateWay.Impl.ThorAzureDatabricks.Chats;
public class AzureDatabricksChatCompletionsService(ILogger<AzureDatabricksChatCompletionsService> logger)
public class AzureDatabricksChatCompletionsService(ILogger<AzureDatabricksChatCompletionsService> logger,IHttpClientFactory httpClientFactory)
: IChatCompletionService
{
private string GetAddress(AiModelDescribe? options, string model)
@@ -31,7 +31,7 @@ public class AzureDatabricksChatCompletionsService(ILogger<AzureDatabricksChatCo
chatCompletionCreate.StreamOptions = null;
var response = await HttpClientFactory.GetHttpClient(address).HttpRequestRaw(
var response = await httpClientFactory.CreateClient().HttpRequestRaw(
address,
chatCompletionCreate, options.ApiKey);
@@ -108,33 +108,33 @@ public class AzureDatabricksChatCompletionsService(ILogger<AzureDatabricksChatCo
continue;
}
var content = result?.Choices?.FirstOrDefault()?.Delta;
if (first && content?.Content == OpenAIConstant.ThinkStart)
{
isThink = true;
continue;
// 需要将content的内容转换到其他字段
}
if (isThink && content?.Content?.Contains(OpenAIConstant.ThinkEnd) == true)
{
isThink = false;
// 需要将content的内容转换到其他字段
continue;
}
if (isThink && result?.Choices != null)
{
// 需要将content的内容转换到其他字段
foreach (var choice in result.Choices)
{
choice.Delta.ReasoningContent = choice.Delta.Content;
choice.Delta.Content = string.Empty;
}
}
first = false;
// var content = result?.Choices?.FirstOrDefault()?.Delta;
//
// if (first && content?.Content == OpenAIConstant.ThinkStart)
// {
// isThink = true;
// continue;
// // 需要将content的内容转换到其他字段
// }
//
// if (isThink && content?.Content?.Contains(OpenAIConstant.ThinkEnd) == true)
// {
// isThink = false;
// // 需要将content的内容转换到其他字段
// continue;
// }
//
// if (isThink && result?.Choices != null)
// {
// // 需要将content的内容转换到其他字段
// foreach (var choice in result.Choices)
// {
// choice.Delta.ReasoningContent = choice.Delta.Content;
// choice.Delta.Content = string.Empty;
// }
// }
//
// first = false;
yield return result;
}
@@ -149,7 +149,7 @@ public class AzureDatabricksChatCompletionsService(ILogger<AzureDatabricksChatCo
using var openai =
Activity.Current?.Source.StartActivity("OpenAI 对话补全");
var response = await HttpClientFactory.GetHttpClient(address).PostJsonAsync(
var response = await httpClientFactory.CreateClient().PostJsonAsync(
address,
chatCompletionCreate, options.ApiKey).ConfigureAwait(false);

View File

@@ -4,13 +4,13 @@ using System.Net.Http.Json;
using System.Runtime.CompilerServices;
using System.Text.Json;
using Microsoft.Extensions.Logging;
using Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi;
using Yi.Framework.AiHub.Domain.AiGateWay.Exceptions;
using Yi.Framework.AiHub.Domain.Shared.Dtos;
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
namespace Yi.Framework.AiHub.Domain.AiGateWay.Impl.ThorAzureOpenAI.Chats;
public class AzureOpenAiChatCompletionCompletionsService(ILogger<AzureOpenAiChatCompletionCompletionsService> logger)
public class AzureOpenAiChatCompletionCompletionsService(ILogger<AzureOpenAiChatCompletionCompletionsService> logger,IHttpClientFactory httpClientFactory)
: IChatCompletionService
{
public async IAsyncEnumerable<ThorChatCompletionsResponse> CompleteChatStreamAsync(AiModelDescribe options,
@@ -21,15 +21,16 @@ public class AzureOpenAiChatCompletionCompletionsService(ILogger<AzureOpenAiChat
Activity.Current?.Source.StartActivity("Azure OpenAI 对话流式补全");
var url = AzureOpenAIFactory.GetAddress(options, chatCompletionCreate.Model);
var response = await HttpClientFactory.GetHttpClient(options.Endpoint).HttpRequestRaw(url,
var response = await httpClientFactory.CreateClient().HttpRequestRaw(url,
chatCompletionCreate, options.ApiKey, "Api-Key");
openai?.SetTag("Model", chatCompletionCreate.Model);
openai?.SetTag("Response", response.StatusCode.ToString());
if (response.StatusCode >= HttpStatusCode.BadRequest)
{
var error = await response.Content.ReadAsStringAsync();
logger.LogError("Azure对话异常 , StatusCode: {StatusCode} 错误响应内容:{Content}", response.StatusCode,
error);
@@ -85,7 +86,7 @@ public class AzureOpenAiChatCompletionCompletionsService(ILogger<AzureOpenAiChat
var url = AzureOpenAIFactory.GetAddress(options, chatCompletionCreate.Model);
var response =
await HttpClientFactory.GetHttpClient(options.Endpoint)
await httpClientFactory.CreateClient()
.PostJsonAsync(url, chatCompletionCreate, options.ApiKey, "Api-Key");
openai?.SetTag("Model", chatCompletionCreate.Model);

View File

@@ -0,0 +1,112 @@
using OpenAI.Images;
using Yi.Framework.AiHub.Domain.AiGateWay;
using Yi.Framework.AiHub.Domain.Shared.Dtos;
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi.Images;
namespace Yi.Framework.AiHub.Domain.AiGateWay.Impl.ThorAzureOpenAI.Images;
public class AzureOpenAIServiceImageService(IHttpClientFactory httpClientFactory) : IImageService
{
public async Task<ImageCreateResponse> CreateImage(ImageCreateRequest imageCreate, AiModelDescribe? options = null,
CancellationToken cancellationToken = default(CancellationToken))
{
var createClient = AzureOpenAIFactory.CreateClient(options);
var client = createClient.GetImageClient(imageCreate.Model);
imageCreate.Size??="1024x1024";
// 将size字符串拆分为宽度和高度
var size = imageCreate.Size.Split('x');
if (size.Length != 2)
{
throw new ArgumentException("Size must be in the format of 'width x height'");
}
var response = await client.GenerateImageAsync(imageCreate.Prompt, new ImageGenerationOptions()
{
Quality = imageCreate.Quality == "standard" ? GeneratedImageQuality.Standard : GeneratedImageQuality.High,
Size = new GeneratedImageSize(Convert.ToInt32(size[0]), Convert.ToInt32(size[1])),
Style = imageCreate.Style == "vivid" ? GeneratedImageStyle.Vivid : GeneratedImageStyle.Natural,
ResponseFormat =
imageCreate.ResponseFormat == "url" ? GeneratedImageFormat.Uri : GeneratedImageFormat.Bytes,
// User = imageCreate.User
EndUserId = imageCreate.User
}, cancellationToken);
var ret = new ImageCreateResponse()
{
Results = new List<ImageCreateResponse.ImageDataResult>()
};
if (response.Value.ImageUri != null)
{
ret.Results.Add(new ImageCreateResponse.ImageDataResult()
{
Url = response.Value.ImageUri.ToString()
});
}
else
{
ret.Results.Add(new ImageCreateResponse.ImageDataResult()
{
B64 = Convert.ToBase64String(response.Value.ImageBytes.ToArray())
});
}
return ret;
}
public async Task<ImageCreateResponse> CreateImageEdit(ImageEditCreateRequest imageEditCreateRequest,
AiModelDescribe? options = null,
CancellationToken cancellationToken = default(CancellationToken))
{
var url = AzureOpenAIFactory.GetEditImageAddress(options, imageEditCreateRequest.Model);
var multipartContent = new MultipartFormDataContent();
if (imageEditCreateRequest.User != null)
{
multipartContent.Add(new StringContent(imageEditCreateRequest.User), "user");
}
if (imageEditCreateRequest.ResponseFormat != null)
{
multipartContent.Add(new StringContent(imageEditCreateRequest.ResponseFormat), "response_format");
}
if (imageEditCreateRequest.Size != null)
{
multipartContent.Add(new StringContent(imageEditCreateRequest.Size), "size");
}
if (imageEditCreateRequest.N != null)
{
multipartContent.Add(new StringContent(imageEditCreateRequest.N.ToString()!), "n");
}
if (imageEditCreateRequest.Model != null)
{
multipartContent.Add(new StringContent(imageEditCreateRequest.Model!), "model");
}
if (imageEditCreateRequest.Mask != null)
{
multipartContent.Add(new ByteArrayContent(imageEditCreateRequest.Mask), "mask",
imageEditCreateRequest.MaskName);
}
multipartContent.Add(new StringContent(imageEditCreateRequest.Prompt), "prompt");
multipartContent.Add(new ByteArrayContent(imageEditCreateRequest.Image), "image",
imageEditCreateRequest.ImageName);
return await httpClientFactory.CreateClient().PostFileAndReadAsAsync<ImageCreateResponse>(
url,
multipartContent, cancellationToken);
}
public Task<ImageCreateResponse> CreateImageVariation(ImageVariationCreateRequest imageEditCreateRequest,
AiModelDescribe? options = null,
CancellationToken cancellationToken = default(CancellationToken))
{
throw new NotImplementedException();
}
}

View File

@@ -0,0 +1,167 @@
using System.Diagnostics;
using System.Net;
using System.Net.Http.Json;
using System.Text.Json;
using Microsoft.Extensions.Logging;
using Yi.Framework.AiHub.Domain.Shared.Dtos;
using Yi.Framework.AiHub.Domain.Shared.Dtos.Anthropic;
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
namespace Yi.Framework.AiHub.Domain.AiGateWay.Impl.ThorClaude.Chats;
public class AnthropicChatCompletionsService(IHttpClientFactory httpClientFactory,ILogger<AnthropicChatCompletionsService> logger)
: IAnthropicChatCompletionService
{
public async Task<AnthropicChatCompletionDto> ChatCompletionsAsync(AiModelDescribe options, AnthropicInput input,
CancellationToken cancellationToken = default)
{
using var openai =
Activity.Current?.Source.StartActivity("Claudia 对话补全");
if (string.IsNullOrEmpty(options.Endpoint))
{
options.Endpoint = "https://api.anthropic.com/";
}
var client = httpClientFactory.CreateClient();
var headers = new Dictionary<string, string>
{
{ "x-api-key", options.ApiKey },
{ "authorization", "Bearer " + options.ApiKey },
{ "anthropic-version", "2023-06-01" }
};
bool isThink = input.Model.EndsWith("-thinking");
input.Model = input.Model.Replace("-thinking", string.Empty);
if (input.MaxTokens is < 2048)
{
input.MaxTokens = 2048;
}
if (isThink && input.Thinking is null)
{
input.Thinking = new AnthropicThinkingInput()
{
Type = "enabled",
BudgetTokens = 4000
};
}
if (input.Thinking is not null && input.Thinking.BudgetTokens > 0 && input.MaxTokens != null)
{
if (input.Thinking.BudgetTokens > input.MaxTokens)
{
input.Thinking.BudgetTokens = input.MaxTokens.Value - 1;
if (input.Thinking.BudgetTokens > 63999)
{
input.Thinking.BudgetTokens = 63999;
}
}
}
var response =
await client.PostJsonAsync(options.Endpoint.TrimEnd('/') + "/v1/messages", input, string.Empty, headers);
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.ToString());
}
var value =
await response.Content.ReadFromJsonAsync<AnthropicChatCompletionDto>(ThorJsonSerializer.DefaultOptions,
cancellationToken: cancellationToken);
return value;
}
public async IAsyncEnumerable<(string, AnthropicStreamDto?)> StreamChatCompletionsAsync(AiModelDescribe options, AnthropicInput input,
CancellationToken cancellationToken = default)
{
using var openai =
Activity.Current?.Source.StartActivity("Claudia 对话补全");
if (string.IsNullOrEmpty(options.Endpoint))
{
options.Endpoint = "https://api.anthropic.com/";
}
var client = httpClientFactory.CreateClient();
var headers = new Dictionary<string, string>
{
{ "x-api-key", options.ApiKey },
{ "authorization", options.ApiKey },
{ "anthropic-version", "2023-06-01" }
};
var isThinking = input.Model.EndsWith("thinking");
input.Model = input.Model.Replace("-thinking", string.Empty);
var response = await client.HttpRequestRaw(options.Endpoint.TrimEnd('/') + "/v1/messages", input, string.Empty,
headers);
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<AnthropicStreamDto>(data,
ThorJsonSerializer.DefaultOptions);
yield return (eventType, result);
}
}
}

View File

@@ -0,0 +1,313 @@
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);
}
}
}

View File

@@ -0,0 +1,167 @@
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;
namespace Yi.Framework.AiHub.Domain.AiGateWay.Impl.ThorCustomOpenAI.Chats;
public sealed class OpenAiChatCompletionsService(ILogger<OpenAiChatCompletionsService> logger,IHttpClientFactory httpClientFactory)
: IChatCompletionService
{
public async IAsyncEnumerable<ThorChatCompletionsResponse> CompleteChatStreamAsync(AiModelDescribe options,
ThorChatCompletionsRequest chatCompletionCreate,
CancellationToken cancellationToken)
{
using var openai =
Activity.Current?.Source.StartActivity("OpenAI 对话流式补全");
var response = await httpClientFactory.CreateClient().HttpRequestRaw(
options?.Endpoint.TrimEnd('/') + "/chat/completions",
chatCompletionCreate, options.ApiKey);
openai?.SetTag("Model", chatCompletionCreate.Model);
openai?.SetTag("Response", response.StatusCode.ToString());
if (response.StatusCode == HttpStatusCode.Unauthorized)
{
throw new UnauthorizedAccessException();
}
// 如果限流则抛出限流异常
if (response.StatusCode == HttpStatusCode.TooManyRequests)
{
throw new ThorRateLimitException();
}
// 大于等于400的状态码都认为是异常
if (response.StatusCode >= HttpStatusCode.BadRequest)
{
var error = await response.Content.ReadAsStringAsync();
logger.LogError("OpenAI对话异常 , StatusCode: {StatusCode} 错误响应内容:{Content}", response.StatusCode,
error);
throw new BusinessException("OpenAI对话异常" + error, response.StatusCode.ToString());
}
using var stream = new StreamReader(await response.Content.ReadAsStreamAsync(cancellationToken));
using StreamReader reader = new(await response.Content.ReadAsStreamAsync(cancellationToken));
string? line = string.Empty;
var first = true;
var isThink = false;
while ((line = await reader.ReadLineAsync().ConfigureAwait(false)) != null)
{
line += Environment.NewLine;
if (line.StartsWith('{'))
{
logger.LogInformation("OpenAI对话异常 , StatusCode: {StatusCode} Response: {Response}", response.StatusCode,
line);
throw new BusinessException("OpenAI对话异常", line);
}
if (line.StartsWith(OpenAIConstant.Data))
line = line[OpenAIConstant.Data.Length..];
line = line.Trim();
if (string.IsNullOrWhiteSpace(line)) continue;
if (line == OpenAIConstant.Done)
{
break;
}
if (line.StartsWith(':'))
{
continue;
}
var result = JsonSerializer.Deserialize<ThorChatCompletionsResponse>(line,
ThorJsonSerializer.DefaultOptions);
if (result == null)
{
continue;
}
var content = result?.Choices?.FirstOrDefault()?.Delta;
if (first && content?.Content == OpenAIConstant.ThinkStart)
{
isThink = true;
continue;
// 需要将content的内容转换到其他字段
}
if (isThink && content?.Content?.Contains(OpenAIConstant.ThinkEnd) == true)
{
isThink = false;
// 需要将content的内容转换到其他字段
continue;
}
if (isThink && result?.Choices != null)
{
// 需要将content的内容转换到其他字段
foreach (var choice in result.Choices)
{
choice.Delta.ReasoningContent = choice.Delta.Content;
choice.Delta.Content = string.Empty;
}
}
first = false;
yield return result;
}
}
public async Task<ThorChatCompletionsResponse> CompleteChatAsync(AiModelDescribe options,
ThorChatCompletionsRequest chatCompletionCreate,
CancellationToken cancellationToken)
{
using var openai =
Activity.Current?.Source.StartActivity("OpenAI 对话补全");
var response = await HttpClientFactory.GetHttpClient(options.Endpoint).PostJsonAsync(
options?.Endpoint.TrimEnd('/') + "/chat/completions",
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<ThorChatCompletionsResponse>(
cancellationToken: cancellationToken).ConfigureAwait(false);
return result;
}
}

View File

@@ -0,0 +1,178 @@
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;
namespace Yi.Framework.AiHub.Domain.AiGateWay.Impl.ThorDeepSeek.Chats;
public sealed class DeepSeekChatCompletionsService(ILogger<DeepSeekChatCompletionsService> logger,IHttpClientFactory httpClientFactory)
: IChatCompletionService
{
public async IAsyncEnumerable<ThorChatCompletionsResponse> CompleteChatStreamAsync(AiModelDescribe options,
ThorChatCompletionsRequest chatCompletionCreate,
CancellationToken cancellationToken)
{
if (string.IsNullOrWhiteSpace(options.Endpoint))
{
options.Endpoint = "https://api.deepseek.com/v1";
}
using var openai =
Activity.Current?.Source.StartActivity("OpenAI 对话流式补全");
var response = await httpClientFactory.CreateClient().HttpRequestRaw(
options?.Endpoint.TrimEnd('/') + "/chat/completions",
chatCompletionCreate, options.ApiKey);
openai?.SetTag("Model", chatCompletionCreate.Model);
openai?.SetTag("Response", response.StatusCode.ToString());
if (response.StatusCode == HttpStatusCode.Unauthorized)
{
throw new UnauthorizedAccessException();
}
if (response.StatusCode == HttpStatusCode.PaymentRequired)
{
throw new PaymentRequiredException();
}
// 如果限流则抛出限流异常
if (response.StatusCode == HttpStatusCode.TooManyRequests)
{
throw new ThorRateLimitException();
}
// 大于等于400的状态码都认为是异常
if (response.StatusCode >= HttpStatusCode.BadRequest)
{
logger.LogError("OpenAI对话异常 , StatusCode: {StatusCode} ", response.StatusCode);
throw new BusinessException("OpenAI对话异常", response.StatusCode.ToString());
}
using var stream = new StreamReader(await response.Content.ReadAsStreamAsync(cancellationToken));
using StreamReader reader = new(await response.Content.ReadAsStreamAsync(cancellationToken));
string? line = string.Empty;
var first = true;
var isThink = false;
while ((line = await reader.ReadLineAsync().ConfigureAwait(false)) != null)
{
line += Environment.NewLine;
if (line.StartsWith('{'))
{
logger.LogInformation("OpenAI对话异常 , StatusCode: {StatusCode} Response: {Response}", response.StatusCode,
line);
throw new BusinessException("OpenAI对话异常", line);
}
if (line.StartsWith(OpenAIConstant.Data))
line = line[OpenAIConstant.Data.Length..];
line = line.Trim();
if (string.IsNullOrWhiteSpace(line)) continue;
if (line == OpenAIConstant.Done)
{
break;
}
if (line.StartsWith(':'))
{
continue;
}
var result = JsonSerializer.Deserialize<ThorChatCompletionsResponse>(line,
ThorJsonSerializer.DefaultOptions);
// var content = result?.Choices?.FirstOrDefault()?.Delta;
//
// // if (first && string.IsNullOrWhiteSpace(content?.Content) && string.IsNullOrEmpty(content?.ReasoningContent))
// // {
// // continue;
// // }
//
// if (first && content.Content == OpenAIConstant.ThinkStart)
// {
// isThink = true;
// //continue;
// // 需要将content的内容转换到其他字段
// }
//
// if (isThink && content.Content.Contains(OpenAIConstant.ThinkEnd))
// {
// isThink = false;
// // 需要将content的内容转换到其他字段
// //continue;
// }
//
// if (isThink)
// {
// // 需要将content的内容转换到其他字段
// foreach (var choice in result.Choices)
// {
// //choice.Delta.ReasoningContent = choice.Delta.Content;
// //choice.Delta.Content = string.Empty;
// }
// }
// first = false;
yield return result;
}
}
public async Task<ThorChatCompletionsResponse> CompleteChatAsync(AiModelDescribe options,
ThorChatCompletionsRequest chatCompletionCreate,
CancellationToken cancellationToken)
{
using var openai =
Activity.Current?.Source.StartActivity("OpenAI 对话补全");
if (string.IsNullOrWhiteSpace(options.Endpoint))
{
options.Endpoint = "https://api.deepseek.com/v1";
}
var response = await httpClientFactory.CreateClient().PostJsonAsync(
options?.Endpoint.TrimEnd('/') + "/chat/completions",
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对话异常 , StatusCode: {StatusCode} Response: {Response}", response.StatusCode, error);
throw new BusinessException("OpenAI对话异常", response.StatusCode.ToString());
}
var result =
await response.Content.ReadFromJsonAsync<ThorChatCompletionsResponse>(
cancellationToken: cancellationToken).ConfigureAwait(false);
return result;
}
}

View File

@@ -0,0 +1,24 @@
using System.Net.Http.Json;
using Yi.Framework.AiHub.Domain.Shared.Dtos;
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi.Embeddings;
namespace Yi.Framework.AiHub.Domain.AiGateWay.Impl.ThorSiliconFlow.Embeddings;
public sealed class SiliconFlowTextEmbeddingService(IHttpClientFactory httpClientFactory)
: ITextEmbeddingService
{
public async Task<EmbeddingCreateResponse> EmbeddingAsync(
EmbeddingCreateRequest createEmbeddingModel,
AiModelDescribe? options = null,
CancellationToken cancellationToken = default)
{
var response = await httpClientFactory.CreateClient().PostJsonAsync(
options?.Endpoint.TrimEnd('/') + "/v1/embeddings",
createEmbeddingModel, options!.ApiKey);
var result =
await response.Content.ReadFromJsonAsync<EmbeddingCreateResponse>(cancellationToken: cancellationToken);
return result;
}
}

View File

@@ -1,6 +1,7 @@
using Microsoft.Extensions.Options;
using Volo.Abp.DependencyInjection;
using Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi;
using Yi.Framework.AiHub.Domain.Shared.Dtos.Anthropic;
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
namespace Yi.Framework.AiHub.Domain.AiGateWay;
@@ -20,4 +21,12 @@ public class SpecialCompatible : ISpecialCompatible,ISingletonDependency
handle(request);
}
}
public void AnthropicCompatible(AnthropicInput request)
{
foreach (var handle in _options.Value.AnthropicHandles)
{
handle(request);
}
}
}

View File

@@ -1,8 +1,10 @@
using Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi;
using Yi.Framework.AiHub.Domain.Shared.Dtos.Anthropic;
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
namespace Yi.Framework.AiHub.Domain.AiGateWay;
public class SpecialCompatibleOptions
{
public List<Action<ThorChatCompletionsRequest>> Handles { get; set; } = new();
public List<Action<AnthropicInput>> AnthropicHandles { get; set; } = new();
}

View File

@@ -0,0 +1,10 @@
using Microsoft.Extensions.Logging;
namespace Yi.Framework.AiHub.Domain.Alipay;
public class AlipayException:UserFriendlyException
{
public AlipayException(string message, string? code = null, string? details = null, Exception? innerException = null, LogLevel logLevel = LogLevel.Warning) : base(message, code, details, innerException, logLevel)
{
}
}

View File

@@ -0,0 +1,70 @@
using Alipay.EasySDK.Factory;
using Alipay.EasySDK.Kernel.Util;
using Alipay.EasySDK.Payment.Page.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Volo.Abp.Domain.Services;
namespace Yi.Framework.AiHub.Domain.Alipay;
public class AlipayManager : DomainService
{
private readonly ILogger<AlipayManager> _logger;
public AlipayManager(ILogger<AlipayManager> logger)
{
_logger = logger;
}
/// <summary>
/// 统一Page支付
/// </summary>
/// <returns></returns>
/// <exception cref="AlipayException"></exception>
public Task<AlipayTradePagePayResponse> PaymentPageAsync(string productName, string orderNumber,
decimal totalAmount, string? returnUrl)
{
try
{
// 2. 发起API调用以创建当面付收款二维码为例
var response = Factory.Payment.Page()
.Pay(productName, orderNumber, totalAmount.ToString(), returnUrl ?? string.Empty);
// 3. 处理响应或异常
if (ResponseChecker.Success(response))
{
_logger.LogInformation($"支付宝PaymentPage发起调用成功返回内容{response.Body}");
//插入数据库
return Task.FromResult(response);
}
else
{
throw new AlipayException($"支付宝PaymentPage发起调用失败原因{response.Body}");
}
}
catch (Exception ex)
{
throw new AlipayException($"支付宝PaymentPage发起调用错误原因{ex.Message}", innerException: ex);
}
}
/// <summary>
/// 通知验签
/// </summary>
/// <param name="form"></param>
/// <returns></returns>
/// <exception cref="AlipayException"></exception>
public Task VerifyNotifyAsync(Dictionary<string, string> form)
{
// 支付宝的验签需要保持原始参数顺序,排序会导致验签失败
var result = Factory.Payment.Common().VerifyNotify(form);
if (result == false)
{
_logger.LogError($"支付宝支付验签失败,回调参数:{System.Text.Json.JsonSerializer.Serialize(form)}");
throw new AlipayException($"支付宝支付,验签失败");
}
_logger.LogInformation("支付宝回调验签成功");
return Task.CompletedTask;
}
}

View File

@@ -0,0 +1,40 @@
using SqlSugar;
using Volo.Abp.Auditing;
using Volo.Abp.Domain.Entities;
namespace Yi.Framework.AiHub.Domain.Entities;
/// <summary>
/// ai用户表
/// </summary>
[SugarTable("Ai_UserExtraInfo")]
[SugarIndex($"index_{nameof(UserId)}", nameof(UserId), OrderByType.Asc)]
public class AiUserExtraInfoEntity : Entity<Guid>, IHasCreationTime, ISoftDelete
{
public AiUserExtraInfoEntity()
{
}
public AiUserExtraInfoEntity(Guid userId, string fuwuhaoOpenId)
{
this.UserId = userId;
this.FuwuhaoOpenId = fuwuhaoOpenId;
}
/// <summary>
/// 用户id
/// </summary>
public Guid UserId { get; set; }
/// <summary>
/// 服务号openid
/// </summary>
public string FuwuhaoOpenId { get; set; }
/// <summary>
/// 创建时间
/// </summary>
public DateTime CreationTime { get; set; }
public bool IsDeleted { get; set; }
}

View File

@@ -1,9 +1,9 @@
using Mapster;
using SqlSugar;
using Volo.Abp.Domain.Entities.Auditing;
using Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi;
using Yi.Framework.AiHub.Domain.Entities.ValueObjects;
using Yi.Framework.AiHub.Domain.Shared.Dtos;
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
using Yi.Framework.AiHub.Domain.Shared.Enums;
namespace Yi.Framework.AiHub.Domain.Entities.Chat;
@@ -19,7 +19,7 @@ public class MessageAggregateRoot : FullAuditedAggregateRoot<Guid>
{
}
public MessageAggregateRoot(Guid userId, Guid? sessionId, string content, string role, string modelId,
public MessageAggregateRoot(Guid? userId, Guid? sessionId, string content, string role, string modelId,
ThorUsageResponse? tokenUsage)
{
UserId = userId;
@@ -29,10 +29,25 @@ public class MessageAggregateRoot : FullAuditedAggregateRoot<Guid>
ModelId = modelId;
if (tokenUsage is not null)
{
long inputTokenCount =
(tokenUsage.PromptTokens.HasValue && tokenUsage.PromptTokens.Value != 0)
? tokenUsage.PromptTokens.Value
: (tokenUsage.InputTokens.HasValue && tokenUsage.InputTokens.Value != 0)
? tokenUsage.InputTokens.Value
: 0;
long outputTokenCount =
(tokenUsage.CompletionTokens.HasValue && tokenUsage.CompletionTokens.Value != 0)
? tokenUsage.CompletionTokens.Value
: (tokenUsage.OutputTokens.HasValue && tokenUsage.OutputTokens.Value != 0)
? tokenUsage.OutputTokens.Value
: 0;
this.TokenUsage = new TokenUsageValueObject
{
OutputTokenCount = tokenUsage.OutputTokens ?? 0,
InputTokenCount = tokenUsage.InputTokens ?? 0,
OutputTokenCount = outputTokenCount,
InputTokenCount = inputTokenCount,
TotalTokenCount = tokenUsage.TotalTokens ?? 0
};
}
@@ -40,11 +55,11 @@ public class MessageAggregateRoot : FullAuditedAggregateRoot<Guid>
this.MessageType = sessionId is null ? MessageTypeEnum.Api : MessageTypeEnum.Web;
}
public Guid UserId { get; set; }
public Guid? UserId { get; set; }
public Guid? SessionId { get; set; }
[SugarColumn(ColumnDataType = StaticConfig.CodeFirst_BigString)]
public string Content { get; set; }
public string? Content { get; set; }
public string Role { get; set; }
public string ModelId { get; set; }

Some files were not shown because too many files have changed in this diff Show More