2023-06-04 22:54:54 +08:00

47 lines
1.0 KiB
Go

package apps
import (
"ai-gateway/common/constant"
"ai-gateway/common/dto"
"ai-gateway/common/utils"
"ai-gateway/infrastructure/remote_http"
"ai-gateway/infrastructure/repository"
"code.freebrio.com/fb-go/lib/fbl"
"github.com/labstack/echo/v4"
"net/http"
)
func InitAiGroup(g *echo.Group) {
g.POST("completions", func(c echo.Context) error {
req := new(dto.AiReq)
if err := c.Bind(req); err != nil {
return c.JSON(500, err)
}
corpId := c.Get(constant.CorpId).(uint64)
bizId := c.Get(constant.BizId).(uint64)
uid := c.Get(constant.UID).(uint64)
ctx := repository.GetFbc(&c)
//groupId
key := fbl.ToString(corpId) + "_" + fbl.ToString(bizId) + "_" + fbl.ToString(uid)
groupId := utils.GetCache(key)
result, err := remote_http.AiApi.Completions(ctx, req.Prompt, groupId)
if err != nil {
return c.JSON(500, err)
}
//缓存
groupId = result.GroupId
if groupId != "" {
utils.PutCache(key, groupId)
} else {
utils.Clear(key)
}
return c.JSON(http.StatusOK, utils.Ok(result.Completions))
})
}