-
-
Notifications
You must be signed in to change notification settings - Fork 745
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
googleai: refactor to better separate generated code (#547)
for #410
- Loading branch information
Showing
5 changed files
with
65 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters