feat:聊天新增ai
This commit is contained in:
@@ -1,24 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.ChatHub.Application.Dtos
|
||||
{
|
||||
public class AiChatContextDto
|
||||
{
|
||||
public AnswererTypeEnum AnswererType { get; set; }
|
||||
|
||||
public string Message { get; set; }
|
||||
|
||||
|
||||
public int Number { get; set; }
|
||||
}
|
||||
|
||||
public enum AnswererTypeEnum
|
||||
{
|
||||
Ai,
|
||||
User
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Volo.Abp.Application.Services;
|
||||
using Yi.Framework.ChatHub.Domain.Managers;
|
||||
using Yi.Framework.ChatHub.Domain.Shared.Dtos;
|
||||
using Yi.Framework.ChatHub.Domain.Shared.Model;
|
||||
using Yi.Framework.ChatHub.Domain.SignalRHubs;
|
||||
|
||||
namespace Yi.Framework.ChatHub.Application.Services
|
||||
{
|
||||
@@ -19,14 +14,24 @@ namespace Yi.Framework.ChatHub.Application.Services
|
||||
private readonly UserMessageManager _userMessageManager;
|
||||
public AiChatService(AiManager aiManager, UserMessageManager userMessageManager) { _aiManager = aiManager; _userMessageManager = userMessageManager; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// ai聊天
|
||||
/// </summary>
|
||||
/// <param name="chatContext"></param>
|
||||
/// <returns></returns>
|
||||
[Authorize]
|
||||
[HttpPost]
|
||||
public async Task ChatAsync()
|
||||
|
||||
public async Task ChatAsync([FromBody] List<AiChatContextDto> chatContext)
|
||||
{
|
||||
await foreach (var aiResult in _aiManager.ChatAsStreamAsync())
|
||||
var contextId = Guid.NewGuid();
|
||||
await foreach (var aiResult in _aiManager.ChatAsStreamAsync(chatContext))
|
||||
{
|
||||
await _userMessageManager.SendMessageAsync(MessageContext.CreateAi(aiResult, CurrentUser.Id!.Value));
|
||||
await _userMessageManager.SendMessageAsync(MessageContext.CreateAi(aiResult, CurrentUser.Id!.Value, contextId));
|
||||
}
|
||||
|
||||
await _userMessageManager.SendMessageAsync(MessageContext.CreateAi(null, CurrentUser.Id!.Value, contextId));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,10 +9,11 @@ namespace Yi.Framework.ChatHub.Domain.Shared.Enums
|
||||
public enum MessageTypeEnum
|
||||
{
|
||||
|
||||
Ai,
|
||||
|
||||
Personal,
|
||||
Group,
|
||||
All
|
||||
All,
|
||||
Ai
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,9 +16,9 @@ namespace Yi.Framework.ChatHub.Domain.Shared.Model
|
||||
{
|
||||
return context.Where(x => x.ReceiveId is not null).Select(x => x.ReceiveId!.Value).Union(context.Select(x => x.SendUserId)).ToList();
|
||||
}
|
||||
public static List<MessageContext> MapperUserInfo(this List<MessageContext> messageContexts,List<UserRoleMenuDto> userRoleMenuDtos)
|
||||
public static List<MessageContext> MapperUserInfo(this List<MessageContext> messageContexts, List<UserRoleMenuDto> userRoleMenuDtos)
|
||||
{
|
||||
var userInfoDic = userRoleMenuDtos.ToDictionary(x => x.User.Id);
|
||||
var userInfoDic = userRoleMenuDtos.Select(x => new UserRoleMenuDto { User = x.User }).ToDictionary(x => x.User.Id);
|
||||
foreach (var context in messageContexts)
|
||||
{
|
||||
if (context.ReceiveId is not null)
|
||||
@@ -42,9 +42,9 @@ namespace Yi.Framework.ChatHub.Domain.Shared.Model
|
||||
}
|
||||
|
||||
|
||||
public static MessageContext CreateAi(string content, Guid receiveId)
|
||||
public static MessageContext CreateAi(string? content, Guid receiveId, Guid id )
|
||||
{
|
||||
return new MessageContext() { MessageType = MessageTypeEnum.Ai, Content = content, ReceiveId = receiveId };
|
||||
return new MessageContext() { MessageType = MessageTypeEnum.Ai, Content = content??string.Empty, ReceiveId = receiveId, Id = id };
|
||||
}
|
||||
|
||||
public static MessageContext CreateAll(string content, Guid sendUserId)
|
||||
@@ -86,6 +86,11 @@ namespace Yi.Framework.ChatHub.Domain.Shared.Model
|
||||
/// 消息内容
|
||||
/// </summary>
|
||||
public string Content { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 上下文Id,主要用于ai流式传输
|
||||
/// </summary>
|
||||
public Guid Id { get; set; }
|
||||
public DateTime CreationTime { get; protected set; } = DateTime.Now;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.Extensions.Options;
|
||||
using OpenAI;
|
||||
using OpenAI.Managers;
|
||||
@@ -10,11 +6,11 @@ using OpenAI.ObjectModels;
|
||||
using OpenAI.ObjectModels.RequestModels;
|
||||
using Volo.Abp.DependencyInjection;
|
||||
using Volo.Abp.Domain.Services;
|
||||
using Yi.Framework.ChatHub.Domain.Shared.Dtos;
|
||||
using Yi.Framework.ChatHub.Domain.Shared.Options;
|
||||
|
||||
namespace Yi.Framework.ChatHub.Domain.Managers
|
||||
{
|
||||
public class AiManager : DomainService, ISingletonDependency
|
||||
public class AiManager : ISingletonDependency
|
||||
{
|
||||
public AiManager(IOptions<AiOptions> options)
|
||||
{
|
||||
@@ -26,25 +22,49 @@ namespace Yi.Framework.ChatHub.Domain.Managers
|
||||
}
|
||||
private OpenAIService OpenAIService { get; }
|
||||
|
||||
public async IAsyncEnumerable<string> ChatAsStreamAsync()
|
||||
public async IAsyncEnumerable<string> ChatAsStreamAsync(List<AiChatContextDto> aiChatContextDtos)
|
||||
{
|
||||
var completionResult = OpenAIService.ChatCompletion.CreateCompletionAsStream(new ChatCompletionCreateRequest
|
||||
{
|
||||
Messages = new List<ChatMessage>
|
||||
{
|
||||
ChatMessage.FromUser("特朗普是谁?"),
|
||||
},
|
||||
Model = Models.Gpt_4,
|
||||
});
|
||||
var temp = "站长正在接入ChatGpt,敬请期待~";
|
||||
|
||||
await foreach (var result in completionResult)
|
||||
for (var i = 0; i < temp.Length; i++)
|
||||
{
|
||||
if (result.Successful)
|
||||
{
|
||||
yield return result.Choices.FirstOrDefault()?.Message.Content??string.Empty;
|
||||
}
|
||||
await Task.Delay(200);
|
||||
yield return temp[i].ToString();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//if (aiChatContextDtos.Count == 0)
|
||||
//{
|
||||
// yield return null;
|
||||
//}
|
||||
|
||||
//List<ChatMessage> messages= aiChatContextDtos.Select(x =>
|
||||
//{
|
||||
// if (x.AnswererType == AnswererTypeEnum.Ai)
|
||||
// {
|
||||
// return ChatMessage.FromSystem(x.Message);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// return ChatMessage.FromUser(x.Message);
|
||||
// }
|
||||
//}).ToList();
|
||||
//var completionResult = OpenAIService.ChatCompletion.CreateCompletionAsStream(new ChatCompletionCreateRequest
|
||||
//{
|
||||
// Messages = messages,
|
||||
// Model = Models.Gpt_3_5_Turbo
|
||||
//});
|
||||
|
||||
//await foreach (var result in completionResult)
|
||||
//{
|
||||
// if (result.Successful)
|
||||
// {
|
||||
// yield return result.Choices.FirstOrDefault()?.Message.Content ?? string.Empty;
|
||||
// }
|
||||
//}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user