Fix Max OutTokens
This commit is contained in:
@@ -12,7 +12,21 @@ using System.Runtime.CompilerServices;
|
|||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
|
||||||
// Clases de Request/Response
|
// Clases de Request/Response
|
||||||
public class GeminiRequest { [JsonPropertyName("contents")] public Content[] Contents { get; set; } = default!; }
|
public class GenerationConfig
|
||||||
|
{
|
||||||
|
[JsonPropertyName("maxOutputTokens")]
|
||||||
|
public int MaxOutputTokens { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class GeminiRequest
|
||||||
|
{
|
||||||
|
[JsonPropertyName("contents")]
|
||||||
|
public Content[] Contents { get; set; } = default!;
|
||||||
|
|
||||||
|
[JsonPropertyName("generationConfig")]
|
||||||
|
public GenerationConfig? GenerationConfig { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
public class Content { [JsonPropertyName("parts")] public Part[] Parts { get; set; } = default!; }
|
public class Content { [JsonPropertyName("parts")] public Part[] Parts { get; set; } = default!; }
|
||||||
public class Part { [JsonPropertyName("text")] public string Text { get; set; } = default!; }
|
public class Part { [JsonPropertyName("text")] public string Text { get; set; } = default!; }
|
||||||
public class GeminiResponse { [JsonPropertyName("candidates")] public Candidate[] Candidates { get; set; } = default!; }
|
public class GeminiResponse { [JsonPropertyName("candidates")] public Candidate[] Candidates { get; set; } = default!; }
|
||||||
@@ -36,6 +50,7 @@ namespace ChatbotApi.Controllers
|
|||||||
|
|
||||||
private static readonly string _siteUrl = "https://www.eldia.com/";
|
private static readonly string _siteUrl = "https://www.eldia.com/";
|
||||||
private static readonly string[] PrefijosAQuitar = { "VIDEO.- ", "VIDEO. ", "FOTOS.- ", "FOTOS. " };
|
private static readonly string[] PrefijosAQuitar = { "VIDEO.- ", "VIDEO. ", "FOTOS.- ", "FOTOS. " };
|
||||||
|
const int OutTokens = 8192;
|
||||||
|
|
||||||
public ChatController(IConfiguration configuration, IMemoryCache memoryCache, IServiceProvider serviceProvider, ILogger<ChatController> logger)
|
public ChatController(IConfiguration configuration, IMemoryCache memoryCache, IServiceProvider serviceProvider, ILogger<ChatController> logger)
|
||||||
{
|
{
|
||||||
@@ -110,9 +125,7 @@ namespace ChatbotApi.Controllers
|
|||||||
string promptInstructions = "";
|
string promptInstructions = "";
|
||||||
string? articleContext = null;
|
string? articleContext = null;
|
||||||
string? errorMessage = null;
|
string? errorMessage = null;
|
||||||
|
IntentType intent = IntentType.Homepage;
|
||||||
// --- CORRECCIÓN: Declarar e inicializar 'intent' aquí ---
|
|
||||||
IntentType intent = IntentType.Homepage; // Default fallback
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -121,7 +134,6 @@ namespace ChatbotApi.Controllers
|
|||||||
articleContext = await GetArticleContentAsync(request.ContextUrl);
|
articleContext = await GetArticleContentAsync(request.ContextUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ya no se declara con "IntentType intent", solo se le asigna el valor.
|
|
||||||
intent = await GetIntentAsync(userMessage, articleContext);
|
intent = await GetIntentAsync(userMessage, articleContext);
|
||||||
|
|
||||||
switch (intent)
|
switch (intent)
|
||||||
@@ -151,11 +163,8 @@ namespace ChatbotApi.Controllers
|
|||||||
{
|
{
|
||||||
_logger.LogError(ex, "Error al procesar la intención y el contexto.");
|
_logger.LogError(ex, "Error al procesar la intención y el contexto.");
|
||||||
errorMessage = "Error: Lo siento, estoy teniendo un problema técnico al procesar tu pregunta.";
|
errorMessage = "Error: Lo siento, estoy teniendo un problema técnico al procesar tu pregunta.";
|
||||||
context = string.Empty;
|
|
||||||
promptInstructions = string.Empty;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ahora 'intent' es accesible aquí.
|
|
||||||
yield return $"INTENT::{intent}";
|
yield return $"INTENT::{intent}";
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(errorMessage))
|
if (!string.IsNullOrEmpty(errorMessage))
|
||||||
@@ -169,7 +178,7 @@ namespace ChatbotApi.Controllers
|
|||||||
{
|
{
|
||||||
var promptBuilder = new StringBuilder();
|
var promptBuilder = new StringBuilder();
|
||||||
promptBuilder.AppendLine("INSTRUCCIONES:");
|
promptBuilder.AppendLine("INSTRUCCIONES:");
|
||||||
promptBuilder.AppendLine("Eres DiaBot, el asistente virtual del periódico El Día. Tu personalidad es profesional, servicial y concisa.");
|
promptBuilder.AppendLine("Eres DiaBot, el asistente virtual del periódico El Día. Tu personalidad es profesional, servicial y concisa. Responde siempre en español Rioplatense.");
|
||||||
promptBuilder.AppendLine(promptInstructions);
|
promptBuilder.AppendLine(promptInstructions);
|
||||||
promptBuilder.AppendLine("NUNCA INVENTES información. Si la respuesta no está en el contexto, indica amablemente que no encontraste la información.");
|
promptBuilder.AppendLine("NUNCA INVENTES información. Si la respuesta no está en el contexto, indica amablemente que no encontraste la información.");
|
||||||
promptBuilder.AppendLine("\nCONTEXTO:\n---");
|
promptBuilder.AppendLine("\nCONTEXTO:\n---");
|
||||||
@@ -180,7 +189,13 @@ namespace ChatbotApi.Controllers
|
|||||||
string finalPrompt = promptBuilder.ToString();
|
string finalPrompt = promptBuilder.ToString();
|
||||||
|
|
||||||
var streamingApiUrl = _apiUrl;
|
var streamingApiUrl = _apiUrl;
|
||||||
var requestData = new GeminiRequest { Contents = new[] { new Content { Parts = new[] { new Part { Text = finalPrompt } } } } };
|
|
||||||
|
var requestData = new GeminiRequest
|
||||||
|
{
|
||||||
|
Contents = new[] { new Content { Parts = new[] { new Part { Text = finalPrompt } } } },
|
||||||
|
GenerationConfig = new GenerationConfig { MaxOutputTokens = OutTokens }
|
||||||
|
};
|
||||||
|
|
||||||
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, streamingApiUrl);
|
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, streamingApiUrl);
|
||||||
httpRequestMessage.Content = JsonContent.Create(requestData);
|
httpRequestMessage.Content = JsonContent.Create(requestData);
|
||||||
|
|
||||||
@@ -189,17 +204,12 @@ namespace ChatbotApi.Controllers
|
|||||||
if (!response.IsSuccessStatusCode)
|
if (!response.IsSuccessStatusCode)
|
||||||
{
|
{
|
||||||
var errorContent = await response.Content.ReadAsStringAsync();
|
var errorContent = await response.Content.ReadAsStringAsync();
|
||||||
_logger.LogWarning("La API de Gemini (Streaming) devolvió un error. Status: {StatusCode}, Content: {ErrorContent}", response.StatusCode, errorContent);
|
_logger.LogWarning("La API (Streaming) devolvió un error. Status: {StatusCode}, Content: {ErrorContent}", response.StatusCode, errorContent);
|
||||||
throw new HttpRequestException("La API de Gemini devolvió un error.");
|
throw new HttpRequestException("La API devolvió un error.");
|
||||||
}
|
}
|
||||||
|
|
||||||
responseStream = await response.Content.ReadAsStreamAsync(cancellationToken);
|
responseStream = await response.Content.ReadAsStreamAsync(cancellationToken);
|
||||||
}
|
}
|
||||||
catch (TaskCanceledException)
|
|
||||||
{
|
|
||||||
_logger.LogInformation("La operación fue cancelada por el cliente durante la configuración del stream.");
|
|
||||||
yield break;
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "Error inesperado durante la configuración del stream.");
|
_logger.LogError(ex, "Error inesperado durante la configuración del stream.");
|
||||||
|
|||||||
Reference in New Issue
Block a user