moving to openrouter
All checks were successful
Mindforge API Build and Deploy / Build Mindforge API Image (push) Successful in 3m39s
Mindforge Cronjob Build and Deploy / Build Mindforge Cronjob Image (push) Successful in 3m49s
Mindforge API Build and Deploy / Deploy Mindforge API (internal) (push) Successful in 38s
Mindforge Cronjob Build and Deploy / Deploy Mindforge Cronjob (internal) (push) Successful in 30s

This commit is contained in:
2026-04-04 21:09:18 -03:00
parent d0543544f8
commit b9736293d3
11 changed files with 58 additions and 342 deletions

View File

@@ -1,8 +0,0 @@
namespace Mindforge.API.Models.Enums
{
public enum LlmProvider
{
OpenAI,
Gemini
}
}

View File

@@ -34,7 +34,6 @@ builder.Services.AddHttpClient();
// Register Providers // Register Providers
builder.Services.AddScoped<ILlmApiProvider, OpenAIApiProvider>(); builder.Services.AddScoped<ILlmApiProvider, OpenAIApiProvider>();
builder.Services.AddScoped<ILlmApiProvider, GeminiApiProvider>();
// Register Services // Register Services
builder.Services.AddScoped<IAgentService, AgentService>(); builder.Services.AddScoped<IAgentService, AgentService>();
@@ -59,30 +58,26 @@ app.UseAuthorization();
app.MapControllers(); app.MapControllers();
// Check for env vars // Check for env vars
var openAiKey = builder.Configuration["OPENAI_API_KEY"]; var openAiApiUrl = builder.Configuration["OPENAI_API_URL"];
var geminiKey = builder.Configuration["GEMINI_API_KEY"]; var openAiToken = builder.Configuration["OPENAI_TOKEN"];
var openAiModel = builder.Configuration["OPENAI_MODEL"];
if (string.IsNullOrEmpty(openAiKey)) if (string.IsNullOrEmpty(openAiApiUrl))
{ app.Logger.LogWarning("OPENAI_API_URL not found in configuration.");
app.Logger.LogWarning("OPENAI_API_KEY not found in configuration.");
}
if (string.IsNullOrEmpty(geminiKey)) if (string.IsNullOrEmpty(openAiToken))
{ app.Logger.LogWarning("OPENAI_TOKEN not found in configuration.");
app.Logger.LogWarning("GEMINI_API_KEY not found in configuration.");
} if (string.IsNullOrEmpty(openAiModel))
app.Logger.LogWarning("OPENAI_MODEL not found in configuration.");
var giteaRepoUrl = builder.Configuration["GITEA_REPO_URL"]; var giteaRepoUrl = builder.Configuration["GITEA_REPO_URL"];
var giteaAccessToken = builder.Configuration["GITEA_ACCESS_TOKEN"]; var giteaAccessToken = builder.Configuration["GITEA_ACCESS_TOKEN"];
if (string.IsNullOrEmpty(giteaRepoUrl)) if (string.IsNullOrEmpty(giteaRepoUrl))
{
app.Logger.LogWarning("GITEA_REPO_URL not found in configuration. Repository features will not work."); app.Logger.LogWarning("GITEA_REPO_URL not found in configuration. Repository features will not work.");
}
if (string.IsNullOrEmpty(giteaAccessToken)) if (string.IsNullOrEmpty(giteaAccessToken))
{
app.Logger.LogWarning("GITEA_ACCESS_TOKEN not found in configuration. Repository features will not work."); app.Logger.LogWarning("GITEA_ACCESS_TOKEN not found in configuration. Repository features will not work.");
}
app.Run(); app.Run();

View File

@@ -1,202 +0,0 @@
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
}
}
}
}
}

View File

@@ -4,7 +4,6 @@ namespace Mindforge.API.Providers
{ {
public interface ILlmApiProvider public interface ILlmApiProvider
{ {
Task<string> SendRequestAsync(string systemPrompt, string userPrompt, string model); Task<string> SendRequestAsync(string systemPrompt, string userPrompt);
Task<string> SendRequestBatchAsync(string systemPrompt, string userPrompt, string model);
} }
} }

View File

