2023-03-04 13:22:10 +03:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/usememos/memos/api"
|
|
|
|
"github.com/usememos/memos/common"
|
|
|
|
"github.com/usememos/memos/plugin/openai"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) registerOpenAIRoutes(g *echo.Group) {
|
2023-03-07 03:38:41 +03:00
|
|
|
g.POST("/openai/chat-completion", func(c echo.Context) error {
|
2023-03-04 13:22:10 +03:00
|
|
|
ctx := c.Request().Context()
|
2023-03-11 07:26:40 +03:00
|
|
|
openAIConfigSetting, err := s.Store.FindSystemSetting(ctx, &api.SystemSettingFind{
|
|
|
|
Name: api.SystemSettingOpenAIConfigName,
|
2023-03-04 13:22:10 +03:00
|
|
|
})
|
|
|
|
if err != nil && common.ErrorCode(err) != common.NotFound {
|
2023-03-11 07:26:40 +03:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find openai key").SetInternal(err)
|
2023-03-04 13:22:10 +03:00
|
|
|
}
|
|
|
|
|
2023-03-11 07:26:40 +03:00
|
|
|
openAIConfig := api.OpenAIConfig{}
|
|
|
|
if openAIConfigSetting != nil {
|
|
|
|
err = json.Unmarshal([]byte(openAIConfigSetting.Value), &openAIConfig)
|
2023-03-04 13:22:10 +03:00
|
|
|
if err != nil {
|
2023-03-11 07:26:40 +03:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to unmarshal openai system setting value").SetInternal(err)
|
2023-03-04 13:22:10 +03:00
|
|
|
}
|
|
|
|
}
|
2023-03-11 07:26:40 +03:00
|
|
|
if openAIConfig.Key == "" {
|
2023-03-04 13:22:10 +03:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "OpenAI API key not set")
|
|
|
|
}
|
|
|
|
|
2023-03-18 17:07:14 +03:00
|
|
|
messages := []openai.ChatCompletionMessage{}
|
|
|
|
if err := json.NewDecoder(c.Request().Body).Decode(&messages); err != nil {
|
2023-03-04 13:22:10 +03:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post chat completion request").SetInternal(err)
|
|
|
|
}
|
2023-03-18 17:07:14 +03:00
|
|
|
if len(messages) == 0 {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "No messages provided")
|
2023-03-04 13:22:10 +03:00
|
|
|
}
|
|
|
|
|
2023-03-18 17:07:14 +03:00
|
|
|
result, err := openai.PostChatCompletion(messages, openAIConfig.Key, openAIConfig.Host)
|
2023-03-04 13:22:10 +03:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to post chat completion").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, composeResponse(result))
|
|
|
|
})
|
|
|
|
|
2023-03-07 03:38:41 +03:00
|
|
|
g.GET("/openai/enabled", func(c echo.Context) error {
|
2023-03-04 13:22:10 +03:00
|
|
|
ctx := c.Request().Context()
|
2023-03-11 07:26:40 +03:00
|
|
|
openAIConfigSetting, err := s.Store.FindSystemSetting(ctx, &api.SystemSettingFind{
|
|
|
|
Name: api.SystemSettingOpenAIConfigName,
|
2023-03-04 13:22:10 +03:00
|
|
|
})
|
|
|
|
if err != nil && common.ErrorCode(err) != common.NotFound {
|
2023-03-11 07:26:40 +03:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find openai key").SetInternal(err)
|
2023-03-04 13:22:10 +03:00
|
|
|
}
|
|
|
|
|
2023-03-11 07:26:40 +03:00
|
|
|
openAIConfig := api.OpenAIConfig{}
|
|
|
|
if openAIConfigSetting != nil {
|
|
|
|
err = json.Unmarshal([]byte(openAIConfigSetting.Value), &openAIConfig)
|
2023-03-04 13:22:10 +03:00
|
|
|
if err != nil {
|
2023-03-11 07:26:40 +03:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to unmarshal openai system setting value").SetInternal(err)
|
2023-03-04 13:22:10 +03:00
|
|
|
}
|
|
|
|
}
|
2023-03-11 07:26:40 +03:00
|
|
|
if openAIConfig.Key == "" {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "OpenAI API key not set")
|
|
|
|
}
|
2023-03-04 13:22:10 +03:00
|
|
|
|
2023-03-11 07:26:40 +03:00
|
|
|
return c.JSON(http.StatusOK, composeResponse(openAIConfig.Key != ""))
|
2023-03-04 13:22:10 +03:00
|
|
|
})
|
|
|
|
}
|