@@ -1,9 +1,6 @@
|
||||
package openai_test
|
||||
|
||||
import (
|
||||
. "github.com/sashabaranov/go-openai"
|
||||
"github.com/sashabaranov/go-openai/internal/test/checks"
|
||||
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
@@ -14,33 +11,36 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/sashabaranov/go-openai"
|
||||
"github.com/sashabaranov/go-openai/internal/test/checks"
|
||||
)
|
||||
|
||||
func TestCompletionsWrongModel(t *testing.T) {
|
||||
config := DefaultConfig("whatever")
|
||||
config := openai.DefaultConfig("whatever")
|
||||
config.BaseURL = "http://localhost/v1"
|
||||
client := NewClientWithConfig(config)
|
||||
client := openai.NewClientWithConfig(config)
|
||||
|
||||
_, err := client.CreateCompletion(
|
||||
context.Background(),
|
||||
CompletionRequest{
|
||||
openai.CompletionRequest{
|
||||
MaxTokens: 5,
|
||||
Model: GPT3Dot5Turbo,
|
||||
Model: openai.GPT3Dot5Turbo,
|
||||
},
|
||||
)
|
||||
if !errors.Is(err, ErrCompletionUnsupportedModel) {
|
||||
if !errors.Is(err, openai.ErrCompletionUnsupportedModel) {
|
||||
t.Fatalf("CreateCompletion should return ErrCompletionUnsupportedModel, but returned: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompletionWithStream(t *testing.T) {
|
||||
config := DefaultConfig("whatever")
|
||||
client := NewClientWithConfig(config)
|
||||
config := openai.DefaultConfig("whatever")
|
||||
client := openai.NewClientWithConfig(config)
|
||||
|
||||
ctx := context.Background()
|
||||
req := CompletionRequest{Stream: true}
|
||||
req := openai.CompletionRequest{Stream: true}
|
||||
_, err := client.CreateCompletion(ctx, req)
|
||||
if !errors.Is(err, ErrCompletionStreamNotSupported) {
|
||||
if !errors.Is(err, openai.ErrCompletionStreamNotSupported) {
|
||||
t.Fatalf("CreateCompletion didn't return ErrCompletionStreamNotSupported")
|
||||
}
|
||||
}
|
||||
@@ -50,7 +50,7 @@ func TestCompletions(t *testing.T) {
|
||||
client, server, teardown := setupOpenAITestServer()
|
||||
defer teardown()
|
||||
server.RegisterHandler("/v1/completions", handleCompletionEndpoint)
|
||||
req := CompletionRequest{
|
||||
req := openai.CompletionRequest{
|
||||
MaxTokens: 5,
|
||||
Model: "ada",
|
||||
Prompt: "Lorem ipsum",
|
||||
@@ -68,12 +68,12 @@ func handleCompletionEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "POST" {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
var completionReq CompletionRequest
|
||||
var completionReq openai.CompletionRequest
|
||||
if completionReq, err = getCompletionBody(r); err != nil {
|
||||
http.Error(w, "could not read request", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
res := CompletionResponse{
|
||||
res := openai.CompletionResponse{
|
||||
ID: strconv.Itoa(int(time.Now().Unix())),
|
||||
Object: "test-object",
|
||||
Created: time.Now().Unix(),
|
||||
@@ -93,14 +93,14 @@ func handleCompletionEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
if completionReq.Echo {
|
||||
completionStr = completionReq.Prompt.(string) + completionStr
|
||||
}
|
||||
res.Choices = append(res.Choices, CompletionChoice{
|
||||
res.Choices = append(res.Choices, openai.CompletionChoice{
|
||||
Text: completionStr,
|
||||
Index: i,
|
||||
})
|
||||
}
|
||||
inputTokens := numTokens(completionReq.Prompt.(string)) * n
|
||||
completionTokens := completionReq.MaxTokens * n
|
||||
res.Usage = Usage{
|
||||
res.Usage = openai.Usage{
|
||||
PromptTokens: inputTokens,
|
||||
CompletionTokens: completionTokens,
|
||||
TotalTokens: inputTokens + completionTokens,
|
||||
@@ -110,16 +110,16 @@ func handleCompletionEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// getCompletionBody Returns the body of the request to create a completion.
|
||||
func getCompletionBody(r *http.Request) (CompletionRequest, error) {
|
||||
completion := CompletionRequest{}
|
||||
func getCompletionBody(r *http.Request) (openai.CompletionRequest, error) {
|
||||
completion := openai.CompletionRequest{}
|
||||
// read the request body
|
||||
reqBody, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
return CompletionRequest{}, err
|
||||
return openai.CompletionRequest{}, err
|
||||
}
|
||||
err = json.Unmarshal(reqBody, &completion)
|
||||
if err != nil {
|
||||
return CompletionRequest{}, err
|
||||
return openai.CompletionRequest{}, err
|
||||
}
|
||||
return completion, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user