@@ -22,28 +22,29 @@ namespace Mindforge.API.Providers
_logger = logger; _logger = logger;
} }
public async Task<string> SendRequestAsync(string systemPrompt, string userPrompt, string model) public async Task<string> SendRequestAsync(string systemPrompt, string userPrompt)
{ {
var apiKey = _configuration["OPENAI_API_KEY"]; var apiUrl = _configuration["OPENAI_API_URL"];
if (string.IsNullOrEmpty(apiKey)) if (string.IsNullOrEmpty(apiUrl))
{ throw new Exception("OPENAI_API_URL not found in configuration.");
throw new Exception("OPENAI_API_KEY not found in configuration.");
}
var apiBase = "https://api.openai.com/v1"; var token = _configuration["OPENAI_TOKEN"];
var url = $"{apiBase.TrimEnd('/')}/responses"; if (string.IsNullOrEmpty(token))
throw new Exception("OPENAI_TOKEN not found in configuration.");
var model = _configuration["OPENAI_MODEL"];
if (string.IsNullOrEmpty(model))
throw new Exception("OPENAI_MODEL not found in configuration.");
var url = $"{apiUrl.TrimEnd('/')}/chat/completions";
var reqBody = new var reqBody = new
{ {
model = model, model = model,
input = new[] messages = new[]
{ {
new { role = "developer", content = systemPrompt }, new { role = "system", content = systemPrompt },
new { role = "user", content = userPrompt } new { role = "user", content = userPrompt }
},
reasoning = new
{
effort = "low"
} }
}; };
@@ -54,7 +55,7 @@ namespace Mindforge.API.Providers
for (int i = 0; i < 5; i++) for (int i = 0; i < 5; i++)
{ {
using var request = new HttpRequestMessage(HttpMethod.Post, url); using var request = new HttpRequestMessage(HttpMethod.Post, url);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", apiKey); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
request.Content = new StringContent(jsonBody, Encoding.UTF8, "application/json"); request.Content = new StringContent(jsonBody, Encoding.UTF8, "application/json");
try try
@@ -64,47 +65,30 @@ namespace Mindforge.API.Providers
if (!response.IsSuccessStatusCode) if (!response.IsSuccessStatusCode)
{ {
lastErr = new Exception($"OpenAI API error status {(int)response.StatusCode}: {responseBody}"); lastErr = new Exception($"API error status {(int)response.StatusCode}: {responseBody}");
await Task.Delay(TimeSpan.FromSeconds(1 << i)); await Task.Delay(TimeSpan.FromSeconds(1 << i));
continue; continue;
} }
var result = JsonSerializer.Deserialize<JsonElement>(responseBody); var result = JsonSerializer.Deserialize<JsonElement>(responseBody);
if (result.TryGetProperty("output", out var outputArray)) if (result.TryGetProperty("choices", out var choices) && choices.GetArrayLength() > 0)
{ {
foreach (var outputItem in outputArray.EnumerateArray()) var message = choices[0].GetProperty("message");
{ return message.GetProperty("content").GetString() ?? string.Empty;
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); _logger.LogWarning("API raw response: {responseBody}", responseBody);
throw new Exception("Empty response from API.");
throw new Exception("empty response from OpenAI API");
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Error in OpenAI API request"); _logger.LogError(ex, "Error in API request");
lastErr = ex; lastErr = ex;
await Task.Delay(TimeSpan.FromSeconds(1 << i)); await Task.Delay(TimeSpan.FromSeconds(1 << i));
} }
} }
throw new Exception($"failed to get OpenAI response after 5 attempts. Last error: {lastErr?.Message}", lastErr); throw new Exception($"Failed to get response after 5 attempts. Last error: {lastErr?.Message}", lastErr);
}
public async Task<string> SendRequestBatchAsync(string systemPrompt, string userPrompt, string model)
{
throw new NotImplementedException();
} }
} }
} }

View File

@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Mindforge.API.Models.Enums;
using Mindforge.API.Providers; using Mindforge.API.Providers;
using Mindforge.API.Services.Interfaces; using Mindforge.API.Services.Interfaces;
@@ -10,39 +6,16 @@ namespace Mindforge.API.Services
{ {
public class AgentService : IAgentService public class AgentService : IAgentService
{ {
private readonly IEnumerable<ILlmApiProvider> _providers; private readonly ILlmApiProvider _provider;
public AgentService(IEnumerable<ILlmApiProvider> providers) public AgentService(ILlmApiProvider provider)
{ {
_providers = providers; _provider = provider;
} }
public Task<string> ProcessRequestAsync(LlmProvider providerEnum, string systemPrompt, string userPrompt, string model) public Task<string> ProcessRequestAsync(string systemPrompt, string userPrompt)
{ {
ILlmApiProvider provider = providerEnum switch return _provider.SendRequestAsync(systemPrompt, userPrompt);
{
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);
} }
} }
} }

View File

