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
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:
@@ -22,28 +22,29 @@ namespace Mindforge.API.Providers
|
||||
_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"];
|
||||
if (string.IsNullOrEmpty(apiKey))
|
||||
{
|
||||
throw new Exception("OPENAI_API_KEY not found in configuration.");
|
||||
}
|
||||
var apiUrl = _configuration["OPENAI_API_URL"];
|
||||
if (string.IsNullOrEmpty(apiUrl))
|
||||
throw new Exception("OPENAI_API_URL not found in configuration.");
|
||||
|
||||
var apiBase = "https://api.openai.com/v1";
|
||||
var url = $"{apiBase.TrimEnd('/')}/responses";
|
||||
var token = _configuration["OPENAI_TOKEN"];
|
||||
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
|
||||
{
|
||||
model = model,
|
||||
input = new[]
|
||||
messages = new[]
|
||||
{
|
||||
new { role = "developer", content = systemPrompt },
|
||||
new { role = "system", content = systemPrompt },
|
||||
new { role = "user", content = userPrompt }
|
||||
},
|
||||
reasoning = new
|
||||
{
|
||||
effort = "low"
|
||||
}
|
||||
};
|
||||
|
||||
@@ -54,7 +55,7 @@ namespace Mindforge.API.Providers
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
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");
|
||||
|
||||
try
|
||||
@@ -64,47 +65,30 @@ namespace Mindforge.API.Providers
|
||||
|
||||
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));
|
||||
continue;
|
||||
}
|
||||
|
||||
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())
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
var message = choices[0].GetProperty("message");
|
||||
return message.GetProperty("content").GetString() ?? string.Empty;
|
||||
}
|
||||
|
||||
_logger.LogWarning("OpenAI API raw response: {responseBody}", responseBody);
|
||||
|
||||
throw new Exception("empty response from OpenAI API");
|
||||
_logger.LogWarning("API raw response: {responseBody}", responseBody);
|
||||
throw new Exception("Empty response from API.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error in OpenAI API request");
|
||||
_logger.LogError(ex, "Error in 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();
|
||||
throw new Exception($"Failed to get response after 5 attempts. Last error: {lastErr?.Message}", lastErr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user