feat: support set openai api host (#1292)

* Docker

* feat: support set openai api host

* fix css

* fix eslint

* use API in backend & put host check in plugin/openai

* fix go-static-checks
This commit is contained in:
Wujiao233
2023-03-06 20:10:53 +08:00
committed by GitHub
parent fd99c5461c
commit 003161ea54
9 changed files with 108 additions and 6 deletions

View File

@ -5,6 +5,7 @@ import (
"errors"
"io"
"net/http"
"net/url"
"strings"
)
@ -18,7 +19,7 @@ type TextCompletionResponse struct {
Choices []TextCompletionChoice `json:"choices"`
}
func PostTextCompletion(prompt string, apiKey string) (string, error) {
func PostTextCompletion(prompt string, apiKey string, apiHost string) (string, error) {
requestBody := strings.NewReader(`{
"prompt": "` + prompt + `",
"temperature": 0.5,
@ -26,7 +27,15 @@ func PostTextCompletion(prompt string, apiKey string) (string, error) {
"n": 1,
"stop": "."
}`)
req, err := http.NewRequest("POST", "https://api.openai.com/v1/completions", requestBody)
if apiHost == "" {
apiHost = "https://api.openai.com"
}
url, err := url.JoinPath(apiHost, "/v1/chat/completions")
if err != nil {
return "", err
}
req, err := http.NewRequest("POST", url, requestBody)
if err != nil {
return "", err
}