Skip to content

Commit

Permalink
fix: release the root command and use sub-commands (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pradumnasaraf authored Jul 9, 2024
1 parent b7a8c4e commit 4f3ce4d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 32 deletions.
1 change: 0 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

- [Golang](https://golang.org/)
- [Cobra](https://github.com/spf13/cobra)
- [Viper](https://github.com/spf13/viper)

## 💥 How to Contribute

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

</div>

**GenCLI** is an AI-powered CLI tool built with Golang that answers your questions using the [Google Gemini API](https://gemini.google.com). It is developed with [Cobra](https://github.com/spf13/cobra), [Viper](https://github.com/spf13/viper), and more.
**GenCLI** is an AI-powered CLI tool built with Golang that answers your questions using the [Google Gemini API](https://gemini.google.com). It is developed with [Cobra](https://github.com/spf13/cobra) and more.

### 🚀 Getting Started

Expand Down
46 changes: 46 additions & 0 deletions cmd/chatCmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cmd

import (
"context"
"fmt"
"log"
"os"
"strings"

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

var chatCmd = &cobra.Command{
Use: "search [your question]",
Short: "Ask a question and get a response",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
res := getApiRespone(args)
fmt.Println(res)
},
}

func getApiRespone(args []string) string {

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

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

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."))
if err != nil {
log.Fatal(err)
}

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

return fmt.Sprint(finalResponse)
}
39 changes: 9 additions & 30 deletions cmd/rootCmd.go
Original file line number Diff line number Diff line change
@@ -1,53 +1,32 @@
package cmd

import (
"context"
"fmt"
"log"
"os"
"strings"

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

var rootCmd = &cobra.Command{
Use: "gencli [your question]",
Use: "gc",
Short: "A CLI tool to interact with the Gemini API",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
res := getApiRespone(args)
fmt.Println(res)
err := cmd.Help()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
},
}

func Execute() {
rootCmd.CompletionOptions.DisableDefaultCmd = true
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func getApiRespone(args []string) string {

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

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

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."))
if err != nil {
log.Fatal(err)
}

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

return fmt.Sprint(finalResponse)
func init() {
rootCmd.AddCommand(chatCmd)
}

0 comments on commit 4f3ce4d

Please sign in to comment.