-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.go
75 lines (63 loc) · 1.5 KB
/
settings.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
package main
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/lonelycode/botMaker"
)
type Settings struct {
Bot botMaker.BotSettings
Instructions string
Help string
ConversationWindow int
template string
}
func getSettings(dir string) (*Settings, error) {
cfgFile := filepath.Join(dir, "lurch.json")
promptTpl := filepath.Join(dir, "prompt.tpl")
_, err := os.Stat(cfgFile)
if err != nil {
return nil, err
}
cfg, err := os.ReadFile(cfgFile)
if err != nil {
return nil, err
}
botSettings := &Settings{}
err = json.Unmarshal(cfg, botSettings)
if err != nil {
return nil, err
}
// just in case
defaults := botMaker.NewBotSettings()
botSettings.Bot.EmbeddingModel = defaults.EmbeddingModel
err = checkInstructions(botSettings)
if err != nil {
return nil, err
}
_, err = os.Stat(promptTpl)
if err != nil {
return nil, err
}
tpl, err := os.ReadFile(promptTpl)
if err != nil {
return nil, err
}
botSettings.template = string(tpl)
return botSettings, nil
}
// checkInstructions will check the `s.Instructions` property for a `file://` reference,
// if found it will read the file and fill the property with the contents of the file.
func checkInstructions(s *Settings) error {
if strings.HasPrefix(s.Instructions, "file://") {
filename := strings.TrimPrefix(s.Instructions, "file://")
b, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
s.Instructions = string(b)
}
return nil
}