-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathconfig.go
92 lines (78 loc) · 2.7 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// manifold/config.go
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/pterm/pterm"
"gopkg.in/yaml.v2"
)
type ServiceConfig struct {
Name string `yaml:"name"`
Host string `yaml:"host"`
Port int `yaml:"port"`
Command string `yaml:"command"`
GPULayers string `yaml:"gpu_layers,omitempty"`
Args []string `yaml:"args,omitempty"`
Model string `yaml:"model,omitempty"`
}
type ToolConfig struct {
Name string `yaml:"name"`
Parameters map[string]interface{} `yaml:"parameters"`
}
type DatabaseConfig struct {
ConnectionString string `yaml:"connection_string"`
}
type CompletionsConfig struct {
DefaultHost string `yaml:"default_host"`
APIKey string `yaml:"api_key"`
}
type EmbeddingsConfig struct {
Host string `yaml:"host"`
APIKey string `yaml:"api_key"`
Dimensions int `yaml:"dimensions"`
EmbedPrefix string `yaml:"embed_prefix"`
SearchPrefix string `yaml:"search_prefix"`
}
type RerankerConfig struct {
Host string `yaml:"host"`
}
type Config struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
DataPath string `yaml:"data_path"`
AnthropicKey string `yaml:"anthropic_key,omitempty"`
OpenAIAPIKey string `yaml:"openai_api_key,omitempty"`
GoogleGeminiKey string `yaml:"google_gemini_key,omitempty"`
HuggingFaceToken string `yaml:"hf_token,omitempty"`
Database DatabaseConfig `yaml:"database"`
Completions CompletionsConfig `yaml:"completions"`
Embeddings EmbeddingsConfig `yaml:"embeddings"`
Reranker RerankerConfig `yaml:"reranker"`
}
// LoadConfig reads the configuration from a YAML file, unmarshals it into a Config struct,
// logs the outcome using pterm, and prints the loaded configuration as pretty printed JSON.
func LoadConfig(filename string) (*Config, error) {
data, err := os.ReadFile(filename)
if err != nil {
pterm.Error.Printf("Error reading config file: %v\n", err)
return nil, fmt.Errorf("error reading config file: %w", err)
}
var config Config
err = yaml.Unmarshal(data, &config)
if err != nil {
pterm.Error.Printf("Error unmarshaling config: %v\n", err)
return nil, fmt.Errorf("error unmarshaling config: %w", err)
}
pterm.Success.Println("Configuration loaded successfully.")
return &config, nil
}
// printPrettyConfig marshals the config as pretty printed JSON and outputs it.
func printPrettyConfig(config *Config) {
prettyJSON, err := json.MarshalIndent(config, "", " ")
if err != nil {
pterm.Error.Printf("Failed to marshal config as pretty JSON: %v\n", err)
return
}
pterm.Info.Println(string(prettyJSON))
}