🎨 接入云端人工智能接口公测 https://github.com/siyuan-note/siyuan/issues/7601

This commit is contained in:
Liang Ding 2023-03-08 12:12:38 +08:00
parent 11c5f14de6
commit 1c9e0356cc
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
3 changed files with 123 additions and 54 deletions

View file

@ -17,7 +17,6 @@
package util
import (
"bytes"
"context"
"net/http"
"net/url"
@ -37,52 +36,8 @@ var (
OpenAIAPIMaxTokens = 0
)
var cachedContextMsg []string
func ChatGPT(msg string) (ret string) {
ret, retCtxMsgs := ChatGPTContinueWrite(msg, cachedContextMsg)
cachedContextMsg = append(cachedContextMsg, retCtxMsgs...)
return
}
func ChatGPTWithAction(msg string, action string) (ret string) {
msg = action + ":\n\n" + msg
ret, _ = ChatGPTContinueWrite(msg, nil)
return
}
func ChatGPTContinueWrite(msg string, contextMsgs []string) (ret string, retContextMsgs []string) {
if "" == OpenAIAPIKey {
return
}
PushEndlessProgress("Requesting...")
defer ClearPushProgress(100)
c := newOpenAIClient()
buf := &bytes.Buffer{}
for i := 0; i < 7; i++ {
part, stop := chatGPT(msg, contextMsgs, c)
buf.WriteString(part)
if stop {
break
}
PushEndlessProgress("Continue requesting...")
}
ret = buf.String()
ret = strings.TrimSpace(ret)
retContextMsgs = append(retContextMsgs, msg, ret)
return
}
func chatGPT(msg string, contextMsgs []string, c *gogpt.Client) (ret string, stop bool) {
func ChatGPT(msg string, contextMsgs []string, c *gogpt.Client) (ret string, stop bool) {
var reqMsgs []gogpt.ChatCompletionMessage
if 7 < len(contextMsgs) {
contextMsgs = contextMsgs[len(contextMsgs)-7:]
}
for _, ctxMsg := range contextMsgs {
reqMsgs = append(reqMsgs, gogpt.ChatCompletionMessage{
@ -129,7 +84,7 @@ func chatGPT(msg string, contextMsgs []string, c *gogpt.Client) (ret string, sto
return
}
func newOpenAIClient() *gogpt.Client {
func NewOpenAIClient() *gogpt.Client {
config := gogpt.DefaultConfig(OpenAIAPIKey)
if "" != OpenAIAPIProxy {
proxyUrl, err := url.Parse(OpenAIAPIProxy)