...
This commit is contained in:
@@ -1,19 +1,47 @@
|
||||
using Grpc.Core;
|
||||
using AIAss;
|
||||
using AIAss.Protos;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text.Json;
|
||||
using System.Text;
|
||||
|
||||
namespace AIAss.Services;
|
||||
|
||||
public class aiAssistanceService: aiAssistance.aiAssistanceBase
|
||||
{
|
||||
public override Task<aiaReply> SendQuestion(aiaRequest request, ServerCallContext context)
|
||||
public async override Task<aiaReply> SendQuestion(aiaRequest request, ServerCallContext context)
|
||||
{
|
||||
var reply = new aiaReply
|
||||
using var client = new HttpClient();
|
||||
client.DefaultRequestHeaders.Authorization =
|
||||
new AuthenticationHeaderValue("Bearer", request.Apitoken);
|
||||
var content = new
|
||||
{
|
||||
Message = $" {request.Question}"
|
||||
model= request.Model,
|
||||
messages = new[]
|
||||
{
|
||||
new { role = "system", content = "شما یک دستیار پاسخگو به سوالات هستید." },
|
||||
new { role = "user", content = $"با توجه به این متن:{request.Prompts}به این سوال پاسخ بده:{ request.Question}" },
|
||||
new { role = "system", content = "به سوالات غیره متن بالا پاسخ نده و بگو در این زمینه اطلاعی ندارم" }
|
||||
}
|
||||
};
|
||||
var response = await client.PostAsync(request.Url,
|
||||
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
|
||||
{
|
||||
reponse="خطا در ارتباط سرور ai";
|
||||
}
|
||||
|
||||
return Task.FromResult(reply);
|
||||
return new aiaReply
|
||||
{
|
||||
Message = $" {reponse}"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user