This commit is contained in:
Loretta 2025-10-23 13:19:31 +02:00
parent 41422340c8
commit 8cf260a6d2
4 changed files with 73 additions and 61 deletions

View File

@ -1,4 +1,5 @@
@using Nop.Core; @using AyCode.Utils.Extensions
@using Nop.Core;
@using Nop.Plugin.Misc.FruitBankPlugin.Services @using Nop.Plugin.Misc.FruitBankPlugin.Services
@using Nop.Core.Domain.Customers @using Nop.Core.Domain.Customers
@inject IWorkContext workContext @inject IWorkContext workContext
@ -16,16 +17,18 @@
<div class="col-md-8"> <div class="col-md-8">
@{ @{
Customer customer = await workContext.GetCurrentCustomerAsync(); Customer customer = await workContext.GetCurrentCustomerAsync();
var WelcomeMessage = await aiCalculationService.GetWelcomeMessageAsync(customer);
var welcomeMessage = await aiCalculationService.GetWelcomeMessageAsync(customer);
if (welcomeMessage.IsNullOrWhiteSpace())
{
<p class="lead">Nincs kapcsolat az AI szerverrel...</p>
}
else
{
var email = customer.Email; var email = customer.Email;
<h4>Ssytem check</h4> <h4>Ssytem check</h4>
<p class="lead"> <p class="lead">@welcomeMessage</p>
@WelcomeMessage }
</p>
<p class="lead">
Itt kezelheti az összes terméket, rendelést és ügyfelet egyszerűen és hatékonyan.
</p>
} }
<p> <p>
Mai dátum: <strong>@DateTime.Now.ToString("yyyy. MMMM dd., dddd", new System.Globalization.CultureInfo("hu-HU"))</strong> Mai dátum: <strong>@DateTime.Now.ToString("yyyy. MMMM dd., dddd", new System.Globalization.CultureInfo("hu-HU"))</strong>

View File

@ -25,7 +25,7 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Services
string userMessage = "Hello"; string userMessage = "Hello";
var response = await _openAIApiService.GetSimpleResponseAsync(systemMessage, userMessage); var response = await _openAIApiService.GetSimpleResponseAsync(systemMessage, userMessage) ?? string.Empty;
return response; 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}"; 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); var response = await _openAIApiService.GetSimpleResponseAsync(systemMessage, userQuestion);
if (response == null) return string.Empty;
var fixedResponse = TextHelper.FixJsonWithoutAI(response); var fixedResponse = TextHelper.FixJsonWithoutAI(response);
return fixedResponse; return fixedResponse;

View File

@ -8,7 +8,7 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Services
{ {
internal interface IAIAPIService 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); Task<string> GetStreamedResponseAsync(string sessionId, string systemMessage, string userMessage, string? assistantMessage = null);
string GetApiKey(); string GetApiKey();

View File

@ -45,7 +45,7 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Services
} }
#region === CHAT (TEXT INPUT) === #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(); string modelName = GetModelName();
StringContent requestContent = new(""); StringContent requestContent = new("");
@ -109,10 +109,13 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Services
requestContent = new StringContent(requestJson, Encoding.UTF8, "application/json"); requestContent = new StringContent(requestJson, Encoding.UTF8, "application/json");
} }
try
{
using var response = await _httpClient.PostAsync(OpenAiEndpoint, requestContent); using var response = await _httpClient.PostAsync(OpenAiEndpoint, requestContent);
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
using var responseStream = await response.Content.ReadAsStreamAsync(); await using var responseStream = await response.Content.ReadAsStreamAsync();
using var document = await JsonDocument.ParseAsync(responseStream); using var document = await JsonDocument.ParseAsync(responseStream);
var inputTokens = document.RootElement.GetProperty("usage").GetProperty("prompt_tokens").GetInt32(); var inputTokens = document.RootElement.GetProperty("usage").GetProperty("prompt_tokens").GetInt32();
@ -125,6 +128,12 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Services
.GetProperty("content") .GetProperty("content")
.GetString() ?? "No response"; .GetString() ?? "No response";
} }
catch (Exception ex)
{
Console.Error.WriteLine($"{ex}");
return null;
}
}
#endregion #endregion
#region === CHAT (STREAMING) === #region === CHAT (STREAMING) ===
@ -190,16 +199,14 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Services
requestContent = new StringContent(requestJson, Encoding.UTF8, "application/json"); requestContent = new StringContent(requestJson, Encoding.UTF8, "application/json");
} }
using var httpRequest = new HttpRequestMessage(HttpMethod.Post, OpenAiEndpoint) using var httpRequest = new HttpRequestMessage(HttpMethod.Post, OpenAiEndpoint);
{ httpRequest.Content = requestContent;
Content = requestContent
};
using var response = await _httpClient.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead); using var response = await _httpClient.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead);
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
var stringBuilder = new StringBuilder(); 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); using var reader = new StreamReader(responseStream);
try try
@ -277,7 +284,7 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Services
return null; return null;
} }
using var content = await response.Content.ReadAsStreamAsync(); await using var content = await response.Content.ReadAsStreamAsync();
var json = await JsonDocument.ParseAsync(content); var json = await JsonDocument.ParseAsync(content);
var base64Image = json.RootElement var base64Image = json.RootElement