-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: release the root command and use sub-commands (#3)
- Loading branch information
1 parent
b7a8c4e
commit 4f3ce4d
Showing
4 changed files
with
56 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |