From 8cf260a6d2bfebd642c5d8c5efc31c8bdb08aa8f Mon Sep 17 00:00:00 2001 From: Loretta Date: Thu, 23 Oct 2025 13:19:31 +0200 Subject: [PATCH] AI fix --- .../Admin/Components/_WelcomeMessage.cshtml | 83 ++++++++++--------- .../Services/AICalculationService.cs | 4 +- .../Services/IAIAPIService.cs | 2 +- .../Services/OpenAIApiService.cs | 45 +++++----- 4 files changed, 73 insertions(+), 61 deletions(-) diff --git a/Nop.Plugin.Misc.AIPlugin/Areas/Admin/Components/_WelcomeMessage.cshtml b/Nop.Plugin.Misc.AIPlugin/Areas/Admin/Components/_WelcomeMessage.cshtml index ce9be29..f1ce668 100644 --- a/Nop.Plugin.Misc.AIPlugin/Areas/Admin/Components/_WelcomeMessage.cshtml +++ b/Nop.Plugin.Misc.AIPlugin/Areas/Admin/Components/_WelcomeMessage.cshtml @@ -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
-
-

- - Üdvözöljük a Fruit Bank rendszerben! -

-
-
-
-
- @{ - Customer customer = await workContext.GetCurrentCustomerAsync(); - var WelcomeMessage = await aiCalculationService.GetWelcomeMessageAsync(customer); - var email = customer.Email; -

Ssytem check

-

- @WelcomeMessage - -

-

- Itt kezelheti az összes terméket, rendelést és ügyfelet egyszerűen és hatékonyan. -

- } -

- Mai dátum: @DateTime.Now.ToString("yyyy. MMMM dd., dddd", new System.Globalization.CultureInfo("hu-HU")) -

- @* +
+

+ + Üdvözöljük a Fruit Bank rendszerben! +

+
+
+
+
+ @{ + Customer customer = await workContext.GetCurrentCustomerAsync(); + + var welcomeMessage = await aiCalculationService.GetWelcomeMessageAsync(customer); + if (welcomeMessage.IsNullOrWhiteSpace()) + { +

Nincs kapcsolat az AI szerverrel...

+ } + else + { + var email = customer.Email; +

Ssytem check

+

@welcomeMessage

+ } + } +

+ Mai dátum: @DateTime.Now.ToString("yyyy. MMMM dd., dddd", new System.Globalization.CultureInfo("hu-HU")) +

+ @*
@@ -42,17 +45,17 @@

Megbízható kiszállítás országszerte

*@ -
-
-
-
Mai összefoglaló
-
    -
  • Bejelentkezés ideje: @DateTime.Now.ToString("HH:mm")
  • -
  • Aktív napok: @DateTime.Now.DayOfYear
  • -
  • Szép napot kívánunk!
  • -
-
-
-
-
+
+
+
+
Mai összefoglaló
+
    +
  • Bejelentkezés ideje: @DateTime.Now.ToString("HH:mm")
  • +
  • Aktív napok: @DateTime.Now.DayOfYear
  • +
  • Szép napot kívánunk!
  • +
+
+
+
+
\ No newline at end of file diff --git a/Nop.Plugin.Misc.AIPlugin/Services/AICalculationService.cs b/Nop.Plugin.Misc.AIPlugin/Services/AICalculationService.cs index f5e65f8..749645f 100644 --- a/Nop.Plugin.Misc.AIPlugin/Services/AICalculationService.cs +++ b/Nop.Plugin.Misc.AIPlugin/Services/AICalculationService.cs @@ -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; diff --git a/Nop.Plugin.Misc.AIPlugin/Services/IAIAPIService.cs b/Nop.Plugin.Misc.AIPlugin/Services/IAIAPIService.cs index 986d3a3..0b56cfd 100644 --- a/Nop.Plugin.Misc.AIPlugin/Services/IAIAPIService.cs +++ b/Nop.Plugin.Misc.AIPlugin/Services/IAIAPIService.cs @@ -8,7 +8,7 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Services { internal interface IAIAPIService { - Task GetSimpleResponseAsync(string systemMessage, string userMessage, string? assistantMessage = null); + Task GetSimpleResponseAsync(string systemMessage, string userMessage, string? assistantMessage = null); Task GetStreamedResponseAsync(string sessionId, string systemMessage, string userMessage, string? assistantMessage = null); string GetApiKey(); diff --git a/Nop.Plugin.Misc.AIPlugin/Services/OpenAIApiService.cs b/Nop.Plugin.Misc.AIPlugin/Services/OpenAIApiService.cs index 7e01266..03c6596 100644 --- a/Nop.Plugin.Misc.AIPlugin/Services/OpenAIApiService.cs +++ b/Nop.Plugin.Misc.AIPlugin/Services/OpenAIApiService.cs @@ -45,7 +45,7 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Services } #region === CHAT (TEXT INPUT) === - public async Task GetSimpleResponseAsync(string systemMessage, string userMessage, string? assistantMessage = null) + public async Task 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