@@ -1,6 +1,4 @@
using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Mindforge.API.Models.Enums;
using Mindforge.API.Models.Requests; using Mindforge.API.Models.Requests;
using Mindforge.API.Services.Interfaces; using Mindforge.API.Services.Interfaces;
using Mindforge.API.Exceptions; using Mindforge.API.Exceptions;
@@ -11,9 +9,6 @@ namespace Mindforge.API.Services
{ {
private readonly IAgentService _agentService; private readonly IAgentService _agentService;
private const LlmProvider DefaultProvider = LlmProvider.OpenAI;
private const string DefaultModel = "gpt-5-mini";
public FileService(IAgentService agentService) public FileService(IAgentService agentService)
{ {
_agentService = agentService; _agentService = agentService;
@@ -55,7 +50,7 @@ Os resumos serão utilizados para concursos públicos, principalmente para a ban
string userPrompt = $"Arquivo: {request.FileName}\nConteúdo:\n{request.FileContent}"; string userPrompt = $"Arquivo: {request.FileName}\nConteúdo:\n{request.FileContent}";
return await _agentService.ProcessRequestAsync(DefaultProvider, systemPrompt, userPrompt, DefaultModel); return await _agentService.ProcessRequestAsync(systemPrompt, userPrompt);
} }
} }
} }

View File

@@ -1,5 +1,4 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using Mindforge.API.Models.Enums;
using Mindforge.API.Models.Requests; using Mindforge.API.Models.Requests;
using Mindforge.API.Services.Interfaces; using Mindforge.API.Services.Interfaces;
@@ -10,9 +9,6 @@ namespace Mindforge.API.Services
private readonly IAgentService _agentService; private readonly IAgentService _agentService;
private readonly ILogger<FlashcardService> _logger; 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) public FlashcardService(IAgentService agentService, ILogger<FlashcardService> logger)
{ {
_agentService = agentService; _agentService = agentService;
@@ -21,25 +17,12 @@ namespace Mindforge.API.Services
public async Task<string> GenerateFlashcardsAsync(FlashcardGenerateRequest request) public async Task<string> GenerateFlashcardsAsync(FlashcardGenerateRequest request)
{ {
var extraPrompt = ""; var extraPrompt = request.Mode switch
switch (request.Mode)
{ {
case FlashcardMode.Basic: FlashcardMode.Detailed => "Crie flashcards mais detalhados.",
DefaultModel = "gemini-3.1-flash-lite-preview"; FlashcardMode.Hyper => "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; _ => ""
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. 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. Baseado no texto fornecido, crie exatamente {request.Amount} flashcards que focam nos conceitos mais importantes e difíceis.
@@ -57,8 +40,7 @@ Com base no arquivo fornecido, crie exatamente {request.Amount} flashcards que f
string userPrompt = $"Arquivo: {request.FileName}\nConteúdo:\n{request.FileContent}"; string userPrompt = $"Arquivo: {request.FileName}\nConteúdo:\n{request.FileContent}";
//var result = await _agentService.ProcessRequestAsync(DefaultProvider, systemPrompt, userPrompt, DefaultModel); var result = await _agentService.ProcessRequestAsync(systemPrompt, userPrompt);
var result = await _agentService.ProcessRequestBatchAsync(DefaultProvider, systemPrompt, userPrompt, DefaultModel);
var lines = result.Split('\n'); var lines = result.Split('\n');

View File

@@ -1,11 +1,9 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using Mindforge.API.Models.Enums;
namespace Mindforge.API.Services.Interfaces namespace Mindforge.API.Services.Interfaces
{ {
public interface IAgentService public interface IAgentService
{ {
Task<string> ProcessRequestAsync(LlmProvider provider, string systemPrompt, string userPrompt, string model); Task<string> ProcessRequestAsync(string systemPrompt, string userPrompt);
Task<string> ProcessRequestBatchAsync(LlmProvider provider, string systemPrompt, string userPrompt, string model);
} }
} }

View File

@@ -6,8 +6,9 @@
} }
}, },
"AllowedHosts": "*", "AllowedHosts": "*",
"OPENAI_API_KEY": "", "OPENAI_API_URL": "https://openrouter.ai/api/v1",
"GEMINI_API_KEY": "", "OPENAI_TOKEN": "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",
"OPENAI_MODEL": "openai/gpt-5.4-mini",
"GITEA_REPO_URL": "", "GITEA_REPO_URL": "",
"GITEA_ACCESS_TOKEN": "" "GITEA_ACCESS_TOKEN": ""
} }

View File

@@ -20,16 +20,15 @@ spec:
ports: ports:
- containerPort: 8080 - containerPort: 8080
env: env:
- name: OPENAI_API_KEY - name: OPENAI_TOKEN
valueFrom: valueFrom:
secretKeyRef: secretKeyRef:
name: mindforge-secrets name: mindforge-secrets
key: OPENAI_API_KEY key: OPENAI_TOKEN
- name: GEMINI_API_KEY - name: OPENAI_API_URL
valueFrom: value: https://openrouter.ai/api/v1
secretKeyRef: - name: OPENAI_MODEL
name: mindforge-secrets value: openai/gpt-5.4-mini
key: GEMINI_API_KEY
- name: GITEA_REPO_URL - name: GITEA_REPO_URL
valueFrom: valueFrom:
secretKeyRef: secretKeyRef: