-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
346 additions
and
0 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,79 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// 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 sources | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/jackc/pgx/v5/pgxpool" | ||
) | ||
|
||
const PostgresKind string = "postgres" | ||
|
||
// validate interface | ||
var _ Config = PostgresConfig{} | ||
|
||
type PostgresConfig struct { | ||
Name string `yaml:"name"` | ||
Kind string `yaml:"kind"` | ||
Host string `yaml:"host"` | ||
Port string `yaml:"port"` | ||
User string `yaml:"user"` | ||
Password string `yaml:"password"` | ||
Database string `yaml:"database"` | ||
} | ||
|
||
func (r PostgresConfig) sourceKind() string { | ||
return PostgresKind | ||
} | ||
|
||
func (r PostgresConfig) Initialize() (Source, error) { | ||
pool, err := initPostgresConnectionPool(r.Host, r.Port, r.User, r.Password, r.Database) | ||
if err != nil { | ||
return nil, fmt.Errorf("Unable to create pool: %w", err) | ||
} | ||
|
||
err = pool.Ping(context.Background()) | ||
if err != nil { | ||
return nil, fmt.Errorf("Unable to connect successfully: %w", err) | ||
} | ||
|
||
s := PostgresSource{ | ||
Name: r.Name, | ||
Kind: PostgresKind, | ||
Pool: pool, | ||
} | ||
return s, nil | ||
} | ||
|
||
var _ Source = PostgresSource{} | ||
|
||
type PostgresSource struct { | ||
Name string `yaml:"name"` | ||
Kind string `yaml:"kind"` | ||
Pool *pgxpool.Pool | ||
} | ||
|
||
func initPostgresConnectionPool(host, port, user, pass, dbname string) (*pgxpool.Pool, error) { | ||
// urlExample := "postgres:dd//username:password@localhost:5432/database_name" | ||
i := fmt.Sprintf("postgres://%s:%s@%s:%s/%s", user, pass, host, port, dbname) | ||
pool, err := pgxpool.New(context.Background(), i) | ||
if err != nil { | ||
return nil, fmt.Errorf("Unable to create connection pool: %w", err) | ||
} | ||
|
||
return pool, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// 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 sources_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/googleapis/genai-toolbox/internal/sources" | ||
"github.com/googleapis/genai-toolbox/internal/testutils" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
func TestParseFromYamlPostgres(t *testing.T) { | ||
tcs := []struct { | ||
desc string | ||
in string | ||
want sources.Configs | ||
}{ | ||
{ | ||
desc: "basic example", | ||
in: ` | ||
sources: | ||
my-pg-instance: | ||
kind: postgres | ||
host: my-host | ||
port: 0000 | ||
database: my_db | ||
`, | ||
want: sources.Configs{ | ||
"my-pg-instance": sources.PostgresConfig{ | ||
Name: "my-pg-instance", | ||
Kind: sources.PostgresKind, | ||
Host: "my-host", | ||
Port: "0000", | ||
Database: "my_db", | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tc := range tcs { | ||
t.Run(tc.desc, func(t *testing.T) { | ||
got := struct { | ||
Sources sources.Configs `yaml:"sources"` | ||
}{} | ||
// Parse contents | ||
err := yaml.Unmarshal(testutils.FormatYaml(tc.in), &got) | ||
if err != nil { | ||
t.Fatalf("unable to unmarshal: %s", err) | ||
} | ||
if !cmp.Equal(tc.want, got.Sources) { | ||
t.Fatalf("incorrect parse: want %v, got %v", tc.want, got.Sources) | ||
} | ||
}) | ||
} | ||
|
||
} |
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
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,105 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// 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 tools | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/googleapis/genai-toolbox/internal/sources" | ||
) | ||
|
||
const PostgresSQLGenericKind string = "postgres-generic" | ||
|
||
// validate interface | ||
var _ Config = PostgresGenericConfig{} | ||
|
||
type PostgresGenericConfig struct { | ||
Name string `yaml:"name"` | ||
Kind string `yaml:"kind"` | ||
Source string `yaml:"source"` | ||
Description string `yaml:"description"` | ||
Statement string `yaml:"statement"` | ||
Parameters []Parameter `yaml:"parameters"` | ||
} | ||
|
||
func (cfg PostgresGenericConfig) toolKind() string { | ||
return PostgresSQLGenericKind | ||
} | ||
|
||
func (cfg PostgresGenericConfig) Initialize(srcs map[string]sources.Source) (Tool, error) { | ||
// verify source exists | ||
rawS, ok := srcs[cfg.Source] | ||
if !ok { | ||
return nil, fmt.Errorf("no source named %q configured", cfg.Source) | ||
} | ||
|
||
// verify the source is the right kind | ||
s, ok := rawS.(sources.PostgresSource) | ||
if !ok { | ||
return nil, fmt.Errorf("sources for %q tools must be of kind %q", PostgresSQLGenericKind, sources.PostgresKind) | ||
} | ||
|
||
// finish tool setup | ||
t := PostgresGenericTool{ | ||
Name: cfg.Name, | ||
Kind: PostgresSQLGenericKind, | ||
Source: s, | ||
Statement: cfg.Statement, | ||
Parameters: cfg.Parameters, | ||
manifest: Manifest{cfg.Description, cfg.Parameters}, | ||
} | ||
return t, nil | ||
} | ||
|
||
// validate interface | ||
var _ Tool = PostgresGenericTool{} | ||
|
||
type PostgresGenericTool struct { | ||
Name string `yaml:"name"` | ||
Kind string `yaml:"kind"` | ||
Source sources.PostgresSource | ||
Statement string | ||
Parameters []Parameter `yaml:"parameters"` | ||
manifest Manifest | ||
} | ||
|
||
func (t PostgresGenericTool) Invoke(params []any) (string, error) { | ||
fmt.Printf("Invoked tool %s\n", t.Name) | ||
results, err := t.Source.Pool.Query(context.Background(), t.Statement, params...) | ||
if err != nil { | ||
return "", fmt.Errorf("unable to execute query: %w", err) | ||
} | ||
|
||
var out strings.Builder | ||
for results.Next() { | ||
v, err := results.Values() | ||
if err != nil { | ||
return "", fmt.Errorf("unable to parse row: %w", err) | ||
} | ||
out.WriteString(fmt.Sprintf("%s", v)) | ||
} | ||
|
||
return fmt.Sprintf("Stub tool call for %q! Parameters parsed: %q \n Output: %s", t.Name, params, out.String()), nil | ||
} | ||
|
||
func (t PostgresGenericTool) ParseParams(data map[string]any) ([]any, error) { | ||
return ParseParams(t.Parameters, data) | ||
} | ||
|
||
func (t PostgresGenericTool) Manifest() Manifest { | ||
return t.manifest | ||
} |
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,81 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// 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 tools_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/googleapis/genai-toolbox/internal/testutils" | ||
"github.com/googleapis/genai-toolbox/internal/tools" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
func TestParseFromYamlPostgres(t *testing.T) { | ||
tcs := []struct { | ||
desc string | ||
in string | ||
want tools.Configs | ||
}{ | ||
{ | ||
desc: "basic example", | ||
in: ` | ||
tools: | ||
example_tool: | ||
kind: postgres-generic | ||
source: my-pg-instance | ||
description: some description | ||
statement: | | ||
SELECT * FROM SQL_STATEMENT; | ||
parameters: | ||
- name: country | ||
type: string | ||
description: some description | ||
`, | ||
want: tools.Configs{ | ||
"example_tool": tools.PostgresGenericConfig{ | ||
Name: "example_tool", | ||
Kind: tools.PostgresSQLGenericKind, | ||
Source: "my-pg-instance", | ||
Description: "some description", | ||
Statement: "SELECT * FROM SQL_STATEMENT;\n", | ||
Parameters: []tools.Parameter{ | ||
{ | ||
Name: "country", | ||
Type: "string", | ||
Description: "some description", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tc := range tcs { | ||
t.Run(tc.desc, func(t *testing.T) { | ||
got := struct { | ||
Tools tools.Configs `yaml:"tools"` | ||
}{} | ||
// Parse contents | ||
err := yaml.Unmarshal(testutils.FormatYaml(tc.in), &got) | ||
if err != nil { | ||
t.Fatalf("unable to unmarshal: %s", err) | ||
} | ||
if diff := cmp.Diff(tc.want, got.Tools); diff != "" { | ||
t.Fatalf("incorrect parse: diff %v", diff) | ||
} | ||
}) | ||
} | ||
|
||
} |
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