-
Notifications
You must be signed in to change notification settings - Fork 1
/
ai.go
206 lines (181 loc) · 4.04 KB
/
ai.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
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/dshills/ai-manager/ai"
"github.com/dshills/termai/config"
"github.com/dshills/termai/format"
"github.com/dshills/termai/prompt"
"github.com/dshills/termai/serial"
)
const conversationStore = ".termai.conv"
type AI struct {
mgr *ai.Manager
model string
conv ai.Conversation
conf *config.Configuration
opts Options
prompt string
orgPrompt string
defGen ai.Generator
}
func newAI(generators []ai.Generator, conf *config.Configuration, opts Options) (*AI, error) {
aim := AI{
mgr: ai.New(),
model: opts.Model,
conf: conf,
opts: opts,
}
// load prev conversation
_ = aim.loadConv()
// Set the model
if aim.model == "" && conf.DefaultModel() == "" {
return &aim, fmt.Errorf("no model set and no default model")
}
if aim.model == "" {
aim.model = conf.DefaultModel()
}
aim.model = modelFromAlias(conf, aim.model)
// set default generator
aim.defGenerator(generators)
// gather active
err := aim.mgr.RegisterGenerators(generators...)
return &aim, err
}
func (a *AI) createPrompt(query string) error {
a.orgPrompt = query
// If specifc ft generate an advanced prompt
if a.opts.FileType != "" {
query = prompt.Inject(query, a.opts.FileType, a.opts.ExplainOutput, a.conf.Prompts)
}
a.prompt = query
// If opt-prompt add optimization of the prompt to the prompt
if a.opts.OptimizePrompt {
optimized, err := prompt.Optimize(query, a.conf.Prompts, a.defGen)
if err != nil {
return err
}
a.prompt = optimized
}
return nil
}
func (a *AI) defGenerator(generators []ai.Generator) {
for _, g := range generators {
if strings.EqualFold(g.Model(), a.model) {
a.defGen = g
return
}
}
}
func (a *AI) printPrompt() {
fmt.Println(a.prompt)
}
func (a *AI) printDefaultModel() {
dm := a.conf.DefaultModel()
if dm == "" {
fmt.Println("No default model set")
return
}
fmt.Println(dm)
}
func (a *AI) listModels() {
for _, m := range a.conf.ActiveModels {
if m.Default {
fmt.Printf("(*) %s %v: %v\n", m.Model, m.Aliases, m.Description)
continue
}
fmt.Printf("( ) %s %v: %v\n", m.Model, m.Aliases, m.Description)
}
}
func (a *AI) printConv() {
for _, msg := range a.conv {
fmt.Printf("%s: %s\n", msg.Role, msg.Text)
}
}
func (a *AI) usePrompt() (string, error) {
// Start a new conversation
if !a.opts.Continue {
a.conv = ai.Conversation{}
}
// Start the AI
tData := ai.ThreadData{Model: a.model, Conversation: a.conv}
thread, err := a.mgr.NewThread(tData)
if err != nil {
return "", fmt.Errorf("%s %w", tData.Model, err)
}
resp, err := thread.Converse(a.prompt)
if err != nil {
return "", fmt.Errorf("%s %w", tData.Model, err)
}
a.conv = thread.Conversation()
if a.opts.ColorOutput {
fmtOut := format.Response(resp.Message.Text)
if fmtOut != "" {
return fmtOut, nil
}
}
return resp.Message.Text, nil
}
func (a *AI) saveConv() error {
convPath, err := convLocation()
if err != nil {
return err
}
file, err := os.Create(convPath)
if err != nil {
return err
}
defer file.Close()
return serial.Serialize(file, a.conv)
}
func (a *AI) loadConv() error {
convPath, err := convLocation()
if err != nil {
return err
}
file, err := os.Open(convPath)
if err != nil {
return err
}
defer file.Close()
conv, err := serial.Hydrate(file)
if err != nil {
return err
}
a.conv = conv
return nil
}
func findHomeDir() (string, error) {
home := os.Getenv("HOME")
if home == "" {
return "", fmt.Errorf("could not determine HOME directory")
}
return home, nil
}
func convLocation() (string, error) {
home, err := findHomeDir()
if err != nil {
return "", err
}
fn := filepath.Join(home, conversationStore)
fn, err = filepath.Abs(fn)
if err != nil {
return "", err
}
return fn, nil
}
func modelFromAlias(conf *config.Configuration, mname string) string {
for _, mod := range conf.Models {
if strings.EqualFold(mod.Model, mname) {
return mod.Model
}
for _, alias := range mod.Aliases {
if strings.EqualFold(alias, mname) {
return mod.Model
}
}
}
return mname
}