61 lines
1.5 KiB
Go
Raw Normal View History

2023-06-04 22:54:54 +08:00
package apps
import (
2024-02-21 12:04:40 +08:00
"context"
2023-06-04 22:54:54 +08:00
"github.com/labstack/echo/v4"
2024-09-29 14:08:55 +08:00
"mylomen_server/common/constant"
"mylomen_server/common/dto"
"mylomen_server/common/utils"
"mylomen_server/infrastructure/remote_http"
"mylomen_server/infrastructure/repository"
2023-06-04 22:54:54 +08:00
"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)
2024-02-21 12:04:40 +08:00
ctx := context.WithValue(context.Background(), constant.DBKey, repository.Db)
2023-06-04 22:54:54 +08:00
//groupId
2023-06-04 23:07:02 +08:00
key := utils.ToString(corpId) + "_" + utils.ToString(bizId) + "_" + utils.ToString(uid)
2023-06-04 22:54:54 +08:00
groupId := utils.GetCache(key)
result, err := remote_http.AiApi.Completions(ctx, req.Prompt, groupId)
2024-02-21 12:37:24 +08:00
if err != nil || result == nil {
return c.JSON(http.StatusOK, utils.Failed(err.Error()))
2023-06-04 22:54:54 +08:00
}
//缓存
groupId = result.GroupId
if groupId != "" {
utils.PutCache(key, groupId)
} else {
utils.Clear(key)
}
return c.JSON(http.StatusOK, utils.Ok(result.Completions))
})
2024-05-22 17:39:32 +08:00
g.POST("completionsTest", func(c echo.Context) error {
req := new(dto.AiReq)
if err := c.Bind(req); err != nil {
return c.JSON(500, err)
}
result, err := remote_http.AiOpenRouter.Completions(context.Background(), req.Prompt, "")
if err != nil || result == nil {
return c.JSON(http.StatusOK, utils.Failed(err.Error()))
}
return c.JSON(http.StatusOK, utils.Ok(result.Completions))
})
2023-06-04 22:54:54 +08:00
}