openai.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // SiYuan - Build Your Eternal Digital Garden
  2. // Copyright (c) 2020-present, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package util
  17. import (
  18. "bytes"
  19. "context"
  20. "net/http"
  21. "net/url"
  22. "os"
  23. "strconv"
  24. "strings"
  25. "time"
  26. gogpt "github.com/sashabaranov/go-gpt3"
  27. "github.com/siyuan-note/logging"
  28. )
  29. var (
  30. OpenAIAPIKey = ""
  31. OpenAIAPITimeout = 30 * time.Second
  32. OpenAIAPIProxy = ""
  33. OpenAIAPIMaxTokens = 0
  34. )
  35. var cachedContextMsg []string
  36. func ChatGPT(msg string) (ret string) {
  37. ret, retCtxMsgs := ChatGPTContinueWrite(msg, cachedContextMsg)
  38. cachedContextMsg = append(cachedContextMsg, retCtxMsgs...)
  39. return
  40. }
  41. func ChatGPTContinueWrite(msg string, contextMsgs []string) (ret string, retContextMsgs []string) {
  42. if "" == OpenAIAPIKey {
  43. return
  44. }
  45. PushEndlessProgress("Requesting...")
  46. defer ClearPushProgress(100)
  47. c := newOpenAIClient()
  48. buf := &bytes.Buffer{}
  49. for i := 0; i < 7; i++ {
  50. part, stop := chatGPT(msg, contextMsgs, c)
  51. buf.WriteString(part)
  52. if stop {
  53. break
  54. }
  55. PushEndlessProgress("Continue writing...")
  56. }
  57. ret = buf.String()
  58. ret = strings.TrimSpace(ret)
  59. retContextMsgs = append(retContextMsgs, msg, ret)
  60. return
  61. }
  62. func chatGPT(msg string, contextMsgs []string, c *gogpt.Client) (ret string, stop bool) {
  63. var reqMsgs []gogpt.ChatCompletionMessage
  64. if 7 < len(contextMsgs) {
  65. contextMsgs = contextMsgs[len(contextMsgs)-7:]
  66. }
  67. for _, ctxMsg := range contextMsgs {
  68. reqMsgs = append(reqMsgs, gogpt.ChatCompletionMessage{
  69. Role: "user",
  70. Content: ctxMsg,
  71. })
  72. }
  73. reqMsgs = append(reqMsgs, gogpt.ChatCompletionMessage{
  74. Role: "user",
  75. Content: msg,
  76. })
  77. req := gogpt.ChatCompletionRequest{
  78. Model: gogpt.GPT3Dot5Turbo,
  79. MaxTokens: OpenAIAPIMaxTokens,
  80. Messages: reqMsgs,
  81. }
  82. ctx, cancel := context.WithTimeout(context.Background(), OpenAIAPITimeout)
  83. defer cancel()
  84. resp, err := c.CreateChatCompletion(ctx, req)
  85. if nil != err {
  86. PushErrMsg("Requesting failed, please check kernel log for more details", 3000)
  87. logging.LogErrorf("create chat completion failed: %s", err)
  88. stop = true
  89. return
  90. }
  91. if 1 > len(resp.Choices) {
  92. stop = true
  93. return
  94. }
  95. buf := &strings.Builder{}
  96. choice := resp.Choices[0]
  97. buf.WriteString(choice.Message.Content)
  98. if "length" == choice.FinishReason {
  99. stop = false
  100. } else {
  101. stop = true
  102. }
  103. ret = buf.String()
  104. ret = strings.TrimSpace(ret)
  105. return
  106. }
  107. func newOpenAIClient() *gogpt.Client {
  108. config := gogpt.DefaultConfig(OpenAIAPIKey)
  109. if "" != OpenAIAPIProxy {
  110. proxyUrl, err := url.Parse(OpenAIAPIProxy)
  111. if nil != err {
  112. logging.LogErrorf("OpenAI API proxy failed: %v", err)
  113. } else {
  114. config.HTTPClient = &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyUrl)}}
  115. }
  116. }
  117. return gogpt.NewClientWithConfig(config)
  118. }
  119. func initOpenAI() {
  120. OpenAIAPIKey = os.Getenv("SIYUAN_OPENAI_API_KEY")
  121. if "" == OpenAIAPIKey {
  122. return
  123. }
  124. timeout := os.Getenv("SIYUAN_OPENAI_API_TIMEOUT")
  125. if "" != timeout {
  126. timeoutInt, err := strconv.Atoi(timeout)
  127. if nil == err {
  128. OpenAIAPITimeout = time.Duration(timeoutInt) * time.Second
  129. }
  130. }
  131. proxy := os.Getenv("SIYUAN_OPENAI_API_PROXY")
  132. if "" != proxy {
  133. OpenAIAPIProxy = proxy
  134. }
  135. maxTokens := os.Getenv("SIYUAN_OPENAI_API_MAX_TOKENS")
  136. if "" != maxTokens {
  137. maxTokensInt, err := strconv.Atoi(maxTokens)
  138. if nil == err {
  139. OpenAIAPIMaxTokens = maxTokensInt
  140. }
  141. }
  142. logging.LogInfof("OpenAI API enabled [maxTokens=%d, timeout=%ds, proxy=%s]", OpenAIAPIMaxTokens, int(OpenAIAPITimeout.Seconds()), OpenAIAPIProxy)
  143. }