Files
Hushian/Presentation/AIAss/Services/aiAssistanceService.cs

117 lines
4.2 KiB
C#
Raw Normal View History

2025-08-26 17:33:05 +03:30
using Grpc.Core;
using AIAss;
using AIAss.Protos;
2025-08-28 14:56:41 +03:30
using System.Net.Http.Headers;
using System.Text.Json;
using System.Text;
2025-09-01 19:46:37 +03:30
using Microsoft.Extensions.Configuration;
2025-08-26 17:33:05 +03:30
namespace AIAss.Services;
2025-09-01 19:46:37 +03:30
public class aiAssistanceService : aiAssistance.aiAssistanceBase
2025-08-26 17:33:05 +03:30
{
2025-09-01 19:46:37 +03:30
private readonly string Apitoken;
private readonly string url;
public aiAssistanceService(IConfiguration configuration)
{
url = configuration["aiSettings:url"];
Apitoken = configuration["aiSettings:apitoken"];
}
2025-08-28 14:56:41 +03:30
public async override Task<aiaReply> SendQuestion(aiaRequest request, ServerCallContext context)
2025-08-26 17:33:05 +03:30
{
2025-08-28 14:56:41 +03:30
using var client = new HttpClient();
client.DefaultRequestHeaders.Authorization =
2025-09-01 19:46:37 +03:30
new AuthenticationHeaderValue("Bearer", Apitoken);
2025-08-28 14:56:41 +03:30
var content = new
2025-08-26 17:33:05 +03:30
{
2025-09-01 19:46:37 +03:30
model = request.Model,
2025-08-28 14:56:41 +03:30
messages = new[]
{
new { role = "system", content = "شما یک دستیار پاسخگو به سوالات هستید." },
new { role = "user", content = $"با توجه به این متن:{request.Prompts}به این سوال پاسخ بده:{ request.Question}" },
new { role = "system", content = "به سوالات غیره متن بالا پاسخ نده و بگو در این زمینه اطلاعی ندارم" }
}
2025-08-26 17:33:05 +03:30
};
2025-09-01 19:46:37 +03:30
var response = await client.PostAsync(url,
2025-08-28 14:56:41 +03:30
new StringContent(JsonSerializer.Serialize(content), Encoding.UTF8, "application/json"));
string reponse = "";
if (response.IsSuccessStatusCode)
{
var json = await response.Content.ReadAsStringAsync();
using var doc = JsonDocument.Parse(json);
reponse = doc.RootElement.GetProperty("choices")[0].GetProperty("message").GetProperty("content").GetString();
}
else
{
2025-09-01 19:46:37 +03:30
reponse = "خطا در ارتباط سرور ai";
2025-08-28 14:56:41 +03:30
}
2025-08-26 17:33:05 +03:30
2025-08-29 19:01:13 +03:30
return new aiaReply
{
Message = $" {reponse}"
};
}
public async override Task<aiaReply> imageAnalize(aiaRequest request, ServerCallContext context)
{
2025-09-01 19:46:37 +03:30
2025-08-29 19:01:13 +03:30
using var client = new HttpClient();
client.DefaultRequestHeaders.Authorization =
2025-09-01 19:46:37 +03:30
new AuthenticationHeaderValue("Bearer", Apitoken);
2025-08-29 19:01:13 +03:30
var content = new
{
model = request.Model,
messages = new object[]
2025-09-01 19:46:37 +03:30
{
new {
role = "system",
content = "شما دستیار برای توصیف آزمایشات هستید."
},
new {
role = "user",
content = new object[]
2025-08-29 19:01:13 +03:30
{
2025-09-01 19:46:37 +03:30
new { type = "text", text = "این تصویر اگر آزمایش انسان است توصیف کن در غیر اینصورت پاسخ نده" },
2025-08-29 19:01:13 +03:30
new {
2025-09-01 19:46:37 +03:30
type = "image_url",
image_url = new {
url = $"data:image/jpeg;base64,{request.Question}"
2025-08-29 19:01:13 +03:30
}
}
}
2025-09-01 19:46:37 +03:30
}
}
2025-08-29 19:01:13 +03:30
};
2025-09-01 19:46:37 +03:30
var response = await client.PostAsync(url,
2025-08-29 19:01:13 +03:30
new StringContent(JsonSerializer.Serialize(content), Encoding.UTF8, "application/json"));
string reponse = "";
if (response.IsSuccessStatusCode)
{
var json = await response.Content.ReadAsStringAsync();
using var doc = JsonDocument.Parse(json);
reponse = doc.RootElement.GetProperty("choices")[0].GetProperty("message").GetProperty("content").GetString();
2025-09-01 19:46:37 +03:30
context.Status = new Status(StatusCode.OK,"is Success");
2025-08-29 19:01:13 +03:30
}
else
{
reponse = "خطا در ارتباط سرور ai";
2025-09-01 19:46:37 +03:30
if (response.StatusCode==System.Net.HttpStatusCode.TooManyRequests)
{
throw new RpcException(new Status(StatusCode.Cancelled, "ترافیک بالای شبکه در زمان دیگری تلاش کنید"));
}
else
throw new RpcException(new Status(StatusCode.Cancelled, "خطا در ارتباط سرور"));
2025-08-29 19:01:13 +03:30
}
2025-08-28 14:56:41 +03:30
return new aiaReply
{
Message = $" {reponse}"
};
2025-08-26 17:33:05 +03:30
}
}