Skip to content

Commit

Permalink
googleai: refactor to better separate generated code (#547)
Browse files Browse the repository at this point in the history
for #410
  • Loading branch information
eliben authored Jan 23, 2024
1 parent 6890623 commit 2545ace
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 50 deletions.
23 changes: 23 additions & 0 deletions llms/googleai/embeddings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package googleai

import (
"context"

"github.com/google/generative-ai-go/genai"
)

// CreateEmbedding creates embeddings from texts.
func (g *GoogleAI) CreateEmbedding(ctx context.Context, texts []string) ([][]float32, error) {
em := g.client.EmbeddingModel(g.opts.defaultEmbeddingModel)

results := make([][]float32, 0, len(texts))
for _, t := range texts {
res, err := em.EmbedContent(ctx, genai.Text(t))
if err != nil {
return results, err
}
results = append(results, res.Embedding.Values)
}

return results, nil
}
49 changes: 0 additions & 49 deletions llms/googleai/googleai_llm.go → llms/googleai/googleai.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// package googleai implements a langchaingo provider for Google AI LLMs.
// See https://ai.google.dev/ for more details.
package googleai

import (
Expand All @@ -10,23 +8,12 @@ import (
"strings"

"github.com/google/generative-ai-go/genai"
"github.com/tmc/langchaingo/callbacks"
"github.com/tmc/langchaingo/llms"
"github.com/tmc/langchaingo/schema"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
)

// GoogleAI is a type that represents a Google AI API client.
type GoogleAI struct {
CallbacksHandler callbacks.Handler
client *genai.Client
opts options
}

var (
_ llms.Model = &GoogleAI{}

ErrNoContentInResponse = errors.New("no content in generation response")
ErrUnknownPartInResponse = errors.New("unknown part type in generation response")
ErrInvalidMimeType = errors.New("invalid mime type on content")
Expand All @@ -40,26 +27,6 @@ const (
RoleUser = "user"
)

// NewGoogleAI creates a new GoogleAI struct.
func NewGoogleAI(ctx context.Context, opts ...Option) (*GoogleAI, error) {
clientOptions := defaultOptions()
for _, opt := range opts {
opt(&clientOptions)
}

gi := &GoogleAI{
opts: clientOptions,
}

client, err := genai.NewClient(ctx, option.WithAPIKey(clientOptions.apiKey))
if err != nil {
return gi, err
}

gi.client = client
return gi, nil
}

// Call implements the [llms.Model] interface.
func (g *GoogleAI) Call(ctx context.Context, prompt string, options ...llms.CallOption) (string, error) {
return llms.GenerateFromSinglePrompt(ctx, g, prompt, options...)
Expand Down Expand Up @@ -148,22 +115,6 @@ func convertCandidates(candidates []*genai.Candidate) (*llms.ContentResponse, er
return &contentResponse, nil
}

// CreateEmbedding creates embeddings from texts.
func (g *GoogleAI) CreateEmbedding(ctx context.Context, texts []string) ([][]float32, error) {
em := g.client.EmbeddingModel(g.opts.defaultEmbeddingModel)

results := make([][]float32, 0, len(texts))
for _, t := range texts {
res, err := em.EmbedContent(ctx, genai.Text(t))
if err != nil {
return results, err
}
results = append(results, res.Embedding.Values)
}

return results, nil
}

// 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
File renamed without changes.
41 changes: 41 additions & 0 deletions llms/googleai/new.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// package googleai implements a langchaingo provider for Google AI LLMs.
// See https://ai.google.dev/ for more details.
package googleai

import (
"context"

"github.com/google/generative-ai-go/genai"
"github.com/tmc/langchaingo/callbacks"
"github.com/tmc/langchaingo/llms"
"google.golang.org/api/option"
)

// GoogleAI is a type that represents a Google AI API client.
type GoogleAI struct {
CallbacksHandler callbacks.Handler
client *genai.Client
opts options
}

var _ llms.Model = &GoogleAI{}

// NewGoogleAI creates a new GoogleAI struct.
func NewGoogleAI(ctx context.Context, opts ...Option) (*GoogleAI, error) {
clientOptions := defaultOptions()
for _, opt := range opts {
opt(&clientOptions)
}

gi := &GoogleAI{
opts: clientOptions,
}

client, err := genai.NewClient(ctx, option.WithAPIKey(clientOptions.apiKey))
if err != nil {
return gi, err
}

gi.client = client
return gi, nil
}
2 changes: 1 addition & 1 deletion llms/googleai/vertex/vertex.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package vertex

// DO NOT EDIT: this code is auto-generated from llms/googleai/googleai_llm.go
// DO NOT EDIT: this code is auto-generated from llms/googleai/googleai.go

import (
"context"
Expand Down

0 comments on commit 2545ace

Please sign in to comment.