Skip to content

Commit

Permalink
feat: add Postgres source and tool
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuan325 committed Oct 22, 2024
1 parent 546ca41 commit b8d7708
Show file tree
Hide file tree
Showing 6 changed files with 346 additions and 0 deletions.
79 changes: 79 additions & 0 deletions internal/sources/postgres.go
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
}
69 changes: 69 additions & 0 deletions internal/sources/postgres_test.go
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)
}
})
}

}
6 changes: 6 additions & 0 deletions internal/sources/sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ func (c *Configs) UnmarshalYAML(node *yaml.Node) error {
return fmt.Errorf("unable to parse as %q: %w", k.Kind, err)
}
(*c)[name] = actual
case PostgresKind:
actual := PostgresConfig{Name: name}
if err := n.Decode(&actual); err != nil {
return fmt.Errorf("unable to parse as %q: %w", k.Kind, err)
}
(*c)[name] = actual
default:
return fmt.Errorf("%q is not a valid kind of data source", k.Kind)
}
Expand Down
105 changes: 105 additions & 0 deletions internal/tools/postgres.go
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
}
81 changes: 81 additions & 0 deletions internal/tools/postgres_test.go
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)
}
})
}

}
6 changes: 6 additions & 0 deletions internal/tools/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ func (c *Configs) UnmarshalYAML(node *yaml.Node) error {
return fmt.Errorf("unable to parse as %q: %w", k.Kind, err)
}
(*c)[name] = actual
case PostgresSQLGenericKind:
actual := PostgresGenericConfig{Name: name}
if err := n.Decode(&actual); err != nil {
return fmt.Errorf("unable to parse as %q: %w", k.Kind, err)
}
(*c)[name] = actual
default:
return fmt.Errorf("%q is not a valid kind of tool", k.Kind)
}
Expand Down

0 comments on commit b8d7708

Please sign in to comment.