Skip to content

Commit

Permalink
feat: Rename chatCmd to searchCmd and add word count flag (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pradumnasaraf authored Jul 12, 2024
1 parent 8c213d3 commit f58f2b5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Flags:
```

> eg: gencli search how big is google
> eg: gencli search what is kubernetes --words 525
### 📜 License

Expand Down
3 changes: 2 additions & 1 deletion cmd/rootCmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ func Execute() {
}

func init() {
rootCmd.AddCommand(chatCmd)
rootCmd.AddCommand(searchCmd)

}
31 changes: 23 additions & 8 deletions cmd/chatCmd.go → cmd/searchCmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import (
"fmt"
"log"
"os"
"strconv"
"strings"

"github.com/google/generative-ai-go/genai"
"github.com/spf13/cobra"
"google.golang.org/api/option"
)

var chatCmd = &cobra.Command{
var numWords string = "150"

var searchCmd = &cobra.Command{
Use: "search [your question]",
Short: "Ask a question and get a response",
Args: cobra.MinimumNArgs(1),
Expand All @@ -24,23 +27,35 @@ var chatCmd = &cobra.Command{

func getApiRespone(args []string) string {

userArgs := strings.Join(args[1:], " ")
userArgs := strings.Join(args[0:], " ")

ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
checkNilError(err)

if err != nil {
log.Fatal(err)
}
defer client.Close()

model := client.GenerativeModel("gemini-1.5-flash")
resp, err := model.GenerateContent(ctx, genai.Text(userArgs+"in 100-120 words."))
// Validate user input is a number
_, err = strconv.Atoi(numWords)
if err != nil {
log.Fatal(err)
log.Fatal("Invalid number of words")
}

model := client.GenerativeModel("gemini-1.5-flash")
resp, err := model.GenerateContent(ctx, genai.Text(userArgs+" in "+numWords+" words."))
checkNilError(err)

finalResponse := resp.Candidates[0].Content.Parts[0]

return fmt.Sprint(finalResponse)
}

func init() {
searchCmd.Flags().StringVarP(&numWords, "words", "w", "150", "Number of words in the response")
}

func checkNilError(e error) {
if e != nil {
log.Fatal(e)
}
}

0 comments on commit f58f2b5

Please sign in to comment.