From 99079457d22d8514d0f803f73815d247d5feef2f Mon Sep 17 00:00:00 2001 From: Takashi Kawachi Date: Thu, 18 May 2023 11:10:24 +0900 Subject: [PATCH] Add support for reading the default model from a config file This commit adds support for reading the default model from a config file located at `~/.aichat/config.yml`. If the `model` flag is not specified when running the program, it will try to read the default model from the config file. If the config file does not exist or does not contain a `model` field, it will use the default model `gogpt.GPT3Dot5Turbo`. --- aichat.go | 17 ++++++++++++++++- config.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 config.go diff --git a/aichat.go b/aichat.go index 122dc5a..9666a38 100644 --- a/aichat.go +++ b/aichat.go @@ -228,7 +228,7 @@ func main() { var listPrompts = false var nonStreaming = false var split = false - var model = gogpt.GPT4 + var model = "" getopt.FlagLong(&temperature, "temperature", 't', "temperature") getopt.FlagLong(&maxTokens, "max-tokens", 0, "max tokens, 0 to use default") getopt.FlagLong(&verbose, "verbose", 'v', "verbose output") @@ -249,6 +249,21 @@ func main() { if err != nil { log.Fatal(err) } + + config, err := ReadConfig() + if err != nil { + log.Fatal(err) + } + + // if model is not specified, use the default model from the config file + if model == "" { + if config.Model == "" { + model = gogpt.GPT3Dot5Turbo + } else { + model = config.Model + } + } + options := chatOptions{ model: model, temperature: temperature, diff --git a/config.go b/config.go new file mode 100644 index 0000000..474d932 --- /dev/null +++ b/config.go @@ -0,0 +1,29 @@ +package main + +import ( + "os" + "path/filepath" +) + +type Config struct { + Model string `yaml:"model"` +} + +func ReadConfig() (*Config, error) { + config := &Config{} + // try to read from ~/.aichat/config.yml + homedir, err := os.UserHomeDir() + if err != nil { + return nil, err + } + path := filepath.Join(homedir, ".aichat", "config.yml") + + if err := ReadYamlFromFile(path, config); err != nil { + // if the file does not exist, return an empty config + if os.IsNotExist(err) { + return config, nil + } + return nil, err + } + return config, nil +}