Skip to content

Commit

Permalink
Add tools for calendars
Browse files Browse the repository at this point in the history
  • Loading branch information
kozaktomas committed Jan 15, 2025
1 parent fbb60de commit 9e0c1fa
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 0 deletions.
2 changes: 2 additions & 0 deletions backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ toolchain go1.23.4

require (
github.com/antchfx/htmlquery v1.3.4
github.com/apognu/gocal v0.9.1
github.com/gorilla/mux v1.8.1
github.com/hako/durafmt v0.0.0-20210608085754-5c1018a4e16b
github.com/joho/godotenv v1.5.1
Expand All @@ -27,6 +28,7 @@ require (

require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/ChannelMeter/iso8601duration v0.0.0-20150204201828-8da3af7a2a61 // indirect
github.com/PuerkitoBio/goquery v1.10.1 // indirect
github.com/andybalholm/cascadia v1.3.3 // indirect
github.com/antchfx/xpath v1.3.3 // indirect
Expand Down
6 changes: 6 additions & 0 deletions backend/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
github.com/ChannelMeter/iso8601duration v0.0.0-20150204201828-8da3af7a2a61 h1:N5Vqww5QISEHsWHOWDEx4PzdIay3Cg0Jp7zItq2ZAro=
github.com/ChannelMeter/iso8601duration v0.0.0-20150204201828-8da3af7a2a61/go.mod h1:GnKXcK+7DYNy/8w2Ex//Uql4IgfaU82Cd5rWKb7ah00=
github.com/PuerkitoBio/goquery v1.10.1 h1:Y8JGYUkXWTGRB6Ars3+j3kN0xg1YqqlwvdTV8WTFQcU=
github.com/PuerkitoBio/goquery v1.10.1/go.mod h1:IYiHrOMps66ag56LEH7QYDDupKXyo5A8qrjIx3ZtujY=
github.com/andybalholm/cascadia v1.3.3 h1:AG2YHrzJIm4BZ19iwJ/DAua6Btl3IwJX+VI4kktS1LM=
Expand All @@ -8,6 +10,8 @@ github.com/antchfx/htmlquery v1.3.4 h1:Isd0srPkni2iNTWCwVj/72t7uCphFeor5Q8nCzj1j
github.com/antchfx/htmlquery v1.3.4/go.mod h1:K9os0BwIEmLAvTqaNSua8tXLWRWZpocZIH73OzWQbwM=
github.com/antchfx/xpath v1.3.3 h1:tmuPQa1Uye0Ym1Zn65vxPgfltWb/Lxu2jeqIGteJSRs=
github.com/antchfx/xpath v1.3.3/go.mod h1:i54GszH55fYfBmoZXapTHN8T8tkcHfRgLyVwwqzXNcs=
github.com/apognu/gocal v0.9.1 h1:e3vlb+YV5wXvqBxYsC6GvkuUAEnRipkvoA1P79gwspM=
github.com/apognu/gocal v0.9.1/go.mod h1:5tNvJsQGJHwS3KqWxHAFZzavC4k42jrJ3ouVmOzS/AM=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
Expand All @@ -16,6 +20,8 @@ github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/channelmeter/iso8601duration v0.0.0-20150204201828-8da3af7a2a61 h1:o64h9XF42kVEUuhuer2ehqrlX8rZmvQSU0+Vpj1rF6Q=
github.com/channelmeter/iso8601duration v0.0.0-20150204201828-8da3af7a2a61/go.mod h1:Rp8e0DCtEKwXFOC6JPJQVTz8tuGoGvw6Xfexggh/ed0=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
2 changes: 2 additions & 0 deletions backend/pkg/ai/ai.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ func NewAi(ctx context.Context, conf *config.Config, s *scale.Scale, m *promethe
ai.tableTennisResultsTool(),
ai.tableTennisTableTool(),
ai.lesempolemRegisteredTool(),
ai.musicConcertsTool(),
ai.pubCalendarTool(),
}

return ai
Expand Down
44 changes: 44 additions & 0 deletions backend/pkg/ai/ai_tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,3 +599,47 @@ func (ai *Ai) lesempolemRegisteredTool() tool {
},
}
}

