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

@ -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)
}