adding new mindforge applications
All checks were successful
Mindforge API Build and Deploy / Build Mindforge API Image (push) Successful in 1m8s
Mindforge Cronjob Build and Deploy / Build Mindforge Cronjob Image (push) Successful in 1m19s
Mindforge API Build and Deploy / Deploy Mindforge API (internal) (push) Successful in 11s
Mindforge Cronjob Build and Deploy / Deploy Mindforge Cronjob (internal) (push) Successful in 10s
Mindforge Web Build and Deploy (internal) / Build Mindforge Web Image (push) Successful in 2m25s
Mindforge Web Build and Deploy (internal) / Deploy Mindforge Web (internal) (push) Successful in 12s
All checks were successful
Mindforge API Build and Deploy / Build Mindforge API Image (push) Successful in 1m8s
Mindforge Cronjob Build and Deploy / Build Mindforge Cronjob Image (push) Successful in 1m19s
Mindforge API Build and Deploy / Deploy Mindforge API (internal) (push) Successful in 11s
Mindforge Cronjob Build and Deploy / Deploy Mindforge Cronjob (internal) (push) Successful in 10s
Mindforge Web Build and Deploy (internal) / Build Mindforge Web Image (push) Successful in 2m25s
Mindforge Web Build and Deploy (internal) / Deploy Mindforge Web (internal) (push) Successful in 12s
This commit is contained in:
3
Mindforge.API/.gitignore
vendored
Normal file
3
Mindforge.API/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
bin
|
||||
obj
|
||||
appsettings.Development.json
|
||||
43
Mindforge.API/Controllers/FileController.cs
Normal file
43
Mindforge.API/Controllers/FileController.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Mindforge.API.Exceptions;
|
||||
using Mindforge.API.Models.Requests;
|
||||
using Mindforge.API.Services.Interfaces;
|
||||
|
||||
namespace Mindforge.API.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/v1/file")]
|
||||
public class FileController : ControllerBase
|
||||
{
|
||||
private readonly IFileService _fileService;
|
||||
|
||||
public FileController(IFileService fileService)
|
||||
{
|
||||
_fileService = fileService;
|
||||
}
|
||||
|
||||
[HttpPost("check")]
|
||||
public async Task<IActionResult> CheckFile([FromBody] FileCheckRequest request)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(request.FileContent) || string.IsNullOrWhiteSpace(request.CheckType))
|
||||
{
|
||||
throw new UserException("FileContent and CheckType are required.");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var base64Bytes = Convert.FromBase64String(request.FileContent);
|
||||
request.FileContent = System.Text.Encoding.UTF8.GetString(base64Bytes);
|
||||
}
|
||||
catch (FormatException)
|
||||
{
|
||||
throw new UserException("FileContent must be a valid base64 string.");
|
||||
}
|
||||
|
||||
var response = await _fileService.CheckFileAsync(request);
|
||||
return Ok(new { result = response });
|
||||
}
|
||||
}
|
||||
}
|
||||
43
Mindforge.API/Controllers/FlashcardController.cs
Normal file
43
Mindforge.API/Controllers/FlashcardController.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Mindforge.API.Exceptions;
|
||||
using Mindforge.API.Models.Requests;
|
||||
using Mindforge.API.Services.Interfaces;
|
||||
|
||||
namespace Mindforge.API.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/v1/flashcard")]
|
||||
public class FlashcardController : ControllerBase
|
||||
{
|
||||
private readonly IFlashcardService _flashcardService;
|
||||
|
||||
public FlashcardController(IFlashcardService flashcardService)
|
||||
{
|
||||
_flashcardService = flashcardService;
|
||||
}
|
||||
|
||||
[HttpPost("generate")]
|
||||
public async Task<IActionResult> Generate([FromBody] FlashcardGenerateRequest request)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(request.FileContent) || request.Amount <= 0)
|
||||
{
|
||||
throw new UserException("FileContent is required and Amount must be greater than 0.");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var base64Bytes = Convert.FromBase64String(request.FileContent);
|
||||
request.FileContent = System.Text.Encoding.UTF8.GetString(base64Bytes);
|
||||
}
|
||||
catch (FormatException)
|
||||
{
|
||||
throw new UserException("FileContent must be a valid base64 string.");
|
||||
}
|
||||
|
||||
var response = await _flashcardService.GenerateFlashcardsAsync(request);
|
||||
return Ok(new { result = response });
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Mindforge.API/Exceptions/UserException.cs
Normal file
11
Mindforge.API/Exceptions/UserException.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using System;
|
||||
|
||||
namespace Mindforge.API.Exceptions
|
||||
{
|
||||
public class UserException : Exception
|
||||
{
|
||||
public UserException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
47
Mindforge.API/Middlewares/ExceptionHandlingMiddleware.cs
Normal file
47
Mindforge.API/Middlewares/ExceptionHandlingMiddleware.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Mindforge.API.Exceptions;
|
||||
|
||||
namespace Mindforge.API.Middlewares
|
||||
{
|
||||
public class ExceptionHandlingMiddleware
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
private readonly ILogger<ExceptionHandlingMiddleware> _logger;
|
||||
|
||||
public ExceptionHandlingMiddleware(RequestDelegate next, ILogger<ExceptionHandlingMiddleware> logger)
|
||||
{
|
||||
_next = next;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task InvokeAsync(HttpContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _next(context);
|
||||
}
|
||||
catch (UserException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "User error");
|
||||
await HandleExceptionAsync(context, StatusCodes.Status400BadRequest, ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Internal server error");
|
||||
await HandleExceptionAsync(context, StatusCodes.Status500InternalServerError, $"Internal server error: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private static Task HandleExceptionAsync(HttpContext context, int statusCode, string message)
|
||||
{
|
||||
context.Response.ContentType = "application/json";
|
||||
context.Response.StatusCode = statusCode;
|
||||
|
||||
var result = JsonSerializer.Serialize(new { error = message });
|
||||
return context.Response.WriteAsync(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
13
Mindforge.API/Mindforge.API.csproj
Normal file
13
Mindforge.API/Mindforge.API.csproj
Normal file
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.12" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
6
Mindforge.API/Mindforge.API.http
Normal file
6
Mindforge.API/Mindforge.API.http
Normal file
@@ -0,0 +1,6 @@
|
||||
@Mindforge.API_HostAddress = http://localhost:5123
|
||||
|
||||
GET {{Mindforge.API_HostAddress}}/weatherforecast/
|
||||
Accept: application/json
|
||||
|
||||
###
|
||||
8
Mindforge.API/Models/Enums/LlmProvider.cs
Normal file
8
Mindforge.API/Models/Enums/LlmProvider.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace Mindforge.API.Models.Enums
|
||||
{
|
||||
public enum LlmProvider
|
||||
{
|
||||
OpenAI,
|
||||
Gemini
|
||||
}
|
||||
}
|
||||
13
Mindforge.API/Models/Requests/FileCheckRequest.cs
Normal file
13
Mindforge.API/Models/Requests/FileCheckRequest.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
namespace Mindforge.API.Models.Requests
|
||||
{
|
||||
public class FileCheckRequest
|
||||
{
|
||||
public string FileContent { get; set; } = string.Empty;
|
||||
public string FileName { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Expected values: "language" or "content"
|
||||
/// </summary>
|
||||
public string CheckType { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
21
Mindforge.API/Models/Requests/FlashcardGenerateRequest.cs
Normal file
21
Mindforge.API/Models/Requests/FlashcardGenerateRequest.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Mindforge.API.Models.Requests
|
||||
{
|
||||
public class FlashcardGenerateRequest
|
||||
{
|
||||
public string FileContent { get; set; } = string.Empty;
|
||||
public string FileName { get; set; } = string.Empty;
|
||||
public int Amount { get; set; }
|
||||
[JsonConverter(typeof(JsonStringEnumConverter))]
|
||||
public FlashcardMode? Mode { get; set; } = FlashcardMode.Simple;
|
||||
}
|
||||
|
||||
public enum FlashcardMode
|
||||
{
|
||||
Basic,
|
||||
Simple,
|
||||
Detailed,
|
||||
Hyper
|
||||
}
|
||||
}
|
||||
71
Mindforge.API/Program.cs
Normal file
71
Mindforge.API/Program.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Mindforge.API.Providers;
|
||||
using Mindforge.API.Services;
|
||||
using Mindforge.API.Services.Interfaces;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
builder.Services.AddControllers();
|
||||
builder.Logging.AddConsole();
|
||||
builder.Logging.AddDebug();
|
||||
|
||||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
||||
builder.Services.AddOpenApi();
|
||||
|
||||
// Configure CORS
|
||||
builder.Services.AddCors(options =>
|
||||
{
|
||||
options.AddPolicy("AllowAll", policy =>
|
||||
{
|
||||
policy.AllowAnyOrigin()
|
||||
.AllowAnyMethod()
|
||||
.AllowAnyHeader();
|
||||
});
|
||||
});
|
||||
|
||||
// Register HttpClient for providers
|
||||
builder.Services.AddHttpClient();
|
||||
|
||||
// Register Providers
|
||||
builder.Services.AddScoped<ILlmApiProvider, OpenAIApiProvider>();
|
||||
builder.Services.AddScoped<ILlmApiProvider, GeminiApiProvider>();
|
||||
|
||||
// Register Services
|
||||
builder.Services.AddScoped<IAgentService, AgentService>();
|
||||
builder.Services.AddScoped<IFileService, FileService>();
|
||||
builder.Services.AddScoped<IFlashcardService, FlashcardService>();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
app.UseCors("AllowAll");
|
||||
|
||||
app.UseMiddleware<Mindforge.API.Middlewares.ExceptionHandlingMiddleware>();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.MapOpenApi();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
app.UseAuthorization();
|
||||
app.MapControllers();
|
||||
|
||||
// Check for env vars
|
||||
var openAiKey = builder.Configuration["OPENAI_API_KEY"];
|
||||
var geminiKey = builder.Configuration["GEMINI_API_KEY"];
|
||||
|
||||
if (string.IsNullOrEmpty(openAiKey))
|
||||
{
|
||||
app.Logger.LogWarning("OPENAI_API_KEY not found in configuration.");
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(geminiKey))
|
||||
{
|
||||
app.Logger.LogWarning("GEMINI_API_KEY not found in configuration.");
|
||||
}
|
||||
|
||||
app.Run();
|
||||
23
Mindforge.API/Properties/launchSettings.json
Normal file
23
Mindforge.API/Properties/launchSettings.json
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/launchsettings.json",
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": false,
|
||||
"applicationUrl": "http://localhost:5123",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"https": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": false,
|
||||
"applicationUrl": "https://localhost:7116;http://localhost:5123",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
202
Mindforge.API/Providers/GeminiApiProvider.cs
Normal file
202
Mindforge.API/Providers/GeminiApiProvider.cs
Normal file
@@ -0,0 +1,202 @@
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Mindforge.API.Providers
|
||||
{
|
||||
public class GeminiApiProvider : ILlmApiProvider
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly ILogger<GeminiApiProvider> _logger;
|
||||
|
||||
public GeminiApiProvider(HttpClient httpClient, IConfiguration configuration, ILogger<GeminiApiProvider> logger)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
_httpClient.Timeout = TimeSpan.FromMinutes(5);
|
||||
_configuration = configuration;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<string> SendRequestAsync(string systemPrompt, string userPrompt, string model)
|
||||
{
|
||||
var apiKey = _configuration["GEMINI_API_KEY"];
|
||||
if (string.IsNullOrEmpty(apiKey))
|
||||
{
|
||||
throw new Exception("GEMINI_API_KEY not found in configuration.");
|
||||
}
|
||||
|
||||
var apiBase = "https://generativelanguage.googleapis.com/v1beta";
|
||||
var url = $"{apiBase.TrimEnd('/')}/models/{model}:generateContent?key={apiKey}";
|
||||
|
||||
var reqBody = new
|
||||
{
|
||||
system_instruction = string.IsNullOrEmpty(systemPrompt) ? null : new
|
||||
{
|
||||
parts = new[] { new { text = systemPrompt } }
|
||||
},
|
||||
contents = new[]
|
||||
{
|
||||
new
|
||||
{
|
||||
role = "user",
|
||||
parts = new[] { new { text = userPrompt } }
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var jsonBody = JsonSerializer.Serialize(reqBody, new JsonSerializerOptions { DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull });
|
||||
|
||||
using var request = new HttpRequestMessage(HttpMethod.Post, url);
|
||||
request.Content = new StringContent(jsonBody, Encoding.UTF8, "application/json");
|
||||
|
||||
var response = await _httpClient.SendAsync(request);
|
||||
var responseBody = await response.Content.ReadAsStringAsync();
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
throw new Exception($"Gemini API error status {(int)response.StatusCode}: {responseBody}");
|
||||
}
|
||||
|
||||
var result = JsonSerializer.Deserialize<JsonElement>(responseBody);
|
||||
if (result.TryGetProperty("candidates", out var candidates) && candidates.GetArrayLength() > 0)
|
||||
{
|
||||
var content = candidates[0].GetProperty("content");
|
||||
if (content.TryGetProperty("parts", out var parts) && parts.GetArrayLength() > 0)
|
||||
{
|
||||
return parts[0].GetProperty("text").GetString() ?? string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
throw new Exception("empty response from Gemini API");
|
||||
}
|
||||
|
||||
public async Task<string> SendRequestBatchAsync(string systemPrompt, string userPrompt, string model)
|
||||
{
|
||||
var apiKey = _configuration["GEMINI_API_KEY"];
|
||||
if (string.IsNullOrEmpty(apiKey))
|
||||
throw new Exception("GEMINI_API_KEY not found in configuration.");
|
||||
|
||||
var apiBase = "https://generativelanguage.googleapis.com/v1beta";
|
||||
var jsonOptions = new JsonSerializerOptions { DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull };
|
||||
|
||||
// Build single inline request
|
||||
var batchBody = new
|
||||
{
|
||||
batch = new
|
||||
{
|
||||
display_name = "mindforge-batch",
|
||||
input_config = new
|
||||
{
|
||||
requests = new
|
||||
{
|
||||
requests = new[]
|
||||
{
|
||||
new
|
||||
{
|
||||
request = new
|
||||
{
|
||||
system_instruction = string.IsNullOrEmpty(systemPrompt) ? null : new
|
||||
{
|
||||
parts = new[] { new { text = systemPrompt } }
|
||||
},
|
||||
contents = new[]
|
||||
{
|
||||
new
|
||||
{
|
||||
role = "user",
|
||||
parts = new[] { new { text = userPrompt } }
|
||||
}
|
||||
}
|
||||
},
|
||||
metadata = new { key = "request-1" }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Submit batch job
|
||||
var createUrl = $"{apiBase}/models/{model}:batchGenerateContent?key={apiKey}";
|
||||
using var createReq = new HttpRequestMessage(HttpMethod.Post, createUrl);
|
||||
createReq.Content = new StringContent(JsonSerializer.Serialize(batchBody, jsonOptions), Encoding.UTF8, "application/json");
|
||||
|
||||
var createResp = await _httpClient.SendAsync(createReq);
|
||||
var createBody = await createResp.Content.ReadAsStringAsync();
|
||||
if (!createResp.IsSuccessStatusCode)
|
||||
throw new Exception($"Gemini Batch API error creating job {(int)createResp.StatusCode}: {createBody}");
|
||||
|
||||
_logger.LogInformation("Gemini Batch API job created");
|
||||
|
||||
var createResult = JsonSerializer.Deserialize<JsonElement>(createBody);
|
||||
if (!createResult.TryGetProperty("name", out var nameEl))
|
||||
throw new Exception("Gemini Batch API did not return a job name.");
|
||||
|
||||
var batchName = nameEl.GetString()!;
|
||||
var pollUrl = $"{apiBase}/{batchName}?key={apiKey}";
|
||||
|
||||
_logger.LogInformation("Gemini Batch API job name: {BatchName}", batchName);
|
||||
|
||||
// Poll until terminal state
|
||||
while (true)
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromSeconds(10));
|
||||
|
||||
using var pollReq = new HttpRequestMessage(HttpMethod.Get, pollUrl);
|
||||
var pollResp = await _httpClient.SendAsync(pollReq);
|
||||
var pollBody = await pollResp.Content.ReadAsStringAsync();
|
||||
|
||||
if (!pollResp.IsSuccessStatusCode)
|
||||
throw new Exception($"Gemini Batch API error polling status {(int)pollResp.StatusCode}: {pollBody}");
|
||||
|
||||
var pollResult = JsonSerializer.Deserialize<JsonElement>(pollBody);
|
||||
var metadata = pollResult.GetProperty("metadata");
|
||||
var state = metadata.GetProperty("state");
|
||||
|
||||
_logger.LogInformation("Gemini Batch API job state: {State}", state.GetString());
|
||||
|
||||
switch (state.GetString())
|
||||
{
|
||||
case "BATCH_STATE_SUCCEEDED":
|
||||
if (pollResult.TryGetProperty("response", out var batchResponse) &&
|
||||
batchResponse.TryGetProperty("inlinedResponses", out var inlinedResponses) &&
|
||||
inlinedResponses.TryGetProperty("inlinedResponses", out var inlinedResponsesInternal) &&
|
||||
inlinedResponsesInternal.GetArrayLength() > 0)
|
||||
{
|
||||
_logger.LogInformation("Gemini Batch API job succeeded");
|
||||
|
||||
var first = inlinedResponsesInternal[0];
|
||||
if (first.TryGetProperty("error", out var reqError))
|
||||
throw new Exception($"Gemini Batch request error: {reqError}");
|
||||
|
||||
if (first.TryGetProperty("response", out var innerResponse) &&
|
||||
innerResponse.TryGetProperty("candidates", out var candidates) &&
|
||||
candidates.GetArrayLength() > 0)
|
||||
{
|
||||
var content = candidates[0].GetProperty("content");
|
||||
if (content.TryGetProperty("parts", out var parts) && parts.GetArrayLength() > 0)
|
||||
return parts[0].GetProperty("text").GetString() ?? string.Empty;
|
||||
}
|
||||
}
|
||||
throw new Exception("Gemini Batch job succeeded but returned no content.");
|
||||
|
||||
case "BATCH_STATE_FAILED":
|
||||
throw new Exception($"Gemini Batch job failed: {pollBody}");
|
||||
|
||||
case "BATCH_STATE_CANCELLED":
|
||||
throw new Exception("Gemini Batch job was cancelled.");
|
||||
|
||||
case "BATCH_STATE_EXPIRED":
|
||||
throw new Exception("Gemini Batch job expired before completing.");
|
||||
|
||||
// BATCH_STATE_PENDING / BATCH_STATE_RUNNING — keep polling
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
10
Mindforge.API/Providers/ILlmApiProvider.cs
Normal file
10
Mindforge.API/Providers/ILlmApiProvider.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Mindforge.API.Providers
|
||||
{
|
||||
public interface ILlmApiProvider
|
||||
{
|
||||
Task<string> SendRequestAsync(string systemPrompt, string userPrompt, string model);
|
||||
Task<string> SendRequestBatchAsync(string systemPrompt, string userPrompt, string model);
|
||||
}
|
||||
}
|
||||
110
Mindforge.API/Providers/OpenAIApiProvider.cs
Normal file
110
Mindforge.API/Providers/OpenAIApiProvider.cs
Normal file
@@ -0,0 +1,110 @@
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Mindforge.API.Providers
|
||||
{
|
||||
public class OpenAIApiProvider : ILlmApiProvider
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly ILogger<OpenAIApiProvider> _logger;
|
||||
|
||||
public OpenAIApiProvider(HttpClient httpClient, IConfiguration configuration, ILogger<OpenAIApiProvider> logger)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
_httpClient.Timeout = TimeSpan.FromMinutes(5);
|
||||
_configuration = configuration;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<string> SendRequestAsync(string systemPrompt, string userPrompt, string model)
|
||||
{
|
||||
var apiKey = _configuration["OPENAI_API_KEY"];
|
||||
if (string.IsNullOrEmpty(apiKey))
|
||||
{
|
||||
throw new Exception("OPENAI_API_KEY not found in configuration.");
|
||||
}
|
||||
|
||||
var apiBase = "https://api.openai.com/v1";
|
||||
var url = $"{apiBase.TrimEnd('/')}/responses";
|
||||
|
||||
var reqBody = new
|
||||
{
|
||||
model = model,
|
||||
input = new[]
|
||||
{
|
||||
new { role = "developer", content = systemPrompt },
|
||||
new { role = "user", content = userPrompt }
|
||||
},
|
||||
reasoning = new
|
||||
{
|
||||
effort = "low"
|
||||
}
|
||||
};
|
||||
|
||||
var jsonBody = JsonSerializer.Serialize(reqBody);
|
||||
|
||||
Exception? lastErr = null;
|
||||
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
using var request = new HttpRequestMessage(HttpMethod.Post, url);
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
|
||||
request.Content = new StringContent(jsonBody, Encoding.UTF8, "application/json");
|
||||
|
||||
try
|
||||
{
|
||||
var response = await _httpClient.SendAsync(request);
|
||||
var responseBody = await response.Content.ReadAsStringAsync();
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
lastErr = new Exception($"OpenAI API error status {(int)response.StatusCode}: {responseBody}");
|
||||
await Task.Delay(TimeSpan.FromSeconds(1 << i));
|
||||
continue;
|
||||
}
|
||||
|
||||
var result = JsonSerializer.Deserialize<JsonElement>(responseBody);
|
||||
if (result.TryGetProperty("output", out var outputArray))
|
||||
{
|
||||
foreach (var outputItem in outputArray.EnumerateArray())
|
||||
{
|
||||
if (outputItem.TryGetProperty("content", out var contentArray))
|
||||
{
|
||||
foreach (var contentItem in contentArray.EnumerateArray())
|
||||
{
|
||||
if (contentItem.TryGetProperty("text", out var textContent))
|
||||
{
|
||||
return textContent.GetString() ?? string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_logger.LogWarning("OpenAI API raw response: {responseBody}", responseBody);
|
||||
|
||||
throw new Exception("empty response from OpenAI API");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error in OpenAI API request");
|
||||
lastErr = ex;
|
||||
await Task.Delay(TimeSpan.FromSeconds(1 << i));
|
||||
}
|
||||
}
|
||||
|
||||
throw new Exception($"failed to get OpenAI response after 5 attempts. Last error: {lastErr?.Message}", lastErr);
|
||||
}
|
||||
|
||||
public async Task<string> SendRequestBatchAsync(string systemPrompt, string userPrompt, string model)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
48
Mindforge.API/Services/AgentService.cs
Normal file
48
Mindforge.API/Services/AgentService.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Mindforge.API.Models.Enums;
|
||||
using Mindforge.API.Providers;
|
||||
using Mindforge.API.Services.Interfaces;
|
||||
|
||||
namespace Mindforge.API.Services
|
||||
{
|
||||
public class AgentService : IAgentService
|
||||
{
|
||||
private readonly IEnumerable<ILlmApiProvider> _providers;
|
||||
|
||||
public AgentService(IEnumerable<ILlmApiProvider> providers)
|
||||
{
|
||||
_providers = providers;
|
||||
}
|
||||
|
||||
public Task<string> ProcessRequestAsync(LlmProvider providerEnum, string systemPrompt, string userPrompt, string model)
|
||||
{
|
||||
ILlmApiProvider provider = providerEnum switch
|
||||
{
|
||||
LlmProvider.OpenAI => _providers.OfType<OpenAIApiProvider>().FirstOrDefault()
|
||||
?? throw new Exception("OpenAI provider not registered"),
|
||||
LlmProvider.Gemini => _providers.OfType<GeminiApiProvider>().FirstOrDefault()
|
||||
?? throw new Exception("Gemini provider not registered"),
|
||||
_ => throw new Exception("Unknown provider")
|
||||
};
|
||||
|
||||
return provider.SendRequestAsync(systemPrompt, userPrompt, model);
|
||||
}
|
||||
|
||||
public Task<string> ProcessRequestBatchAsync(LlmProvider providerEnum, string systemPrompt, string userPrompt, string model)
|
||||
{
|
||||
ILlmApiProvider provider = providerEnum switch
|
||||
{
|
||||
LlmProvider.OpenAI => _providers.OfType<OpenAIApiProvider>().FirstOrDefault()
|
||||
?? throw new Exception("OpenAI provider not registered"),
|
||||
LlmProvider.Gemini => _providers.OfType<GeminiApiProvider>().FirstOrDefault()
|
||||
?? throw new Exception("Gemini provider not registered"),
|
||||
_ => throw new Exception("Unknown provider")
|
||||
};
|
||||
|
||||
return provider.SendRequestBatchAsync(systemPrompt, userPrompt, model);
|
||||
}
|
||||
}
|
||||
}
|
||||
61
Mindforge.API/Services/FileService.cs
Normal file
61
Mindforge.API/Services/FileService.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Mindforge.API.Models.Enums;
|
||||
using Mindforge.API.Models.Requests;
|
||||
using Mindforge.API.Services.Interfaces;
|
||||
using Mindforge.API.Exceptions;
|
||||
|
||||
namespace Mindforge.API.Services
|
||||
{
|
||||
public class FileService : IFileService
|
||||
{
|
||||
private readonly IAgentService _agentService;
|
||||
|
||||
private const LlmProvider DefaultProvider = LlmProvider.OpenAI;
|
||||
private const string DefaultModel = "gpt-5-mini";
|
||||
|
||||
public FileService(IAgentService agentService)
|
||||
{
|
||||
_agentService = agentService;
|
||||
}
|
||||
|
||||
public async Task<string> CheckFileAsync(FileCheckRequest request)
|
||||
{
|
||||
string systemPrompt;
|
||||
|
||||
if (request.CheckType.ToLower() == "language")
|
||||
{
|
||||
systemPrompt = $@"Você é um revisor de textos sênior e especialista em língua portuguesa do Brasil.
|
||||
Sua tarefa é analisar o texto fornecido e corrigir rigorosamente todos os erros gramaticais, de concordância, pontuação, garantindo clareza, coesão e fluidez.
|
||||
Por favor, siga esta estrutura:
|
||||
1. Retorne o texto completamente revisado e polido.
|
||||
2. Não adicione nenhum texto adicional, apenas o texto revisado.
|
||||
3. Não altere o sentido do texto, mantenha a mesma ideia. Apenas corrija os erros gramaticais, de concordância e pontuação.
|
||||
Responda única e exclusivamente em português do Brasil.
|
||||
Foque em textos de concursos públicos, principalmente para a banca Cebraspe.";
|
||||
}
|
||||
else if (request.CheckType.ToLower() == "content")
|
||||
{
|
||||
systemPrompt = $@"Você é um analista de conteúdo experiente, especializado em revisão crítica e verificação de fatos.
|
||||
Sua tarefa é realizar uma análise rigorosa do texto fornecido, identificando erros lógicos, imprecisões de conteúdo, contradições internas e inconsistências argumentativas.
|
||||
Por favor, siga esta estrutura:
|
||||
1. Destaque cada problema encontrado no texto original.
|
||||
2. Explique detalhadamente por que é um erro ou inconsistência.
|
||||
3. Apresente sugestões claras e embasadas para reescrever ou corrigir os trechos problemáticos, melhorando a coerência e a lógica e a clareza do conteúdo.
|
||||
Responda única e exclusivamente em português do Brasil e mantenha um tom analítico e construtivo.
|
||||
|
||||
Responda em tópicos para ser apresentados ao usuário, sendo sucinto e não extremamente detalhado.
|
||||
Os resumos serão utilizados para concursos públicos, principalmente para a banca Cebraspe.
|
||||
";
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new UserException("Tipo de verificação inválido. Use 'language' ou 'content'.");
|
||||
}
|
||||
|
||||
string userPrompt = $"Arquivo: {request.FileName}\nConteúdo:\n{request.FileContent}";
|
||||
|
||||
return await _agentService.ProcessRequestAsync(DefaultProvider, systemPrompt, userPrompt, DefaultModel);
|
||||
}
|
||||
}
|
||||
}
|
||||
83
Mindforge.API/Services/FlashcardService.cs
Normal file
83
Mindforge.API/Services/FlashcardService.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
using System.Threading.Tasks;
|
||||
using Mindforge.API.Models.Enums;
|
||||
using Mindforge.API.Models.Requests;
|
||||
using Mindforge.API.Services.Interfaces;
|
||||
|
||||
namespace Mindforge.API.Services
|
||||
{
|
||||
public class FlashcardService : IFlashcardService
|
||||
{
|
||||
private readonly IAgentService _agentService;
|
||||
private readonly ILogger<FlashcardService> _logger;
|
||||
|
||||
private const LlmProvider DefaultProvider = LlmProvider.Gemini;
|
||||
private string DefaultModel = "gemini-3.1-flash-image-preview";
|
||||
|
||||
public FlashcardService(IAgentService agentService, ILogger<FlashcardService> logger)
|
||||
{
|
||||
_agentService = agentService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<string> GenerateFlashcardsAsync(FlashcardGenerateRequest request)
|
||||
{
|
||||
var extraPrompt = "";
|
||||
|
||||
switch (request.Mode)
|
||||
{
|
||||
case FlashcardMode.Basic:
|
||||
DefaultModel = "gemini-3.1-flash-lite-preview";
|
||||
break;
|
||||
case FlashcardMode.Simple:
|
||||
DefaultModel = "gemini-3.1-flash-image-preview";
|
||||
break;
|
||||
case FlashcardMode.Detailed:
|
||||
DefaultModel = "gemini-3.1-flash-image-preview";
|
||||
extraPrompt = "Crie flashcards mais detalhados.";
|
||||
break;
|
||||
case FlashcardMode.Hyper:
|
||||
DefaultModel = "gemini-3.1-pro-preview";
|
||||
extraPrompt = "Adicione também pequenas questões para fixação, para que o usuário possa testar seus conhecimentos. As questões devem ser curtas e objetivas, como se fosse cobradas em prova mesmo.";
|
||||
break;
|
||||
}
|
||||
|
||||
string systemPrompt = $@"Você é um assistente educacional especializado em criar flashcards para o Anki.
|
||||
Baseado no texto fornecido, crie exatamente {request.Amount} flashcards que focam nos conceitos mais importantes e difíceis.
|
||||
A resposta FINAL deve ser APENAS no formato CSV, pronto para importação no Anki, sem nenhum texto adicional antes ou depois.
|
||||
O formato CSV deve ter duas colunas: a frente da carta (pergunta/conceito) e o verso (resposta/explicação). Use ponto e vírgula (;) como separador. Não adicione o cabeçalho.
|
||||
As perguntas e respostas devem estar estritamente em Português do Brasil.
|
||||
|
||||
Exemplo de saída:
|
||||
""Qual é a capital do Brasil?"";""Brasília""
|
||||
""Qual é a maior cidade do Brasil?"";""São Paulo""
|
||||
|
||||
Com base no arquivo fornecido, crie exatamente {request.Amount} flashcards que focam nos conceitos mais importantes e difíceis.
|
||||
{extraPrompt}
|
||||
";
|
||||
|
||||
string userPrompt = $"Arquivo: {request.FileName}\nConteúdo:\n{request.FileContent}";
|
||||
|
||||
//var result = await _agentService.ProcessRequestAsync(DefaultProvider, systemPrompt, userPrompt, DefaultModel);
|
||||
var result = await _agentService.ProcessRequestBatchAsync(DefaultProvider, systemPrompt, userPrompt, DefaultModel);
|
||||
|
||||
var lines = result.Split('\n');
|
||||
|
||||
if (lines.Length == 0)
|
||||
{
|
||||
throw new Exception("Nenhum flashcard gerado.");
|
||||
}
|
||||
|
||||
if (lines.Length > request.Amount)
|
||||
{
|
||||
_logger.LogWarning("Quantidade de flashcards excede o limite.");
|
||||
}
|
||||
|
||||
if (lines.Length < request.Amount)
|
||||
{
|
||||
_logger.LogWarning("Quantidade de flashcards abaixo do limite.");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Mindforge.API/Services/Interfaces/IAgentService.cs
Normal file
11
Mindforge.API/Services/Interfaces/IAgentService.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using System.Threading.Tasks;
|
||||
using Mindforge.API.Models.Enums;
|
||||
|
||||
namespace Mindforge.API.Services.Interfaces
|
||||
{
|
||||
public interface IAgentService
|
||||
{
|
||||
Task<string> ProcessRequestAsync(LlmProvider provider, string systemPrompt, string userPrompt, string model);
|
||||
Task<string> ProcessRequestBatchAsync(LlmProvider provider, string systemPrompt, string userPrompt, string model);
|
||||
}
|
||||
}
|
||||
10
Mindforge.API/Services/Interfaces/IFileService.cs
Normal file
10
Mindforge.API/Services/Interfaces/IFileService.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using System.Threading.Tasks;
|
||||
using Mindforge.API.Models.Requests;
|
||||
|
||||
namespace Mindforge.API.Services.Interfaces
|
||||
{
|
||||
public interface IFileService
|
||||
{
|
||||
Task<string> CheckFileAsync(FileCheckRequest request);
|
||||
}
|
||||
}
|
||||
10
Mindforge.API/Services/Interfaces/IFlashcardService.cs
Normal file
10
Mindforge.API/Services/Interfaces/IFlashcardService.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using System.Threading.Tasks;
|
||||
using Mindforge.API.Models.Requests;
|
||||
|
||||
namespace Mindforge.API.Services.Interfaces
|
||||
{
|
||||
public interface IFlashcardService
|
||||
{
|
||||
Task<string> GenerateFlashcardsAsync(FlashcardGenerateRequest request);
|
||||
}
|
||||
}
|
||||
9
Mindforge.API/appsettings.json
Normal file
9
Mindforge.API/appsettings.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
@@ -49,3 +49,24 @@ spec:
|
||||
ports:
|
||||
- port: 80
|
||||
targetPort: 8080
|
||||
---
|
||||
apiVersion: networking.k8s.io/v1
|
||||
kind: Ingress
|
||||
metadata:
|
||||
name: mindforge-api
|
||||
namespace: mindforge
|
||||
labels:
|
||||
app.kubernetes.io/name: mindforge-api
|
||||
spec:
|
||||
ingressClassName: nginx
|
||||
rules:
|
||||
- host: "api.mindforge.haven"
|
||||
http:
|
||||
paths:
|
||||
- path: "/"
|
||||
pathType: Prefix
|
||||
backend:
|
||||
service:
|
||||
name: mindforge-api
|
||||
port:
|
||||
number: 80
|
||||
|
||||
Reference in New Issue
Block a user