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"
)
@ -23,12 +24,20 @@ type ChatCompletionResponse struct {
Choices []ChatCompletionChoice `json:"choices"`
}
func PostChatCompletion(prompt string, apiKey string) (string, error) {
func PostChatCompletion(prompt string, apiKey string, apiHost string) (string, error) {
requestBody := strings.NewReader(`{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "` + prompt + `"}]
}`)
req, err := http.NewRequest("POST", "https://api.openai.com/v1/chat/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
}