-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change: rewrite gemini-vertex-model-provider in Go
- Loading branch information
1 parent
ec539d0
commit 69fd5cf
Showing
8 changed files
with
667 additions
and
506 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,16 @@ | ||
module github.com/obot-platform/tools/gemini-vertex-model-provider | ||
|
||
go 1.23.4 | ||
|
||
require ( | ||
github.com/gptscript-ai/chat-completion-client v0.0.0-20241219123536-85c44096bc10 | ||
golang.org/x/oauth2 v0.23.0 | ||
google.golang.org/genai v0.0.0-20250107232730-7bfd6e5a3ff7 | ||
) | ||
|
||
require ( | ||
cloud.google.com/go v0.116.0 // indirect | ||
cloud.google.com/go/compute/metadata v0.5.0 // indirect | ||
github.com/google/go-cmp v0.6.0 // indirect | ||
golang.org/x/sys v0.25.0 // indirect | ||
) |
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,14 @@ | ||
cloud.google.com/go v0.116.0 h1:B3fRrSDkLRt5qSHWe40ERJvhvnQwdZiHu0bJOpldweE= | ||
cloud.google.com/go v0.116.0/go.mod h1:cEPSRWPzZEswwdr9BxE6ChEn01dWlTaF05LiC2Xs70U= | ||
cloud.google.com/go/compute/metadata v0.5.0 h1:Zr0eK8JbFv6+Wi4ilXAR8FJ3wyNdpxHKJNPos6LTZOY= | ||
cloud.google.com/go/compute/metadata v0.5.0/go.mod h1:aHnloV2TPI38yx4s9+wAZhHykWvVCfu7hQbF+9CWoiY= | ||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= | ||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= | ||
github.com/gptscript-ai/chat-completion-client v0.0.0-20241219123536-85c44096bc10 h1:v251qdhjAE+mCi3s+ekmGbqV9BurrMTl0Vd8/0MvsTY= | ||
github.com/gptscript-ai/chat-completion-client v0.0.0-20241219123536-85c44096bc10/go.mod h1:7P/o6/IWa1KqsntVf68hSnLKuu3+xuqm6lYhch1w4jo= | ||
golang.org/x/oauth2 v0.23.0 h1:PbgcYx2W7i4LvjJWEbf0ngHV6qJYr86PkAV3bXdLEbs= | ||
golang.org/x/oauth2 v0.23.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= | ||
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= | ||
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= | ||
google.golang.org/genai v0.0.0-20250107232730-7bfd6e5a3ff7 h1:sGAkRQ7mZxfaV5S6gK/E8UU0ULMKGUuYkzlGcSXjGl4= | ||
google.golang.org/genai v0.0.0-20250107232730-7bfd6e5a3ff7/go.mod h1:oOXmTgRmvfizGLLCWeqvGyKJjDluaibHnZdFIZEob0k= |
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,99 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/obot-platform/tools/gemini-vertex-model-provider/server" | ||
"golang.org/x/oauth2/google" | ||
"google.golang.org/genai" | ||
) | ||
|
||
func main() { | ||
ctx := context.Background() | ||
|
||
args := os.Args[1:] | ||
if len(args) == 1 && args[0] == "validate" { | ||
if err := validate(ctx); err != nil { | ||
fmt.Printf("{\"error\": \"%s\"}\n", err) | ||
} | ||
os.Exit(0) | ||
} | ||
|
||
c, err := configure(ctx) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
port := os.Getenv("PORT") | ||
if port == "" { | ||
port = "8000" | ||
} | ||
|
||
if err := server.Run(c, port); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func validate(ctx context.Context) error { | ||
_, err := configure(ctx) | ||
return err | ||
} | ||
|
||
func configure(ctx context.Context) (*genai.Client, error) { | ||
|
||
// Ensure that we have some valid credentials JSON data | ||
credsJSON := os.Getenv("OBOT_GEMINI_VERTEX_MODEL_PROVIDER_GOOGLE_CREDENTIALS_JSON") | ||
if credsJSON == "" { | ||
return nil, fmt.Errorf("google application credentials content is required") | ||
} | ||
|
||
var creds map[string]any | ||
if err := json.Unmarshal([]byte(credsJSON), &creds); err != nil { | ||
return nil, fmt.Errorf("failed to parse google application credentials json: %w", err) | ||
} | ||
|
||
gcreds, err := google.CredentialsFromJSON(ctx, []byte(credsJSON)) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse google credentials JSON: %w", err) | ||
} | ||
|
||
// Ensure that we have a Project ID set | ||
var pid string | ||
if p, ok := creds["project_id"]; ok { | ||
pid = p.(string) | ||
} else { | ||
pid = os.Getenv("OBOT_GEMINI_VERTEX_MODEL_PROVIDER_GOOGLE_CLOUD_PROJECT") | ||
} | ||
if pid == "" { | ||
return nil, fmt.Errorf("google cloud project id is required") | ||
} | ||
|
||
// Ensure that we have a Location set | ||
var loc string | ||
if l, ok := creds["location"]; ok { | ||
loc = l.(string) | ||
} else { | ||
pid = os.Getenv("OBOT_GEMINI_VERTEX_MODEL_PROVIDER_GOOGLE_CLOUD_LOCATION") | ||
} | ||
if loc == "" { | ||
return nil, fmt.Errorf("google cloud location is required") | ||
} | ||
|
||
cc := &genai.ClientConfig{ | ||
Backend: genai.BackendVertexAI, | ||
Credentials: gcreds, | ||
Project: pid, | ||
Location: loc, | ||
} | ||
|
||
client, err := genai.NewClient(ctx, cc) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create genai client: %w", err) | ||
} | ||
|
||
return client, nil | ||
} |
Oops, something went wrong.