Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add embeddings tests #237

Merged
merged 1 commit into from
Apr 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions embeddings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ package openai_test

import (
. "github.com/sashabaranov/go-openai"
"github.com/sashabaranov/go-openai/internal/test"
"github.com/sashabaranov/go-openai/internal/test/checks"

"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"testing"
)

Expand Down Expand Up @@ -45,3 +49,43 @@ func TestEmbedding(t *testing.T) {
}
}
}

func TestEmbeddingModel(t *testing.T) {
var em EmbeddingModel
err := em.UnmarshalText([]byte("text-similarity-ada-001"))
checks.NoError(t, err, "Could not marshal embedding model")

if em != AdaSimilarity {
t.Errorf("Model is not equal to AdaSimilarity")
}

err = em.UnmarshalText([]byte("some-non-existent-model"))
checks.NoError(t, err, "Could not marshal embedding model")
if em != Unknown {
t.Errorf("Model is not equal to Unknown")
}
}

func TestEmbeddingEndpoint(t *testing.T) {
server := test.NewTestServer()
server.RegisterHandler(
"/v1/embeddings",
func(w http.ResponseWriter, r *http.Request) {
resBytes, _ := json.Marshal(EmbeddingResponse{})
fmt.Fprintln(w, string(resBytes))
},
)
// create the test server
var err error
ts := server.OpenAITestServer()
ts.Start()
defer ts.Close()

config := DefaultConfig(test.GetTestToken())
config.BaseURL = ts.URL + "/v1"
client := NewClientWithConfig(config)
ctx := context.Background()

_, err = client.CreateEmbeddings(ctx, EmbeddingRequest{})
checks.NoError(t, err, "CreateEmbeddings error")
}