Skip to content

Commit 8bd874f

Browse files
committed
Add setModel
1 parent 40290d3 commit 8bd874f

18 files changed

+111
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ ottodocs
22
bin/**
33
dist/**
44
*.bleve
5+
**.DS_Store**

ai/markdown.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/chand1012/ottodocs/constants"
1010
)
1111

12-
func Markdown(filePath, contents, chatPrompt, APIKey string) (string, error) {
12+
func Markdown(filePath, contents, chatPrompt, APIKey, model string) (string, error) {
1313
openai := gopenai.NewOpenAI(&gopenai.OpenAIOpts{
1414
APIKey: APIKey,
1515
})
@@ -41,6 +41,7 @@ func Markdown(filePath, contents, chatPrompt, APIKey string) (string, error) {
4141
req := ai_types.NewDefaultChatRequest("")
4242
req.Messages = messages
4343
req.MaxTokens = maxTokens
44+
req.Model = model
4445
// lower the temperature to make the model more deterministic
4546
// req.Temperature = 0.3
4647

ai/question.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/chand1012/ottodocs/constants"
1010
)
1111

12-
func Question(filePath, fileContent, chatPrompt, APIKey string) (string, error) {
12+
func Question(filePath, fileContent, chatPrompt, APIKey, model string) (string, error) {
1313
openai := gopenai.NewOpenAI(&gopenai.OpenAIOpts{
1414
APIKey: APIKey,
1515
})
@@ -41,6 +41,7 @@ func Question(filePath, fileContent, chatPrompt, APIKey string) (string, error)
4141
req := ai_types.NewDefaultChatRequest("")
4242
req.Messages = messages
4343
req.MaxTokens = maxTokens
44+
req.Model = model
4445
// lower the temperature to make the model more deterministic
4546
// req.Temperature = 0.3
4647

ai/single_file.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func extractLineNumber(line string) (int, error) {
3434
}
3535

3636
// Document a file using the OpenAI ChatGPT API. Takes a file path, a prompt, and an API key as arguments.
37-
func SingleFile(filePath, contents, chatPrompt, APIKey string) (string, error) {
37+
func SingleFile(filePath, contents, chatPrompt, APIKey, model string) (string, error) {
3838

3939
openai := gopenai.NewOpenAI(&gopenai.OpenAIOpts{
4040
APIKey: APIKey,
@@ -74,6 +74,7 @@ func SingleFile(filePath, contents, chatPrompt, APIKey string) (string, error) {
7474
req := ai_types.NewDefaultChatRequest("")
7575
req.Messages = messages
7676
req.MaxTokens = maxTokens
77+
req.Model = model
7778
// lower the temperature to make the model more deterministic
7879
req.Temperature = 0.3
7980

cmd/ask.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ Requires a path to a repository or file as a positional argument.`,
135135
os.Exit(1)
136136
}
137137

138-
resp, err := ai.Question(fileName, content, chatPrompt, conf.APIKey)
138+
resp, err := ai.Question(fileName, content, chatPrompt, conf.APIKey, conf.Model)
139139

140140
if err != nil {
141141
log.Errorf("Error asking question: %s", err)

cmd/doc.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ var docCmd = &cobra.Command{
4747
}
4848

4949
if inlineMode || !markdownMode {
50-
contents, err = ai.SingleFile(filePath, fileContents, chatPrompt, conf.APIKey)
50+
contents, err = ai.SingleFile(filePath, fileContents, chatPrompt, conf.APIKey, conf.Model)
5151
} else {
52-
contents, err = ai.Markdown(filePath, fileContents, chatPrompt, conf.APIKey)
52+
contents, err = ai.Markdown(filePath, fileContents, chatPrompt, conf.APIKey, conf.Model)
5353
}
5454

5555
if err != nil {

cmd/docs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ search for files in the directory and document them.
8787
}
8888

8989
if inlineMode || !markdownMode {
90-
contents, err = ai.SingleFile(path, fileContents, chatPrompt, conf.APIKey)
90+
contents, err = ai.SingleFile(path, fileContents, chatPrompt, conf.APIKey, conf.Model)
9191
} else {
92-
contents, err = ai.Markdown(path, fileContents, chatPrompt, conf.APIKey)
92+
contents, err = ai.Markdown(path, fileContents, chatPrompt, conf.APIKey, conf.Model)
9393
}
9494

9595
if err != nil {

cmd/setModel.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
3+
*/
4+
package cmd
5+
6+
import (
7+
"github.com/spf13/cobra"
8+
9+
"github.com/chand1012/ottodocs/config"
10+
"github.com/chand1012/ottodocs/utils"
11+
)
12+
13+
var VALID_MODELS = []string{"gpt-4", "gpt-4-0314", "gpt-4-32k", "gpt-4-32k-0314", "gpt-3.5-turbo", "gpt-3.5-turbo-0301"}
14+
15+
// setModelCmd represents the setModel command
16+
var setModelCmd = &cobra.Command{
17+
Use: "setModel",
18+
Short: "Set the model to use for documentation",
19+
Long: `Sets the model to use for documentation. Takes a valid ChatGPT API model name as a single positional argument.
20+
Valid models are: gpt-4, gpt-4-0314, gpt-4-32k, gpt-4-32k-0314, gpt-3.5-turbo, gpt-3.5-turbo-0301
21+
See here for more information: https://platform.openai.com/docs/models/model-endpoint-compatibility
22+
`,
23+
Run: func(cmd *cobra.Command, args []string) {
24+
model := args[0]
25+
c, err := config.Load()
26+
if err != nil {
27+
log.Errorf("Error loading config: %s", err)
28+
}
29+
if !utils.Contains(VALID_MODELS, model) {
30+
log.Errorf("Invalid model: %s", model)
31+
log.Errorf("Valid models are: %s", VALID_MODELS)
32+
return
33+
}
34+
35+
c.Model = model
36+
37+
err = config.Save(c)
38+
if err != nil {
39+
log.Errorf("Error saving config: %s", err)
40+
}
41+
log.Infof("Set model to %s", model)
42+
},
43+
Args: cobra.ExactArgs(1),
44+
}
45+
46+
func init() {
47+
RootCmd.AddCommand(setModelCmd)
48+
}

