forked from sammcj/gollama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
229 lines (194 loc) · 5.48 KB
/
main.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
package main
import (
"context"
"flag"
"fmt"
"os"
"path/filepath"
"sort"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/progress"
"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/ollama/ollama/api"
"golang.org/x/term"
"github.com/sammcj/gollama/config"
"github.com/sammcj/gollama/logging"
)
type AppModel struct {
width int
height int
ollamaModelsDir string
cfg *config.Config
inspectedModel Model
list list.Model
models []Model
selectedForDeletion []Model
confirmDeletion bool
inspecting bool
editing bool
message string
keys KeyMap
client *api.Client
lmStudioModelsDir string
noCleanup bool
table table.Model
filterInput tea.Model
showTop bool
progress progress.Model
altscreenActive bool
view View
showProgress bool
}
type progressMsg struct {
modelName string
}
type runFinishedMessage struct{ err error }
type pushSuccessMsg struct {
modelName string
}
type pushErrorMsg struct {
err error
}
var Version = "development"
func main() {
cfg, err := config.LoadConfig()
if err != nil {
fmt.Println("Error loading config:", err)
os.Exit(1)
}
err = logging.Init(cfg.LogLevel, cfg.LogFilePath)
if err != nil {
fmt.Println("Error initializing logging:", err)
os.Exit(1)
}
listFlag := flag.Bool("l", false, "List all available Ollama models and exit")
ollamaDirFlag := flag.String("ollama-dir", cfg.OllamaAPIKey, "Custom Ollama models directory")
lmStudioDirFlag := flag.String("lm-dir", cfg.LMStudioFilePaths, "Custom LM Studio models directory")
noCleanupFlag := flag.Bool("no-cleanup", false, "Don't cleanup broken symlinks")
cleanupFlag := flag.Bool("cleanup", false, "Remove all symlinked models and empty directories and exit")
versionFlag := flag.Bool("v", false, "Print the version and exit")
flag.Parse()
if *versionFlag {
fmt.Println(Version)
os.Exit(0)
}
os.Setenv("EDITOR", cfg.Editor)
logging.DebugLogger.Println("EDITOR set to", cfg.Editor)
client, err := api.ClientFromEnvironment()
if err != nil {
logging.ErrorLogger.Println("Error creating API client:", err)
return
}
ctx := context.Background()
resp, err := client.List(ctx)
if err != nil {
logging.ErrorLogger.Println("Error fetching models:", err)
return
}
logging.InfoLogger.Println("Fetched models from API")
models := parseAPIResponse(resp)
modelMap := make(map[string][]Model)
for _, model := range models {
model.Size = normalizeSize(model.Size)
modelMap[model.ID] = append(modelMap[model.ID], model)
}
groupedModels := make([]Model, 0)
for _, group := range modelMap {
groupedModels = append(groupedModels, group...)
}
switch cfg.SortOrder {
case "name":
sort.Slice(groupedModels, func(i, j int) bool {
return groupedModels[i].Name < groupedModels[j].Name
})
case "size":
sort.Slice(groupedModels, func(i, j int) bool {
return groupedModels[i].Size > groupedModels[j].Size
})
case "modified":
sort.Slice(groupedModels, func(i, j int) bool {
return groupedModels[i].Modified.After(groupedModels[j].Modified)
})
case "family":
sort.Slice(groupedModels, func(i, j int) bool {
return groupedModels[i].Family < groupedModels[j].Family
})
}
items := make([]list.Item, len(groupedModels))
for i, model := range groupedModels {
items[i] = model
}
keys := NewKeyMap()
width, height, err := term.GetSize(int(os.Stdout.Fd()))
if err != nil {
width, height = 80, 24
}
app := AppModel{
client: client,
keys: *keys,
models: groupedModels,
width: width,
height: height,
ollamaModelsDir: *ollamaDirFlag,
lmStudioModelsDir: *lmStudioDirFlag,
noCleanup: *noCleanupFlag,
cfg: &cfg,
progress: progress.New(progress.WithDefaultGradient()),
}
if *ollamaDirFlag == "" {
app.ollamaModelsDir = filepath.Join(os.Getenv("HOME"), ".ollama", "models")
}
if *lmStudioDirFlag == "" {
app.lmStudioModelsDir = filepath.Join(os.Getenv("HOME"), ".cache", "lm-studio", "models")
}
if *listFlag {
listModels(models)
os.Exit(0)
}
if *cleanupFlag {
cleanupSymlinkedModels(app.lmStudioModelsDir)
os.Exit(0)
}
l := list.New(items, NewItemDelegate(&app), width, height-5)
l.Title = "Ollama Models"
l.Help.Styles.ShortDesc.Bold(true)
l.Help.Styles.ShortDesc.UnsetFaint()
l.AdditionalShortHelpKeys = func() []key.Binding {
return []key.Binding{
keys.Space,
keys.Delete,
keys.SortByName,
keys.SortBySize,
keys.SortByModified,
keys.SortByQuant,
keys.SortByFamily,
keys.RunModel,
keys.ConfirmYes,
keys.ConfirmNo,
keys.LinkModel,
keys.LinkAllModels,
keys.CopyModel,
keys.PushModel,
keys.Top,
keys.UpdateModel,
}
}
app.list = l
p := tea.NewProgram(&app, tea.WithAltScreen(), tea.WithMouseCellMotion())
if _, err := p.Run(); err != nil {
logging.ErrorLogger.Printf("Error: %v", err)
} else {
fmt.Print("\033[H\033[2J")
}
// Throw a warning if the users terminal cannot display colours
if !term.IsTerminal(int(os.Stdout.Fd())) {
fmt.Println("Warning: Your terminal does not support colours. Please consider using a terminal that does.")
}
cfg.SortOrder = keys.GetSortOrder()
if err := config.SaveConfig(cfg); err != nil {
panic(err)
}
p.ReleaseTerminal()
}