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

feat(vertexai): Elastic Text-Embedding Model demo. #4127

Merged
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 15 additions & 3 deletions aiplatform/snippets/embeddings.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@ import (
)

func embedTexts(
apiEndpoint, project, model string, texts []string, task string) ([][]float32, error) {
project string, texts []string) ([][]float32, error) {
ctx := context.Background()

apiEndpoint := "us-central1-aiplatform.googleapis.com:443"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blocker: samples do not specify the API Endpoint unless specifically needed for the sample. Setting the region explicitly is typical.

model := "text-embedding-004"
task := "QUESTION_ANSWERING"
customOutputDimensionality := 5

client, err := aiplatform.NewPredictionClient(ctx, option.WithEndpoint(apiEndpoint))
if err != nil {
return nil, err
Expand All @@ -52,10 +57,17 @@ func embedTexts(
},
})
}
outputDimensionality := structpb.NewNullValue()
outputDimensionality = structpb.NewNumberValue(float64(customOutputDimensionality))

params := structpb.NewStructValue(&structpb.Struct{
Fields: map[string]*structpb.Value{"outputDimensionality": outputDimensionality},
})

req := &aiplatformpb.PredictRequest{
Endpoint: endpoint,
Instances: instances,
Endpoint: endpoint,
Instances: instances,
Parameters: params,
}
resp, err := client.Predict(ctx, req)
if err != nil {
Expand Down
14 changes: 9 additions & 5 deletions aiplatform/snippets/embeddings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@ import (

func TestGenerateEmbeddings(t *testing.T) {
tc := testutil.SystemTest(t)
apiEndpoint := "us-central1-aiplatform.googleapis.com:443"
model := "textembedding-gecko@003"
texts := []string{"banana muffins? ", "banana bread? banana muffins?"}
embeddings, err := embedTexts(apiEndpoint, tc.ProjectID, model, texts, "RETRIEVAL_DOCUMENT")
dimensionality := 5
embeddings, err := embedTexts(tc.ProjectID, texts)
if err != nil {
t.Fatal(err)
}
if len(embeddings) != len(texts) || len(embeddings[0]) != 768 {
t.Errorf("len(embeddings), len(embeddings[0]) = %d, %d, want %d, 768", len(embeddings), len(embeddings[0]), len(texts))

embeddingsLen := len(embeddings)
textsLen := len(texts)
embeddingDimensionality := len(embeddings[0])

if embeddingsLen != textsLen || embeddingDimensionality != dimensionality {
t.Errorf("embeddingsLen, embeddingDimensionality = %d, %d, want %d, %d", embeddingsLen, embeddingDimensionality, textsLen, dimensionality)
}
}

Expand Down
73 changes: 0 additions & 73 deletions aiplatform/text-embeddings/embeddings.go

This file was deleted.

43 changes: 0 additions & 43 deletions aiplatform/text-embeddings/embeddings_test.go

This file was deleted.