Files
zeekayandHanzo Dev 04d6c97420 deps: drop google.golang.org/genai for a native Gemini REST client
The SDK reaches cloud.google.com/go/auth for credentials, which drags
s2a-go, gax-go, google.golang.org/api, gRPC and protobuf into every
binary that links this module. Gemini is an API-key REST service; none
of that machinery buys anything.

internal/gemini covers exactly the four calls we make — countTokens,
generateContent, batchEmbedContents and models.list — against the same
v1beta endpoints and x-goog-api-key auth the SDK used, and errors where
the SDK call sites used to risk a nil deref on a candidate-less
(safety-blocked) response.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-27 17:44:00 -07:00

72 lines
2.0 KiB
Go

// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
// Portions Copyright 2023 The OpenAgent Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package embedding
import (
"context"
"github.com/hanzoai/ai/internal/gemini"
"github.com/hanzoai/ai/proxy"
)
type GeminiEmbeddingProvider struct {
subType string
secretKey string
}
func NewGeminiEmbeddingProvider(subType string, secretKey string) (*GeminiEmbeddingProvider, error) {
p := &GeminiEmbeddingProvider{
subType: subType,
secretKey: secretKey,
}
return p, nil
}
func (p *GeminiEmbeddingProvider) GetPricing() string {
return `URL:
https://cloud.google.com/vertex-ai/generative-ai/pricing
Embedding Models:
| Model | Output Price |
|----------------------|-----------------------|
| multimodalembeddings | $0.0002/1K characters |
`
}
func (p *GeminiEmbeddingProvider) calculatePrice(res *EmbeddingResult) error {
pricePerThousandTokens := 0.0002
res.Price = getPrice(res.TokenCount, pricePerThousandTokens)
res.Currency = "USD"
return nil
}
func (p *GeminiEmbeddingProvider) QueryVector(text string, ctx context.Context, lang string) ([]float32, *EmbeddingResult, error) {
vectors, err := gemini.New(p.secretKey, proxy.ProxyHttpClient).
Embed(ctx, p.subType, []*gemini.Content{gemini.Text(text, gemini.RoleUser)})
if err != nil {
return nil, nil, err
}
embeddingResult := &EmbeddingResult{TokenCount: 0}
err = p.calculatePrice(embeddingResult)
if err != nil {
return nil, nil, err
}
return vectors[0], embeddingResult, nil
}