...
This commit is contained in:
14
Hushian.Application/Contracts/Iaiass.cs
Normal file
14
Hushian.Application/Contracts/Iaiass.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using Hushian.Application.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Hushian.Application.Contracts
|
||||
{
|
||||
public interface Iaiass
|
||||
{
|
||||
Task<ResponseBase<string>> SendQuestion(aiRequestModel Request);
|
||||
}
|
||||
}
|
29
Hushian.Application/Models/aiRequestModel.cs
Normal file
29
Hushian.Application/Models/aiRequestModel.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Hushian.Application.Models
|
||||
{
|
||||
public class aiRequestModel
|
||||
{
|
||||
public aiRequestModel(string question, string[] prompts)
|
||||
{
|
||||
this.question = question;
|
||||
this.prompts = prompts;
|
||||
}
|
||||
|
||||
public string question { get; set; }
|
||||
public string[] prompts { get; set; }
|
||||
public override string ToString()
|
||||
{
|
||||
string str = "";
|
||||
foreach (var item in prompts)
|
||||
{
|
||||
str += '\n'+item;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
}
|
||||
}
|
14
Hushian.Application/Models/aia/aiSetting.cs
Normal file
14
Hushian.Application/Models/aia/aiSetting.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Hushian.Application.Models.aia
|
||||
{
|
||||
public class aiSetting
|
||||
{
|
||||
public string url { get; set; }
|
||||
public string apitoken { get; set; }
|
||||
}
|
||||
}
|
@@ -1,5 +1,6 @@
|
||||
using AutoMapper;
|
||||
using Common.Dtos;
|
||||
using Hushian.Application.Contracts;
|
||||
using Hushian.Application.Contracts.Persistence;
|
||||
using Hushian.Application.Models;
|
||||
using Hushian.Domain.Entites;
|
||||
@@ -8,65 +9,79 @@ using System.Linq;
|
||||
|
||||
namespace Hushian.Application.Services
|
||||
{
|
||||
public class AIService
|
||||
{
|
||||
private readonly IGenericRepository<AIA> _aiaRepository;
|
||||
private readonly IMapper _mapper;
|
||||
public class AIService
|
||||
{
|
||||
private readonly IGenericRepository<AIA> _aiaRepository;
|
||||
private readonly IGenericRepository<Prompt> _promprtRepository;
|
||||
private readonly Iaiass _aiass;
|
||||
|
||||
public AIService(IGenericRepository<AIA> aiaRepository, IMapper mapper)
|
||||
{
|
||||
_aiaRepository = aiaRepository;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task<ResponseBase<aiResponseDto>> NewRequest
|
||||
(aiNewResponseDto dto)
|
||||
{
|
||||
ResponseBase<aiResponseDto> response = new();
|
||||
try
|
||||
{
|
||||
string responeai="همین جوری";
|
||||
bool sucessresponseai =!false;
|
||||
|
||||
public AIService(IGenericRepository<AIA> aiaRepository, Iaiass aiass, IGenericRepository<Prompt> promprtRepository)
|
||||
{
|
||||
_aiaRepository = aiaRepository;
|
||||
_aiass = aiass;
|
||||
_promprtRepository = promprtRepository;
|
||||
}
|
||||
|
||||
public async Task<ResponseBase<aiResponseDto>> NewRequest
|
||||
(aiNewResponseDto dto)
|
||||
{
|
||||
ResponseBase<aiResponseDto> response = new();
|
||||
try
|
||||
{
|
||||
string responeai = "";
|
||||
bool sucessresponseai = false;
|
||||
|
||||
var prompts = await _promprtRepository.Get()
|
||||
.Where(w => w.CompanyID == dto.companyId).Select(s => s.Test).ToArrayAsync();
|
||||
var requsetai= await _aiass.SendQuestion(new aiRequestModel(dto.requestText,prompts));
|
||||
if(requsetai.Success)
|
||||
{
|
||||
sucessresponseai = true;
|
||||
responeai = requsetai.Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
response.Errors = requsetai.Errors;
|
||||
}
|
||||
var entity = new AIA
|
||||
{
|
||||
CompanyID = dto.companyId,
|
||||
aiKeyUser = dto.aiKeyUser,
|
||||
Request = dto.requestText,
|
||||
Response = responeai,
|
||||
Cdatetime = DateTime.Now
|
||||
};
|
||||
var added = await _aiaRepository.ADD(entity);
|
||||
{
|
||||
CompanyID = dto.companyId,
|
||||
aiKeyUser = dto.aiKeyUser,
|
||||
Request = dto.requestText,
|
||||
Response = responeai,
|
||||
Cdatetime = DateTime.Now
|
||||
};
|
||||
var added = await _aiaRepository.ADD(entity);
|
||||
|
||||
if(sucessresponseai)
|
||||
response.Value = new() { dateTime=added.Cdatetime,responseText=added.Response,requestText=added.Request};
|
||||
response.Success = sucessresponseai;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
response.Errors.Add("خطا در ذخیره سازی");
|
||||
}
|
||||
return response;
|
||||
}
|
||||
if (sucessresponseai)
|
||||
response.Value = new() { dateTime = added.Cdatetime, responseText = added.Response, requestText = added.Request };
|
||||
response.Success = sucessresponseai;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
response.Errors.Add("خطا در ذخیره سازی");
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
public async Task<List<aiResponseDto>> GetCurrent(int companyId, string aiKeyUser)
|
||||
{
|
||||
return await _aiaRepository
|
||||
.Get()
|
||||
.Where(w => w.CompanyID == companyId && w.aiKeyUser == aiKeyUser && w.Cdatetime.AddHours(3) >= DateTime.Now)
|
||||
.OrderBy(o => o.ID)
|
||||
.Select(s=>new aiResponseDto() { responseText = s.Response, requestText = s.Request,dateTime=s.Cdatetime})
|
||||
.ToListAsync();
|
||||
}
|
||||
public async Task<List<aiResponseDto>> GetCurrent(int companyId, string aiKeyUser)
|
||||
{
|
||||
return await _aiaRepository
|
||||
.Get()
|
||||
.Where(w => w.CompanyID == companyId && w.aiKeyUser == aiKeyUser && w.Cdatetime.AddHours(3) >= DateTime.Now)
|
||||
.OrderBy(o => o.ID)
|
||||
.Select(s => new aiResponseDto() { responseText = s.Response, requestText = s.Request, dateTime = s.Cdatetime })
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
//public async Task<bool> DeleteEntry(int id, int companyId)
|
||||
//{
|
||||
// var entity = await _aiaRepository.Get().FirstOrDefaultAsync(f => f.ID == id && f.CompanyID == companyId);
|
||||
// if (entity == null) return false;
|
||||
// return await _aiaRepository.DELETE(entity);
|
||||
//}
|
||||
}
|
||||
//public async Task<bool> DeleteEntry(int id, int companyId)
|
||||
//{
|
||||
// var entity = await _aiaRepository.Get().FirstOrDefaultAsync(f => f.ID == id && f.CompanyID == companyId);
|
||||
// if (entity == null) return false;
|
||||
// return await _aiaRepository.DELETE(entity);
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user