From b95f1ee3931198d31a4392720fd76c512bb0f445 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Sat, 7 Dec 2024 09:33:32 +0330 Subject: [PATCH] #453 ai/generate/poem/summaries --- RMuseum/Controllers/GanjoorController.cs | 19 +++- RMuseum/RMuseum.xml | 18 ++++ RMuseum/Services/IGanjoorService.cs | 11 ++- .../GanjoorService-OpenAI-CoupletSummaries.cs | 86 +++++++++++++++++++ .../GanjoorService-TajikSQLiteImport.cs | 4 +- 5 files changed, 131 insertions(+), 7 deletions(-) diff --git a/RMuseum/Controllers/GanjoorController.cs b/RMuseum/Controllers/GanjoorController.cs index 249754db..356ff519 100644 --- a/RMuseum/Controllers/GanjoorController.cs +++ b/RMuseum/Controllers/GanjoorController.cs @@ -287,6 +287,7 @@ namespace RMuseum.Controllers /// get poet image /// /// sample: hafez + /// /// [HttpGet("poet/image/{url}.gif")] [AllowAnonymous] @@ -4374,11 +4375,11 @@ namespace RMuseum.Controllers [Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + RMuseumSecurableItem.ImportOperationShortName)] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(bool))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] - public async Task TajikOneTimeImportFromSqlite() + public IActionResult TajikOneTimeImportFromSqlite() { RServiceResult res = - await _ganjoorService.TajikImportFromSqlite(0, "C:\\Tools\\import\\output.s3db"); + _ganjoorService.TajikImportFromSqlite(0, "C:\\Tools\\import\\output.s3db"); if (res.Result) return Ok(); return BadRequest(res.ExceptionString); @@ -4474,6 +4475,20 @@ namespace RMuseum.Controllers return Ok(); } + /// + /// fill poem summaries using open ai + /// + /// + [HttpPut("ai/generate/poem/summaries")] + [Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + SecurableItem.ModifyOperationShortName)] + [ProducesResponseType((int)HttpStatusCode.OK)] + [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] + public IActionResult OpenAIStartFillingPoemSummaries() + { + _ganjoorService.OpenAIStartFillingPoemSummaries(); + return Ok(); + } + diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index 6ab40c8d..f9437ffb 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -995,6 +995,7 @@ get poet image sample: hafez + @@ -2294,6 +2295,12 @@ + + + fill poem summaries using open ai + + + readonly mode @@ -16951,6 +16958,7 @@ tajik import from sqlite + @@ -16982,6 +16990,11 @@ fill couplet summaries using open ai + + + fill poem summaries using open ai + + translation service implementation @@ -18911,6 +18924,11 @@ fill couplet summaries using open ai + + + fill poem summaries using open ai + + category paper sources diff --git a/RMuseum/Services/IGanjoorService.cs b/RMuseum/Services/IGanjoorService.cs index 928717f3..b5ec9da1 100644 --- a/RMuseum/Services/IGanjoorService.cs +++ b/RMuseum/Services/IGanjoorService.cs @@ -1499,8 +1499,9 @@ namespace RMuseum.Services /// tajik import from sqlite /// /// + /// /// - Task> TajikImportFromSqlite(int poetId, string filePath); + RServiceResult TajikImportFromSqlite(int poetId, string filePath); /// /// tajik poets @@ -1532,7 +1533,13 @@ namespace RMuseum.Services /// /// fill couplet summaries using open ai /// - public void OpenAIStartFillingCoupletSummaries(); + void OpenAIStartFillingCoupletSummaries(); + + + /// + /// fill poem summaries using open ai + /// + void OpenAIStartFillingPoemSummaries(); } } diff --git a/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-OpenAI-CoupletSummaries.cs b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-OpenAI-CoupletSummaries.cs index de9ac3f8..494739e5 100644 --- a/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-OpenAI-CoupletSummaries.cs +++ b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-OpenAI-CoupletSummaries.cs @@ -112,6 +112,92 @@ namespace RMuseum.Services.Implementation } + await jobProgressServiceEF.UpdateJob(job.Id, 100, "", true); + } + catch (Exception exp) + { + await jobProgressServiceEF.UpdateJob(job.Id, 100, "", false, exp.ToString()); + } + + } + }); + + } + + + /// + /// fill poem summaries using open ai + /// + public void OpenAIStartFillingPoemSummaries() + { + _backgroundTaskQueue.QueueBackgroundWorkItem + ( + async token => + { + using (RMuseumDbContext context = new RMuseumDbContext(new DbContextOptions())) //this is long running job, so _context might be already been freed/collected by GC + { + LongRunningJobProgressServiceEF jobProgressServiceEF = new LongRunningJobProgressServiceEF(context); + var job = (await jobProgressServiceEF.NewJob($"OpenAIStartFillingPoemSummaries", "Open AI initialization")).Result; + try + { + + var openAiService = new OpenAIService(new OpenAIOptions() + { + ApiKey = Configuration["OpenAIAPIKey"], + BaseDomain = Configuration["OpenAIBaseUrl"] + }); + + await jobProgressServiceEF.UpdateJob(job.Id, 0, "Query data"); + + var poems = await context.GanjoorPoems.AsNoTracking() + .Where( + p => string.IsNullOrEmpty(p.PoemSummary) + ) + .ToListAsync(); + await jobProgressServiceEF.UpdateJob(job.Id, 0, $"Poems count = {poems.Count}"); + + for (var i = 0; i < poems.Count; i++) + { + var poem = poems[i]; + + if (!string.IsNullOrEmpty(poem.PlainText)) + { + await jobProgressServiceEF.UpdateJob(job.Id, i, $"{i} از {poems.Count}"); + string command = "به فارسی روان خلاصه کن:"; + var completionResult = await openAiService.ChatCompletion.CreateCompletion(new ChatCompletionCreateRequest + { + Messages = new List + { + ChatMessage.FromSystem(command + + + Environment.NewLine + + + poem.PlainText + ), + }, + Model = Betalgo.Ranul.OpenAI.ObjectModels.Models.Gpt_4o_mini, + }); + if (completionResult.Successful) + { + string summary = completionResult.Choices.First().Message.Content; + if (summary.Contains("اکتبر")) + { + summary = ""; + } + if (!string.IsNullOrEmpty(summary)) + { + summary = "هوش مصنوعی: " + summary; + var editablePoem = await context.GanjoorPoems.Where(p => p.Id == poem.Id).SingleAsync(); + editablePoem.PoemSummary = summary; + context.Update(editablePoem); + await context.SaveChangesAsync(); + } + } + } + + } + + await jobProgressServiceEF.UpdateJob(job.Id, 100, "", true); } catch (Exception exp) diff --git a/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-TajikSQLiteImport.cs b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-TajikSQLiteImport.cs index 49782996..748c4a0c 100644 --- a/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-TajikSQLiteImport.cs +++ b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-TajikSQLiteImport.cs @@ -1,11 +1,9 @@ using Microsoft.EntityFrameworkCore; using System; using RMuseum.Models.Ganjoor; -using System.Threading.Tasks; using RSecurityBackend.Models.Generic; using System.Linq; using RSecurityBackend.Services.Implementation; -using Microsoft.AspNetCore.Http; using Microsoft.Data.Sqlite; using RMuseum.DbContext; using System.Data; @@ -19,7 +17,7 @@ namespace RMuseum.Services.Implementation /// public partial class GanjoorService : IGanjoorService { - public async Task> TajikImportFromSqlite(int poetId, string filePath) + public RServiceResult TajikImportFromSqlite(int poetId, string filePath) { try {