-
-
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.
feat: Add an image search functionality command (#11)
- Loading branch information
1 parent
31cfa8a
commit 76d6a11
Showing
5 changed files
with
87 additions
and
15 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
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) | ||
} | ||
} |
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,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) | ||
} |
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