Skip to content

Commit

Permalink
feat: Add an image search functionality command (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pradumnasaraf authored Aug 3, 2024
1 parent 31cfa8a commit 76d6a11
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 15 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ Usage:

Available Commands:
help Help about any command
search Ask a question and get a response
image Know details about an image (Please put your question in quotes)
search Ask a question and get a response (Please put your question in quotes)

Flags:
-h, --help help for gencli
```

> eg: gencli search how big is google
> eg: gencli search what is kubernetes --words 525
> eg: gencli search "What is kubernetes" --words 525
> eg: gencli image "What is this image about?" --path /path/to/image.jpg --format jpg
### 📜 License

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

import "log"

func CheckNilError(err error) {
if err != nil {
log.Fatal(err)
}
}
65 changes: 65 additions & 0 deletions cmd/imageCmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package cmd

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

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

var (
imageFilePath string
imageFileFormat string
)

var imageCmd = &cobra.Command{
Use: "image [your question] --path [image path] --format [image format]",
Example: "gencli image 'What this image is about?' --path cat.png --format png",
Short: "Know details about an image (Please put your question in quotes)",
Long: "Ask a question about an image and get a response. You need to provide the path of the image and the format of the image. The supported formats are jpg, jpeg, png, and gif.",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
res := imageFunc(args)
fmt.Println(res)
},
}

func imageFunc(args []string) string {
userArgs := strings.Join(args[0:], " ")

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

model := client.GenerativeModel("gemini-1.5-flash")

imgData, err := os.ReadFile(imageFilePath)
CheckNilError(err)

// Supports multiple image inputs
prompt := []genai.Part{
genai.ImageData(imageFileFormat, imgData),
genai.Text(userArgs),
}

resp, err := model.GenerateContent(ctx, prompt...)
CheckNilError(err)

finalResponse := resp.Candidates[0].Content.Parts[0]
return fmt.Sprint(finalResponse)

}

func init() {
imageCmd.Flags().StringVarP(&imageFilePath, "path", "p", "", "Enter the image path")
imageCmd.Flags().StringVarP(&imageFileFormat, "format", "f", "", "Enter the image format (jpeg, png, etc.)")
errPathF := imageCmd.MarkFlagRequired("path")
errFormatF := imageCmd.MarkFlagRequired("format")
CheckNilError(errPathF)
CheckNilError(errFormatF)
}
3 changes: 2 additions & 1 deletion cmd/rootCmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
var rootCmd = &cobra.Command{
Use: "gencli",
Short: "A CLI tool to interact with the Gemini API",
Long: "A CLI tool to interact with the Gemini API. You can ask questions and get responses in text or image format.",
Run: func(cmd *cobra.Command, args []string) {
err := cmd.Help()
if err != nil {
Expand All @@ -29,5 +30,5 @@ func Execute() {

func init() {
rootCmd.AddCommand(searchCmd)

rootCmd.AddCommand(imageCmd)
}
18 changes: 7 additions & 11 deletions cmd/searchCmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ import (
var numWords string = "150"

var searchCmd = &cobra.Command{
Use: "search [your question]",
Short: "Ask a question and get a response",
Args: cobra.MinimumNArgs(1),
Use: "search [your question]",
Example: "gencli search 'What is new in Golang?'",
Short: "Ask a question and get a response (Please put your question in quotes)",
Long: "Ask a question and get a response in a specified number of words. The default number of words is 150. You can change the number of words by using the --words flag.",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
res := getApiResponse(args)
fmt.Println(res)
Expand All @@ -31,7 +33,7 @@ func getApiResponse(args []string) string {

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

// Validate user input is a number
Expand All @@ -42,7 +44,7 @@ func getApiResponse(args []string) string {

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

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

Expand Down Expand Up @@ -76,9 +78,3 @@ func formatAsPlainText(input string) string {
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 76d6a11

Please sign in to comment.