func (ai *Ai) musicConcertsTool() tool {
return tool{
Definition: anthropic.ToolDefinition{
Name: "music_concerts",
Description: "Provides music concerts and festivals which might be interesting for the pub visitors",
InputSchema: jsonschema.Definition{
Type: jsonschema.Object,
Properties: map[string]jsonschema.Definition{},
Required: []string{},
},
},
Fn: func(_ string) (string, error) {
output, err := ProvideCalendar(ai.config.CalendarConcertsURL, time.Now().Add(-30*24*time.Hour), time.Now().Add(365*24*time.Hour))
if err != nil {
return "", fmt.Errorf("could not get concerts calendar data: %w", err)
}

return output, nil
},
}
}

func (ai *Ai) pubCalendarTool() tool {
return tool{
Definition: anthropic.ToolDefinition{
Name: "pub_calendar",
Description: "Events in the pub calendar (e.g. birthdays, parties, tap sanitizations, special beer days, etc.)",
InputSchema: jsonschema.Definition{
Type: jsonschema.Object,
Properties: map[string]jsonschema.Definition{},
Required: []string{},
},
},
Fn: func(_ string) (string, error) {
output, err := ProvideCalendar(ai.config.CalendarPubURL, time.Now().Add(-60*24*time.Hour), time.Now().Add(365*24*time.Hour))
if err != nil {
return "", fmt.Errorf("could not get pub calendar data: %w", err)
}

return output, nil
},
}
}
61 changes: 61 additions & 0 deletions backend/pkg/ai/provider_calendar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package ai

import (
"encoding/json"
"fmt"
"net/http"
"time"

"github.com/apognu/gocal"
)

func ProvideCalendar(url string, start, end time.Time) (string, error) {
resp, err := http.DefaultClient.Get(url)
if err != nil {
return "", fmt.Errorf("could not get calendar: %w", err)
}
if resp.Body == nil {
defer resp.Body.Close()
}

calendar := gocal.NewParser(resp.Body)
calendar.Start, calendar.End = &start, &end

err = calendar.Parse()
if err != nil {
return "", fmt.Errorf("could not parse calendar: %w", err)
}

type Event struct {
Summary string `json:"summary"`
Start string `json:"start"`
End string `json:"end"`
Description string `json:"description"`
Location string `json:"location"`
}

events := make([]Event, len(calendar.Events))
for i, e := range calendar.Events {
events[i] = Event{
Summary: e.Summary,
Start: formatCalendarDate(e.Start),
End: formatCalendarDate(e.End),
Description: e.Description,
Location: e.Location,
}
}

jsonData, err := json.Marshal(events)
if err != nil {
return "", fmt.Errorf("could not marshal calendar events: %w", err)
}

return fmt.Sprintf("Calendar contains following events:\n<json>\n%s\n</json>", string(jsonData)), nil
}

func formatCalendarDate(date *time.Time) string {
if date == nil {
return "unknown"
}
return date.Format("2006-01-02")
}
6 changes: 6 additions & 0 deletions backend/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ type Config struct {

WhatsAppOpenJid string
AnthropicAPIKey string

CalendarPubURL string
CalendarConcertsURL string
}

func NewConfig() *Config {
Expand All @@ -49,6 +52,9 @@ func NewConfig() *Config {

WhatsAppOpenJid: getStringEnvDefault("WHATSAPP_OPEN_JID", ""),
AnthropicAPIKey: getStringEnvDefault("ANTHROPIC_API_KEY", ""),

CalendarPubURL: getStringEnvDefault("CALENDAR_PUB_URL", ""),
CalendarConcertsURL: getStringEnvDefault("CALENDAR_CONCERTS_URL", ""),
}
}

Expand Down

0 comments on commit 9e0c1fa

Please sign in to comment.