Skip to content

Commit

Permalink
Load static tools dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
kozaktomas committed Dec 12, 2024
1 parent 22f4177 commit 1a4d740
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions backend/pkg/ai/ai.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -43,23 +44,17 @@ func NewAi(ctx context.Context, conf *config.Config, s *scale.Scale, m *promethe
logger: l,
}

tools := []tool{
ai.tools = []tool{
ai.currentTimeTool(),

ai.isOpenTool(),
ai.pubOpenedAtTool(),

ai.currentKegTools(),
ai.beersLeftTool(),
ai.kegTappedAtTool(),

ai.warehouseTotalTool(),
ai.warehouseKegTool(),

ai.scaleWifiStrengthTool(),

ai.suppliersTool(),

ai.localNewsTool(),
ai.tennisTool(),
ai.lunchMenuTool(),
Expand All @@ -69,14 +64,6 @@ func NewAi(ctx context.Context, conf *config.Config, s *scale.Scale, m *promethe
ai.sdhEventsTool(),
}

staticTools, err := ai.staticTools()
if err != nil {
l.Fatalf("could not load StaticConfig tools: %v", err)
}
tools = append(tools, staticTools...)

ai.tools = tools

return ai
}

Expand Down Expand Up @@ -157,13 +144,21 @@ func (ai *Ai) GetResponse(history []ChatMessage) (Response, error) {
}
}

staticTools, err := ai.staticTools()
if err != nil {
ai.logger.Errorf("could not load StaticConfig tools: %v", err)
return output, fmt.Errorf("could not load StaticConfig tools: %w", err)
}

tools := slices.Concat(ai.tools, staticTools) // merge default and static tools

running := true
sem := 0
for running && sem < 10 {
sem++

requestTools := make([]anthropic.ToolDefinition, len(ai.tools))
for i, tool := range ai.tools {
requestTools := make([]anthropic.ToolDefinition, len(tools))
for i, tool := range tools {
requestTools[i] = tool.Definition
}
resp, err := ai.client.CreateMessages(ai.ctx, anthropic.MessagesRequest{
Expand Down Expand Up @@ -198,7 +193,7 @@ func (ai *Ai) GetResponse(history []ChatMessage) (Response, error) {
for _, content := range resp.Content {
requestedTool := content.MessageContentToolUse
if requestedTool != nil {
for _, aiTool := range ai.tools {
for _, aiTool := range tools {
if aiTool.Definition.Name == requestedTool.Name {
ai.logger.Infof("running tool %s", requestedTool.Name)
toolResponse, err := aiTool.Fn(string(requestedTool.Input))
Expand Down

0 comments on commit 1a4d740

Please sign in to comment.