This commit is contained in:
Liang Ding 2023-04-30 21:47:24 +08:00
parent f6f3ebbcb2
commit 7daafcb3af
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
13 changed files with 63 additions and 19 deletions

View file

@ -29,6 +29,7 @@ type OpenAI struct {
APIKey string `json:"apiKey"`
APITimeout int `json:"apiTimeout"`
APIProxy string `json:"apiProxy"`
APIModel string `json:"apiModel"`
APIMaxTokens int `json:"apiMaxTokens"`
APIBaseURL string `json:"apiBaseURL"`
}

View file

@ -42,7 +42,7 @@ require (
github.com/panjf2000/ants/v2 v2.7.3
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/radovskyb/watcher v1.0.7
github.com/sashabaranov/go-gpt3 v1.4.0
github.com/sashabaranov/go-openai v1.9.0
github.com/shirou/gopsutil/v3 v3.23.2
github.com/siyuan-note/dejavu v0.0.0-20230425070132-9eeaf90cb5ba
github.com/siyuan-note/encryption v0.0.0-20220713091850-5ecd92177b75

View file

@ -267,8 +267,8 @@ github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6po
github.com/rwtodd/Go.Sed v0.0.0-20210816025313-55464686f9ef/go.mod h1:8AEUvGVi2uQ5b24BIhcr0GCcpd/RNAFWaN2CJFrWIIQ=
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 h1:OkMGxebDjyw0ULyrTYWeN0UNCCkmCWfjPnIA2W6oviI=
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06/go.mod h1:+ePHsJ1keEjQtpvf9HHw0f4ZeJ0TLRsxhunSI2hYJSs=
github.com/sashabaranov/go-gpt3 v1.4.0 h1:UqHYdXgJNtNvTtbzDnnQgkQ9TgTnHtCXx966uFTYXvU=
github.com/sashabaranov/go-gpt3 v1.4.0/go.mod h1:BIZdbwdzxZbCrcKGMGH6u2eyGe1xFuX9Anmh3tCP8lQ=
github.com/sashabaranov/go-openai v1.9.0 h1:NoiO++IISxxJ1pRc0n7uZvMGMake0G+FJ1XPwXtprsA=
github.com/sashabaranov/go-openai v1.9.0/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
github.com/scylladb/termtables v0.0.0-20191203121021-c4c0b6d42ff4/go.mod h1:C1a7PQSMz9NShzorzCiG2fk9+xuCgLkPeCvMHYR2OWg=
github.com/shirou/gopsutil/v3 v3.23.2 h1:PAWSuiAszn7IhPMBtXsbSCafej7PqUOvY6YywlQUExU=
github.com/shirou/gopsutil/v3 v3.23.2/go.mod h1:gv0aQw33GLo3pG8SiWKiQrbDzbRY1K80RyZJ7V4Th1M=

View file

@ -22,7 +22,7 @@ import (
"github.com/88250/lute/ast"
"github.com/88250/lute/parse"
gogpt "github.com/sashabaranov/go-gpt3"
"github.com/sashabaranov/go-openai"
"github.com/siyuan-note/siyuan/kernel/treenode"
"github.com/siyuan-note/siyuan/kernel/util"
)
@ -154,11 +154,11 @@ type GPT interface {
}
type OpenAIGPT struct {
c *gogpt.Client
c *openai.Client
}
func (gpt *OpenAIGPT) chat(msg string, contextMsgs []string) (partRet string, stop bool, err error) {
return util.ChatGPT(msg, contextMsgs, gpt.c, Conf.AI.OpenAI.APIMaxTokens, Conf.AI.OpenAI.APITimeout)
return util.ChatGPT(msg, contextMsgs, gpt.c, Conf.AI.OpenAI.APIModel, Conf.AI.OpenAI.APIMaxTokens, Conf.AI.OpenAI.APITimeout)
}
type CloudGPT struct {

View file

@ -19,6 +19,7 @@ package model
import (
"bytes"
"fmt"
"github.com/sashabaranov/go-openai"
"os"
"path/filepath"
"runtime"
@ -337,14 +338,18 @@ func InitConf() {
if nil == Conf.AI {
Conf.AI = conf.NewAI()
}
if "" == Conf.AI.OpenAI.APIModel {
Conf.AI.OpenAI.APIModel = openai.GPT4
}
if "" != Conf.AI.OpenAI.APIKey {
logging.LogInfof("OpenAI API enabled\n"+
" baseURL=%s\n"+
" timeout=%ds\n"+
" proxy=%s\n"+
" model=%s\n"+
" maxTokens=%d",
Conf.AI.OpenAI.APIBaseURL, Conf.AI.OpenAI.APITimeout, Conf.AI.OpenAI.APIProxy, Conf.AI.OpenAI.APIMaxTokens)
Conf.AI.OpenAI.APIBaseURL, Conf.AI.OpenAI.APITimeout, Conf.AI.OpenAI.APIProxy, Conf.AI.OpenAI.APIModel, Conf.AI.OpenAI.APIMaxTokens)
}
Conf.ReadOnly = util.ReadOnly

View file

@ -18,30 +18,31 @@ package util
import (
"context"
gogpt "github.com/sashabaranov/go-gpt3"
"github.com/siyuan-note/logging"
"net/http"
"net/url"
"strings"
"time"
"github.com/sashabaranov/go-openai"
"github.com/siyuan-note/logging"
)
func ChatGPT(msg string, contextMsgs []string, c *gogpt.Client, maxTokens, timeout int) (ret string, stop bool, err error) {
var reqMsgs []gogpt.ChatCompletionMessage
func ChatGPT(msg string, contextMsgs []string, c *openai.Client, model string, maxTokens, timeout int) (ret string, stop bool, err error) {
var reqMsgs []openai.ChatCompletionMessage
for _, ctxMsg := range contextMsgs {
reqMsgs = append(reqMsgs, gogpt.ChatCompletionMessage{
reqMsgs = append(reqMsgs, openai.ChatCompletionMessage{
Role: "user",
Content: ctxMsg,
})
}
reqMsgs = append(reqMsgs, gogpt.ChatCompletionMessage{
reqMsgs = append(reqMsgs, openai.ChatCompletionMessage{
Role: "user",
Content: msg,
})
req := gogpt.ChatCompletionRequest{
Model: gogpt.GPT3Dot5Turbo,
req := openai.ChatCompletionRequest{
Model: model,
MaxTokens: maxTokens,
Messages: reqMsgs,
}
@ -74,8 +75,8 @@ func ChatGPT(msg string, contextMsgs []string, c *gogpt.Client, maxTokens, timeo
return
}
func NewOpenAIClient(apiKey, apiProxy, apiBaseURL string) *gogpt.Client {
config := gogpt.DefaultConfig(apiKey)
func NewOpenAIClient(apiKey, apiProxy, apiBaseURL string) *openai.Client {
config := openai.DefaultConfig(apiKey)
if "" != apiProxy {
proxyUrl, err := url.Parse(apiProxy)
if nil != err {
@ -86,5 +87,5 @@ func NewOpenAIClient(apiKey, apiProxy, apiBaseURL string) *gogpt.Client {
}
config.BaseURL = apiBaseURL
return gogpt.NewClientWithConfig(config)
return openai.NewClientWithConfig(config)
}