Merge pull request #855 from trheyi/main

Update OpenAI test and API configuration
This commit is contained in:
Max 2025-02-08 16:01:12 +08:00 committed by GitHub
commit ed4553f155
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 57 additions and 34 deletions

View file

@ -221,11 +221,12 @@ func (openai OpenAI) ChatCompletionsWith(ctx context.Context, messages []map[str
// Edits Creates a new edit for the provided input, instruction, and parameters. // Edits Creates a new edit for the provided input, instruction, and parameters.
// https://platform.openai.com/docs/api-reference/edits/create // https://platform.openai.com/docs/api-reference/edits/create
func (openai OpenAI) Edits(instruction string, option map[string]interface{}) (interface{}, *exception.Exception) { func (openai OpenAI) Edits(instruction string, option map[string]interface{}) (interface{}, *exception.Exception) {
if option == nil { return nil, exception.New("Edits is not deprecated", 404)
option = map[string]interface{}{} // if option == nil {
} // option = map[string]interface{}{}
option["instruction"] = instruction // }
return openai.post(openai.baseURL+"/edits", option) // option["instruction"] = instruction
// return openai.post(openai.baseURL+"/edits", option)
} }
// Embeddings Creates an embedding vector representing the input text. // Embeddings Creates an embedding vector representing the input text.
@ -377,9 +378,15 @@ func (openai OpenAI) post(path string, payload map[string]interface{}) (interfac
req := http.New(url) req := http.New(url)
if openai.azure { if openai.azure {
req.WithHeader(map[string][]string{"api-key": {openai.key}}) req.WithHeader(map[string][]string{
"Content-Type": {"application/json; charset=utf-8"},
"api-key": {openai.key},
})
} else { } else {
req.WithHeader(map[string][]string{"Authorization": {fmt.Sprintf("Bearer %s", openai.key)}}) req.WithHeader(map[string][]string{
"Content-Type": {"application/json; charset=utf-8"},
"Authorization": {fmt.Sprintf("Bearer %s", openai.key)},
})
} }
res := req.Post(payload) res := req.Post(payload)
@ -411,14 +418,22 @@ func (openai OpenAI) postWithoutModel(path string, payload map[string]interface{
func (openai OpenAI) postFile(path string, files map[string][]byte, option map[string]interface{}) (interface{}, *exception.Exception) { func (openai OpenAI) postFile(path string, files map[string][]byte, option map[string]interface{}) (interface{}, *exception.Exception) {
url := fmt.Sprintf("%s%s", openai.host, path) url := fmt.Sprintf("%s%s", openai.host, path)
option["model"] = openai.model if _, ok := option["model"].(string); !ok {
option["model"] = openai.model
}
req := http.New(url).WithHeader(map[string][]string{"Content-Type": {"multipart/form-data"}}) req := http.New(url)
if openai.azure { if openai.azure {
req.WithHeader(map[string][]string{"api-key": {openai.key}}) req.WithHeader(map[string][]string{
"Content-Type": {"multipart/form-data"},
"api-key": {openai.key},
})
} else { } else {
req.WithHeader(map[string][]string{"Authorization": {fmt.Sprintf("Bearer %s", openai.key)}}) req.WithHeader(map[string][]string{
"Content-Type": {"multipart/form-data"},
"Authorization": {fmt.Sprintf("Bearer %s", openai.key)},
})
} }
for name, data := range files { for name, data := range files {
@ -459,14 +474,23 @@ func (openai OpenAI) postFileWithoutModel(path string, files map[string][]byte,
// stream post request // stream post request
func (openai OpenAI) stream(ctx context.Context, path string, payload map[string]interface{}, cb func(data []byte) int) *exception.Exception { func (openai OpenAI) stream(ctx context.Context, path string, payload map[string]interface{}, cb func(data []byte) int) *exception.Exception {
url := fmt.Sprintf("%s%s", openai.host, path) url := fmt.Sprintf("%s%s", openai.host, path)
payload["model"] = openai.model
req := http.New(url)
req.WithHeader(map[string][]string{"Content-Type": {"application/json; charset=utf-8"}})
// If the model is not set, set the model to the default model
if _, ok := payload["model"].(string); !ok {
payload["model"] = openai.model
}
req := http.New(url)
if openai.azure { if openai.azure {
req.WithHeader(map[string][]string{"api-key": {openai.key}}) req.WithHeader(map[string][]string{
"Content-Type": {"application/json; charset=utf-8"},
"api-key": {openai.key},
})
} else { } else {
req.WithHeader(map[string][]string{"Authorization": {fmt.Sprintf("Bearer %s", openai.key)}}) req.WithHeader(map[string][]string{
"Content-Type": {"application/json; charset=utf-8"},
"Authorization": {fmt.Sprintf("Bearer %s", openai.key)},
})
} }
err := req.Stream(ctx, "POST", payload, cb) err := req.Stream(ctx, "POST", payload, cb)

View file

@ -6,7 +6,6 @@ import (
"testing" "testing"
"time" "time"
"github.com/google/uuid"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/yaoapp/gou/fs" "github.com/yaoapp/gou/fs"
"github.com/yaoapp/yao/config" "github.com/yaoapp/yao/config"
@ -18,7 +17,7 @@ func TestCompletions(t *testing.T) {
test.Prepare(t, config.Conf) test.Prepare(t, config.Conf)
defer test.Clean() defer test.Clean()
openai := prepare(t, "text-davinci-003") openai := prepare(t, "gpt-3_5-turbo-instruct")
data, err := openai.Completions("Hello", nil, nil) data, err := openai.Completions("Hello", nil, nil)
if err != nil { if err != nil {
t.Fatal(err.Message) t.Fatal(err.Message)
@ -58,7 +57,7 @@ func TestCompletionsWith(t *testing.T) {
test.Prepare(t, config.Conf) test.Prepare(t, config.Conf)
defer test.Clean() defer test.Clean()
openai := prepare(t, "text-davinci-003") openai := prepare(t, "gpt-3_5-turbo-instruct")
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
go func() { go func() {
@ -152,24 +151,24 @@ func TestChatCompletionsWith(t *testing.T) {
assert.Contains(t, err.Message, "context canceled") assert.Contains(t, err.Message, "context canceled")
} }
func TestEdits(t *testing.T) { // func TestEdits(t *testing.T) {
test.Prepare(t, config.Conf) // test.Prepare(t, config.Conf)
defer test.Clean() // defer test.Clean()
openai := prepare(t, "text-davinci-edit-001") // openai := prepare(t, "gpt-4o")
data, err := openai.Edits("Hello world"+uuid.NewString(), nil) // data, err := openai.Edits("Hello world"+uuid.NewString(), nil)
if err != nil { // if err != nil {
t.Fatal(err.Message) // t.Fatal(err.Message)
} // }
assert.NotNil(t, data.(map[string]interface{})["created"]) // assert.NotNil(t, data.(map[string]interface{})["created"])
data, err = openai.Edits("Fix the spelling mistakes 2nd"+uuid.NewString(), map[string]interface{}{"input": "What day of the wek is it?"}) // data, err = openai.Edits("Fix the spelling mistakes 2nd"+uuid.NewString(), map[string]interface{}{"input": "What day of the wek is it?"})
if err != nil { // if err != nil {
t.Fatal(err.Message) // t.Fatal(err.Message)
} // }
assert.NotNil(t, data.(map[string]interface{})["created"]) // assert.NotNil(t, data.(map[string]interface{})["created"])
} // }
func TestEmbeddings(t *testing.T) { func TestEmbeddings(t *testing.T) {
test.Prepare(t, config.Conf) test.Prepare(t, config.Conf)