This commit is contained in:
mmrbnjd
2025-08-18 17:30:02 +03:30
parent 067607d98a
commit 561f58784c
15 changed files with 588 additions and 282 deletions

View 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);
}
}

View 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;
}
}
}

View 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; }
}
}

View File

@@ -1,5 +1,6 @@
using AutoMapper; using AutoMapper;
using Common.Dtos; using Common.Dtos;
using Hushian.Application.Contracts;
using Hushian.Application.Contracts.Persistence; using Hushian.Application.Contracts.Persistence;
using Hushian.Application.Models; using Hushian.Application.Models;
using Hushian.Domain.Entites; using Hushian.Domain.Entites;
@@ -11,12 +12,15 @@ namespace Hushian.Application.Services
public class AIService public class AIService
{ {
private readonly IGenericRepository<AIA> _aiaRepository; private readonly IGenericRepository<AIA> _aiaRepository;
private readonly IMapper _mapper; private readonly IGenericRepository<Prompt> _promprtRepository;
private readonly Iaiass _aiass;
public AIService(IGenericRepository<AIA> aiaRepository, IMapper mapper)
public AIService(IGenericRepository<AIA> aiaRepository, Iaiass aiass, IGenericRepository<Prompt> promprtRepository)
{ {
_aiaRepository = aiaRepository; _aiaRepository = aiaRepository;
_mapper = mapper; _aiass = aiass;
_promprtRepository = promprtRepository;
} }
public async Task<ResponseBase<aiResponseDto>> NewRequest public async Task<ResponseBase<aiResponseDto>> NewRequest
@@ -25,10 +29,21 @@ namespace Hushian.Application.Services
ResponseBase<aiResponseDto> response = new(); ResponseBase<aiResponseDto> response = new();
try try
{ {
string responeai="همین جوری"; string responeai = "";
bool sucessresponseai =!false; 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 var entity = new AIA
{ {
CompanyID = dto.companyId, CompanyID = dto.companyId,

View File

@@ -21,6 +21,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hushian.WebApi", "Presentat
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HushianWebApp", "Presentation\HushianWebApp\HushianWebApp.csproj", "{80D865DC-1CCD-9C25-5DAF-153E7B33408E}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HushianWebApp", "Presentation\HushianWebApp\HushianWebApp.csproj", "{80D865DC-1CCD-9C25-5DAF-153E7B33408E}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AIAss", "Presentation\AIAss\AIAss.csproj", "{AF6AE286-63CF-4A8A-A0B4-DBDA047FC9AE}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@@ -55,6 +57,10 @@ Global
{80D865DC-1CCD-9C25-5DAF-153E7B33408E}.Debug|Any CPU.Build.0 = Debug|Any CPU {80D865DC-1CCD-9C25-5DAF-153E7B33408E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{80D865DC-1CCD-9C25-5DAF-153E7B33408E}.Release|Any CPU.ActiveCfg = Release|Any CPU {80D865DC-1CCD-9C25-5DAF-153E7B33408E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{80D865DC-1CCD-9C25-5DAF-153E7B33408E}.Release|Any CPU.Build.0 = Release|Any CPU {80D865DC-1CCD-9C25-5DAF-153E7B33408E}.Release|Any CPU.Build.0 = Release|Any CPU
{AF6AE286-63CF-4A8A-A0B4-DBDA047FC9AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AF6AE286-63CF-4A8A-A0B4-DBDA047FC9AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AF6AE286-63CF-4A8A-A0B4-DBDA047FC9AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AF6AE286-63CF-4A8A-A0B4-DBDA047FC9AE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@@ -64,6 +70,7 @@ Global
{05D292C2-BB17-4524-B1F2-8A2B6B213C6A} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8} {05D292C2-BB17-4524-B1F2-8A2B6B213C6A} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
{37AD460D-1663-4755-AC15-703BFFBF20D2} = {F9F2A965-B4E4-4990-B547-F18200AF2631} {37AD460D-1663-4755-AC15-703BFFBF20D2} = {F9F2A965-B4E4-4990-B547-F18200AF2631}
{80D865DC-1CCD-9C25-5DAF-153E7B33408E} = {F9F2A965-B4E4-4990-B547-F18200AF2631} {80D865DC-1CCD-9C25-5DAF-153E7B33408E} = {F9F2A965-B4E4-4990-B547-F18200AF2631}
{AF6AE286-63CF-4A8A-A0B4-DBDA047FC9AE} = {F9F2A965-B4E4-4990-B547-F18200AF2631}
EndGlobalSection EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F0E59B62-9EDF-47DC-AAFD-F841443D0AAE} SolutionGuid = {F0E59B62-9EDF-47DC-AAFD-F841443D0AAE}

View File

@@ -1,4 +1,6 @@
using Common.Contracts.Infrastructure; using Common.Contracts.Infrastructure;
using Hushian.Application.Contracts;
using Hushian.Application.Models.aia;
using Hushian.Application.Models.Message; using Hushian.Application.Models.Message;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@@ -17,8 +19,8 @@ namespace Hushian.Infrastructure
services.AddTransient<IMessageSender, MessageSender>(); services.AddTransient<IMessageSender, MessageSender>();
//services.Configure<aiSetting>(configuration.GetSection("aiSettings")); services.Configure<aiSetting>(configuration.GetSection("aiSettings"));
//services.AddTransient<IOpenai, OpenaiService>(); services.AddTransient<Iaiass, openAI>();
services.AddScoped(c => new Melipayamak.RestClient(configuration.GetSection("MessageSettings:UserName").Value, configuration.GetSection("MessageSettings:Password").Value)); services.AddScoped(c => new Melipayamak.RestClient(configuration.GetSection("MessageSettings:UserName").Value, configuration.GetSection("MessageSettings:Password").Value));
return services; return services;

View File

@@ -0,0 +1,65 @@
using Hushian.Application.Contracts;
using Hushian.Application.Models;
using Hushian.Application.Models.aia;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace Hushian.Infrastructure
{
public class openAI : Iaiass
{
const string model = "gpt-4o-mini";
private aiSetting _aiSettings;
public openAI(IOptions<aiSetting> aiSettings)
{
_aiSettings = aiSettings.Value;
}
public async Task<ResponseBase<string>> SendQuestion(aiRequestModel Request)
{
var Response = new ResponseBase<string>();
try
{
using var client = new HttpClient();
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", _aiSettings.apitoken);
var content = new
{
model = model,
messages = new[]
{
new { role = "system", content = "شما یک دستیار پاسخگو به سوالات هستید." },
new { role = "user", content = $"با توجه به این متن:{Request.ToString()}به این سوال پاسخ بده:{ Request.question}" },
new { role = "system", content = "به سوالات غیره متن بالا پاسخ نده و بگو در این زمینه اطلاعی ندارم" }
}
};
var response = await client.PostAsync(_aiSettings.url,
new StringContent(JsonSerializer.Serialize(content), Encoding.UTF8, "application/json"));
if (response.IsSuccessStatusCode)
{
var json = await response.Content.ReadAsStringAsync();
using var doc = JsonDocument.Parse(json);
Response.Value = doc.RootElement.GetProperty("choices")[0].GetProperty("message").GetProperty("content").GetString();
Response.Success = true;
}
else
{
Response.Errors.Add("خطا در ارتباط سرور ai");
}
}
catch (Exception ex)
{
Response.Errors.Add("خطا مدیریت نشده در ارتباط سرور ai");
}
return Response;
}
}
}

View File

@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<Protobuf Include="Protos\greet.proto" GrpcServices="Server" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Grpc.AspNetCore" Version="2.64.0" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,14 @@
using AIAss.Services;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddGrpc();
var app = builder.Build();
// Configure the HTTP request pipeline.
app.MapGrpcService<GreeterService>();
app.MapGet("/", () => "Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909");
app.Run();

View File

@@ -0,0 +1,23 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "http://localhost:5010",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "https://localhost:7015;http://localhost:5010",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@@ -0,0 +1,21 @@
syntax = "proto3";
option csharp_namespace = "AIAss";
package greet;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply);
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings.
message HelloReply {
string message = 1;
}

View File

@@ -0,0 +1,21 @@
using Grpc.Core;
using AIAss;
namespace AIAss.Services;
public class GreeterService : Greeter.GreeterBase
{
private readonly ILogger<GreeterService> _logger;
public GreeterService(ILogger<GreeterService> logger)
{
_logger = logger;
}
public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
{
return Task.FromResult(new HelloReply
{
Message = "Hello " + request.Name
});
}
}

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@@ -0,0 +1,14 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"Kestrel": {
"EndpointDefaults": {
"Protocols": "Http2"
}
}
}

View File

@@ -13,11 +13,10 @@
@using Microsoft.AspNetCore.Components.Web @using Microsoft.AspNetCore.Components.Web
@inject CompanyService companyService @inject CompanyService companyService
@inject ToastService toastService @inject ToastService toastService
<PageTitle>گفتگو با دستیار هوشمند @CompanyInfo.FullName</PageTitle>
<div class="container-fluid"> <div class="container-fluid">
<div class="row" style="height:85vh"> <div class="row" style="height:85vh">
@if (isReady) @if (isReady)
{
@if (isLogin)
{ {
<div class="col-md-12 d-flex flex-column" style="margin-top:10px"> <div class="col-md-12 d-flex flex-column" style="margin-top:10px">
<div class="input-group"> <div class="input-group">
@@ -54,30 +53,36 @@
</div> </div>
<div class="message-input-container mt-2"> <div class="message-input-container mt-2">
<div class="input-wrapper"> <div class="input-wrapper">
<input type="text" @bind-value="inputText" class="message-input" placeholder="متن خود را بنویسید..." @onkeydown="HandleKeyDown" /> <input type="text" @bind-value="inputText" disabled="@disSend" class="message-input" placeholder="متن خود را بنویسید..." @onkeydown="HandleKeyDown" />
<Button Color="ButtonColor.Primary" Size=ButtonSize.Small @onclick="Send" class="send-btn" title="ارسال"> <Button Color="ButtonColor.Primary" Size=ButtonSize.Small @onclick="Send" class="send-btn" title="ارسال">
@if (!disSend)
{
<Icon Name="IconName.Send" /> <Icon Name="IconName.Send" />
}
else
{
<Spinner Type="SpinnerType.Grow" Color="SpinnerColor.Primary" />
}
</Button> </Button>
</div> </div>
</div> </div>
</div> </div>
} }
else else
{ {
<div class="d-flex justify-content-center align-items-center" style="height:100%"> <div class="d-flex justify-content-center align-items-center" style="height:100%">
<div class="text-center"> <div class="text-center">
<Spinner Type="SpinnerType.Dots" Color="SpinnerColor.Primary" /> @if (isError)
<p class="mt-3 text-muted">نیاز به ورود دارید</p> {
</div> <p class="mt-3 text-muted" style="color:orangered">@msgError</p>
</div>
}
} }
else else
{ {
<div class="d-flex justify-content-center align-items-center" style="height:100%">
<div class="text-center">
<Spinner Type="SpinnerType.Dots" Color="SpinnerColor.Primary" /> <Spinner Type="SpinnerType.Dots" Color="SpinnerColor.Primary" />
<p class="mt-3 text-muted">در حال بارگذاری ...</p> <p class="mt-3 text-muted">در حال بارگذاری ...</p>
}
</div> </div>
</div> </div>
} }
@@ -89,22 +94,24 @@
[Parameter] public int CompanyID { get; set; } [Parameter] public int CompanyID { get; set; }
List<aiResponseDto> messages = new(); List<aiResponseDto> messages = new();
string inputText = string.Empty; string inputText = string.Empty;
bool isLogin = false;
bool isReady = false; bool isReady = false;
public string aikeyUser { get; set; } bool isError= false;
string msgError= "";
public string aikeyUser { get; set; } = "";
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
await EnsureAuth(); await EnsureAuth();
if (isLogin)
{
CompanyInfo = await companyService.GetCompany(CompanyID); CompanyInfo = await companyService.GetCompany(CompanyID);
if (CompanyInfo != null) if (CompanyInfo != null && CompanyInfo.allowBot)
{
await LoadMessages(); await LoadMessages();
isReady = true;
}
else else
{ {
toastService.Notify(new ToastMessage(ToastType.Danger, "شناسه شرکت صحیح نمی باشد")); isError = true;
msgError = "دستیار هوشمند یافت نشد";
}
} }
} }
@@ -116,8 +123,7 @@
aikeyUser = Guid.NewGuid().ToString(); aikeyUser = Guid.NewGuid().ToString();
await localStorageService.SetItem("aikeyUser", aikeyUser); await localStorageService.SetItem("aikeyUser", aikeyUser);
} }
isLogin = true;
isReady = true;
} }
private async Task LoadMessages() private async Task LoadMessages()
@@ -126,11 +132,11 @@
StateHasChanged(); StateHasChanged();
await JS.InvokeVoidAsync("scrollToBottom", "ai-chat"); await JS.InvokeVoidAsync("scrollToBottom", "ai-chat");
} }
bool disSend = false;
private async Task Send() private async Task Send()
{ {
if (string.IsNullOrWhiteSpace(inputText)) return; if (string.IsNullOrWhiteSpace(inputText)) return;
disSend = true;
var dto = new aiNewResponseDto { companyId = CompanyID, aiKeyUser = aikeyUser, requestText = inputText }; var dto = new aiNewResponseDto { companyId = CompanyID, aiKeyUser = aikeyUser, requestText = inputText };
var okmodel = await conversationService.AiNewResponse(dto); var okmodel = await conversationService.AiNewResponse(dto);
if (okmodel != null) if (okmodel != null)
@@ -144,6 +150,7 @@
{ {
toastService.Notify(new ToastMessage(ToastType.Danger, "ارسال ناموفق بود")); toastService.Notify(new ToastMessage(ToastType.Danger, "ارسال ناموفق بود"));
} }
disSend = false;
} }
private async Task HandleKeyDown(KeyboardEventArgs e) private async Task HandleKeyDown(KeyboardEventArgs e)
@@ -154,7 +161,6 @@
private async Task Logout() private async Task Logout()
{ {
await localStorageService.RemoveItem("aiKeyUser"); await localStorageService.RemoveItem("aiKeyUser");
isLogin = false;
} }
} }
@@ -171,6 +177,7 @@
position: relative; position: relative;
backdrop-filter: blur(10px); backdrop-filter: blur(10px);
} }
.chat-bubble { .chat-bubble {
padding: 0.75rem 1rem; padding: 0.75rem 1rem;
border-radius: 18px; border-radius: 18px;
@@ -183,6 +190,7 @@
transition: all 0.3s ease; transition: all 0.3s ease;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1); box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
} }
.chat-other { .chat-other {
background: linear-gradient(135deg, #ffffff 0%, #f8f9fa 100%); background: linear-gradient(135deg, #ffffff 0%, #f8f9fa 100%);
color: #333; color: #333;
@@ -190,6 +198,7 @@
border: 1px solid #e9ecef; border: 1px solid #e9ecef;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08); box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
} }
.chat-ai { .chat-ai {
background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%); background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
color: #495057; color: #495057;
@@ -197,10 +206,43 @@
border: 1px solid #dee2e6; border: 1px solid #dee2e6;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08); box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
} }
.message-input-container { border-radius: 0.5rem; background-color: #f8f9fa; padding: .5rem; }
.input-wrapper { display:flex; align-items:center; gap:.75rem; } .message-input-container {
.message-input { flex:1; border:none; background:transparent; padding:.5rem .75rem; font-size:.95rem; color:#495057; outline:none; border-radius:20px; } border-radius: 0.5rem;
.send-btn { border-radius:50%; width:38px; height:38px; padding:0; display:flex; align-items:center; justify-content:center; background:linear-gradient(135deg,#0d6efd 0%,#0b5ed7 100%); border:none; box-shadow:0 4px 12px rgba(13,110,253,.3); color:white; } background-color: #f8f9fa;
padding: .5rem;
}
.input-wrapper {
display: flex;
align-items: center;
gap: .75rem;
}
.message-input {
flex: 1;
border: none;
background: transparent;
padding: .5rem .75rem;
font-size: .95rem;
color: #495057;
outline: none;
border-radius: 20px;
}
.send-btn {
border-radius: 50%;
width: 38px;
height: 38px;
padding: 0;
display: flex;
align-items: center;
justify-content: center;
background: linear-gradient(135deg,#0d6efd 0%,#0b5ed7 100%);
border: none;
box-shadow: 0 4px 12px rgba(13,110,253,.3);
color: white;
}
.logout-btn { .logout-btn {
border-radius: 20px; border-radius: 20px;