Skip to content

Commit

Permalink
Move all config into environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
jhalter committed Aug 10, 2024
1 parent 5d17760 commit 61d7400
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 55 deletions.
13 changes: 6 additions & 7 deletions gptbot/config.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package gptbot

type Config struct {
Name string `yaml:"Name" env:"BOT_NAME,default=Durandal"`
IconID int `yaml:"IconID" env:"BOT_ICON,default=4121"`
GreetUsers bool `yaml:"GreetUsers" env:"BOT_GREET_USERS,default=false"`
Greeting string `yaml:"Greeting" env:"BOT_GREETING"`
Instructions string `yaml:"Instructions" env:"BOT_INSTRUCTIONS"`

Model string `env:"MODEL,default=gpt-4o-mini"`
Name string `env:"BOT_NAME,default=Durandal"`
IconID int `env:"BOT_ICON,default=4121"`
GreetUsers bool `env:"BOT_GREET_USERS,default=false"`
Greeting string `env:"BOT_GREETING"`
Instructions string `env:"BOT_INSTRUCTIONS"`
Model string `env:"OPENAI_MODEL,default=gpt-4o-mini"`
}
69 changes: 21 additions & 48 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,72 +2,51 @@ package main

import (
"context"
"flag"
"fmt"
"github.com/Netflix/go-env"
"github.com/jhalter/mobius/hotline"
"github.com/sashabaranov/go-openai"
"gopkg.in/yaml.v3"
"hotline-chat-gpt-bot/gptbot"
"log"
"log/slog"
"os"
"time"
)

type Environment struct {
APIKey string `env:"OPENAI_API_KEY,required=true"`
APIKey string `env:"OPENAI_API_KEY,required=true"`
ServerAddr string `env:"SERVER_ADDR"`
Login string `env:"SERVER_LOGIN,default=guest"`
Password string `env:"SERVER_PASS"`

BotConfig gptbot.Config
}

// Values swapped in by go-releaser at build time
var (
version = "dev"
commit = "none"
)
// Value swapped in by go-releaser at build time
var version = "dev"

func main() {
srvAddr := flag.String("server", "localhost:5600", "Hotline server hostname:port")
login := flag.String("login", "guest", "Hotline server login")
pass := flag.String("pass", "", "Hotline server password")
logLevel := flag.String("log-level", "info", "Log level")
config := flag.String("config", "", "Path to config file")
printVersion := flag.Bool("version", false, "Print version and exit")
flag.Parse()

if *printVersion {
fmt.Printf("version %s, commit %s\n", version, commit)
os.Exit(0)
var environment Environment
_, err := env.UnmarshalFromEnviron(&environment)
if err != nil {
log.Fatal(err)
}

logger := slog.New(
slog.NewTextHandler(
os.Stdout,
&slog.HandlerOptions{Level: logLevels[*logLevel]},
&slog.HandlerOptions{
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.TimeKey {
// Remove the milliseconds from the time field to save a few columns.
a.Value = slog.StringValue(a.Value.Time().Format(time.RFC3339))
}
return a
},
},
),
)

ctx := context.Background()

var environment Environment
_, err := env.UnmarshalFromEnviron(&environment)
if err != nil {
log.Fatal(err)
}

if *config != "" {
fh, err := os.Open(*config)
if err != nil {
panic(err)
}

decoder := yaml.NewDecoder(fh)
err = decoder.Decode(&environment.BotConfig)
if err != nil {
panic(err)
}
}

bot, err := gptbot.New(
ctx,
environment.BotConfig,
Expand Down Expand Up @@ -102,7 +81,7 @@ func main() {
logger.InfoContext(ctx, "Started Mobius GPT Bot", "version", version, "model", environment.BotConfig.Model)

// Connect to the Hotline server.
err = bot.HotlineClient.Connect(*srvAddr, *login, *pass)
err = bot.HotlineClient.Connect(environment.ServerAddr, environment.Login, environment.Password)
if err != nil {
logger.Error("Hotline connection error", "error", err)
os.Exit(1)
Expand All @@ -126,9 +105,3 @@ func main() {
os.Exit(1)
}
}

var logLevels = map[string]slog.Level{
"debug": slog.LevelDebug,
"info": slog.LevelInfo,
"error": slog.LevelError,
}

0 comments on commit 61d7400

Please sign in to comment.