Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions common/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package common
// DATABASE env key
const (
//DATABASE_POSTGRESQL
DATABASE_POSTGRESQL_USERNAME = "DATABASE_POSTGRESQL_USERNAME"
DATABASE_POSTGRESQL_USER = "DATABASE_POSTGRESQL_USER"
DATABASE_POSTGRESQL_PASSWORD = "DATABASE_POSTGRESQL_PASSWORD"
DATABASE_POSTGRESQL_HOST = "DATABASE_POSTGRESQL_HOST"
DATABASE_POSTGRESQL_PORT = "DATABASE_POSTGRESQL_PORT"
DATABASE_POSTGRESQL_SCHEMA = "DATABASE_POSTGRESQL_SCHEMA"
DATABASE_POSTGRESQL_DATABASE = "DATABASE_POSTGRESQL_DATABASE"
DATABASE_POSTGRESQL_DBURL = "DATABASE_POSTGRESQL_DBURL"
DATABASE_POSTGRESQL_GORMLOGLEVEL = "DATABASE_POSTGRESQL_GORMLOGLEVEL"
)
Expand Down
24 changes: 12 additions & 12 deletions configs/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,26 @@ import (
)

type CommonDatabaseConfig struct {
UserName string
Password string
Host string
Port string
Schema string
DBUrl string
User string `yaml:"user"`
Password string `yaml:"password"`
Host string `yaml:"host"`
Port string `yaml:"port"`
Database string `yaml:"database"`
DBUrl string `yaml:"db_url"`
}
type DatabaseConfig struct {
Postgresql *CommonDatabaseConfig
Viking *VikingConfig `yaml:"viking"`
TOS *TosClientConf `yaml:"tos"`
Mem0 *Mem0Config `yaml:"mem0"`
Postgresql *CommonDatabaseConfig `yaml:"postgresql"`
Viking *VikingConfig `yaml:"viking"`
TOS *TosClientConf `yaml:"tos"`
Mem0 *Mem0Config `yaml:"mem0"`
}

func (c *DatabaseConfig) MapEnvToConfig() {
c.Postgresql.UserName = utils.GetEnvWithDefault(common.DATABASE_POSTGRESQL_USERNAME)
c.Postgresql.User = utils.GetEnvWithDefault(common.DATABASE_POSTGRESQL_USER)
c.Postgresql.Password = utils.GetEnvWithDefault(common.DATABASE_POSTGRESQL_PASSWORD)
c.Postgresql.Host = utils.GetEnvWithDefault(common.DATABASE_POSTGRESQL_HOST)
c.Postgresql.Port = utils.GetEnvWithDefault(common.DATABASE_POSTGRESQL_PORT)
c.Postgresql.Schema = utils.GetEnvWithDefault(common.DATABASE_POSTGRESQL_SCHEMA)
c.Postgresql.Database = utils.GetEnvWithDefault(common.DATABASE_POSTGRESQL_DATABASE)
c.Postgresql.DBUrl = utils.GetEnvWithDefault(common.DATABASE_POSTGRESQL_DBURL)

c.Viking.MapEnvToConfig()
Expand Down
105 changes: 105 additions & 0 deletions examples/tool/function-tools/agent_with_function_tool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates.
//
// 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 main

import (
"context"
"fmt"
"log"

veagent "github.com/volcengine/veadk-go/agent/llmagent"
"github.com/volcengine/veadk-go/apps"
"github.com/volcengine/veadk-go/apps/agentkit_server_app"
"github.com/volcengine/veadk-go/utils"
"google.golang.org/adk/agent"
"google.golang.org/adk/agent/llmagent"
"google.golang.org/adk/tool"
"google.golang.org/adk/tool/functiontool"
)

// CalculatorAddArgs 定义加法工具的入参。使用静态类型,便于 LLM 以 JSON 方式调用。
type CalculatorAddArgs struct {
A float64 `json:"a" jsonschema:"第一个加数,支持整数或小数"`
B float64 `json:"b" jsonschema:"第二个加数,支持整数或小数"`
}

// CalculatorAddTool 返回一个符合 ADK functiontool 规范的工具。
// 该工具用于执行两数相加,并返回 result 字段。
func CalculatorAddTool() (tool.Tool, error) {
handler := func(ctx tool.Context, args CalculatorAddArgs) (map[string]any, error) {
result := args.A + args.B
return map[string]any{
"result": result,
"explain": fmt.Sprintf("%g + %g = %g", args.A, args.B, result),
}, nil
}

return functiontool.New(
functiontool.Config{
Name: "calculator_add",
Description: "一个简单的计算器工具,执行两数相加。参数: a, b; 返回: result(浮点数)",
},
handler,
)
}

type MessageCheckerArgs struct {
UserMessage string `json:"user_message" jsonschema:"The user message to check."`
}

func NewMessageCheckerTool() (tool.Tool, error) {
messageCheckerHandler := func(ctx tool.Context, args MessageCheckerArgs) (map[string]any, error) {
return map[string]any{
"result": fmt.Sprintf("Checked message: %s", args.UserMessage),
"explain": map[string]string{
"user_message": ctx.AgentName(),
"app_name": ctx.AppName(),
"agent_name": ctx.AgentName(),
"user_id": ctx.UserID(),
"session_id": ctx.SessionID(),
},
}, nil
}

return functiontool.New(
functiontool.Config{
Name: "message_checker",
Description: "Check message and log context information.",
},
messageCheckerHandler,
)
}

func main() {
ctx := context.Background()
rootAgent, err := veagent.New(&veagent.Config{
Config: llmagent.Config{
Tools: []tool.Tool{utils.Must(CalculatorAddTool()), utils.Must(NewMessageCheckerTool())},
},
})

if err != nil {
log.Fatalf("Failed to create agent: %v", err)
}

app := agentkit_server_app.NewAgentkitServerApp(apps.DefaultApiConfig())

err = app.Run(ctx, &apps.RunConfig{
AgentLoader: agent.NewSingleLoader(rootAgent),
})
if err != nil {
fmt.Printf("Run failed: %v", err)
}
}
5 changes: 2 additions & 3 deletions memory/short_term_memory_backends/postgresql_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ func NewPostgreSqlSTMBackend(config *PostgresqlBackendConfig) (session.Service,
if config == nil {
return nil, fmt.Errorf("postgresql config is nil")
}

if config.DBUrl != "" {
log.Info("DbURL is set, ignore backend option")
if strings.Count(config.DBUrl, "@") > 1 || strings.Count(config.DBUrl, ":") > 3 {
Expand All @@ -47,13 +46,13 @@ func NewPostgreSqlSTMBackend(config *PostgresqlBackendConfig) (session.Service,
)
}
} else {
encodedUsername := url.QueryEscape(config.UserName)
encodedUsername := url.QueryEscape(config.User)
encodedPassword := url.QueryEscape(config.Password)

config.DBUrl = fmt.Sprintf(
"postgresql://%s:%s@%s:%s/%s",
encodedUsername, encodedPassword,
config.Host, config.Port, config.Schema,
config.Host, config.Port, config.Database,
)
}

Expand Down
4 changes: 2 additions & 2 deletions memory/short_term_memory_backends/postgresql_backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ func TestNewPostgreSqlSTMBackend(t *testing.T) {
name: "no db url",
config: &PostgresqlBackendConfig{
CommonDatabaseConfig: &configs.CommonDatabaseConfig{
UserName: "test@",
User: "test@",
Password: "test@",
Host: "127.0.0.1",
Port: "5432",
Schema: "test_veadk",
Database: "test_veadk",
DBUrl: "",
},
},
Expand Down