diff options
| author | Jeff Carr <[email protected]> | 2025-08-30 14:42:57 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-08-30 14:42:57 -0500 |
| commit | 7f8f5e3b9b37fccbb357b70e6490823a8b22ae7e (patch) | |
| tree | 11b6f74e8d3f33b3fdb82421cb79653f65685fe8 /doConnect.go | |
| parent | c971bfddf74d48d87f10f5f050b1eef3fd12bf20 (diff) | |
basic JSON parsing into genai struc
Diffstat (limited to 'doConnect.go')
| -rw-r--r-- | doConnect.go | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/doConnect.go b/doConnect.go new file mode 100644 index 0000000..2285f0b --- /dev/null +++ b/doConnect.go @@ -0,0 +1,73 @@ +package main + +import ( + "context" + "fmt" + "os" + + "go.wit.com/log" + "google.golang.org/genai" +) + +// doConnect initializes the Gemini client and handles the request flow. +func doConnect() error { + apiKey := os.Getenv("GEMINI_API_KEY") + if apiKey == "" { + return log.Errorf("GEMINI_API_KEY environment variable not set") + } + + ctx := context.Background() + client, err := genai.NewClient(ctx, &genai.ClientConfig{APIKey: apiKey}) + if err != nil { + return log.Errorf("failed to create new genai client: %w", err) + } + + if argv.JsonFile != "" { + req, err := parseJSON(argv.JsonFile) + if err != nil { + return err + } + log.Info("parseJSON() ok. model =", req.Model) + + genaiContent, err := convertToGenai(req) + if err != nil { + return log.Errorf("failed to convert to genai.Content: %w", err) + } + log.Info("Successfully converted JSON to genai.Content") + // Here you would now use the 'genaiContent' to send to the API + _ = genaiContent // Prevent unused variable error for now + + return nil + } + + log.Info("doing sampleHello()") + return sampleHello(client) +} + +// sampleHello sends a hardcoded prompt to the model and prints the response. +func sampleHello(client *genai.Client) error { + log.Info("Sending 'hello, how are you' to the Gemini API...") + ctx := context.Background() + + // Create the parts slice + parts := []*genai.Part{ + {Text: "hello, how are you"}, + } + + content := []*genai.Content{{Parts: parts}} + + resp, err := client.Models.GenerateContent(ctx, "gemini-1.5-flash-latest", content, nil) + if err != nil { + return log.Errorf("error sending message: %v", err) + } + + log.Info("Response from API:") + for _, cand := range resp.Candidates { + if cand.Content != nil { + for _, part := range cand.Content.Parts { + fmt.Println(part) + } + } + } + return nil +} |
