-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfig.go
261 lines (230 loc) · 7.92 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package main
import (
"fmt"
"log"
"strings"
"time"
"github.com/sashabaranov/go-openai"
vip "github.com/spf13/viper"
)
var ModifiableConfigKeys = []string{
"model",
"addressed",
"prompt",
"maxtokens",
"temperature",
"top_p",
"admins",
"tools",
}
type ModelConfig struct {
Model string
MaxTokens int
Temperature float32
TopP float32
}
type BotConfig struct {
Admins []string
Verbose bool
Addressed bool
Prompt string
Greeting string
ToolsDir string
ToolsEnabled bool
}
type ServerConfig struct {
Nick string
Server string
Port int
Channel string
SSL bool
TLSInsecure bool
SASLNick string
SASLPass string
}
type SessionConfig struct {
MaxHistory int
TTL time.Duration
ChunkMax int
}
type APIConfig struct {
Type string
Key string
Stream bool
Timeout time.Duration
URL string
}
type Configuration struct {
Server *ServerConfig
Bot *BotConfig
Model *ModelConfig
Session *SessionConfig
API *APIConfig
}
type SystemImpl struct {
Store SessionStore
LLM LLM
Tools *ToolRegistry
}
func (s SystemImpl) GetLLM() LLM {
return s.LLM
}
func (s SystemImpl) GetToolRegistry() *ToolRegistry {
return s.Tools
}
func (s SystemImpl) GetSessionStore() SessionStore {
return s.Store
}
func NewSystem(c *Configuration) System {
s := SystemImpl{}
// initialize tools
if c.Bot.ToolsEnabled {
toolsDir := vip.GetString("toolsdir")
registry, err := NewToolRegistry(toolsDir)
if err != nil {
log.Println("config: failed to initialize tools:", err)
c.Bot.ToolsEnabled = false
} else {
RegisterIrcTools(registry)
s.Tools = registry
}
} else {
log.Println("config: tools are disabled")
s.Tools = &ToolRegistry{}
}
// initialize the api for completions
if c.API.Type == "openai" {
s.LLM = NewOpenAIClient(*c.API)
} else if c.API.Type == "anthropic" {
s.LLM = NewAnthropicClient(*c.API)
} else {
log.Fatal("config: unknown api type:", c.API.Type)
}
// initialize sessions
s.Store = NewSessionStore(c)
return s
}
func (c *Configuration) PrintConfig() {
fmt.Printf("nick: %s\n", c.Server.Nick)
fmt.Printf("server: %s\n", c.Server.Server)
fmt.Printf("port: %d\n", c.Server.Port)
fmt.Printf("channel: %s\n", c.Server.Channel)
fmt.Printf("tls: %t\n", c.Server.SSL)
fmt.Printf("tlsinsecure: %t\n", c.Server.TLSInsecure)
fmt.Printf("saslnick: %s\n", c.Server.SASLNick)
fmt.Printf("saslpass: %s\n", c.Server.SASLPass)
fmt.Printf("admins: %v\n", c.Bot.Admins)
fmt.Printf("verbose: %t\n", c.Bot.Verbose)
fmt.Printf("addressed: %t\n", c.Bot.Addressed)
fmt.Printf("chunkmax: %d\n", c.Session.ChunkMax)
fmt.Printf("clienttimeout: %s\n", c.API.Timeout)
fmt.Printf("maxhistory: %d\n", c.Session.MaxHistory)
fmt.Printf("maxtokens: %d\n", c.Model.MaxTokens)
fmt.Printf("tools: %t\n", c.Bot.ToolsEnabled)
fmt.Printf("toolsdir: %s\n", c.Bot.ToolsDir)
fmt.Printf("sessionduration: %s\n", c.Session.TTL)
if len(c.API.Key) > 3 && c.API.Key != "" {
fmt.Printf("apikey: %s\n", strings.Repeat("*", len(c.API.Key)-3)+c.API.Key[len(c.API.Key)-3:])
} else {
fmt.Printf("apikey: %s\n", c.API.Key)
}
fmt.Printf("apiurl: %s\n", c.API.URL)
fmt.Printf("apitype: %s\n", c.API.Type)
fmt.Printf("streaming: %t\n", c.API.Stream)
fmt.Printf("model: %s\n", c.Model.Model)
fmt.Printf("temperature: %f\n", c.Model.Temperature)
fmt.Printf("topp: %f\n", c.Model.TopP)
fmt.Printf("prompt: %s\n", c.Bot.Prompt)
fmt.Printf("greeting: %s\n", c.Bot.Greeting)
}
func NewConfiguration() *Configuration {
configfile := vip.GetString("config")
if configfile != "" {
vip.SetConfigFile(configfile)
if err := vip.ReadInConfig(); err != nil {
log.Fatal("config: config file not found", configfile)
} else {
log.Println("config: using config file:", vip.ConfigFileUsed())
}
}
config := &Configuration{
Server: &ServerConfig{
Nick: vip.GetString("nick"),
Server: vip.GetString("server"),
Port: vip.GetInt("port"),
Channel: vip.GetString("channel"),
SSL: vip.GetBool("tls"),
TLSInsecure: vip.GetBool("tlsinsecure"),
SASLNick: vip.GetString("saslnick"),
SASLPass: vip.GetString("saslpass"),
},
Bot: &BotConfig{
Admins: vip.GetStringSlice("admins"),
Verbose: vip.GetBool("verbose"),
Addressed: vip.GetBool("addressed"),
Prompt: vip.GetString("prompt"),
Greeting: vip.GetString("greeting"),
ToolsEnabled: vip.GetBool("tools"),
ToolsDir: vip.GetString("toolsdir"),
},
Model: &ModelConfig{
Model: vip.GetString("model"),
MaxTokens: vip.GetInt("maxtokens"),
Temperature: float32(vip.GetFloat64("temperature")),
TopP: float32(vip.GetFloat64("top_p")),
},
Session: &SessionConfig{
ChunkMax: vip.GetInt("chunkmax"),
MaxHistory: vip.GetInt("sessionhistory"),
TTL: vip.GetDuration("sessionduration"),
},
API: &APIConfig{
Timeout: vip.GetDuration("apitimeout"),
Key: vip.GetString("apikey"),
Stream: vip.GetBool("stream"),
URL: vip.GetString("apiurl"),
Type: vip.GetString("apitype"),
},
}
return config
}
func initializeConfig() {
cmd := root
// irc client configuration
cmd.PersistentFlags().StringP("nick", "n", "soulshack", "bot's nickname on the irc server")
cmd.PersistentFlags().StringP("server", "s", "localhost", "irc server address")
cmd.PersistentFlags().BoolP("tls", "e", false, "enable TLS for the IRC connection")
cmd.PersistentFlags().BoolP("tlsinsecure", "", false, "skip TLS certificate verification")
cmd.PersistentFlags().IntP("port", "p", 6667, "irc server port")
cmd.PersistentFlags().StringP("channel", "c", "", "irc channel to join")
cmd.PersistentFlags().StringP("saslnick", "", "", "nick used for SASL")
cmd.PersistentFlags().StringP("saslpass", "", "", "password for SASL plain")
// bot configuration
cmd.PersistentFlags().StringP("config", "b", "", "use the named configuration file")
cmd.PersistentFlags().StringSliceP("admins", "A", []string{}, "comma-separated list of allowed hostmasks to administrate the bot (e.g. alex!~alex@localhost, josh!~josh@localhost)")
// informational
cmd.PersistentFlags().BoolP("verbose", "v", false, "enable verbose logging of sessions and configuration")
// openai configuration
cmd.PersistentFlags().String("apikey", "", "api key")
cmd.PersistentFlags().String("apiurl", "", "alternative base url to use instead of openai")
cmd.PersistentFlags().String("apitype", "openai", "api type to use for completion requests")
cmd.PersistentFlags().Int("maxtokens", 512, "maximum number of tokens to generate")
cmd.PersistentFlags().String("model", openai.GPT4o, "model to be used for responses")
cmd.PersistentFlags().DurationP("apitimeout", "t", time.Minute*5, "timeout for each completion request")
cmd.PersistentFlags().Float32("temperature", 0.7, "temperature for the completion")
cmd.PersistentFlags().Float32("top_p", 1, "top P value for the completion")
cmd.PersistentFlags().Bool("tools", false, "enable tool use")
cmd.PersistentFlags().Bool("stream", true, "enable streaming completion")
cmd.PersistentFlags().String("toolsdir", "examples/tools", "directory to load tools from")
// timeouts and behavior
cmd.PersistentFlags().BoolP("addressed", "a", true, "require bot be addressed by nick for response")
cmd.PersistentFlags().DurationP("sessionduration", "S", time.Minute*3, "message context will be cleared after it is unused for this duration")
cmd.PersistentFlags().IntP("sessionhistory", "H", 250, "maximum number of lines of context to keep per session")
cmd.PersistentFlags().IntP("chunkmax", "m", 350, "maximum number of characters to send as a single message")
// personality / prompting
cmd.PersistentFlags().String("greeting", "hello.", "prompt to be used when the bot joins the channel")
cmd.PersistentFlags().String("prompt", "you are a helpful chatbot. do not use caps. do not use emoji.", "initial system prompt")
vip.BindPFlags(cmd.PersistentFlags())
vip.SetEnvPrefix("SOULSHACK")
vip.AutomaticEnv()
}