91 lines
2.7 KiB
C#
91 lines
2.7 KiB
C#
using ARW.Model.Chat;
|
|
using ARW.Model.Chat.ChatGPT;
|
|
using ARW.Service.Business.IBusinessService.Chat;
|
|
using Infrastructure;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using OpenAI.GPT3.Managers;
|
|
using OpenAI.GPT3.ObjectModels.RequestModels;
|
|
using OpenAI.GPT3.ObjectModels;
|
|
using OpenAI.GPT3;
|
|
using OpenAI_API;
|
|
using OpenAI_API.Completions;
|
|
|
|
namespace ARW.Admin.WebApi.Controllers.Business.Api.ChatGPT
|
|
{
|
|
/// <summary>
|
|
/// ChatGPT控制器
|
|
/// </summary>
|
|
[Route("api/[controller]")]
|
|
public class ChatGPTController : BaseController
|
|
{
|
|
const string OPENAPI_TOKEN = "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";//输入自己的api-key
|
|
|
|
|
|
private readonly IChatGPTLogService _ChatGPTLogService;
|
|
|
|
public ChatGPTController(IChatGPTLogService chatGPTLogService)
|
|
{
|
|
_ChatGPTLogService = chatGPTLogService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取与ChatGPT的聊天记录
|
|
/// </summary>
|
|
[HttpGet("getChatGPTLog")]
|
|
public async Task<IActionResult> GetChatGPTLog([FromQuery] ChatGPTQueryDto parm)
|
|
{
|
|
if (parm.UserGuId == 0) throw new CustomException("请传用户的Guid");
|
|
var list = await _ChatGPTLogService.GetChatGPTLogList(parm);
|
|
|
|
return SUCCESS(list);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 请求ChatGPT接口
|
|
/// </summary>
|
|
[HttpPost("getChatGPT")]
|
|
public async Task<IActionResult> GetChatGPT([FromBody] CahtGPTDto parm)
|
|
{
|
|
//return SUCCESS("你好!我是一个语言模型。");
|
|
|
|
OpenAIService service = new OpenAIService(new OpenAiOptions() { ApiKey = OPENAPI_TOKEN });
|
|
CompletionCreateRequest createRequest = new CompletionCreateRequest()
|
|
{
|
|
TopP = 1,
|
|
Prompt = parm.Content,
|
|
Temperature = 0.3f,
|
|
MaxTokens = 1000
|
|
};
|
|
|
|
var res = await service.Completions.CreateCompletion(createRequest, Models.TextDavinciV3);
|
|
|
|
if (res.Successful)
|
|
{
|
|
var ss = res.Choices.FirstOrDefault().Text;
|
|
//Console.WriteLine(ss);
|
|
|
|
return SUCCESS(ss);
|
|
}
|
|
else
|
|
{
|
|
return SUCCESS(res.Error);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 添加聊天记录
|
|
/// </summary>
|
|
[HttpPost("addChatGPTLog")]
|
|
public IActionResult addChatGPTLog([FromBody] CahtGPTDto parm)
|
|
{
|
|
var id = _ChatGPTLogService.AddChatGPTLog(parm);
|
|
if (id == 0) throw new CustomException("添加记录失败");
|
|
|
|
return SUCCESS("添加记录成功");
|
|
}
|
|
|
|
}
|
|
}
|