Skip to content

Commit c360146

Browse files
added delete, set default, change api key options
1 parent f29c332 commit c360146

3 files changed

Lines changed: 235 additions & 8 deletions

File tree

cmd/cli/llmSetup.go

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,66 @@ func SetupLLM() error {
4848
}
4949

5050
func UpdateLLM() error {
51-
fmt.Println("Update LLM config")
51+
52+
SavedModels, err := store.ListSavedModels()
53+
if err != nil {
54+
fmt.Println(err)
55+
}
56+
57+
models := []string{}
58+
options := []string{"Set Default", "Change API Key", "Delete"}
59+
60+
for _, p := range SavedModels.LLMProviders {
61+
models = append(models, p.LLM)
62+
}
63+
64+
prompt := promptui.Select{
65+
Label: "Select from saved models",
66+
Items: models,
67+
}
68+
69+
_,model,err := prompt.Run()
70+
if err != nil {
71+
fmt.Println(err)
72+
}
73+
74+
75+
prompt = promptui.Select{
76+
Label: "Select Option",
77+
Items: options,
78+
}
79+
opNo,_,err := prompt.Run()
80+
if err != nil {
81+
fmt.Println(err)
82+
}
83+
84+
apiKeyprompt := promptui.Prompt {
85+
Label: "Enter API Key",
86+
}
87+
88+
89+
switch opNo {
90+
case 0:
91+
err := store.ChangeDefault(model)
92+
if err != nil {
93+
return err
94+
}
95+
fmt.Printf("%s set as default", model)
96+
case 1:
97+
apiKey, err := apiKeyprompt.Run()
98+
if err != nil {
99+
return err
100+
}
101+
store.UpdateAPIKey(model, apiKey)
102+
fmt.Printf("%s API Key Updated", model)
103+
case 2:
104+
err := store.DeleteModel(model)
105+
if err == nil {
106+
fmt.Printf("%s model deleted", model)
107+
}
108+
109+
110+
}
111+
52112
return nil
53113
}

cmd/cli/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ func init() {
6868
// Cobra also supports local flags, which will only run
6969
// when this action is called directly.
7070
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
71+
rootCmd.AddCommand(creatCommitMsg)
7172
rootCmd.AddCommand(llmCmd)
7273
llmCmd.AddCommand(llmSetupCmd)
7374
llmCmd.AddCommand(llmUpdateCmd)
74-
rootCmd.AddCommand(creatCommitMsg)
7575
}
7676

cmd/cli/store/store.go

