Skip to content

Commit

Permalink
implement possibility to read configuration from yaml files
Browse files Browse the repository at this point in the history
  • Loading branch information
rainu committed Jan 3, 2025
1 parent 19e64f5 commit de3ab8a
Show file tree
Hide file tree
Showing 26 changed files with 439 additions and 146 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ https://github.com/user-attachments/assets/a6d16332-55a1-4866-9f3e-31490a488935
* [Anthropic](https://www.anthropic.com/)
* Scriptable
* All settings can be set via:
* yaml configuration file
* environment variables
* command line arguments
* The users questions and models answers will be printed out in the terminal
Expand Down
12 changes: 6 additions & 6 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func TestConfig_Parse(t *testing.T) {
name: "Set UI file dialog resolves aliases",
args: []string{"--ui-file-dialog-resolves-aliases=true"},
expected: modifiedConfig(func(c *Config) {
c.UI.FileDialog.ResolvesAliases = true
c.UI.FileDialog.ResolveAliases = true
}),
},
{
Expand Down Expand Up @@ -257,8 +257,8 @@ func TestConfig_Parse(t *testing.T) {
{
name: "Set UI quit shortcut",
args: []string{
"--ui-quit-shortcut-key", "q",
"--ui-quit-shortcut-ctrl",
"--ui-quit-key", "q",
"--ui-quit-ctrl",
},
expected: modifiedConfig(func(c *Config) {
c.UI.QuitShortcut = Shortcut{Code: "q", Ctrl: true}
Expand Down Expand Up @@ -435,7 +435,7 @@ func TestConfig_Parse(t *testing.T) {
name: "Set environment variable for UI file dialog resolves aliases",
env: []string{EnvironmentPrefix + "UI_FILE_DIALOG_RESOLVES_ALIASES=true"},
expected: modifiedConfig(func(c *Config) {
c.UI.FileDialog.ResolvesAliases = true
c.UI.FileDialog.ResolveAliases = true
}),
},
{
Expand Down Expand Up @@ -544,8 +544,8 @@ func TestConfig_Parse(t *testing.T) {
{
name: "Set environment variable for UI quit shortcut",
env: []string{
EnvironmentPrefix + "UI_QUIT_SHORTCUT_KEY=q",
EnvironmentPrefix + "UI_QUIT_SHORTCUT_CTRL=true",
EnvironmentPrefix + "UI_QUIT_KEY=q",
EnvironmentPrefix + "UI_QUIT_CTRL=true",
},
expected: modifiedConfig(func(c *Config) {
c.UI.QuitShortcut = Shortcut{Code: "q", Ctrl: true}
Expand Down
4 changes: 2 additions & 2 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func defaultConfig() *Config {
},
FileDialog: FileDialogConfig{
ShowHiddenFiles: true,
ResolvesAliases: true,
ResolveAliases: true,
TreatPackagesAsDirectories: true,
FilterDisplay: []string{},
FilterPattern: []string{},
Expand All @@ -60,7 +60,7 @@ func defaultConfig() *Config {
Printer: PrinterConfig{
Format: PrinterFormatJSON,
Targets: []io.WriteCloser{os.Stdout},
TargetsRaw: PrinterTargetOut,
TargetsRaw: []string{PrinterTargetOut},
},
}
}
4 changes: 2 additions & 2 deletions config/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

type ExpressionContainer struct {
Expression string `config:""`
Value float64 `config:"-"`
Expression string `config:"" yaml:"expression"`
Value float64 `config:"-" yaml:"value"`
}

type Expression string
Expand Down
10 changes: 5 additions & 5 deletions config/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
)

type Config struct {
UI UIConfig `config:"ui"`
UI UIConfig `yaml:"ui"`

LLM llm.LLMConfig `config:""`
LLM llm.LLMConfig `config:"" yaml:"llm"`

Printer PrinterConfig `config:"print"`
Printer PrinterConfig `yaml:"print"`

LogLevel int `config:"log-level"`
PrintVersion bool `config:"version" short:"v" usage:"Show the version"`
LogLevel int `yaml:"log-level"`
PrintVersion bool `config:"version" yaml:"-" short:"v" usage:"Show the version"`
}

func (c *Config) GetUsage(field string) string {
Expand Down
18 changes: 18 additions & 0 deletions config/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,24 @@ func printUsage(output io.Writer, fields resolvedFieldInfos) {
fmt.Fprintf(output, " %s%s\t%s\n", env, strings.Repeat(" ", maxLen-len(env)), field.Usage)
}

sort.Slice(fields, func(i, j int) bool {
return strings.Join(fields[i].YamlPath, "") < strings.Join(fields[j].YamlPath, "")
})

fmt.Fprintf(output, "\nYaml keys:\n")
for _, field := range fields {
yamlKey := strings.TrimLeft(strings.Join(field.YamlPath, "."), ".")
if strings.HasSuffix(yamlKey, "-") {
continue
}
fmt.Fprintf(output, " %s%s\t%s\n", yamlKey, strings.Repeat(" ", maxLen-len(yamlKey)), field.Usage)
}

fmt.Fprintf(output, "\nYaml lookup file locations:\n")
for _, location := range yamlLookupLocations() {
fmt.Fprintf(output, " - %s\n", location)
}

fmt.Fprintf(output, "\nAvailable code styles:\n")
for _, style := range availableCodeStyles {
fmt.Fprintf(output, " - %s\n", style)
Expand Down
6 changes: 3 additions & 3 deletions config/llm/anthropic.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
)

type AnthropicConfig struct {
Token string `config:"api-key" usage:"API Key"`
BaseUrl string `config:"base-url" usage:"BaseUrl"`
Model string `config:"model" usage:"Model"`
Token string `yaml:"api-key" usage:"API Key"`
BaseUrl string `yaml:"base-url" usage:"BaseUrl"`
Model string `yaml:"model" usage:"Model"`
}

func (c *AnthropicConfig) AsOptions() (opts []anthropic.Option) {
Expand Down
6 changes: 3 additions & 3 deletions config/llm/anythingllm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
)

type AnythingLLMConfig struct {
BaseURL string `config:"base-url" usage:"Base URL"`
Token string `config:"token" usage:"Token"`
Workspace string `config:"workspace" usage:"Workspace"`
BaseURL string `yaml:"base-url" usage:"Base URL"`
Token string `yaml:"token" usage:"Token"`
Workspace string `yaml:"workspace" usage:"Workspace"`
}

func (c *AnythingLLMConfig) Validate() error {
Expand Down
14 changes: 7 additions & 7 deletions config/llm/calloptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
)

type CallOptionsConfig struct {
SystemPrompt string `config:"system-prompt" short:"S" usage:"System Prompt"`
MaxToken int `config:"max-token" usage:"Max Token"`
Temperature float64 `config:"temperature" usage:"Temperature"`
TopK int `config:"top-k" usage:"Top-K"`
TopP float64 `config:"top-p" usage:"Top-P"`
MinLength int `config:"min-length" usage:"Min Length"`
MaxLength int `config:"max-length" usage:"Max Length"`
SystemPrompt string `yaml:"system-prompt" short:"S" usage:"System Prompt"`
MaxToken int `yaml:"max-token" usage:"Max Token"`
Temperature float64 `yaml:"temperature" usage:"Temperature"`
TopK int `yaml:"top-k" usage:"Top-K"`
TopP float64 `yaml:"top-p" usage:"Top-P"`
MinLength int `yaml:"min-length" usage:"Min Length"`
MaxLength int `yaml:"max-length" usage:"Max Length"`
}

func (c *CallOptionsConfig) AsOptions() (opts []llms.CallOption) {
Expand Down
16 changes: 8 additions & 8 deletions config/llm/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ const (
)

type LLMConfig struct {
Backend string `config:"backend" short:"b"`
Backend string `yaml:"backend" short:"b"`

LocalAI LocalAIConfig `config:"localai" usage:"LocalAI: "`
OpenAI OpenAIConfig `config:"openai" usage:"OpenAI: "`
AnythingLLM AnythingLLMConfig `config:"anythingllm" usage:"AnythingLLM: "`
Ollama OllamaConfig `config:"ollama" usage:"Ollama: "`
Mistral MistralConfig `config:"mistral" usage:"Mistral: "`
Anthropic AnthropicConfig `config:"anthropic" usage:"Anthropic: "`
CallOptions CallOptionsConfig `config:"call" usage:"LLM-CALL: "`
LocalAI LocalAIConfig `yaml:"localai" usage:"LocalAI: "`
OpenAI OpenAIConfig `yaml:"openai" usage:"OpenAI: "`
AnythingLLM AnythingLLMConfig `yaml:"anythingllm" usage:"AnythingLLM: "`
Ollama OllamaConfig `yaml:"ollama" usage:"Ollama: "`
Mistral MistralConfig `yaml:"mistral" usage:"Mistral: "`
Anthropic AnthropicConfig `yaml:"anthropic" usage:"Anthropic: "`
CallOptions CallOptionsConfig `yaml:"call" usage:"LLM-CALL: "`
}

func (c *LLMConfig) GetUsage(field string) string {
Expand Down
13 changes: 3 additions & 10 deletions config/llm/localai.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,13 @@ package llm

import (
"fmt"
flag "github.com/spf13/pflag"
"github.com/tmc/langchaingo/llms/openai"
)

type LocalAIConfig struct {
APIKey string `config:"api-key" usage:"API Key"`
Model string `config:"model" usage:"Model"`
BaseUrl string `config:"base-url" usage:"BaseUrl"`
}

func configureLocalai(c *LocalAIConfig) {
flag.StringVar(&c.APIKey, "localai-api-key", "", "LocalAI API Key")
flag.StringVar(&c.Model, "localai-model", "", "LocalAI chat model")
flag.StringVar(&c.BaseUrl, "localai-base-url", "", "LocalAI API Base-URL")
APIKey string `yaml:"api-key" usage:"API Key"`
Model string `yaml:"model" usage:"Model"`
BaseUrl string `yaml:"base-url" usage:"BaseUrl"`
}

func (c *LocalAIConfig) AsOptions() (opts []openai.Option) {
Expand Down
13 changes: 3 additions & 10 deletions config/llm/mistral.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,13 @@ package llm

import (
"fmt"
flag "github.com/spf13/pflag"
"github.com/tmc/langchaingo/llms/mistral"
)

type MistralConfig struct {
ApiKey string `config:"api-key" usage:"API Key"`
Endpoint string `config:"endpoint" usage:"Endpoint"`
Model string `config:"model" usage:"Model"`
}

func configureMistral(c *MistralConfig) {
flag.StringVar(&c.ApiKey, "mistral-api-key", "", "API Key for Mistral")
flag.StringVar(&c.Endpoint, "mistral-endpoint", "", "Endpoint for Mistral")
flag.StringVar(&c.Model, "mistral-model", "", "Model for Mistral")
ApiKey string `yaml:"api-key" usage:"API Key"`
Endpoint string `yaml:"endpoint" usage:"Endpoint"`
Model string `yaml:"model" usage:"Model"`
}

func (c *MistralConfig) AsOptions() (opts []mistral.Option) {
Expand Down
10 changes: 2 additions & 8 deletions config/llm/ollama.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@ package llm

import (
"fmt"
flag "github.com/spf13/pflag"
"github.com/tmc/langchaingo/llms/ollama"
)

type OllamaConfig struct {
ServerURL string `config:"server-url" usage:"Server URL"`
Model string `config:"model" usage:"Model"`
}

func configureOllama(c *OllamaConfig) {
flag.StringVar(&c.ServerURL, "ollama-server-url", "", "Server URL for Ollama")
flag.StringVar(&c.Model, "ollama-model", "", "Model for Ollama")
ServerURL string `yaml:"server-url" usage:"Server URL"`
Model string `yaml:"model" usage:"Model"`
}

func (c *OllamaConfig) AsOptions() (opts []ollama.Option) {
Expand Down
22 changes: 6 additions & 16 deletions config/llm/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ package llm

import (
"fmt"
flag "github.com/spf13/pflag"
"github.com/tmc/langchaingo/llms/openai"
)

type OpenAIConfig struct {
APIKey string `config:"api-key" usage:"API Key"`
APIType string `config:"api-type"`
APIVersion string `config:"api-version" usage:"API Version"`
APIKey string `yaml:"api-key" usage:"API Key"`
APIType string `yaml:"api-type"`
APIVersion string `yaml:"api-version" usage:"API Version"`

Model string `config:"model" usage:"Model"`
BaseUrl string `config:"base-url" usage:"BaseUrl"`
Organization string `config:"organization" usage:"Organization"`
Model string `yaml:"model" usage:"Model"`
BaseUrl string `yaml:"base-url" usage:"BaseUrl"`
Organization string `yaml:"organization" usage:"Organization"`
}

func (c *OpenAIConfig) GetUsage(field string) string {
Expand All @@ -24,15 +23,6 @@ func (c *OpenAIConfig) GetUsage(field string) string {
return ""
}

func configureOpenai(c *OpenAIConfig) {
flag.StringVar(&c.APIKey, "openai-api-key", "", "OpenAI API Key")
flag.StringVar(&c.APIType, "openai-api-type", string(openai.APITypeOpenAI), fmt.Sprintf("OpenAI API Type (%s, %s, %s)", openai.APITypeOpenAI, openai.APITypeAzure, openai.APITypeAzureAD))
flag.StringVar(&c.APIVersion, "openai-api-version", "", "OpenAI API Version")
flag.StringVar(&c.Model, "openai-model", "gpt-4o-mini", "OpenAI chat model")
flag.StringVar(&c.BaseUrl, "openai-base-url", "", "OpenAI API Base-URL")
flag.StringVar(&c.Organization, "openai-organization", "", "OpenAI Organization")
}

func (c *OpenAIConfig) AsOptions() (opts []openai.Option) {
if c.APIKey != "" {
opts = append(opts, openai.WithToken(c.APIKey))
Expand Down
Loading

0 comments on commit de3ab8a

Please sign in to comment.