AI fix
This commit is contained in:
parent
41422340c8
commit
8cf260a6d2
|
|
@ -1,36 +1,39 @@
|
|||
@using Nop.Core;
|
||||
@using AyCode.Utils.Extensions
|
||||
@using Nop.Core;
|
||||
@using Nop.Plugin.Misc.FruitBankPlugin.Services
|
||||
@using Nop.Core.Domain.Customers
|
||||
@inject IWorkContext workContext
|
||||
@inject AICalculationService aiCalculationService
|
||||
|
||||
<div class="card card-primary">
|
||||
<div class="card-header">
|
||||
<h3 class="card-title">
|
||||
<i class="fas fa-leaf"></i>
|
||||
Üdvözöljük a Fruit Bank rendszerben!
|
||||
</h3>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<div class="row">
|
||||
<div class="col-md-8">
|
||||
@{
|
||||
Customer customer = await workContext.GetCurrentCustomerAsync();
|
||||
var WelcomeMessage = await aiCalculationService.GetWelcomeMessageAsync(customer);
|
||||
var email = customer.Email;
|
||||
<h4>Ssytem check</h4>
|
||||
<p class="lead">
|
||||
@WelcomeMessage
|
||||
|
||||
</p>
|
||||
<p class="lead">
|
||||
Itt kezelheti az összes terméket, rendelést és ügyfelet egyszerűen és hatékonyan.
|
||||
</p>
|
||||
}
|
||||
<p>
|
||||
Mai dátum: <strong>@DateTime.Now.ToString("yyyy. MMMM dd., dddd", new System.Globalization.CultureInfo("hu-HU"))</strong>
|
||||
</p>
|
||||
@*
|
||||
<div class="card-header">
|
||||
<h3 class="card-title">
|
||||
<i class="fas fa-leaf"></i>
|
||||
Üdvözöljük a Fruit Bank rendszerben!
|
||||
</h3>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<div class="row">
|
||||
<div class="col-md-8">
|
||||
@{
|
||||
Customer customer = await workContext.GetCurrentCustomerAsync();
|
||||
|
||||
var welcomeMessage = await aiCalculationService.GetWelcomeMessageAsync(customer);
|
||||
if (welcomeMessage.IsNullOrWhiteSpace())
|
||||
{
|
||||
<p class="lead">Nincs kapcsolat az AI szerverrel...</p>
|
||||
}
|
||||
else
|
||||
{
|
||||
var email = customer.Email;
|
||||
<h4>Ssytem check</h4>
|
||||
<p class="lead">@welcomeMessage</p>
|
||||
}
|
||||
}
|
||||
<p>
|
||||
Mai dátum: <strong>@DateTime.Now.ToString("yyyy. MMMM dd., dddd", new System.Globalization.CultureInfo("hu-HU"))</strong>
|
||||
</p>
|
||||
@*
|
||||
<hr>
|
||||
<div class="row">
|
||||
<div class="col-sm-6">
|
||||
|
|
@ -42,17 +45,17 @@
|
|||
<p class="text-muted">Megbízható kiszállítás országszerte</p>
|
||||
</div>
|
||||
</div> *@
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<div class="bg-light p-3 rounded">
|
||||
<h5><i class="fas fa-chart-line text-warning"></i> Mai összefoglaló</h5>
|
||||
<ul class="list-unstyled">
|
||||
<li><i class="fas fa-clock text-info"></i> Bejelentkezés ideje: @DateTime.Now.ToString("HH:mm")</li>
|
||||
<li><i class="fas fa-calendar text-success"></i> Aktív napok: @DateTime.Now.DayOfYear</li>
|
||||
<li><i class="fas fa-sun text-warning"></i> Szép napot kívánunk!</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<div class="bg-light p-3 rounded">
|
||||
<h5><i class="fas fa-chart-line text-warning"></i> Mai összefoglaló</h5>
|
||||
<ul class="list-unstyled">
|
||||
<li><i class="fas fa-clock text-info"></i> Bejelentkezés ideje: @DateTime.Now.ToString("HH:mm")</li>
|
||||
<li><i class="fas fa-calendar text-success"></i> Aktív napok: @DateTime.Now.DayOfYear</li>
|
||||
<li><i class="fas fa-sun text-warning"></i> Szép napot kívánunk!</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
@ -25,7 +25,7 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Services
|
|||
|
||||
string userMessage = "Hello";
|
||||
|
||||
var response = await _openAIApiService.GetSimpleResponseAsync(systemMessage, userMessage);
|
||||
var response = await _openAIApiService.GetSimpleResponseAsync(systemMessage, userMessage) ?? string.Empty;
|
||||
return response;
|
||||
}
|
||||
|
||||
|
|
@ -34,6 +34,8 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Services
|
|||
string systemMessage = $"You are a pdf analyzis assistant, the user is asking you questions about a PDF document, that you have access to. The content of the PDF document is the following: {pdfText}";
|
||||
var response = await _openAIApiService.GetSimpleResponseAsync(systemMessage, userQuestion);
|
||||
|
||||
if (response == null) return string.Empty;
|
||||
|
||||
var fixedResponse = TextHelper.FixJsonWithoutAI(response);
|
||||
|
||||
return fixedResponse;
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Services
|
|||
{
|
||||
internal interface IAIAPIService
|
||||
{
|
||||
Task<string> GetSimpleResponseAsync(string systemMessage, string userMessage, string? assistantMessage = null);
|
||||
Task<string?> GetSimpleResponseAsync(string systemMessage, string userMessage, string? assistantMessage = null);
|
||||
Task<string> GetStreamedResponseAsync(string sessionId, string systemMessage, string userMessage, string? assistantMessage = null);
|
||||
|
||||
string GetApiKey();
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Services
|
|||
}
|
||||
|
||||
#region === CHAT (TEXT INPUT) ===
|
||||
public async Task<string> GetSimpleResponseAsync(string systemMessage, string userMessage, string? assistantMessage = null)
|
||||
public async Task<string?> GetSimpleResponseAsync(string systemMessage, string userMessage, string? assistantMessage = null)
|
||||
{
|
||||
string modelName = GetModelName();
|
||||
StringContent requestContent = new("");
|
||||
|
|
@ -109,21 +109,30 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Services
|
|||
requestContent = new StringContent(requestJson, Encoding.UTF8, "application/json");
|
||||
}
|
||||
|
||||
using var response = await _httpClient.PostAsync(OpenAiEndpoint, requestContent);
|
||||
response.EnsureSuccessStatusCode();
|
||||
try
|
||||
{
|
||||
using var response = await _httpClient.PostAsync(OpenAiEndpoint, requestContent);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
using var responseStream = await response.Content.ReadAsStreamAsync();
|
||||
using var document = await JsonDocument.ParseAsync(responseStream);
|
||||
await using var responseStream = await response.Content.ReadAsStreamAsync();
|
||||
|
||||
var inputTokens = document.RootElement.GetProperty("usage").GetProperty("prompt_tokens").GetInt32();
|
||||
var outputTokens = document.RootElement.GetProperty("usage").GetProperty("completion_tokens").GetInt32();
|
||||
Console.WriteLine($"USAGE STATS - Tokens: {inputTokens} + {outputTokens} = {inputTokens + outputTokens}");
|
||||
using var document = await JsonDocument.ParseAsync(responseStream);
|
||||
|
||||
return document.RootElement
|
||||
.GetProperty("choices")[0]
|
||||
.GetProperty("message")
|
||||
.GetProperty("content")
|
||||
.GetString() ?? "No response";
|
||||
var inputTokens = document.RootElement.GetProperty("usage").GetProperty("prompt_tokens").GetInt32();
|
||||
var outputTokens = document.RootElement.GetProperty("usage").GetProperty("completion_tokens").GetInt32();
|
||||
Console.WriteLine($"USAGE STATS - Tokens: {inputTokens} + {outputTokens} = {inputTokens + outputTokens}");
|
||||
|
||||
return document.RootElement
|
||||
.GetProperty("choices")[0]
|
||||
.GetProperty("message")
|
||||
.GetProperty("content")
|
||||
.GetString() ?? "No response";
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.Error.WriteLine($"{ex}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
|
@ -190,16 +199,14 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Services
|
|||
requestContent = new StringContent(requestJson, Encoding.UTF8, "application/json");
|
||||
}
|
||||
|
||||
using var httpRequest = new HttpRequestMessage(HttpMethod.Post, OpenAiEndpoint)
|
||||
{
|
||||
Content = requestContent
|
||||
};
|
||||
using var httpRequest = new HttpRequestMessage(HttpMethod.Post, OpenAiEndpoint);
|
||||
httpRequest.Content = requestContent;
|
||||
|
||||
using var response = await _httpClient.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var stringBuilder = new StringBuilder();
|
||||
using var responseStream = await response.Content.ReadAsStreamAsync();
|
||||
await using var responseStream = await response.Content.ReadAsStreamAsync();
|
||||
using var reader = new StreamReader(responseStream);
|
||||
|
||||
try
|
||||
|
|
@ -277,7 +284,7 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Services
|
|||
return null;
|
||||
}
|
||||
|
||||
using var content = await response.Content.ReadAsStreamAsync();
|
||||
await using var content = await response.Content.ReadAsStreamAsync();
|
||||
var json = await JsonDocument.ParseAsync(content);
|
||||
|
||||
var base64Image = json.RootElement
|
||||
|
|
|
|||
Loading…
Reference in New Issue