#453 ai/generate/poem/summaries
This commit is contained in:
parent
b82ac6860d
commit
b95f1ee393
@ -287,6 +287,7 @@ namespace RMuseum.Controllers
|
||||
/// get poet image
|
||||
/// </summary>
|
||||
/// <param name="url">sample: hafez</param>
|
||||
/// <param name="nocache"></param>
|
||||
/// <returns></returns>
|
||||
[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<IActionResult> TajikOneTimeImportFromSqlite()
|
||||
public IActionResult TajikOneTimeImportFromSqlite()
|
||||
{
|
||||
|
||||
RServiceResult<bool> 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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// fill poem summaries using open ai
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -995,6 +995,7 @@
|
||||
get poet image
|
||||
</summary>
|
||||
<param name="url">sample: hafez</param>
|
||||
<param name="nocache"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:RMuseum.Controllers.GanjoorController.UploadPoetImage(System.Int32)">
|
||||
@ -2294,6 +2295,12 @@
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:RMuseum.Controllers.GanjoorController.OpenAIStartFillingPoemSummaries">
|
||||
<summary>
|
||||
fill poem summaries using open ai
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="P:RMuseum.Controllers.GanjoorController.ReadOnlyMode">
|
||||
<summary>
|
||||
readonly mode
|
||||
@ -16951,6 +16958,7 @@
|
||||
tajik import from sqlite
|
||||
</summary>
|
||||
<param name="poetId"></param>
|
||||
<param name="filePath"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:RMuseum.Services.IGanjoorService.GetTajikPoetsAsync">
|
||||
@ -16982,6 +16990,11 @@
|
||||
fill couplet summaries using open ai
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:RMuseum.Services.IGanjoorService.OpenAIStartFillingPoemSummaries">
|
||||
<summary>
|
||||
fill poem summaries using open ai
|
||||
</summary>
|
||||
</member>
|
||||
<member name="T:RMuseum.Services.IGanjoorTranslationService">
|
||||
<summary>
|
||||
translation service implementation
|
||||
@ -18911,6 +18924,11 @@
|
||||
fill couplet summaries using open ai
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:RMuseum.Services.Implementation.GanjoorService.OpenAIStartFillingPoemSummaries">
|
||||
<summary>
|
||||
fill poem summaries using open ai
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:RMuseum.Services.Implementation.GanjoorService.GetCategoryPaperSourcesAsync(System.Int32)">
|
||||
<summary>
|
||||
category paper sources
|
||||
|
||||
@ -1499,8 +1499,9 @@ namespace RMuseum.Services
|
||||
/// tajik import from sqlite
|
||||
/// </summary>
|
||||
/// <param name="poetId"></param>
|
||||
/// <param name="filePath"></param>
|
||||
/// <returns></returns>
|
||||
Task<RServiceResult<bool>> TajikImportFromSqlite(int poetId, string filePath);
|
||||
RServiceResult<bool> TajikImportFromSqlite(int poetId, string filePath);
|
||||
|
||||
/// <summary>
|
||||
/// tajik poets
|
||||
@ -1532,7 +1533,13 @@ namespace RMuseum.Services
|
||||
/// <summary>
|
||||
/// fill couplet summaries using open ai
|
||||
/// </summary>
|
||||
public void OpenAIStartFillingCoupletSummaries();
|
||||
void OpenAIStartFillingCoupletSummaries();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// fill poem summaries using open ai
|
||||
/// </summary>
|
||||
void OpenAIStartFillingPoemSummaries();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// fill poem summaries using open ai
|
||||
/// </summary>
|
||||
public void OpenAIStartFillingPoemSummaries()
|
||||
{
|
||||
_backgroundTaskQueue.QueueBackgroundWorkItem
|
||||
(
|
||||
async token =>
|
||||
{
|
||||
using (RMuseumDbContext context = new RMuseumDbContext(new DbContextOptions<RMuseumDbContext>())) //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>
|
||||
{
|
||||
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)
|
||||
|
||||
@ -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
|
||||
/// </summary>
|
||||
public partial class GanjoorService : IGanjoorService
|
||||
{
|
||||
public async Task<RServiceResult<bool>> TajikImportFromSqlite(int poetId, string filePath)
|
||||
public RServiceResult<bool> TajikImportFromSqlite(int poetId, string filePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
Loading…
Reference in New Issue
Block a user