...
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
using Common.Contracts.Infrastructure;
|
||||
using Hushian.Application.Contracts;
|
||||
using Hushian.Application.Models.aia;
|
||||
using Hushian.Application.Models.Message;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
@@ -17,8 +19,8 @@ namespace Hushian.Infrastructure
|
||||
services.AddTransient<IMessageSender, MessageSender>();
|
||||
|
||||
|
||||
//services.Configure<aiSetting>(configuration.GetSection("aiSettings"));
|
||||
//services.AddTransient<IOpenai, OpenaiService>();
|
||||
services.Configure<aiSetting>(configuration.GetSection("aiSettings"));
|
||||
services.AddTransient<Iaiass, openAI>();
|
||||
|
||||
services.AddScoped(c => new Melipayamak.RestClient(configuration.GetSection("MessageSettings:UserName").Value, configuration.GetSection("MessageSettings:Password").Value));
|
||||
return services;
|
||||
|
65
Infrastructure/Infrastructure/openAI.cs
Normal file
65
Infrastructure/Infrastructure/openAI.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user