Skip to content
This repository has been archived by the owner on Nov 18, 2024. It is now read-only.

Commit

Permalink
fix: backend, ai tool bug
Browse files Browse the repository at this point in the history
  • Loading branch information
hyacinthus committed Nov 16, 2024
1 parent bfebbf1 commit b885616
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
42 changes: 33 additions & 9 deletions backend/service/vapi_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ import (
"github.com/crestalnetwork/ethglobal-bangkok/backend/types"
)

func (s *Service) VAPIFunctionTrade(ctx context.Context, msg *types.VapiServerMessageToolCall) (*types.FunctionResult, error) {
func (s *Service) VAPIFunctionTrade(ctx context.Context, msg *types.VapiServerMessageToolCall) (*types.ToolResults, error) {
var id string
var trade = new(types.Trade)
var resp = new(types.ToolResults)
resp.Results = make([]types.ToolResult, 0)
for _, tool := range msg.Message.ToolCallList {
if tool.Function != nil {
if tool.Function.Name == "trade" {
id = tool.Id
curr, ok := tool.Function.Arguments["currency"]
if !ok {
return nil, types.NewError(fiber.StatusBadRequest, "BadRequest", "currency is required")
Expand All @@ -35,15 +39,21 @@ func (s *Service) VAPIFunctionTrade(ctx context.Context, msg *types.VapiServerMe
}
}
s.log.Warn("VAPIFunction called", "trade", trade)
return &types.FunctionResult{Result: fmt.Sprintf("Tell the user:The prize of %s is $%f now, are you sure you want to go ahead with this transaction?",
trade.Currency, rand.Float64()*100)}, nil
res := types.ToolResult{ToolCallID: id, Result: fmt.Sprintf("Tell the user:The prize of %s is $%f now, are you sure you want to go ahead with this transaction?",
trade.Currency, rand.Float64()*100)}
resp.Results = append(resp.Results, res)
return resp, nil
}

func (s *Service) VAPIFunctionConfirm(ctx context.Context, msg *types.VapiServerMessageToolCall) (*types.FunctionResult, error) {
func (s *Service) VAPIFunctionConfirm(ctx context.Context, msg *types.VapiServerMessageToolCall) (*types.ToolResults, error) {
var id string
var confirm bool
var resp = new(types.ToolResults)
resp.Results = make([]types.ToolResult, 0)
for _, tool := range msg.Message.ToolCallList {
if tool.Function != nil {
if tool.Function.Name == "confirm" {
id = tool.Id
curr, ok := tool.Function.Arguments["confirm"]
if !ok {
return nil, types.NewError(fiber.StatusBadRequest, "BadRequest", "currency is required")
Expand All @@ -58,16 +68,25 @@ func (s *Service) VAPIFunctionConfirm(ctx context.Context, msg *types.VapiServer
}
s.log.Warn("VAPIFunction called", "confirm", confirm)
if confirm {
return &types.FunctionResult{Result: `Tell the user:"This is the transaction waiting for your signature. Can you authorize the AI assistant to sign for you?"`}, nil
res := types.ToolResult{ToolCallID: id, Result: `Tell the user:"This is the transaction waiting for your signature. Can you authorize the AI assistant to sign for you"`}
resp.Results = append(resp.Results, res)
} else {
res := types.ToolResult{ToolCallID: id, Result: `Tell the
user:"We will stop this deal and if there are any new tasks, you can wake me up again."`}
resp.Results = append(resp.Results, res)
}
return &types.FunctionResult{Result: `Tell the user:"We will stop this deal and if there are any new tasks, you can wake me up again."`}, nil
return resp, nil
}

func (s *Service) VAPIFunctionSign(ctx context.Context, msg *types.VapiServerMessageToolCall) (*types.FunctionResult, error) {
func (s *Service) VAPIFunctionSign(ctx context.Context, msg *types.VapiServerMessageToolCall) (*types.ToolResults, error) {
var id string
var confirm bool
var resp = new(types.ToolResults)
resp.Results = make([]types.ToolResult, 0)
for _, tool := range msg.Message.ToolCallList {
if tool.Function != nil {
if tool.Function.Name == "sign" {
id = tool.Id
curr, ok := tool.Function.Arguments["sign"]
if !ok {
return nil, types.NewError(fiber.StatusBadRequest, "BadRequest", "currency is required")
Expand All @@ -82,9 +101,14 @@ func (s *Service) VAPIFunctionSign(ctx context.Context, msg *types.VapiServerMes
}
s.log.Warn("VAPIFunction called", "sign", confirm)
if confirm {
return &types.FunctionResult{Result: `Tell the user:"Thank you, I will sign for you to complete the transaction."`}, nil
res := types.ToolResult{ToolCallID: id, Result: `Tell the user:"Thank you, I will sign for you to complete the transaction."`}
resp.Results = append(resp.Results, res)
} else {
res := types.ToolResult{ToolCallID: id, Result: `Tell the
user:"We will stop this deal and if there are any new tasks, you can wake me up again."`}
resp.Results = append(resp.Results, res)
}
return &types.FunctionResult{Result: `Tell the user:"We will stop this deal and if there are any new tasks, you can wake me up again."`}, nil
return resp, nil
}

func (s *Service) VAPIFunction(ctx context.Context, genericMessage map[string]interface{}) error {
Expand Down
9 changes: 7 additions & 2 deletions backend/types/vapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,13 @@ func (p StatusUpdatePayload) GetCallType() VapiWebhookEnum {
return p.Type
}

type FunctionResult struct {
Result string `json:"result"`
type ToolResults struct {
Results []ToolResult `json:"results"`
}

type ToolResult struct {
ToolCallID string `json:"toolCallId"`
Result string `json:"result"`
}

type VapiServerMessageToolCall struct {
Expand Down

0 comments on commit b885616

Please sign in to comment.