-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.go
More file actions
62 lines (50 loc) · 1.55 KB
/
Copy pathmodels.go
File metadata and controls
62 lines (50 loc) · 1.55 KB
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
package discord_chatbot
import (
"context"
"errors"
"fmt"
"os"
yaml "github.com/goccy/go-yaml"
"github.com/rs/zerolog/log"
"github.com/taythebot/discord_chatbot/provider"
)
// Model yaml struct
type Model struct {
Name string `yaml:"name"`
Value string `yaml:"value"`
}
// ParseModels parses and validates models from the YAML file
func ParseModels(ctx context.Context, file string, provider *provider.Client) ([]Model, error) {
data, err := os.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("failed to read file: %w", err)
}
var models []Model
if err := yaml.Unmarshal(data, &models); err != nil {
return nil, fmt.Errorf("failed to unmarshal yaml: %w", err)
}
if len(models) == 0 {
return nil, errors.New("no models found")
} else if len(models) > 25 {
return nil, errors.New("cannot load more than 25 models")
}
// Validate all models with provider
apiModels, err := provider.GetModels(ctx, log.Logger)
if err != nil {
return nil, fmt.Errorf("failed to get models from provider: %w", err)
}
for _, model := range models {
var found bool
for _, apiModel := range apiModels.Data {
if apiModel.ID == model.Value {
found = true
break
}
}
if !found {
return nil, fmt.Errorf("failed to find model '%s' at provider", model.Name)
}
}
return models, nil
}
var DefaultModelPrompt = "You are a very loyal and helpful assistant. You will always answer each question and request to the best of your abilities! Each message from this point on will have the user's name and message in the format `$user: $message`."