fix: ia controller usa endpoint default si no hay config en BD + valida status HTTP de Ollama
This commit is contained in:
@@ -33,11 +33,12 @@ func GeneraTextoStream(c *fiber.Ctx) error {
|
||||
|
||||
config, err := models.GetAiConfigForService("ia")
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusServiceUnavailable).JSON(fiber.Map{
|
||||
"error": "No hay configuración de IA activa. Configura una en /app/ai-config",
|
||||
})
|
||||
config = &models.AiConfig{
|
||||
BaseURL: "http://72.60.24.97:8080/ollama",
|
||||
ApiKey: "",
|
||||
ModelName: "gemma3:1b",
|
||||
}
|
||||
}
|
||||
|
||||
if data.Model == "" {
|
||||
data.Model = config.ModelName
|
||||
}
|
||||
@@ -45,7 +46,6 @@ func GeneraTextoStream(c *fiber.Ctx) error {
|
||||
data.Model = "gemma3:1b"
|
||||
}
|
||||
|
||||
// Usar endpoint nativo de Ollama (/api/generate), no OpenAI compat (/v1/...)
|
||||
baseURL := strings.TrimSuffix(strings.TrimRight(config.BaseURL, "/"), "/v1")
|
||||
endpoint := baseURL + "/api/generate"
|
||||
|
||||
@@ -55,7 +55,6 @@ func GeneraTextoStream(c *fiber.Ctx) error {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
// Sin auth para red interna; api_key como token para URL pública
|
||||
if config.ApiKey != "" && config.ApiKey != "ollama" {
|
||||
req.SetBasicAuth("ollama", config.ApiKey)
|
||||
}
|
||||
@@ -65,6 +64,12 @@ func GeneraTextoStream(c *fiber.Ctx) error {
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusBadGateway).JSON(fiber.Map{"error": err.Error()})
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
resp.Body.Close()
|
||||
return c.Status(fiber.StatusBadGateway).JSON(fiber.Map{
|
||||
"error": fmt.Sprintf("Ollama respondió con status %d", resp.StatusCode),
|
||||
})
|
||||
}
|
||||
|
||||
c.Set("Content-Type", "text/plain; charset=utf-8")
|
||||
c.Set("Cache-Control", "no-cache")
|
||||
@@ -73,6 +78,7 @@ func GeneraTextoStream(c *fiber.Ctx) error {
|
||||
c.Context().SetBodyStreamWriter(func(w *bufio.Writer) {
|
||||
defer resp.Body.Close()
|
||||
scanner := bufio.NewScanner(resp.Body)
|
||||
scanner.Buffer(make([]byte, 0, 64*1024), 1024*1024)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Bytes()
|
||||
if len(line) == 0 {
|
||||
|
||||
Reference in New Issue
Block a user