diff --git a/rest/controllers/api/ia_controller.go b/rest/controllers/api/ia_controller.go index 7d1e3d8..ae34a22 100644 --- a/rest/controllers/api/ia_controller.go +++ b/rest/controllers/api/ia_controller.go @@ -5,6 +5,8 @@ import ( "bytes" "encoding/json" "fmt" + "io" + "log" "net/http" "strings" "time" @@ -22,6 +24,7 @@ type DataIa struct { type respuestaOllama struct { Response string `json:"response"` Done bool `json:"done"` + Error string `json:"error"` } func GeneraTextoStream(c *fiber.Ctx) error { @@ -31,8 +34,10 @@ func GeneraTextoStream(c *fiber.Ctx) error { } data.Stream = true + hasDBConfig := true config, err := models.GetAiConfigForService("ia") if err != nil { + hasDBConfig = false config = &models.AiConfig{ BaseURL: "http://72.60.24.97:8080/ollama", ApiKey: "", @@ -49,6 +54,12 @@ func GeneraTextoStream(c *fiber.Ctx) error { baseURL := strings.TrimSuffix(strings.TrimRight(config.BaseURL, "/"), "/v1") endpoint := baseURL + "/api/generate" + if !hasDBConfig { + log.Printf("[IA] Usando config default: endpoint=%s model=%s", endpoint, data.Model) + } else { + log.Printf("[IA] Usando config BD: endpoint=%s model=%s", endpoint, data.Model) + } + jsonData, _ := json.Marshal(data) req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(jsonData)) if err != nil { @@ -62,31 +73,39 @@ func GeneraTextoStream(c *fiber.Ctx) error { client := &http.Client{Timeout: 120 * time.Second} resp, err := client.Do(req) if err != nil { + log.Printf("[IA] Error conectando a Ollama: %v", err) return c.Status(fiber.StatusBadGateway).JSON(fiber.Map{"error": err.Error()}) } if resp.StatusCode != http.StatusOK { + bodyBytes, _ := io.ReadAll(resp.Body) resp.Body.Close() + log.Printf("[IA] Ollama status %d: %s", resp.StatusCode, string(bodyBytes)) return c.Status(fiber.StatusBadGateway).JSON(fiber.Map{ - "error": fmt.Sprintf("Ollama respondió con status %d", resp.StatusCode), + "error": fmt.Sprintf("Ollama respondió con status %d", resp.StatusCode), + "details": string(bodyBytes), }) } c.Set("Content-Type", "text/plain; charset=utf-8") c.Set("Cache-Control", "no-cache") - c.Set("Connection", "keep-alive") + c.Set("X-Accel-Buffering", "no") 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 { - continue - } + dec := json.NewDecoder(resp.Body) + for { var msg respuestaOllama - if err := json.Unmarshal(line, &msg); err != nil { - continue + if err := dec.Decode(&msg); err != nil { + if err != io.EOF { + log.Printf("[IA] Error decodificando: %v", err) + } + break + } + if msg.Error != "" { + log.Printf("[IA] Ollama error: %s", msg.Error) + fmt.Fprintf(w, "[Error: %s]", msg.Error) + w.Flush() + break } if msg.Response != "" { fmt.Fprint(w, msg.Response)