Skip to content

Commit 85e6926

Browse files
authored
feat: intergrate gemini embedding (#783)
1 parent a9b87c1 commit 85e6926

File tree

6 files changed

+115
-3
lines changed

6 files changed

+115
-3
lines changed

backend/application/base/appinfra/app_infra.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ import (
2525
"strings"
2626
"time"
2727

28+
"google.golang.org/genai"
2829
"gorm.io/gorm"
2930

31+
"github.com/cloudwego/eino-ext/components/embedding/gemini"
3032
"github.com/cloudwego/eino-ext/components/embedding/ollama"
3133
"github.com/cloudwego/eino-ext/components/embedding/openai"
3234
"github.com/milvus-io/milvus/client/v2/milvusclient"
@@ -499,7 +501,61 @@ func getEmbedding(ctx context.Context) (embedding.Embedder, error) {
499501
if err != nil {
500502
return nil, fmt.Errorf("init ollama embedding failed, err=%w", err)
501503
}
504+
case "gemini":
505+
var (
506+
geminiEmbeddingBaseURL = os.Getenv("GEMINI_EMBEDDING_BASE_URL")
507+
geminiEmbeddingModel = os.Getenv("GEMINI_EMBEDDING_MODEL")
508+
geminiEmbeddingApiKey = os.Getenv("GEMINI_EMBEDDING_API_KEY")
509+
geminiEmbeddingDims = os.Getenv("GEMINI_EMBEDDING_DIMS")
510+
geminiEmbeddingBackend = os.Getenv("GEMINI_EMBEDDING_BACKEND") // "1" for BackendGeminiAPI / "2" for BackendVertexAI
511+
geminiEmbeddingProject = os.Getenv("GEMINI_EMBEDDING_PROJECT")
512+
geminiEmbeddingLocation = os.Getenv("GEMINI_EMBEDDING_LOCATION")
513+
)
514+
515+
if len(geminiEmbeddingModel) == 0 {
516+
return nil, fmt.Errorf("GEMINI_EMBEDDING_MODEL environment variable is required")
517+
}
518+
if len(geminiEmbeddingApiKey) == 0 {
519+
return nil, fmt.Errorf("GEMINI_EMBEDDING_API_KEY environment variable is required")
520+
}
521+
if len(geminiEmbeddingDims) == 0 {
522+
return nil, fmt.Errorf("GEMINI_EMBEDDING_DIMS environment variable is required")
523+
}
524+
if len(geminiEmbeddingBackend) == 0 {
525+
return nil, fmt.Errorf("GEMINI_EMBEDDING_BACKEND environment variable is required")
526+
}
527+
528+
dims, convErr := strconv.ParseInt(geminiEmbeddingDims, 10, 64)
529+
if convErr != nil {
530+
return nil, fmt.Errorf("invalid GEMINI_EMBEDDING_DIMS value: %s, err=%w", geminiEmbeddingDims, convErr)
531+
}
502532

533+
backend, convErr := strconv.ParseInt(geminiEmbeddingBackend, 10, 64)
534+
if convErr != nil {
535+
return nil, fmt.Errorf("invalid GEMINI_EMBEDDING_BACKEND value: %s, err=%w", geminiEmbeddingBackend, convErr)
536+
}
537+
538+
geminiCli, err := genai.NewClient(ctx, &genai.ClientConfig{
539+
APIKey: geminiEmbeddingApiKey,
540+
Backend: genai.Backend(backend),
541+
Project: geminiEmbeddingProject,
542+
Location: geminiEmbeddingLocation,
543+
HTTPOptions: genai.HTTPOptions{
544+
BaseURL: geminiEmbeddingBaseURL,
545+
},
546+
})
547+
if err != nil {
548+
return nil, fmt.Errorf("init gemini client failed, err=%w", err)
549+
}
550+
551+
emb, err = wrap.NewGeminiEmbedder(ctx, &gemini.EmbeddingConfig{
552+
Client: geminiCli,
553+
Model: geminiEmbeddingModel,
554+
OutputDimensionality: ptr.Of(int32(dims)),
555+
}, dims, batchSize)
556+
if err != nil {
557+
return nil, fmt.Errorf("init gemini embedding failed, err=%w", err)
558+
}
503559
case "http":
504560
var (
505561
httpEmbeddingBaseURL = os.Getenv("HTTP_EMBEDDING_ADDR")

backend/go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ require (
7474
github.com/yuin/goldmark v1.4.13
7575
golang.org/x/image v0.22.0
7676
golang.org/x/oauth2 v0.23.0
77-
google.golang.org/genai v1.13.0
77+
google.golang.org/genai v1.18.0
7878
)
7979

8080
require (
@@ -85,6 +85,7 @@ require (
8585
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.37 // indirect
8686
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.7.5 // indirect
8787
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.18.18 // indirect
88+
github.com/cloudwego/eino-ext/components/embedding/gemini v0.0.0-20250814083140-54b99ff82f8e // indirect
8889
github.com/cloudwego/gopkg v0.1.4 // indirect
8990
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
9091
github.com/extrame/ole2 v0.0.0-20160812065207-d69429661ad7 // indirect

backend/go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,8 @@ github.com/cloudwego/eino v0.3.55 h1:lMZrGtEh0k3qykQTLNXSXuAa98OtF2tS43GMHyvN7nA
928928
github.com/cloudwego/eino v0.3.55/go.mod h1:wUjz990apdsaOraOXdh6CdhVXq8DJsOvLsVlxNTcNfY=
929929
github.com/cloudwego/eino-ext/components/embedding/ark v0.1.0 h1:AuJsMdaTXc+dGUDQp82MifLYK8oiJf4gLQPUETmKISM=
930930
github.com/cloudwego/eino-ext/components/embedding/ark v0.1.0/go.mod h1:0FZG/KRBl3hGWkNsm55UaXyVa6PDVIy5u+QvboAB+cY=
931+
github.com/cloudwego/eino-ext/components/embedding/gemini v0.0.0-20250814083140-54b99ff82f8e h1:46D2fFDbUysA7kUD5x/wK3huneMEvTQfuWcHqI3M6iQ=
932+
github.com/cloudwego/eino-ext/components/embedding/gemini v0.0.0-20250814083140-54b99ff82f8e/go.mod h1:mz3PGQenODaRelcH+lmX012PAHT8vnuHsiL6EgFw3FA=
931933
github.com/cloudwego/eino-ext/components/embedding/ollama v0.0.0-20250728060543-79ec300857b8 h1:uJrs6SmfYnca8A+k9+3qJ4MYwYHMncUlGac1mYQT+Ak=
932934
github.com/cloudwego/eino-ext/components/embedding/ollama v0.0.0-20250728060543-79ec300857b8/go.mod h1:nav79aUcd+UR24dLA+7l7RcHCMlg26zbDAKvjONdrw0=
933935
github.com/cloudwego/eino-ext/components/embedding/openai v0.0.0-20250522060253-ddb617598b09 h1:C8RjF193iguUuevkuv0q4SC+XGlM/DlJEgic7l8OUAI=
@@ -2614,6 +2616,8 @@ google.golang.org/appengine v1.6.6/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCID
26142616
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
26152617
google.golang.org/genai v1.13.0 h1:LRhwx5PU+bXhfnXyPEHu2kt9yc+MpvuYbajxSorOJjg=
26162618
google.golang.org/genai v1.13.0/go.mod h1:QPj5NGJw+3wEOHg+PrsWwJKvG6UC84ex5FR7qAYsN/M=
2619+
google.golang.org/genai v1.18.0 h1:fTmK7y30CO0CL8xRyyFSjTkd1MNbYUeFUehvDyU/2gQ=
2620+
google.golang.org/genai v1.18.0/go.mod h1:QPj5NGJw+3wEOHg+PrsWwJKvG6UC84ex5FR7qAYsN/M=
26172621
google.golang.org/genproto v0.0.0-20180518175338-11a468237815/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
26182622
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
26192623
google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2025 coze-dev Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package wrap
18+
19+
import (
20+
"context"
21+
22+
"github.com/cloudwego/eino-ext/components/embedding/gemini"
23+
24+
contract "github.com/coze-dev/coze-studio/backend/infra/contract/embedding"
25+
)
26+
27+
func NewGeminiEmbedder(ctx context.Context, config *gemini.EmbeddingConfig, dimensions int64, batchSize int) (contract.Embedder, error) {
28+
emb, err := gemini.NewEmbedder(ctx, config)
29+
if err != nil {
30+
return nil, err
31+
}
32+
return &denseOnlyWrap{dims: dimensions, batchSize: batchSize, Embedder: emb}, nil
33+
}

docker/.env.debug.example

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export VIKING_DB_MODEL_NAME="" # if vikingdb model name is not set, you need to
101101
# The Embedding model relied on by knowledge base vectorization does not need to be configured
102102
# if the vector database comes with built-in Embedding functionality (such as VikingDB). Currently,
103103
# Coze Studio supports four access methods: openai, ark, ollama, and custom http. Users can simply choose one of them when using
104-
# embedding type: openai / ark / ollama / http
104+
# embedding type: openai / ark / ollama / http / gemini
105105
export EMBEDDING_TYPE="ark"
106106
export EMBEDDING_MAX_BATCH_SIZE=100
107107

@@ -126,6 +126,15 @@ export OLLAMA_EMBEDDING_BASE_URL="" # (string, required) Ollama embedding base_u
126126
export OLLAMA_EMBEDDING_MODEL="" # (string, required) Ollama embedding model
127127
export OLLAMA_EMBEDDING_DIMS="" # (int, required) Ollama embedding dimensions
128128

129+
# gemini embedding
130+
export GEMINI_EMBEDDING_BASE_URL="" # (string, required) Gemini embedding base_url
131+
export GEMINI_EMBEDDING_MODEL="gemini-embedding-001" # (string, required) Gemini embedding model.
132+
export GEMINI_EMBEDDING_API_KEY="" # (string, required) Gemini embedding api_key
133+
export GEMINI_EMBEDDING_DIMS=2048 # (int, required) Gemini embedding dimensions
134+
export GEMINI_EMBEDDING_BACKEND="1" # (string, required) Gemini embedding backend, should be "1" for BackendGeminiAPI / "2" for BackendVertexAI.
135+
export GEMINI_EMBEDDING_PROJECT="" # (string, optional) Gemini embedding project
136+
export GEMINI_EMBEDDING_LOCATION="" # (string, optional) Gemini embedding location
137+
129138
# http embedding
130139
export HTTP_EMBEDDING_ADDR="" # (string, required) http embedding address
131140
export HTTP_EMBEDDING_DIMS=1024 # (string, required) http embedding dimensions

docker/.env.example

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export VIKING_DB_MODEL_NAME="" # if vikingdb model name is not set, you need to
9999
# The Embedding model relied on by knowledge base vectorization does not need to be configured
100100
# if the vector database comes with built-in Embedding functionality (such as VikingDB). Currently,
101101
# Coze Studio supports four access methods: openai, ark, ollama, and custom http. Users can simply choose one of them when using
102-
# embedding type: ark / openai / ollama / http
102+
# embedding type: ark / openai / ollama / gemini / http
103103
export EMBEDDING_TYPE="ark"
104104
export EMBEDDING_MAX_BATCH_SIZE=100
105105

@@ -124,6 +124,15 @@ export OLLAMA_EMBEDDING_BASE_URL="" # (string, required) Ollama embedding base_u
124124
export OLLAMA_EMBEDDING_MODEL="" # (string, required) Ollama embedding model
125125
export OLLAMA_EMBEDDING_DIMS="" # (int, required) Ollama embedding dimensions
126126

127+
# gemini embedding
128+
export GEMINI_EMBEDDING_BASE_URL="" # (string, required) Gemini embedding base_url
129+
export GEMINI_EMBEDDING_MODEL="gemini-embedding-001" # (string, required) Gemini embedding model.
130+
export GEMINI_EMBEDDING_API_KEY="" # (string, required) Gemini embedding api_key
131+
export GEMINI_EMBEDDING_DIMS=2048 # (int, required) Gemini embedding dimensions
132+
export GEMINI_EMBEDDING_BACKEND="1" # (string, required) Gemini embedding backend, should be "1" for BackendGeminiAPI / "2" for BackendVertexAI.
133+
export GEMINI_EMBEDDING_PROJECT="" # (string, optional) Gemini embedding project
134+
export GEMINI_EMBEDDING_LOCATION="" # (string, optional) Gemini embedding location
135+
127136
# http embedding
128137
export HTTP_EMBEDDING_ADDR="" # (string, required) http embedding address
129138
export HTTP_EMBEDDING_DIMS=1024 # (string, required) http embedding dimensions

0 commit comments

Comments
 (0)