mirror of
https://github.com/usememos/memos.git
synced 2025-03-21 05:00:39 +01:00
fix: request body format in openai api (#1309)
This commit is contained in:
parent
ce7564a91b
commit
ccf6af4dc3
@ -1,12 +1,12 @@
|
|||||||
package openai
|
package openai
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type ChatCompletionMessage struct {
|
type ChatCompletionMessage struct {
|
||||||
@ -25,10 +25,6 @@ type ChatCompletionResponse struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func PostChatCompletion(prompt string, apiKey string, apiHost 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 + `"}]
|
|
||||||
}`)
|
|
||||||
if apiHost == "" {
|
if apiHost == "" {
|
||||||
apiHost = "https://api.openai.com"
|
apiHost = "https://api.openai.com"
|
||||||
}
|
}
|
||||||
@ -37,7 +33,16 @@ func PostChatCompletion(prompt string, apiKey string, apiHost string) (string, e
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", url, requestBody)
|
values := map[string]interface{}{
|
||||||
|
"model": "gpt-3.5-turbo",
|
||||||
|
"messages": []map[string]string{{"role": "user", "content": prompt}},
|
||||||
|
}
|
||||||
|
jsonValue, err := json.Marshal(values)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonValue))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
package openai
|
package openai
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type TextCompletionChoice struct {
|
type TextCompletionChoice struct {
|
||||||
@ -20,13 +20,6 @@ type TextCompletionResponse struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func PostTextCompletion(prompt string, apiKey string, apiHost string) (string, error) {
|
func PostTextCompletion(prompt string, apiKey string, apiHost string) (string, error) {
|
||||||
requestBody := strings.NewReader(`{
|
|
||||||
"prompt": "` + prompt + `",
|
|
||||||
"temperature": 0.5,
|
|
||||||
"max_tokens": 100,
|
|
||||||
"n": 1,
|
|
||||||
"stop": "."
|
|
||||||
}`)
|
|
||||||
if apiHost == "" {
|
if apiHost == "" {
|
||||||
apiHost = "https://api.openai.com"
|
apiHost = "https://api.openai.com"
|
||||||
}
|
}
|
||||||
@ -35,7 +28,20 @@ func PostTextCompletion(prompt string, apiKey string, apiHost string) (string, e
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", url, requestBody)
|
values := map[string]interface{}{
|
||||||
|
"model": "gpt-3.5-turbo",
|
||||||
|
"prompt": prompt,
|
||||||
|
"temperature": 0.5,
|
||||||
|
"max_tokens": 100,
|
||||||
|
"n": 1,
|
||||||
|
"stop": ".",
|
||||||
|
}
|
||||||
|
jsonValue, err := json.Marshal(values)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonValue))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user