Feat: Ajustes y Preparación Docker

This commit is contained in:
2025-11-20 12:39:23 -03:00
parent c94936d56e
commit 1e85b2ed86
11 changed files with 317 additions and 56 deletions

View File

@@ -48,7 +48,7 @@ namespace ChatbotApi.Controllers
var baseUrl = configuration["Gemini:GeminiApiUrl"];
_apiUrl = $"{baseUrl}{apiKey}";
}
private async Task<IntentType> GetIntentAsync(string userMessage, string? activeArticleContent)
{
var promptBuilder = new StringBuilder();
@@ -92,12 +92,12 @@ namespace ChatbotApi.Controllers
return IntentType.Homepage;
}
}
[HttpPost("stream-message")]
[EnableRateLimiting("fixed")]
public async IAsyncEnumerable<string> StreamMessage(
[FromBody] ChatRequest request,
[EnumeratorCancellation] CancellationToken cancellationToken)
[FromBody] ChatRequest request,
[EnumeratorCancellation] CancellationToken cancellationToken)
{
if (string.IsNullOrWhiteSpace(request?.Message))
{
@@ -111,6 +111,9 @@ namespace ChatbotApi.Controllers
string? articleContext = null;
string? errorMessage = null;
// --- CORRECCIÓN: Declarar e inicializar 'intent' aquí ---
IntentType intent = IntentType.Homepage; // Default fallback
try
{
if (!string.IsNullOrEmpty(request.ContextUrl))
@@ -118,7 +121,8 @@ namespace ChatbotApi.Controllers
articleContext = await GetArticleContentAsync(request.ContextUrl);
}
IntentType intent = await GetIntentAsync(userMessage, articleContext);
// Ya no se declara con "IntentType intent", solo se le asigna el valor.
intent = await GetIntentAsync(userMessage, articleContext);
switch (intent)
{
@@ -138,7 +142,7 @@ namespace ChatbotApi.Controllers
case IntentType.Homepage:
default:
_logger.LogInformation("Ejecutando intención: Noticias de Portada.");
context = await GetWebsiteNewsAsync(_siteUrl, 15);
context = await GetWebsiteNewsAsync(_siteUrl, 25);
promptInstructions = "Tu tarea es responder la 'PREGUNTA DEL USUARIO' basándote ESTRICTA Y ÚNICAMENTE en el 'CONTEXTO' que contiene una lista de noticias de portada. Si encuentras una noticia relevante, proporciona su enlace en formato Markdown: '[título](URL)'.";
break;
}
@@ -151,6 +155,9 @@ namespace ChatbotApi.Controllers
promptInstructions = string.Empty;
}
// Ahora 'intent' es accesible aquí.
yield return $"INTENT::{intent}";
if (!string.IsNullOrEmpty(errorMessage))
{
yield return errorMessage;
@@ -267,7 +274,7 @@ namespace ChatbotApi.Controllers
_logger.LogError(logEx, "Error al guardar el log de la conversación después del streaming.");
}
}
private async Task<string?> FindBestDbItemAsync(string userMessage, Dictionary<string, string> knowledgeBase)
{
if (knowledgeBase == null || !knowledgeBase.Any()) return null;

32
ChatbotApi/Dockerfile.api Normal file
View File

@@ -0,0 +1,32 @@
# Dockerfile.api
# ---- Etapa de Compilación (Build) ----
# Usamos la imagen del SDK de .NET para compilar la aplicación
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
WORKDIR /src
# Copiamos los archivos .csproj y restauramos las dependencias primero
# Esto aprovecha el cache de Docker para acelerar futuras compilaciones
COPY ["ChatbotApi.csproj", "."]
RUN dotnet restore "./ChatbotApi.csproj"
# Copiamos el resto del código fuente y construimos la aplicación
COPY . .
WORKDIR "/src/."
RUN dotnet build "ChatbotApi.csproj" -c Release -o /app/build
# Publicamos la aplicación en modo Release
FROM build AS publish
RUN dotnet publish "ChatbotApi.csproj" -c Release -o /app/publish /p:UseAppHost=false
# ---- Etapa Final (Runtime) ----
# Usamos la imagen de runtime de ASP.NET, que es mucho más ligera
FROM mcr.microsoft.com/dotnet/aspnet:10.0 AS final
WORKDIR /app
COPY --from=publish /app/publish .
# Exponemos el puerto 80, el puerto estándar para HTTP dentro del contenedor
EXPOSE 80
# El comando para iniciar la aplicación cuando el contenedor se ejecute
ENTRYPOINT ["dotnet", "ChatbotApi.dll"]

View File

@@ -20,7 +20,8 @@ builder.Services.AddCors(options =>
options.AddPolicy(name: myAllowSpecificOrigins,
policy =>
{
policy.WithOrigins("http://localhost:5173", "http://localhost:5174") // La URL de tu frontend
//policy.WithOrigins("http://localhost:5173", "http://localhost:5174")
policy.WithOrigins("http://192.168.5.129:8081", "http://192.168.5.129:8082")
.AllowAnyHeader()
.AllowAnyMethod();
});