openai.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package server
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "github.com/labstack/echo/v4"
  6. "github.com/usememos/memos/api"
  7. "github.com/usememos/memos/common"
  8. "github.com/usememos/memos/plugin/openai"
  9. )
  10. func (s *Server) registerOpenAIRoutes(g *echo.Group) {
  11. g.POST("/opanai/chat-completion", func(c echo.Context) error {
  12. ctx := c.Request().Context()
  13. openAIApiKeySetting, err := s.Store.FindSystemSetting(ctx, &api.SystemSettingFind{
  14. Name: api.SystemSettingOpenAIAPIKeyName,
  15. })
  16. if err != nil && common.ErrorCode(err) != common.NotFound {
  17. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find openai api key").SetInternal(err)
  18. }
  19. openAIApiKey := ""
  20. if openAIApiKeySetting != nil {
  21. err = json.Unmarshal([]byte(openAIApiKeySetting.Value), &openAIApiKey)
  22. if err != nil {
  23. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to unmarshal system setting value").SetInternal(err)
  24. }
  25. }
  26. if openAIApiKey == "" {
  27. return echo.NewHTTPError(http.StatusBadRequest, "OpenAI API key not set")
  28. }
  29. completionRequest := api.OpenAICompletionRequest{}
  30. if err := json.NewDecoder(c.Request().Body).Decode(&completionRequest); err != nil {
  31. return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post chat completion request").SetInternal(err)
  32. }
  33. if completionRequest.Prompt == "" {
  34. return echo.NewHTTPError(http.StatusBadRequest, "Prompt is required")
  35. }
  36. result, err := openai.PostChatCompletion(completionRequest.Prompt, openAIApiKey)
  37. if err != nil {
  38. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to post chat completion").SetInternal(err)
  39. }
  40. return c.JSON(http.StatusOK, composeResponse(result))
  41. })
  42. g.POST("/opanai/text-completion", func(c echo.Context) error {
  43. ctx := c.Request().Context()
  44. openAIApiKeySetting, err := s.Store.FindSystemSetting(ctx, &api.SystemSettingFind{
  45. Name: api.SystemSettingOpenAIAPIKeyName,
  46. })
  47. if err != nil && common.ErrorCode(err) != common.NotFound {
  48. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find openai api key").SetInternal(err)
  49. }
  50. openAIApiKey := ""
  51. if openAIApiKeySetting != nil {
  52. err = json.Unmarshal([]byte(openAIApiKeySetting.Value), &openAIApiKey)
  53. if err != nil {
  54. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to unmarshal system setting value").SetInternal(err)
  55. }
  56. }
  57. if openAIApiKey == "" {
  58. return echo.NewHTTPError(http.StatusBadRequest, "OpenAI API key not set")
  59. }
  60. textCompletion := api.OpenAICompletionRequest{}
  61. if err := json.NewDecoder(c.Request().Body).Decode(&textCompletion); err != nil {
  62. return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post text completion request").SetInternal(err)
  63. }
  64. if textCompletion.Prompt == "" {
  65. return echo.NewHTTPError(http.StatusBadRequest, "Prompt is required")
  66. }
  67. result, err := openai.PostTextCompletion(textCompletion.Prompt, openAIApiKey)
  68. if err != nil {
  69. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to post text completion").SetInternal(err)
  70. }
  71. return c.JSON(http.StatusOK, composeResponse(result))
  72. })
  73. g.GET("/opanai/enabled", func(c echo.Context) error {
  74. ctx := c.Request().Context()
  75. openAIApiKeySetting, err := s.Store.FindSystemSetting(ctx, &api.SystemSettingFind{
  76. Name: api.SystemSettingOpenAIAPIKeyName,
  77. })
  78. if err != nil && common.ErrorCode(err) != common.NotFound {
  79. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find openai api key").SetInternal(err)
  80. }
  81. openAIApiKey := ""
  82. if openAIApiKeySetting != nil {
  83. err = json.Unmarshal([]byte(openAIApiKeySetting.Value), &openAIApiKey)
  84. if err != nil {
  85. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to unmarshal system setting value").SetInternal(err)
  86. }
  87. }
  88. return c.JSON(http.StatusOK, composeResponse(openAIApiKey != ""))
  89. })
  90. }