ia ollama
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
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"`
|
||||
}
|
||||
|
||||
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"})
|
||||
}
|
||||
|
||||
// Forzar stream = true
|
||||
data.Stream = true
|
||||
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", "http://72.60.24.97:8080/ollama/api/generate", bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer sk-43804e00f5294200ad1e599550fbee4f")
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Configurar headers para streaming
|
||||
c.Set("Content-Type", "text/plain; charset=utf-8")
|
||||
c.Set("Cache-Control", "no-cache")
|
||||
c.Set("Connection", "keep-alive")
|
||||
|
||||
c.Context().SetBodyStreamWriter(func(w *bufio.Writer) {
|
||||
defer resp.Body.Close()
|
||||
|
||||
scanner := bufio.NewScanner(resp.Body)
|
||||
|
||||
for scanner.Scan() {
|
||||
line := scanner.Bytes()
|
||||
if len(line) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
var msg respuestaOllama
|
||||
if err := json.Unmarshal(line, &msg); err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if msg.Response != "" {
|
||||
fmt.Fprint(w, msg.Response)
|
||||
w.Flush() // <-- envía inmediatamente
|
||||
}
|
||||
|
||||
if msg.Done {
|
||||
break
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ package controllers
|
||||
import (
|
||||
"time"
|
||||
|
||||
"net/url"
|
||||
|
||||
"github.com/gofiber/fiber/v2" //nolint:goimports
|
||||
"github.com/gofiber/fiber/v2/middleware/session"
|
||||
"github.com/sujit-baniya/fiber-boilerplate/app"
|
||||
@@ -19,30 +21,21 @@ func LoginGet(c *fiber.Ctx) error {
|
||||
func LoginPost(c *fiber.Ctx) error {
|
||||
// Verificar si falta la contraseña
|
||||
if c.Locals("missing_password") == true {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
"status": "missing",
|
||||
"message": "no_password",
|
||||
})
|
||||
return c.Redirect("/update-credentials") // Redirige directamente
|
||||
}
|
||||
|
||||
// Obtener el usuario desde el contexto
|
||||
user := c.Locals("user").(*models.Users)
|
||||
|
||||
// Intentar realizar el login
|
||||
_, err := auth.Login(c, user.ID, app.Http.Token.AppJwtSecret) //nolint:wsl
|
||||
_, err := auth.Login(c, user.ID, app.Http.Token.AppJwtSecret)
|
||||
if err != nil {
|
||||
// Si ocurre un error, devolverlo en formato JSON
|
||||
return c.JSON(fiber.Map{
|
||||
"status": "error",
|
||||
"message": err.Error(),
|
||||
})
|
||||
// Si hay error, podrías mandar de nuevo al login con mensaje
|
||||
return c.Redirect("/login?error=" + url.QueryEscape(err.Error()))
|
||||
}
|
||||
|
||||
// Si todo está bien, devolver éxito y el token JWT
|
||||
return c.JSON(fiber.Map{
|
||||
"status": "success",
|
||||
"message": "Login exitoso",
|
||||
})
|
||||
// Si todo está bien, redirige al inicio de la app
|
||||
return c.Redirect("/app/servidor")
|
||||
}
|
||||
|
||||
var store = session.New()
|
||||
|
||||
@@ -23,6 +23,7 @@ func v1AuthRoutes(api fiber.Router) {
|
||||
api.Post("/dlocal/payment/crear-pago", apiControllers.CreatePago)
|
||||
api.Post("/rapyd/wallet/create", apiControllers.MakeWallet)
|
||||
api.Post("/generate-url-qr", apiControllers.CreateUrlQr)
|
||||
api.Post("/ia/ollama-generate", apiControllers.GeneraTextoStream)
|
||||
}
|
||||
|
||||
func v1Routes(api fiber.Router) {
|
||||
|
||||
Reference in New Issue
Block a user