-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
285 lines (259 loc) · 6.2 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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/fatih/color"
"github.com/gin-gonic/gin"
"github.com/hako/durafmt"
"github.com/joho/godotenv"
"github.com/sausheong/ishell/v2"
)
var model string
var images []string
var cyan = color.New(color.FgCyan).SprintFunc()
var yellow = color.New(color.FgHiYellow).SprintFunc()
var white = color.New(color.FgWhite, color.Bold).SprintFunc()
var green = color.New(color.FgHiGreen, color.Bold).SprintFunc()
var red = color.New(color.FgRed, color.Bold).SprintFunc()
func init() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
model = os.Getenv("MODEL")
gin.SetMode(gin.ReleaseMode)
go startOllamaServer()
}
func main() {
shell := ishell.New()
// display info.
shell.Println(white("waldo."))
shell.SetPrompt(getPrompt())
shell.AddCmd(&ishell.Cmd{
Name: "shell",
Help: "run shell commands",
Func: func(c *ishell.Context) {
c.Print(cyan("shell> "))
line := c.ReadLine()
defer c.SetPrompt(getPrompt())
if line == "" || line == "exit" {
return
}
args := strings.Split(line, " ")
out, err := run(args[0], args[1:])
if err != nil {
c.Println(red(string(out)))
c.Println(red(err))
return
} else {
if len(out) > 0 {
c.Println(yellow(string(out)))
}
}
c.Cmd.Func(c)
},
})
shell.AddCmd(&ishell.Cmd{
Name: "ask",
Help: "ask waldo",
Func: func(c *ishell.Context) {
c.Print(cyan("ask> "))
line := c.ReadLine()
defer c.SetPrompt(getPrompt())
if line == "" || line == "exit" {
return
}
ask(model, line)
c.Cmd.Func(c)
},
})
shell.AddCmd(&ishell.Cmd{
Name: "search",
Help: "search the Internet",
Func: func(c *ishell.Context) {
c.Print(cyan("search> "))
line := c.ReadLine()
defer c.SetPrompt(getPrompt())
if line == "" || line == "exit" {
return
}
err := search(model, line)
if err != nil {
c.Println(red(err))
}
c.Cmd.Func(c)
},
})
// ask question about images
shell.AddCmd(&ishell.Cmd{
Name: "image",
Help: "ask questions about an image file",
Func: func(c *ishell.Context) {
defer c.SetPrompt(getPrompt())
if !isImageModel() {
c.Println(red("Please switch to an image model like llava or Gemini-Pro-Vision or GPT-4-Vision first."))
return
}
if len(images) == 0 {
c.Print(cyan("image files? "))
imagesInput := c.ReadLine()
images = strings.Split(strings.TrimSpace(imagesInput), " ")
}
c.Println(yellow(getFilenames(images)))
c.Print(cyan("image> "))
line := c.ReadLine()
if line == "" || line == "exit" {
return
}
if strings.HasPrefix(line, "/clear") {
images = []string{}
} else if strings.HasPrefix(line, "/show") {
if os.Getenv("TERM_PROGRAM") == "iTerm.app" {
for _, img := range images {
printInlineImage(img)
}
} else {
fmt.Println(red("showing inline images only available on iTerm2."))
}
} else {
askImage(model, line, images)
}
c.Cmd.Func(c)
},
})
// exit walso
shell.AddCmd(&ishell.Cmd{
Name: "exit",
Help: "exit waldo",
Func: exit,
})
shell.AddCmd(&ishell.Cmd{
Name: "switch",
Help: "switch to a different model",
Func: func(c *ishell.Context) {
choices, err := getModels()
if err != nil {
log.Println(err)
c.Println(red("model not switched."))
return
}
choice := c.MultiChoice(choices, cyan("Your current model is ")+yellow(model)+cyan(". Which model to switch to?"))
model = choices[choice]
c.SetPrompt(getPrompt())
c.Println()
},
})
shell.AddCmd(&ishell.Cmd{
Name: "add",
Help: "add a new model to Waldo",
Func: func(c *ishell.Context) {
c.Print(cyan("model name? "))
line := c.ReadLine()
defer c.SetPrompt(getPrompt())
if line == "" || line == "exit" {
return
}
err := pullModel(line)
if err != nil {
c.Println(red(err))
}
c.Println()
},
})
shell.AddCmd(&ishell.Cmd{
Name: "info",
Help: "information about Waldo",
Func: func(c *ishell.Context) {
c.Println(yellow("model:"), cyan(model))
c.SetPrompt(getPrompt())
c.Println()
},
})
// start shell
shell.Run()
// teardown
shell.Close()
}
func exit(c *ishell.Context) {
c.Stop()
}
func getPrompt() string {
return "waldo> "
}
func isImageModel() bool {
return strings.Contains(model, "llava") || strings.Contains(model, "-vision")
}
func pullModel(name string) error {
reqJson := `{
"name": "` + name + `"
}`
r := bytes.NewReader([]byte(reqJson))
httpResp, err := http.Post("http://localhost:11435/api/pull", "application/json", r)
if err != nil {
fmt.Println("err in calling ollama:", err)
return err
}
decoder := json.NewDecoder(httpResp.Body)
t0 := time.Now()
for {
resp := &PullResponse{}
decoder.Decode(&resp)
if resp.Status == "success" {
elapsed := durafmt.Parse(time.Since(t0)).LimitFirstN(2)
fmt.Print("\033[2K\r")
fmt.Printf(cyan("success\n(%s)"), elapsed)
fmt.Println()
break
} else {
if resp.Status[0:7] == "pulling" {
fmt.Print("\033[2K\r")
percentage := float64(resp.Completed) * 100.0 / float64(resp.Total)
if percentage < 100.0 && percentage != 0.0 {
fmt.Printf(cyan("downloading ... %.1f%%"), percentage)
}
} else {
fmt.Println(cyan(resp.Status))
}
}
}
return err
}
func getModels() ([]string, error) {
models := &Models{}
httpResp, err := http.Get("http://localhost:11435/api/tags")
if err != nil {
fmt.Println("err in calling ollama:", err)
return []string{}, err
}
decoder := json.NewDecoder(httpResp.Body)
err = decoder.Decode(models)
if err != nil {
fmt.Println("err in getting models:", err)
return []string{}, err
}
results := []string{"gpt-3.5-turbo", "gpt-4", "gpt-4-turbo", "gpt-4-vision", "gemini-pro", "gemini-pro-vision"}
for _, m := range models.Models {
results = append(results, m.Name)
}
return results, nil
}
func getFilenames(filepaths []string) []string {
files := []string{}
for _, path := range filepaths {
files = append(files, filepath.Base(path))
}
return files
}
func printInlineImage(filePath string) {
bytes, _ := os.ReadFile(filePath)
b64 := base64.StdEncoding.EncodeToString(bytes)
fmt.Printf("\x1b]1337;File=inline=1;width=256px:%s\a\n", b64)
}