Skip to content

Commit

Permalink
googleai: add embeddings to vertex (#546)
Browse files Browse the repository at this point in the history
for #410
  • Loading branch information
eliben authored Jan 23, 2024
1 parent 6f20ee5 commit 6890623
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 14 deletions.
2 changes: 1 addition & 1 deletion llms/googleai/palm/palm_llm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/tmc/langchaingo/callbacks"
"github.com/tmc/langchaingo/llms"
"github.com/tmc/langchaingo/llms/googleai/palm/internal/palmclient"
"github.com/tmc/langchaingo/llms/googleai/internal/palmclient"
)

var (
Expand Down
28 changes: 28 additions & 0 deletions llms/googleai/vertex/embeddings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package vertex

import (
"context"
"errors"
"fmt"

"github.com/tmc/langchaingo/llms/googleai/internal/palmclient"
)

// CreateEmbedding creates embeddings from texts.
func (g *Vertex) CreateEmbedding(ctx context.Context, texts []string) ([][]float32, error) {
embeddings, err := g.palmClient.CreateEmbedding(ctx, &palmclient.EmbeddingRequest{
Input: texts,
})
if err != nil {
return [][]float32{}, err
}

if len(embeddings) == 0 {
return nil, errors.New("empty response")
}
if len(texts) != len(embeddings) {
return embeddings, fmt.Errorf("returned %d embeddings for %d texts", len(embeddings), len(texts))
}

return embeddings, nil
}
22 changes: 14 additions & 8 deletions llms/googleai/vertex/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@ package vertex

import (
"context"
"log"

"cloud.google.com/go/vertexai/genai"
"github.com/tmc/langchaingo/callbacks"
"github.com/tmc/langchaingo/llms"
"github.com/tmc/langchaingo/llms/googleai/internal/palmclient"
)

// Vertex is a type that represents a Vertex AI API client.
//
// TODO: This isn't in common code; may need PaLM client for embeddings, etc.
// Note the deltas: type of topk, candidate count.
// Right now, the Vertex Gemini SDK doesn't support embeddings; therefore,
// for embeddings we also hold a palmclient.
type Vertex struct {
CallbacksHandler callbacks.Handler
client *genai.Client
opts options
palmClient *palmclient.PaLMClient
}

var _ llms.Model = &Vertex{}
Expand All @@ -31,15 +32,20 @@ func NewVertex(ctx context.Context, opts ...Option) (*Vertex, error) {
opt(&clientOptions)
}

v := &Vertex{
opts: clientOptions,
client, err := genai.NewClient(ctx, clientOptions.cloudProject, clientOptions.cloudLocation)
if err != nil {
return nil, err
}

client, err := genai.NewClient(ctx, clientOptions.cloudProject, clientOptions.cloudLocation)
palmClient, err := palmclient.New(clientOptions.cloudProject) //nolint:contextcheck
if err != nil {
log.Fatal(err)
return nil, err
}

v.client = client
v := &Vertex{
opts: clientOptions,
client: client,
palmClient: palmClient,
}
return v, nil
}
5 changes: 0 additions & 5 deletions llms/googleai/vertex/vertex.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,6 @@ func convertCandidates(candidates []*genai.Candidate) (*llms.ContentResponse, er
return &contentResponse, nil
}

// CreateEmbedding creates embeddings from texts.
func (g *Vertex) CreateEmbedding(ctx context.Context, texts []string) ([][]float32, error) {
panic("not implemented")
}

// convertParts converts between a sequence of langchain parts and genai parts.
func convertParts(parts []llms.ContentPart) ([]genai.Part, error) {
convertedParts := make([]genai.Part, 0, len(parts))
Expand Down
13 changes: 13 additions & 0 deletions llms/googleai/vertex/vertex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,16 @@ func TestMultiContentTextStream(t *testing.T) {
assert.GreaterOrEqual(t, len(chunks), 2)
assert.Regexp(t, "(?i)dog|canid|canine", sb.String())
}

func TestEmbeddings(t *testing.T) {
t.Parallel()
llm := newClient(t)

texts := []string{"foo", "parrot"}
res, err := llm.CreateEmbedding(context.Background(), texts)
require.NoError(t, err)

assert.Equal(t, len(texts), len(res))
assert.NotEmpty(t, res[0])
assert.NotEmpty(t, res[1])
}

0 comments on commit 6890623

Please sign in to comment.