Lines changed: 173 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,6 @@ func Save(LLMConfig LLMProvider) error {
7272

7373
cfg.Default = LLMConfig.LLM
7474

75-
file, err := os.OpenFile(configPath, os.O_CREATE| os.O_WRONLY| os.O_TRUNC, 0600)
76-
if err != nil {
77-
fmt.Println("error opening file")
78-
}
79-
defer file.Close()
8075

8176
data, err = json.MarshalIndent(cfg, "", " ")
8277
if err != nil {
@@ -149,7 +144,6 @@ func getConfigPath() (string, error) {
149144

150145
}
151146

152-
153147
func DefaultLLMKey() (*LLMProvider, error) {
154148

155149
var cfg Config
@@ -192,4 +186,177 @@ func DefaultLLMKey() (*LLMProvider, error) {
192186
}
193187
}
194188
return nil, errors.New("not found default model in config")
189+
}
190+
191+
192+
func ListSavedModels() (*Config, error){
193+
194+
var cfg Config
195+
196+
configPath, err := getConfigPath()
197+
if err != nil {
198+
log.Fatal(err)
199+
}
200+
201+
isConfigExists := checkConfig(configPath)
202+
if !isConfigExists {
203+
log.Fatal("config file not exists")
204+
}
205+
206+
data, err := os.ReadFile(configPath)
207+
if err != nil {
208+
fmt.Println("Error", err)
209+
log.Fatal(err)
210+
}
211+
212+
if len(data) > 0 {
213+
err = json.Unmarshal(data, &cfg)
214+
if err != nil {
215+
fmt.Println("unmarshal error", err)
216+
log.Fatal(err)
217+
}
218+
} else {
219+
fmt.Println("Config file is empty, Please add atlead one LLM Key")
220+
return nil, errors.New("config file is empty")
221+
}
222+
223+
224+
return &cfg, nil
225+
226+
}
227+
228+
229+
func ChangeDefault(Model string) error {
230+
231+
var cfg Config
232+
233+
configPath, err := getConfigPath()
234+
if err != nil {
235+
return err
236+
}
237+
238+
isConfigExists := checkConfig(configPath)
239+
if !isConfigExists {
240+
return errors.New("config file not exists")
241+
}
242+
243+
data, err := os.ReadFile(configPath)
244+
if err != nil {
245+
return err
246+
}
247+
248+
if len(data) > 0 {
249+
err = json.Unmarshal(data, &cfg)
250+
if err != nil {
251+
return err
252+
}
253+
}
254+
255+
cfg.Default = Model
256+
257+
data, err = json.MarshalIndent(cfg, "", " ")
258+
if err != nil {
259+
return err
260+
}
261+
262+
return os.WriteFile(configPath, data, 0600)
263+
}
264+
265+
266+
func DeleteModel(Model string) error {
267+
268+
var cfg Config
269+
var newCfg Config
270+
271+
configPath, err := getConfigPath()
272+
if err != nil {
273+
return err
274+
}
275+
276+
isConfigExists := checkConfig(configPath)
277+
if !isConfigExists {
278+
return errors.New("config file not exists")
279+
}
280+
281+
data, err := os.ReadFile(configPath)
282+
if err != nil {
283+
return err
284+
}
285+
286+
if len(data) > 0 {
287+
err = json.Unmarshal(data, &cfg)
288+
if err != nil {
289+
return err
290+
}
291+
}
292+
293+
294+
if Model == cfg.Default {
295+
if len(cfg.LLMProviders) > 1 {
296+
fmt.Println("Please set other model as default, Cant delete default model")
297+
return nil
298+
} else {
299+
return os.WriteFile(configPath, []byte("{}"), 0600)
300+
}
301+
} else {
302+
303+
for _,p := range cfg.LLMProviders {
304+
305+
if p.LLM != Model {
306+
newCfg.LLMProviders = append(newCfg.LLMProviders, p)
307+
}
308+
}
309+
310+
newCfg.Default = cfg.Default
311+
312+
data, err = json.MarshalIndent(newCfg, "", " ")
313+
if err != nil {
314+
return err
315+
}
316+
return os.WriteFile(configPath, data, 0600)
317+
318+
}
319+
}
320+
321+
322+
func UpdateAPIKey(Model, APIKey string) error {
323+
324+
var cfg Config
325+
326+
327+
configPath, err := getConfigPath()
328+
if err != nil {
329+
return err
330+
}
331+
332+
isConfigExists := checkConfig(configPath)
333+
if !isConfigExists {
334+
return errors.New("config file not exists")
335+
}
336+
337+
data, err := os.ReadFile(configPath)
338+
if err != nil {
339+
return err
340+
}
341+
342+
if len(data) > 0 {
343+
err = json.Unmarshal(data, &cfg)
344+
if err != nil {
345+
return err
346+
}
347+
}
348+
349+
for i, p := range cfg.LLMProviders {
350+
if p.LLM == Model {
351+
cfg.LLMProviders[i].APIKey = APIKey
352+
}
353+
}
354+
355+
data, err = json.MarshalIndent(cfg, "", " ")
356+
if err != nil {
357+
return err
358+
}
359+
360+
return os.WriteFile(configPath, data, 0600)
361+
195362
}

0 commit comments

Comments
 (0)