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 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
19 changes: 15 additions & 4 deletions aiplatform/snippets/embeddings.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,21 @@ import (
)

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

apiEndpoint := fmt.Sprintf("%s-aiplatform.googleapis.com:443", location)
model := "text-embedding-004"
task := "QUESTION_ANSWERING"
customOutputDimensionality := 5

client, err := aiplatform.NewPredictionClient(ctx, option.WithEndpoint(apiEndpoint))
if err != nil {
return nil, err
}
defer client.Close()

match := regexp.MustCompile(`^(\w+-\w+)`).FindStringSubmatch(apiEndpoint)
location := "us-central1"
if match != nil {
location = match[1]
}
Expand All @@ -52,10 +56,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
15 changes: 10 additions & 5 deletions aiplatform/snippets/embeddings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,20 @@ 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
location := "us-central1"
embeddings, err := embedTexts(tc.ProjectID, location, 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.

Loading