config/config.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
// Config represents the configuration file
1111
type Config struct {
1212
APIKey string `json:"api_key"`
13+
Model string `json:"model"`
1314
}
1415

1516
// also returns the path to the config file
@@ -39,7 +40,10 @@ func createIfNotExists() (string, error) {
3940
return "", err
4041
}
4142
// add an empty config to the file
42-
blankConfig := Config{}
43+
blankConfig := Config{
44+
APIKey: "",
45+
Model: "gpt-3.5-turbo",
46+
}
4347
err = json.NewEncoder(file).Encode(blankConfig)
4448
if err != nil {
4549
return "", err

docs/otto.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ Code documentation made easy using GPT.
1414

1515
### SEE ALSO
1616

17+
* [otto ask](otto_ask.md) - Ask a question about a file or repo
1718
* [otto chat](otto_chat.md) - Ask ChatGPT a question from the command line.
1819
* [otto doc](otto_doc.md) - Document a file
1920
* [otto docs](otto_docs.md) - Document a repository of files
2021
* [otto login](otto_login.md) - Add an API key to your configuration
2122
* [otto prompt](otto_prompt.md) - Generates a ChatGPT prompt from a given Git repo
23+
* [otto setModel](otto_setModel.md) - Set the model to use for documentation
2224

23-
###### Auto generated by spf13/cobra on 18-Mar-2023
25+
###### Auto generated by spf13/cobra on 18-Apr-2023

docs/otto_ask.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ otto ask [flags]
2323
### SEE ALSO
2424

2525
* [otto](otto.md) - Document your code with ease
26+
27+
###### Auto generated by spf13/cobra on 18-Apr-2023

docs/otto_chat.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ otto chat [flags]
2424

2525
* [otto](otto.md) - Document your code with ease
2626

27-
###### Auto generated by spf13/cobra on 18-Mar-2023
27+
###### Auto generated by spf13/cobra on 18-Apr-2023

docs/otto_doc.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ otto doc [flags]
2626

2727
* [otto](otto.md) - Document your code with ease
2828

29-
###### Auto generated by spf13/cobra on 18-Mar-2023
29+
###### Auto generated by spf13/cobra on 18-Apr-2023

docs/otto_docs.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ otto docs [flags]
2929

3030
* [otto](otto.md) - Document your code with ease
3131

32-
###### Auto generated by spf13/cobra on 18-Mar-2023
32+
###### Auto generated by spf13/cobra on 18-Apr-2023

docs/otto_login.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ otto login [flags]
2222

2323
* [otto](otto.md) - Document your code with ease
2424

25-
###### Auto generated by spf13/cobra on 18-Mar-2023
25+
###### Auto generated by spf13/cobra on 18-Apr-2023

docs/otto_prompt.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ otto prompt [flags]
2626

2727
* [otto](otto.md) - Document your code with ease
2828

29-
###### Auto generated by spf13/cobra on 18-Mar-2023
29+
###### Auto generated by spf13/cobra on 18-Apr-2023

docs/otto_setModel.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## otto setModel
2+
3+
Set the model to use for documentation
4+
5+
### Synopsis
6+
7+
Sets the model to use for documentation. Takes a valid ChatGPT API model name as a single positional argument.
8+
Valid models are: gpt-4, gpt-4-0314, gpt-4-32k, gpt-4-32k-0314, gpt-3.5-turbo, gpt-3.5-turbo-0301
9+
See here for more information: https://platform.openai.com/docs/models/model-endpoint-compatibility
10+
11+
12+
```
13+
otto setModel [flags]
14+
```
15+
16+
### Options
17+
18+
```
19+
-h, --help help for setModel
20+
```
21+
22+
### SEE ALSO
23+
24+
* [otto](otto.md) - Document your code with ease
25+
26+
###### Auto generated by spf13/cobra on 18-Apr-2023

utils/contains.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package utils
2+
3+
func Contains(slice []string, item string) bool {
4+
for _, i := range slice {
5+
if i == item {
6+
return true
7+
}
8+
}
9+
return false
10+
}

0 commit comments

Comments
 (0)