regex/doConnect.go

55 lines
1.2 KiB
Go

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() (*genai.Client, error) {
apiKey := os.Getenv("GEMINI_API_KEY")
if apiKey == "" {
return nil, 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 nil, log.Errorf("failed to create new genai client: %w", err)
}
return client, err
}
// sampleHello sends a hardcoded prompt to the model and prints the response.
func simpleHello(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-2.5-flash", 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
}