Files
soft_usite/rest/controllers/api/ia_controller.go
T

189 lines
4.5 KiB
Go

package controllers
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strings"
"time"
"github.com/gofiber/fiber/v2"
"github.com/sujit-baniya/fiber-boilerplate/pkg/models"
)
type DataIa struct {
Prompt string `json:"prompt"`
Model string `json:"model"`
Stream bool `json:"stream"`
}
type respuestaOllama struct {
Response string `json:"response"`
Done bool `json:"done"`
Error string `json:"error"`
}
type openaiChatMsg struct {
Role string `json:"role"`
Content string `json:"content"`
}
type openaiChatReq struct {
Model string `json:"model"`
Messages []openaiChatMsg `json:"messages"`
Stream bool `json:"stream"`
}
type openaiChatChoice struct {
Delta struct {
Content string `json:"content"`
} `json:"delta"`
FinishReason *string `json:"finish_reason"`
}
type openaiChatResp struct {
Choices []openaiChatChoice `json:"choices"`
}
func GeneraTextoStream(c *fiber.Ctx) error {
var data DataIa
if err := c.BodyParser(&data); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Datos inválidos"})
}
data.Stream = true
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",
})
}
if data.Model == "" {
data.Model = config.ModelName
}
if data.Model == "" {
data.Model = "gemma3:1b"
}
provider := strings.ToLower(config.Provider)
baseURL := strings.TrimRight(config.BaseURL, "/")
hasV1 := strings.Contains(baseURL, "/v1")
var endpoint string
var bodyReader io.Reader
useOpenAI := provider != "ollama" || hasV1
// OpenAI-compatible (openai, anthropic, qwen, o llameo con /v1)
if !useOpenAI {
// Native Ollama API
endpoint = baseURL + "/api/generate"
jsonData, _ := json.Marshal(data)
bodyReader = bytes.NewBuffer(jsonData)
} else {
// OpenAI-compatible (/v1/chat/completions)
baseURL = strings.TrimSuffix(baseURL, "/v1")
endpoint = baseURL + "/v1/chat/completions"
chatReq := openaiChatReq{
Model: data.Model,
Messages: []openaiChatMsg{
{Role: "user", Content: data.Prompt},
},
Stream: true,
}
jsonData, _ := json.Marshal(chatReq)
bodyReader = bytes.NewBuffer(jsonData)
}
log.Printf("[IA] endpoint=%s model=%s provider=%s", endpoint, data.Model, provider)
req, err := http.NewRequest("POST", endpoint, bodyReader)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
// Auth
if provider == "ollama" && config.ApiKey != "" && config.ApiKey != "ollama" {
req.SetBasicAuth("ollama", config.ApiKey)
} else if config.ApiKey != "" {
req.Header.Set("Authorization", "Bearer "+config.ApiKey)
}
client := &http.Client{Timeout: 120 * time.Second}
resp, err := client.Do(req)
if err != nil {
log.Printf("[IA] Error conectando: %v", err)
return c.Status(fiber.StatusBadGateway).JSON(fiber.Map{
"error": fmt.Sprintf("Error conectando a %s: %v", provider, err),
})
}
if resp.StatusCode != http.StatusOK {
bodyBytes, _ := io.ReadAll(resp.Body)
resp.Body.Close()
log.Printf("[IA] %s status %d: %s", provider, resp.StatusCode, string(bodyBytes))
return c.Status(fiber.StatusBadGateway).JSON(fiber.Map{
"error": fmt.Sprintf("%s respondió con status %d", provider, resp.StatusCode),
"details": string(bodyBytes),
})
}
c.Set("Content-Type", "text/plain; charset=utf-8")
c.Set("Cache-Control", "no-cache")
c.Set("X-Accel-Buffering", "no")
c.Context().SetBodyStreamWriter(func(w *bufio.Writer) {
defer resp.Body.Close()
dec := json.NewDecoder(resp.Body)
for {
if useOpenAI {
var msg openaiChatResp
if err := dec.Decode(&msg); err != nil {
if err != io.EOF {
log.Printf("[IA] Error decodificando respuesta OpenAI: %v", err)
}
break
}
if len(msg.Choices) == 0 {
continue
}
content := msg.Choices[0].Delta.Content
if content != "" {
fmt.Fprint(w, content)
w.Flush()
}
if msg.Choices[0].FinishReason != nil {
break
}
} else {
var msg respuestaOllama
if err := dec.Decode(&msg); err != nil {
if err != io.EOF {
log.Printf("[IA] Error decodificando respuesta Ollama: %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)
w.Flush()
}
if msg.Done {
break
}
}
}
})
return nil
}