...
This commit is contained in:
@@ -4,19 +4,29 @@ using AIAss.Protos;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text.Json;
|
||||
using System.Text;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace AIAss.Services;
|
||||
|
||||
public class aiAssistanceService: aiAssistance.aiAssistanceBase
|
||||
public class aiAssistanceService : aiAssistance.aiAssistanceBase
|
||||
{
|
||||
private readonly string Apitoken;
|
||||
private readonly string url;
|
||||
public aiAssistanceService(IConfiguration configuration)
|
||||
{
|
||||
url = configuration["aiSettings:url"];
|
||||
Apitoken = configuration["aiSettings:apitoken"];
|
||||
|
||||
}
|
||||
|
||||
public async override Task<aiaReply> SendQuestion(aiaRequest request, ServerCallContext context)
|
||||
{
|
||||
using var client = new HttpClient();
|
||||
client.DefaultRequestHeaders.Authorization =
|
||||
new AuthenticationHeaderValue("Bearer", request.Apitoken);
|
||||
new AuthenticationHeaderValue("Bearer", Apitoken);
|
||||
var content = new
|
||||
{
|
||||
model= request.Model,
|
||||
model = request.Model,
|
||||
messages = new[]
|
||||
{
|
||||
new { role = "system", content = "شما یک دستیار پاسخگو به سوالات هستید." },
|
||||
@@ -24,51 +34,7 @@ namespace AIAss.Services;
|
||||
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 new aiaReply
|
||||
{
|
||||
Message = $" {reponse}"
|
||||
};
|
||||
}
|
||||
public async override Task<aiaReply> imageAnalize(aiaRequest request, ServerCallContext context)
|
||||
{
|
||||
using var client = new HttpClient();
|
||||
client.DefaultRequestHeaders.Authorization =
|
||||
new AuthenticationHeaderValue("Bearer", request.Apitoken);
|
||||
var content = new
|
||||
{
|
||||
model = request.Model,
|
||||
messages = new object[]
|
||||
{
|
||||
new {
|
||||
role = "user",
|
||||
content = new object[]
|
||||
{
|
||||
new { type = "text", text =request.Prompts},
|
||||
new {
|
||||
type = "image_url",
|
||||
image_url = new {
|
||||
url = $"data:image/jpeg;base64,{request.Question}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
var response = await client.PostAsync(request.Url,
|
||||
var response = await client.PostAsync(url,
|
||||
new StringContent(JsonSerializer.Serialize(content), Encoding.UTF8, "application/json"));
|
||||
string reponse = "";
|
||||
if (response.IsSuccessStatusCode)
|
||||
@@ -82,6 +48,65 @@ namespace AIAss.Services;
|
||||
reponse = "خطا در ارتباط سرور ai";
|
||||
}
|
||||
|
||||
return new aiaReply
|
||||
{
|
||||
Message = $" {reponse}"
|
||||
};
|
||||
}
|
||||
public async override Task<aiaReply> imageAnalize(aiaRequest request, ServerCallContext context)
|
||||
{
|
||||
|
||||
|
||||
using var client = new HttpClient();
|
||||
client.DefaultRequestHeaders.Authorization =
|
||||
new AuthenticationHeaderValue("Bearer", Apitoken);
|
||||
var content = new
|
||||
{
|
||||
model = request.Model,
|
||||
messages = new object[]
|
||||
{
|
||||
new {
|
||||
role = "system",
|
||||
content = "شما دستیار برای توصیف آزمایشات هستید."
|
||||
},
|
||||
new {
|
||||
role = "user",
|
||||
content = new object[]
|
||||
{
|
||||
new { type = "text", text = "این تصویر اگر آزمایش انسان است توصیف کن در غیر اینصورت پاسخ نده" },
|
||||
new {
|
||||
type = "image_url",
|
||||
image_url = new {
|
||||
url = $"data:image/jpeg;base64,{request.Question}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var response = await client.PostAsync(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();
|
||||
context.Status = new Status(StatusCode.OK,"is Success");
|
||||
}
|
||||
else
|
||||
{
|
||||
reponse = "خطا در ارتباط سرور ai";
|
||||
if (response.StatusCode==System.Net.HttpStatusCode.TooManyRequests)
|
||||
{
|
||||
throw new RpcException(new Status(StatusCode.Cancelled, "ترافیک بالای شبکه در زمان دیگری تلاش کنید"));
|
||||
|
||||
}
|
||||
else
|
||||
throw new RpcException(new Status(StatusCode.Cancelled, "خطا در ارتباط سرور"));
|
||||
}
|
||||
|
||||
return new aiaReply
|
||||
{
|
||||
Message = $" {reponse}"
|
||||
|
Reference in New Issue
Block a user